Advertisement
Yagotonini

Arduino

Apr 9th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. #define DEBUG
  5.  
  6. #define L1 D4 //pino de saida para acionamento da Lampada L1
  7.  
  8. //informações da rede WIFI
  9. const char* ssid = "RedeSegura11"; //SSID da rede WIFI
  10. const char* password = "987654321"; //senha da rede wifi
  11.  
  12. //informações do broker MQTT - Verifique as informações geradas pelo CloudMQTT
  13. const char* mqttServer = "m16.cloudmqtt.com"; //server
  14. const char* mqttUser = "wwwnjzwn"; //user
  15. const char* mqttPassword = "ZxePSQvbzxPp"; //password
  16. const int mqttPort = 11441; //port
  17. const char* mqttTopicSub ="casa/L1"; //tópico que sera assinado
  18.  
  19.  
  20. WiFiClient espClient;
  21. PubSubClient client(espClient);
  22.  
  23. void setup() {
  24.  
  25. Serial.begin(115200);
  26. pinMode(L1, OUTPUT);
  27.  
  28. WiFi.begin(ssid, password);
  29.  
  30. while (WiFi.status() != WL_CONNECTED) {
  31. delay(500);
  32. #ifdef DEBUG
  33. Serial.println("Conectando ao WiFi..");
  34. #endif
  35. }
  36. #ifdef DEBUG
  37. Serial.println("Conectado na rede WiFi");
  38. #endif
  39.  
  40. client.setServer(mqttServer, mqttPort);
  41. client.setCallback(callback);
  42.  
  43. while (!client.connected()) {
  44. #ifdef DEBUG
  45. Serial.println("Conectando ao Broker MQTT...");
  46. #endif
  47.  
  48. if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
  49. #ifdef DEBUG
  50. Serial.println("Conectado");
  51. #endif
  52.  
  53. } else {
  54. #ifdef DEBUG
  55. Serial.print("falha estado ");
  56. Serial.print(client.state());
  57. #endif
  58. delay(2000);
  59.  
  60. }
  61. }
  62.  
  63. //subscreve no tópico
  64. client.subscribe(mqttTopicSub);
  65.  
  66. }
  67.  
  68. void callback(char* topic, byte* payload, unsigned int length) {
  69.  
  70. //armazena msg recebida em uma sring
  71. payload[length] = '\0';
  72. String strMSG = String((char*)payload);
  73.  
  74. #ifdef DEBUG
  75. Serial.print("Mensagem chegou do tópico: ");
  76. Serial.println(topic);
  77. Serial.print("Mensagem:");
  78. Serial.print(strMSG);
  79. Serial.println();
  80. Serial.println("-----------------------");
  81. #endif
  82.  
  83. //aciona saída conforme msg recebida
  84. if (strMSG == "1"){ //se msg "1"
  85. digitalWrite(L1, LOW); //coloca saída em LOW para ligar a Lampada - > o módulo RELE usado tem acionamento invertido. Se necessário ajuste para o seu modulo
  86. }else if (strMSG == "0"){ //se msg "0"
  87. digitalWrite(L1, HIGH); //coloca saída em HIGH para desligar a Lampada - > o módulo RELE usado tem acionamento invertido. Se necessário ajuste para o seu modulo
  88. }
  89.  
  90. }
  91.  
  92. //função pra reconectar ao servidor MQTT
  93. void reconect() {
  94. //Enquanto estiver desconectado
  95. while (!client.connected()) {
  96. #ifdef DEBUG
  97. Serial.print("Tentando conectar ao servidor MQTT");
  98. #endif
  99.  
  100. bool conectado = strlen(mqttUser) > 0 ?
  101. client.connect("ESP8266Client", mqttUser, mqttPassword) :
  102. client.connect("ESP8266Client");
  103.  
  104. if(conectado) {
  105. #ifdef DEBUG
  106. Serial.println("Conectado!");
  107. #endif
  108. //subscreve no tópico
  109. client.subscribe(mqttTopicSub, 1); //nivel de qualidade: QoS 1
  110. } else {
  111. #ifdef DEBUG
  112. Serial.println("Falha durante a conexão.Code: ");
  113. Serial.println( String(client.state()).c_str());
  114. Serial.println("Tentando novamente em 10 s");
  115. #endif
  116. //Aguarda 10 segundos
  117. delay(10000);
  118. }
  119. }
  120. }
  121.  
  122. void loop() {
  123. if (!client.connected()) {
  124. reconect();
  125. }
  126. client.loop();
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement