|
| 1 | +/* 1.0.0 VERSION */ |
| 2 | + |
| 3 | +#include <Arduino.h> |
| 4 | +#include <ArduinoJson.h> |
| 5 | + |
| 6 | +#include "config.h" |
| 7 | + |
| 8 | +#include <ddcommon.h> |
| 9 | +#include <ddwifi.h> |
| 10 | +#include <ddmqtt.h> |
| 11 | +#include <ddmq4.h> |
| 12 | + |
| 13 | +int currentSample; |
| 14 | + |
| 15 | +//Wifi |
| 16 | +DDWifi wifi(ssid, password, wifihostname, LEDSTATUSPIN); |
| 17 | + |
| 18 | +//MQTT |
| 19 | +DDMqtt clientMqtt(DEVICE, MQTT_HOST, MQTT_PORT, MQTT_USER, MQTT_PWD, TOPIC_S, MQTT_QOS, LEDSTATUSPIN); |
| 20 | + |
| 21 | +//MQ-4 Gas sensor |
| 22 | +DDMQ4 mq4Sensor(MQ4PIN); |
| 23 | +DDMQ4Val sampleGasValues; |
| 24 | +int countSampleGas; |
| 25 | + |
| 26 | +//JSON |
| 27 | +DynamicJsonBuffer jsonBuffer; |
| 28 | +JsonObject &configRoot = jsonBuffer.createObject(); |
| 29 | +JsonObject &root = jsonBuffer.createObject(); |
| 30 | +JsonObject &gas = root.createNestedObject("gas"); |
| 31 | + |
| 32 | +void printDebugGas(DDMQ4Val gasValue) |
| 33 | +{ |
| 34 | + writeToSerial("GAS Success = ", false); |
| 35 | + writeToSerial(gasValue.success ? "True" : "False", true); |
| 36 | + if (!gasValue.success) |
| 37 | + writeToSerial(gasValue.errorMsg, true); |
| 38 | + else |
| 39 | + { |
| 40 | + writeToSerial("analog = ", false); |
| 41 | + writeToSerial(gasValue.sensorValue, true); |
| 42 | + writeToSerial("ppm = ", false); |
| 43 | + writeToSerial(gasValue.ppm, true); |
| 44 | + writeToSerial("percentage = ", false); |
| 45 | + writeToSerial(gasValue.percentage, false); |
| 46 | + writeToSerial(" %", true); |
| 47 | + writeToSerial("real value = ", false); |
| 48 | + writeToSerial(gasValue.realValue, true); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +String generateJsonMessage(DDMQ4Val gasValue, int countSampleGas) |
| 53 | +{ |
| 54 | + DDMQ4Val gasValueTot; |
| 55 | + if (countSampleGas > 0) |
| 56 | + { |
| 57 | + gasValueTot.sensorValue = gasValue.sensorValue / countSampleGas; |
| 58 | + gasValueTot.ppm = gasValue.ppm / countSampleGas; |
| 59 | + gasValueTot.percentage = gasValue.percentage / countSampleGas; |
| 60 | + gasValueTot.realValue = gasValue.realValue / countSampleGas; |
| 61 | + gas["error"] = ""; |
| 62 | + } |
| 63 | + else |
| 64 | + gas["error"] = "No samples"; |
| 65 | + gas["sensorValue"] = gasValueTot.sensorValue; |
| 66 | + gas["ppm"] = gasValueTot.ppm; |
| 67 | + gas["percentage"] = gasValueTot.percentage; |
| 68 | + gas["realValue"] = gasValueTot.realValue; |
| 69 | + String json; |
| 70 | + root.printTo(json); |
| 71 | + return json; |
| 72 | +} |
| 73 | + |
| 74 | +void createJsonConfig() |
| 75 | +{ |
| 76 | + configRoot["readInterval"] = READ_INTERVAL; |
| 77 | + configRoot["numSamples"] = NUM_SAMPLES; |
| 78 | + configRoot["warmupTime"] = WARMUP_TIME; |
| 79 | +} |
| 80 | + |
| 81 | +void setup() |
| 82 | +{ |
| 83 | + createJsonConfig(); |
| 84 | + |
| 85 | + pinMode(LEDSTATUSPIN, OUTPUT); |
| 86 | + pinMode(MQ4PIN, INPUT); |
| 87 | + |
| 88 | + digitalWrite(LEDSTATUSPIN, LOW); |
| 89 | + |
| 90 | + if (SERIAL_ENABLED) |
| 91 | + Serial.begin(SERIAL_BAUDRATE); |
| 92 | + |
| 93 | + writeToSerial("ESP8266MCU11 Booting...", true); |
| 94 | + |
| 95 | + // WIFI |
| 96 | + wifi.connect(); |
| 97 | + |
| 98 | + //MQTT |
| 99 | + clientMqtt.reconnectMQTT(); |
| 100 | + |
| 101 | + // MQ4 |
| 102 | + countSampleGas = 0; |
| 103 | + currentSample = 0; |
| 104 | + |
| 105 | + // WARM UP |
| 106 | + writeToSerial("Warming up sensors", true); |
| 107 | + int i; |
| 108 | + for (i = 0; i < configRoot["warmupTime"]; i++) |
| 109 | + { |
| 110 | + digitalWrite(LEDSTATUSPIN, LOW); |
| 111 | + delay(500); |
| 112 | + digitalWrite(LEDSTATUSPIN, HIGH); |
| 113 | + delay(500); |
| 114 | + writeToSerial(".", false); |
| 115 | + } |
| 116 | + |
| 117 | + writeToSerial("", true); |
| 118 | + writeToSerial("Warmup finished", true); |
| 119 | +} |
| 120 | + |
| 121 | +void loop() |
| 122 | +{ |
| 123 | + delay(configRoot["readInterval"]); |
| 124 | + clientMqtt.loop(); |
| 125 | + |
| 126 | + // Get GAS Value |
| 127 | + DDMQ4Val gasValue = mq4Sensor.getValue(); |
| 128 | + printDebugGas(gasValue); |
| 129 | + |
| 130 | + if (gasValue.success) |
| 131 | + { |
| 132 | + sampleGasValues.sensorValue += gasValue.sensorValue; |
| 133 | + sampleGasValues.ppm += gasValue.ppm; |
| 134 | + sampleGasValues.percentage += gasValue.percentage; |
| 135 | + sampleGasValues.realValue += gasValue.realValue; |
| 136 | + countSampleGas++; |
| 137 | + } |
| 138 | + |
| 139 | + writeToSerial("currentSample ", false); |
| 140 | + writeToSerial(currentSample, true); |
| 141 | + |
| 142 | + if (++currentSample >= configRoot["numSamples"]) |
| 143 | + { |
| 144 | + clientMqtt.sendMessage(TOPIC_P, generateJsonMessage(sampleGasValues, countSampleGas)); |
| 145 | + currentSample = 0; |
| 146 | + countSampleGas = 0; |
| 147 | + sampleGasValues.sensorValue = 0.0; |
| 148 | + sampleGasValues.ppm = 0.0; |
| 149 | + sampleGasValues.percentage = 0.0; |
| 150 | + sampleGasValues.realValue = 0.0; |
| 151 | + } |
| 152 | +} |
0 commit comments