Guest User

Untitled

a guest
Jun 19th, 2017
78
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266mDNS.h>
  3. #include <WiFiUdp.h>
  4. #include <ArduinoOTA.h>
  5. #include <ESP8266httpUpdate.h>
  6. #include <ESP8266HTTPClient.h>
  7. #include <ArduinoJson.h>
  8. #include <FS.h>
  9. #include <ESPMetRED.h>
  10.  
  11.  
  12. const char* ssid = "xxx";
  13. const char* password = "yyy";
  14.  
  15. const char* MQTT_PUBLISH_TOPIC = "lumiblob/receive";
  16. const char* MQTT_SUBSCRIBE_TOPIC = "lumiblob/send";
  17. const char* CLIENT_ID = "ESPMetRED1";
  18. const char* WIFI_SSID = "xxx;
  19. const char* WIFI_PASSWORD = "yyyy";
  20. const char* MQTT_SERVER = "192.168.1.13";
  21. const char* MQTT_USER = "User_Name";
  22. const char* MQTT_PASSWORD = "Password";
  23.  
  24. ESPMetRED client;
  25.  
  26.  
  27. // RGB INTITIALISE
  28. #define REDPIN D1
  29. #define GREENPIN D2
  30. #define BLUEPIN D3
  31. // END RGB Initialise
  32.  
  33.  
  34.  
  35. //*********SETUP*****
  36. void setup() {
  37.  
  38. //JI Variables
  39. String JIESP="TBC";
  40. String JIMAC="TBC";
  41. int esp = 0;
  42. // end JI Variables
  43.  
  44. // BEGIN WIFISETUP
  45. Serial.begin(115200);
  46. Serial.println("Connecting WiFi...");
  47. WiFi.mode(WIFI_STA);
  48. WiFi.begin(ssid, password);
  49. while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  50. Serial.println("Connection Failed! Rebooting...");
  51. delay(5000);
  52. ESP.restart();
  53. }
  54.  
  55. // RGB LIGHT INITIALISE
  56. pinMode(REDPIN, OUTPUT);
  57. pinMode(GREENPIN, OUTPUT);
  58. pinMode(BLUEPIN, OUTPUT);
  59. Serial.begin(115200);
  60. Serial.println("Finished RGB setup");
  61. // END RGB INITIALISE
  62.  
  63. // Port defaults to 8266
  64. // ArduinoOTA.setPort(8266);
  65.  
  66. // Hostname defaults to esp8266-[ChipID]
  67. // ArduinoOTA.setHostname("JIesp8266");
  68.  
  69. // No authentication by default
  70. // ArduinoOTA.setPassword((const char *)"123");
  71.  
  72. ArduinoOTA.onStart([]() {
  73. Serial.println("Start");
  74. });
  75. ArduinoOTA.onEnd([]() {
  76. Serial.println("\nEnd");
  77. });
  78. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  79. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  80. });
  81. ArduinoOTA.onError([](ota_error_t error) {
  82. Serial.printf("Error[%u]: ", error);
  83. if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  84. else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  85. else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  86. else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  87. else if (error == OTA_END_ERROR) Serial.println("End Failed");
  88. });
  89. ArduinoOTA.begin();
  90. Serial.println("Ready");
  91. // JI code
  92. // Commands can be found here : https://www.arduino.cc/en/Reference/WiFi
  93. Serial.println('\n');
  94. Serial.print("Local Mac Address : ");
  95. Serial.println(WiFi.macAddress());
  96. Serial.print("Connected to Access Point: ");
  97. Serial.println(WiFi.SSID());
  98. // end JI code
  99. Serial.print("Connected as IP address: ");
  100. Serial.println(WiFi.localIP());
  101.  
  102. // END WIFI SETUP
  103.  
  104. // JI code to set which ESP8266
  105. String JIMAC2={WiFi.macAddress()};
  106. Serial.println("James JIMAC2 variable = ");
  107. Serial.println(JIMAC2);
  108.  
  109. if (JIMAC2=="A0:20:A6:13:E7:C3")
  110. {
  111. String JIESP="ESP1";
  112. int esp = 1;
  113. Serial.print("This is ESP A : ");
  114. Serial.println(JIESP);
  115. Serial.println(esp);
  116. }
  117.  
  118. Serial.print("This is ESP B : ");
  119. Serial.println(JIESP);
  120. Serial.print("This is ESP C : ");
  121. Serial.println(esp);
  122.  
  123. // End JI code to set which ESP8266
  124.  
  125. client.setcallback(callback);
  126. client.Publish(MQTT_PUBLISH_TOPIC,"ESP8266 Number 1 connected");
  127.  
  128. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Begin code to publish to MQTT to say connected
  129. client.Publish("lumiblob/receive","ESPHEREA");
  130. client.setcallback(callback);
  131. client.Publish("lumiblob/receive","ESPHEREB");
  132. client.Publish(MQTT_PUBLISH_TOPIC,"ESPHEREC");
  133. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++End code to publish MQTT to say connected
  134.  
  135. // end void setup
  136. }
  137.  
  138. // Perform your desired functions on payload received;
  139. void callback(String mqtt_payload)
  140. {
  141. // ENTER CODE HERE - Perform your desired functions on payload received;
  142. Serial.println('\n');
  143. Serial.println("ESP8266 received : "+ mqtt_payload);
  144. client.Publish(MQTT_PUBLISH_TOPIC,"ESP8266 received " + mqtt_payload);
  145.  
  146. if (mqtt_payload="GO")
  147. {
  148. Serial.print("GO LOOP");
  149. analogWrite(REDPIN, 0);
  150. analogWrite(GREENPIN, 1000);
  151. analogWrite(BLUEPIN, 1000);
  152. delay(1000);
  153. analogWrite(REDPIN, 1000);
  154. analogWrite(GREENPIN, 0);
  155. analogWrite(BLUEPIN, 1000);
  156. delay(1000);
  157. analogWrite(REDPIN, 1000);
  158. analogWrite(GREENPIN, 1000);
  159. analogWrite(BLUEPIN, 0);
  160. delay(1000);
  161. analogWrite(REDPIN, 1000);
  162. analogWrite(BLUEPIN, 1000);
  163. analogWrite(GREENPIN, 1000);
  164. }
  165.  
  166. // END ENTER CODE - Perform your desired functions on payload received ABOVE HERE
  167. }
  168.  
  169. // ******* VOID LOOP START *************
  170. void loop() {
  171. ArduinoOTA.handle();
  172.  
  173. client.keepalive();
  174.  
  175. }
RAW Paste Data