Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266mDNS.h>
- #include <WiFiUdp.h>
- #include <ArduinoOTA.h>
- #include <ESP8266httpUpdate.h>
- #include <ESP8266HTTPClient.h>
- #include <ArduinoJson.h>
- #include <FS.h>
- #include <ESPMetRED.h>
- const char* ssid = "xxx";
- const char* password = "yyy";
- const char* MQTT_PUBLISH_TOPIC = "lumiblob/receive";
- const char* MQTT_SUBSCRIBE_TOPIC = "lumiblob/send";
- const char* CLIENT_ID = "ESPMetRED1";
- const char* WIFI_SSID = "xxx;
- const char* WIFI_PASSWORD = "yyyy";
- const char* MQTT_SERVER = "192.168.1.13";
- const char* MQTT_USER = "User_Name";
- const char* MQTT_PASSWORD = "Password";
- ESPMetRED client;
- // RGB INTITIALISE
- #define REDPIN D1
- #define GREENPIN D2
- #define BLUEPIN D3
- // END RGB Initialise
- //*********SETUP*****
- void setup() {
- //JI Variables
- String JIESP="TBC";
- String JIMAC="TBC";
- int esp = 0;
- // end JI Variables
- // BEGIN WIFISETUP
- Serial.begin(115200);
- Serial.println("Connecting WiFi...");
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.waitForConnectResult() != WL_CONNECTED) {
- Serial.println("Connection Failed! Rebooting...");
- delay(5000);
- ESP.restart();
- }
- // RGB LIGHT INITIALISE
- pinMode(REDPIN, OUTPUT);
- pinMode(GREENPIN, OUTPUT);
- pinMode(BLUEPIN, OUTPUT);
- Serial.begin(115200);
- Serial.println("Finished RGB setup");
- // END RGB INITIALISE
- // Port defaults to 8266
- // ArduinoOTA.setPort(8266);
- // Hostname defaults to esp8266-[ChipID]
- // ArduinoOTA.setHostname("JIesp8266");
- // No authentication by default
- // ArduinoOTA.setPassword((const char *)"123");
- ArduinoOTA.onStart([]() {
- Serial.println("Start");
- });
- ArduinoOTA.onEnd([]() {
- Serial.println("\nEnd");
- });
- ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
- Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
- });
- ArduinoOTA.onError([](ota_error_t error) {
- Serial.printf("Error[%u]: ", error);
- if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
- else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
- else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
- else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
- else if (error == OTA_END_ERROR) Serial.println("End Failed");
- });
- ArduinoOTA.begin();
- Serial.println("Ready");
- // JI code
- // Commands can be found here : https://www.arduino.cc/en/Reference/WiFi
- Serial.println('\n');
- Serial.print("Local Mac Address : ");
- Serial.println(WiFi.macAddress());
- Serial.print("Connected to Access Point: ");
- Serial.println(WiFi.SSID());
- // end JI code
- Serial.print("Connected as IP address: ");
- Serial.println(WiFi.localIP());
- // END WIFI SETUP
- // JI code to set which ESP8266
- String JIMAC2={WiFi.macAddress()};
- Serial.println("James JIMAC2 variable = ");
- Serial.println(JIMAC2);
- if (JIMAC2=="A0:20:A6:13:E7:C3")
- {
- String JIESP="ESP1";
- int esp = 1;
- Serial.print("This is ESP A : ");
- Serial.println(JIESP);
- Serial.println(esp);
- }
- Serial.print("This is ESP B : ");
- Serial.println(JIESP);
- Serial.print("This is ESP C : ");
- Serial.println(esp);
- // End JI code to set which ESP8266
- client.setcallback(callback);
- client.Publish(MQTT_PUBLISH_TOPIC,"ESP8266 Number 1 connected");
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Begin code to publish to MQTT to say connected
- client.Publish("lumiblob/receive","ESPHEREA");
- client.setcallback(callback);
- client.Publish("lumiblob/receive","ESPHEREB");
- client.Publish(MQTT_PUBLISH_TOPIC,"ESPHEREC");
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++End code to publish MQTT to say connected
- // end void setup
- }
- // Perform your desired functions on payload received;
- void callback(String mqtt_payload)
- {
- // ENTER CODE HERE - Perform your desired functions on payload received;
- Serial.println('\n');
- Serial.println("ESP8266 received : "+ mqtt_payload);
- client.Publish(MQTT_PUBLISH_TOPIC,"ESP8266 received " + mqtt_payload);
- if (mqtt_payload="GO")
- {
- Serial.print("GO LOOP");
- analogWrite(REDPIN, 0);
- analogWrite(GREENPIN, 1000);
- analogWrite(BLUEPIN, 1000);
- delay(1000);
- analogWrite(REDPIN, 1000);
- analogWrite(GREENPIN, 0);
- analogWrite(BLUEPIN, 1000);
- delay(1000);
- analogWrite(REDPIN, 1000);
- analogWrite(GREENPIN, 1000);
- analogWrite(BLUEPIN, 0);
- delay(1000);
- analogWrite(REDPIN, 1000);
- analogWrite(BLUEPIN, 1000);
- analogWrite(GREENPIN, 1000);
- }
- // END ENTER CODE - Perform your desired functions on payload received ABOVE HERE
- }
- // ******* VOID LOOP START *************
- void loop() {
- ArduinoOTA.handle();
- client.keepalive();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement