Skip to content

Commit 6eff753

Browse files
committed
first release
1 parent e5e002a commit 6eff753

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.pio
2+
.pioenvs
3+
.piolibdeps
4+
5+
### VisualStudioCode ###
6+
.vscode/*
7+
*.code-workspace
8+
9+
# Local History for Visual Studio Code
10+
.history/
11+
12+
### VisualStudioCode Patch ###
13+
# Ignore all local history of files
14+
.history
15+
.ionide
16+
lib/readme.txt
17+
src/user-config.h

platformio.ini

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[platformio]
2+
3+
[env]
4+
5+
[env:esp12e]
6+
platform = espressif8266
7+
board = esp12e
8+
framework = arduino
9+
lib_deps =
10+
paulstoffregen/OneWire@^2.3.5
11+
milesburton/DallasTemperature@^3.9.1
12+
adafruit/DHT sensor library@^1.4.2
13+
adafruit/Adafruit Unified Sensor@^1.1.4
14+
knolleary/PubSubClient@^2.8
15+
bblanchon/ArduinoJson@5.13.4

src/config.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* 1.0.0 VERSION */
2+
3+
#ifndef config_h
4+
#define config_h
5+
6+
// USER SETTINGS
7+
#include "user-config.h"
8+
9+
// GENERAL SETTINGS
10+
#define LEDSTATUSPIN 2 // digital pin for status led
11+
#define SERIAL_BAUDRATE 9600
12+
#define READ_INTERVAL 5000; // ms
13+
#define NUM_SAMPLES 12; // num data samples before send to MQTT
14+
#define WARMUP_TIME 60; // seconds - MQ-4 sensor needs a warmup time
15+
16+
// MQ-4 SETTINGS
17+
#define MQ4PIN A0 // analog pin for MQ-4 sensor
18+
19+
// WIFI SETTINGS (see user-config.h)
20+
const char *ssid = USER_SETTINGS_WIFI_SSID;
21+
const char *password = USER_SETTINGS_WIFI_PASSWORD;
22+
const char *wifihostname = USER_SETTINGS_WIFI_HOSTNAME;
23+
24+
// MQTT SETTINGS (see user-config.h)
25+
const char MQTT_HOST[] = USER_SETTINGS_MQTT_HOST;
26+
int MQTT_PORT = USER_SETTINGS_MQTT_PORT;
27+
const char TOPIC_P[] = USER_SETTINGS_MQTT_TOPIC_P;
28+
const char TOPIC_S[] = USER_SETTINGS_MQTT_TOPIC_S;
29+
const char DEVICE[] = USER_SETTINGS_MQTT_DEVICE;
30+
const char MQTT_USER[] = USER_SETTINGS_MQTT_USER;
31+
const char MQTT_PWD[] = USER_SETTINGS_MQTT_PWD;
32+
int MQTT_QOS = USER_SETTINGS_MQTT_QOS;
33+
34+
#endif

src/main.cpp

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
}

src/user-config-template.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* 1.0.0 VERSION */
2+
3+
#ifndef user_config_h
4+
#define user_config_h
5+
6+
// WIFI SETTINGS
7+
#define USER_SETTINGS_WIFI_SSID "X"
8+
#define USER_SETTINGS_WIFI_PASSWORD "X"
9+
#define USER_SETTINGS_WIFI_HOSTNAME "X"
10+
11+
// MQTT SETTINGS
12+
#define USER_SETTINGS_MQTT_HOST "X"
13+
#define USER_SETTINGS_MQTT_PORT 1833
14+
#define USER_SETTINGS_MQTT_TOPIC_P "X"
15+
#define USER_SETTINGS_MQTT_TOPIC_S "X" // not used
16+
#define USER_SETTINGS_MQTT_DEVICE "X"
17+
#define USER_SETTINGS_MQTT_USER "X"
18+
#define USER_SETTINGS_MQTT_PWD "X"
19+
#define USER_SETTINGS_MQTT_QOS 1
20+
21+
#endif

0 commit comments

Comments
 (0)