Advertisement
Guest User

Untitled

a guest
May 5th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1.  
  2. /* ESP8266+Arduino IDE Test code
  3. Object :
  4. - TCP Socket(port 4444) Server.
  5. - ESP8266 AP mode test.
  6. Requirement :
  7. - ESP8266WiFi.h Library (https://github.com/esp8266/Arduino)
  8. - PubSubClient.h Library (https://github.com/Imroy/pubsubclient)
  9. - DW.miniESP board (https://github.com/deaware/dwminiesp_firmware_support)
  10. - Arduino IDE 1.6.4
  11.  
  12. Write by : Myconfix (http://www.myconfix.com/)
  13. Fanpage : https://www.facebook.com/myconfix
  14. github : https://github.com/myconfix/ESP8266ArduinoExample
  15. */
  16.  
  17.  
  18. #include <ESP8266WiFi.h>
  19. #include <PubSubClient.h>
  20.  
  21. // Prepare AP id&password.
  22. //const char *ssid = "Desktech"; //your access point ssid // cannot be longer than 32 characters!
  23. //const char *password = "N1B8072824"; //your access point password
  24.  
  25. const char *ssid = "ifce-alunos"; //your access point ssid // cannot be longer than 32 characters!
  26. const char *password = "ifce4lun0s"; //your access point password
  27.  
  28.  
  29. //Prepare Mqtt connection data
  30. const char *MQTTBroker = "m10.cloudmqtt.com";
  31. const char *MQTTuser = "water";
  32. const char *MQTTpassword = "12345678";
  33. const char *Topic = "/water";
  34. const char *Topicid = "/water";
  35. uint16_t port = 11536;
  36. //uint16_t port = 1883; //defalut port
  37.  
  38.  
  39. int count = 0;
  40.  
  41. PubSubClient mqtt(MQTTBroker,port);
  42.  
  43. void mqtt_callback(char* topic, byte* payload, unsigned int length)
  44. {
  45.  
  46. String msg;
  47.  
  48. //obtem a string do payload recebido
  49. for(int i = 0; i < length; i++)
  50. {
  51. char c = (char)payload[i];
  52. msg += c;
  53. }
  54.  
  55. //toma ação dependendo da string recebida:
  56. //verifica se deve colocar nivel alto de tensão na saída D0:
  57. //IMPORTANTE: o Led já contido na placa é acionado com lógica invertida (ou seja,
  58. //enviar HIGH para o output faz o Led apagar / enviar LOW faz o Led acender)
  59. if (msg.equals("L"))
  60. {
  61. Serial.println("Aqui2");
  62. // digitalWrite(D0, LOW);
  63. // EstadoSaida = '1';
  64. }
  65.  
  66. //verifica se deve colocar nivel alto de tensão na saída D0:
  67. if (msg.equals("D"))
  68. {
  69. // digitalWrite(D0, HIGH);
  70. // EstadoSaida = '0';
  71. }
  72.  
  73. }
  74.  
  75. void callback(const MQTT::Publish& pub){
  76. // handle message arrived
  77. // Serial.println("Aqui");
  78. Serial.println(pub.payload_string());
  79.  
  80. if (pub.payload_string().equals("L"))
  81. {
  82. Serial.println("Liga");
  83. digitalWrite(D4, LOW);
  84. // EstadoSaida = '1';
  85. }
  86.  
  87. //verifica se deve colocar nivel alto de tensão na saída D0:
  88. if (pub.payload_string().equals("D"))
  89. {
  90. digitalWrite(D4, HIGH);
  91. // EstadoSaida = '0';
  92. }
  93. }
  94. void setup()
  95. {
  96. pinMode(D4, OUTPUT);
  97. Serial.begin(115200);
  98. WiFi.mode(WIFI_STA);
  99. WiFi.disconnect();
  100. WiFi.begin(ssid, password);
  101. while (WiFi.status() != WL_CONNECTED) {
  102. delay(500);
  103. Serial.print(".");
  104. }
  105. Serial.println("");
  106. Serial.println("WiFi connected");
  107. Serial.println("IP address: ");
  108. Serial.println(WiFi.localIP());
  109. WiFi.printDiag(Serial);
  110.  
  111. mqtt.set_callback(callback); //Set Callback function
  112.  
  113. if (mqtt.connect(MQTT::Connect("esp8266").set_auth(MQTTuser,MQTTpassword))){
  114. mqtt.publish(Topic, "Hello"); //publish
  115. mqtt.subscribe(Topicid); //subscribe
  116. }
  117.  
  118. }
  119.  
  120.  
  121. void loop()
  122. {
  123.  
  124.  
  125. count++;
  126. String con = String(count);
  127. // delay(1000);
  128. // Serial.println(con);
  129.  
  130. // mqtt.publish(Topic, con); //publish
  131. // if(mqtt.subscribe(Topicid)!=0) //subscribe
  132. // {
  133. // Serial.println(Topicid);
  134. // }
  135.  
  136.  
  137. mqtt.loop();
  138.  
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement