Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include <PubSubClient.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266WebServer.h>
  4.  
  5. const char* wifi_ssid = "XXXXX";
  6. const char* wifi_password = "XXXXX";
  7. char* mqtt_server = "iot.eclipse.org";
  8. char* mqtt_user = "";
  9. char* mqtt_password = "";
  10.  
  11.  
  12. WiFiClient wifiClient;
  13. PubSubClient mqtt_client(mqtt_server, 1883, mqtt_callback, wifiClient);
  14.  
  15. void mqtt_callback(char* topic, byte* payload, unsigned int len) {
  16.  
  17. String tmp=topic;
  18.  
  19. Serial.print("mqtt_callback topic=");
  20. Serial.println(tmp);
  21.  
  22.  
  23. if(tmp.indexOf("/murilo/esp/rele/on")>=0){
  24. Serial.println("relay on");
  25. digitalWrite(E2, 1);
  26. }
  27.  
  28. if(tmp.indexOf("/murilo/esp/rele/off")>=0){
  29. Serial.println("relay off");
  30. digitalWrite(E2, 0);
  31. }
  32.  
  33. }
  34.  
  35.  
  36. String macToStr(const uint8_t* mac)
  37. {
  38. String result;
  39. for (int i = 0; i < 6; ++i) {
  40. result += String(mac[i], 16);
  41. if (i < 5)
  42. result += ':';
  43. }
  44. return result;
  45. }
  46.  
  47. ESP8266WebServer webserver(80);
  48.  
  49. void web_handle_root() {
  50. Serial.println("web root");
  51. String html_payload = "WiFi.macAddress=";
  52.  
  53. uint8_t mac[6];
  54. WiFi.macAddress(mac);
  55. html_payload += macToStr(mac);
  56.  
  57. webserver.send(200, "text/plain", html_payload);
  58. }
  59.  
  60. void web_handle_ron() {
  61. Serial.println("web ron");
  62. webserver.send(200, "text/plain", "Relay on");
  63. digitalWrite(E2, 1);
  64. }
  65. void web_handle_roff() {
  66. Serial.println("web roff");
  67. webserver.send(200, "text/plain", "Relay off");
  68. digitalWrite(E2, 0);
  69. }
  70.  
  71.  
  72.  
  73. void web_setup() {
  74. webserver.on("/", web_handle_root);
  75. webserver.on("/on", web_handle_ron);
  76. webserver.on("/off", web_handle_roff);
  77. webserver.begin();
  78. }
  79.  
  80.  
  81. void mqtt_setup() {
  82.  
  83.  
  84. if (!mqtt_client.connected()) {
  85. uint8_t mac[6];
  86. WiFi.macAddress(mac);
  87.  
  88. String clientName;
  89. clientName += "esp8266-";
  90. clientName += macToStr(mac);
  91.  
  92.  
  93. if (mqtt_client.connect( (char*) clientName.c_str())) {
  94. Serial.println("mqtt connected");
  95. if (mqtt_client.subscribe("/murilo/esp/rele/on")) {
  96. Serial.println("subcribe /murilo/esp/rele/on ok");
  97. } else {
  98. Serial.println("subcribe /murilo/esp/rele/on fail");
  99. }
  100. if (mqtt_client.subscribe("/murilo/esp/rele/off")) {
  101. Serial.println("subcribe /murilo/esp/rele/off ok");
  102. } else {
  103. Serial.println("subcribe /murilo/esp/rele/off fail");
  104. }
  105. } else {
  106. Serial.println("mqtt connect fail");
  107. }
  108.  
  109. }
  110. else {
  111. static int contador=0;
  112. contador++;
  113. String payload="";
  114. payload = contador;
  115.  
  116. mqtt_client.publish("/murilo/esp/rele/count",(char*)payload.c_str());
  117. }
  118.  
  119.  
  120.  
  121.  
  122. }
  123.  
  124. void setup() {
  125. //
  126. Serial.begin(115200);
  127. //
  128. pinMode(E2, OUTPUT);
  129.  
  130.  
  131. //
  132. WiFi.begin(wifi_ssid, wifi_password);
  133.  
  134. //
  135. while (WiFi.status() != WL_CONNECTED) {
  136. delay(500);
  137. Serial.print(".");
  138. }
  139.  
  140. //
  141. Serial.println("");
  142. Serial.println("WiFi connected");
  143. Serial.println("IP address: ");
  144. Serial.println(WiFi.localIP());
  145.  
  146. web_setup();
  147. }
  148.  
  149.  
  150. void loop() {
  151. mqtt_setup();
  152.  
  153. //
  154. webserver.handleClient();
  155. //
  156. mqtt_client.loop();
  157.  
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement