Advertisement
Guest User

Code on Node

a guest
Jun 4th, 2019
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <SoftwareSerial.h>
  4. #include <String.h>
  5. SoftwareSerial fromUno(D5,D6); // (Rx, Tx)
  6.  
  7. char* ssid = "B";
  8. char* password = "A";
  9. const char* mqttServer = "Z";
  10. const int mqttPort = 1;
  11. const char* mqttUser = "Y";
  12. const char* mqttPassword = "X;
  13.  
  14. WiFiClient espClient;
  15. PubSubClient client(espClient);
  16.  
  17. void setup() {
  18.  
  19. Serial.begin(9600);
  20. fromUno.begin(115200);
  21.  
  22. WiFi.begin(ssid, password);
  23.  
  24. while (WiFi.status() != WL_CONNECTED) {
  25. delay(500);
  26. Serial.println("Connecting to WiFi..");
  27. }
  28.  
  29. Serial.println("Connected to the WiFi network");
  30.  
  31. client.setServer(mqttServer, mqttPort);
  32.  
  33. while (!client.connected()) {
  34. Serial.println("Connecting to MQTT...");
  35.  
  36. if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
  37.  
  38. Serial.println("connected");
  39.  
  40. } else {
  41.  
  42. Serial.print("failed with state ");
  43. Serial.print(client.state());
  44. delay(2000);
  45.  
  46. }
  47. }
  48. }
  49.  
  50. void loop() {
  51. char json[100];
  52. json[0] = fromUno.read();
  53. String json2 = "{\"planta\":{\"umidade\":200,\"criticidade\":1}}";
  54.  
  55. Serial.println("Sending message to MQTT topic..");
  56.  
  57. if (client.publish("test", json2) == true) {
  58. Serial.println("Success sending message");
  59. } else {
  60. Serial.println("Error sending message");
  61. }
  62.  
  63. client.loop();
  64. Serial.println("-------------");
  65.  
  66. delay(10000);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement