Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <ArduinoJson.h>
- SoftwareSerial s(D6,D5);
- const char* ssid = "cro";
- const char* password = "cheguevara";
- const char* mqtt_server = "test.mosquitto.org";
- WiFiClient espClient;
- PubSubClient client(espClient);
- int pornit=0;
- int Md =0;
- int lim1 =0;
- int lim2 =0;
- int lim3 =0;
- int lim4 =0;
- int lim5 =0;
- int lim6 =0;
- int pir1 =0;
- int pir2 =0;
- int apa =0;
- int temperatura =0;
- void setup() {
- // Initialize Serial port
- Serial.begin(9600);
- setup_wifi();
- client.setServer(mqtt_server, 1883);
- s.begin(9600);
- while (!Serial) continue;
- }
- void loop() {
- const size_t capacity = JSON_OBJECT_SIZE(16) + 16;
- DynamicJsonDocument doc(capacity);
- DeserializationError error = deserializeJson(doc, s);
- if (error) {
- Serial.print(F("deserializeJson() failed: "));
- Serial.println(error.c_str());
- return;
- }
- Serial.println("JSON received and parsed");
- serializeJsonPretty(doc,Serial);
- pornit=doc["pornit"];
- Md=doc["mod"];
- lim1=doc["lim1"];
- lim2=doc["lim2"];
- lim3=doc["lim3"];
- lim4=doc["lim4"];
- lim5=doc["lim5"];
- lim6=doc["lim6"];
- pir1=doc["pir1"];
- pir2=doc["pir2"];
- apa=doc["apa"];
- temperatura=doc["temperatura"];
- if (!client.connected()) {
- reconnect();
- }
- if(!client.loop())
- client.connect("ESP8266Client");
- static char z1[10];
- static char z2[10];
- static char z3[10];
- static char z4[10];
- static char z5[10];
- static char z6[10];
- static char z7[10];
- static char z8[10];
- static char z9[10];
- static char z10[10];
- static char z11[10];
- static char z12[10];
- dtostrf(pornit, 6, 4, z1);
- dtostrf(Md, 6, 4, z2);
- dtostrf(lim1, 6, 4, z3);
- dtostrf(lim2, 6, 4, z4);
- dtostrf(lim3, 6, 4, z5);
- dtostrf(lim4, 6, 5, z6);
- dtostrf(lim5, 6, 4, z7);
- dtostrf(lim6, 6, 4, z8);
- dtostrf(pir1, 6, 4, z9);
- dtostrf(pir2, 6, 5, z10);
- dtostrf(apa, 6, 4, z11);
- dtostrf(temperatura, 6, 4, z12);
- client.publish("pornit", z1);
- client.publish("Md", z2);
- client.publish("lim1", z3);
- client.publish("lim2", z4);
- client.publish("lim3", z5);
- client.publish("lim4", z6);
- client.publish("lim5", z7);
- client.publish("lim6", z8);
- client.publish("pir1", z9);
- client.publish("pir2", z10);
- client.publish("apa", z11);
- client.publish("temperatura", z12);
- }
- void setup_wifi() {
- delay(10);
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("WiFi connected - ESP IP address: ");
- Serial.println(WiFi.localIP());
- }
- void reconnect() {
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- if (client.connect("ESP8266Client")) {
- Serial.println("connected");
- //client.subscribe("room/lamp");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- delay(5000);
- }
- }
- }
Add Comment
Please, Sign In to add comment