Skip to content

Commit 1e4a5fe

Browse files
authored
Add files via upload
1 parent 67b4153 commit 1e4a5fe

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <MQ135.h>
2+
#include <Adafruit_Sensor.h>
3+
#include <ArduinoJson.h>
4+
#include <ESP8266WiFi.h>
5+
#include <PubSubClient.h>
6+
7+
#include "DHT.h"
8+
9+
#include <stdlib.h>
10+
11+
#define DHTPIN 4 // pino do sensor de temperatura
12+
#define MQ135PIN 5 // pino do sensor de gas
13+
#define DHTTYPE DHT22 // DHT 22
14+
15+
DHT dht(DHTPIN, DHTTYPE);
16+
MQ135 gasSensor = MQ135(MQ135PIN);
17+
18+
// Update these with values suitable for your network.
19+
20+
const char* ssid = "2.4G_COTONETES";
21+
const char* password = "31012015anapaulalucas";
22+
const char* mqttServer = "broker.hivemq.com";
23+
const int mqttPort = 1883;
24+
const char* mqttUser = "";
25+
const char* mqttPassword = "";
26+
27+
char json[100];
28+
29+
WiFiClient espClient;
30+
PubSubClient client(espClient);
31+
32+
void setup()
33+
{
34+
Serial.begin(115200);
35+
Serial.println("DHTxx test!");
36+
dht.begin();
37+
38+
WiFi.begin(ssid, password);
39+
40+
while (WiFi.status() != WL_CONNECTED) {
41+
delay(500);
42+
Serial.println("Connecting to WiFi..");
43+
}
44+
45+
Serial.println("Connected to the WiFi network");
46+
47+
client.setServer(mqttServer, mqttPort);
48+
49+
while (!client.connected()) {
50+
Serial.println("Connecting to MQTT...");
51+
52+
if (client.connect("NodeMCUFulvio", mqttUser, mqttPassword)) {
53+
54+
Serial.println("connected");
55+
56+
} else {
57+
58+
Serial.print("failed with state ");
59+
Serial.print(client.state());
60+
delay(2000);
61+
62+
}
63+
}
64+
}
65+
66+
void loop()
67+
{
68+
float valor_co2 = gasSensor.getPPM();
69+
// testa se retorno é valido, caso contrário algo está errado.
70+
if (isnan(valor_co2))
71+
{
72+
Serial.println("Failed to read from MQ-135");
73+
}
74+
else
75+
{
76+
77+
DynamicJsonBuffer jBuffer;
78+
JsonObject& root = jBuffer.createObject();
79+
80+
root["nivel_co2"] = valor_co2;
81+
82+
root.printTo(Serial);
83+
Serial.println();
84+
85+
root.printTo(json);
86+
87+
client.publish("smartcity/tdc2", json);
88+
89+
}
90+
91+
client.loop();
92+
delay(1000);
93+
94+
}

0 commit comments

Comments
 (0)