Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****
- All the resources for this project:
- https://rntlab.com/
- This is the code to flash sonoff basic. Search for sonoffxxX
- and replace xx with next higher number.
- As of 6/11/2019 there are 01X , 02X ,03X, 04X, 05X, 06X, 10X, A1, A2, in operation.
- The X designates the device that sends the state message.
- The XA designates the device that sends a state message and has manual control
- *****/
- // Loading the ESP8266WiFi library and the PubSubClient library
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- // Change the credentials below, so your ESP8266 connects to your router
- const char* ssid = "xxxxx";
- const char* password = "xxxxxxx";
- // Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
- const char* mqtt_server = "192.168.1.xxx";
- // Initializes the espClient
- WiFiClient espClient;
- PubSubClient client(espClient);
- // GPIOs of your ESP8266 on your sonoffxxX
- int gpio13Led = 13;
- int gpio12Relay = 12;
- int buttonPin = 0;
- // Variables will change:
- int ledState = HIGH; // the current state of the output pin
- int buttonState; // the current reading from the input pin
- int lastButtonState = LOW; // the previous reading from the input pin
- // the following variables are unsigned longs because the time, measured in
- // milliseconds, will quickly become a bigger number than can be stored in an int.
- unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
- unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
- // Don't change the function below. This functions connects your ESP8266 to your router
- void setup_wifi() {
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("WiFi connected - ESP IP address: ");
- Serial.println(WiFi.localIP());
- }
- // This functions is executed when some device publishes a message
- // to a topic that your ESP8266 is subscribed to
- // Change the function below to add logic to your program,
- // so when a device publishes a message to a topic that
- // your ESP8266 is subscribed you can actually do something
- void callback(String topic, byte* message, unsigned int length) {
- Serial.print("Message arrived on topic: ");
- Serial.print(topic);
- Serial.print(". Message: ");
- String messageTemp;
- for (int i = 0; i < length; i++) {
- Serial.print((char)message[i]);
- messageTemp += (char)message[i];
- }
- Serial.println();
- // Feel free to add more if statements to control more GPIOs with MQTT
- // If a message is received on the topic home/office/sonoffxxX,
- // you check if the message is either 1 or 0.
- // Turns the ESP GPIO according to the message
- if(topic=="home/office/sonoff02X"){
- Serial.print("Changing sonoff02X to ");
- if(messageTemp == "on");
- else if (ledState == HIGH); {
- digitalWrite(gpio13Led, LOW); //turns LED ON
- digitalWrite(gpio12Relay, HIGH); //energizes relay
- Serial.print("On");
- client.publish("home/office/sonoff02X/state","on"); //sends "ON" message to NR
- }
- }
- else if(messageTemp == "off"){
- digitalWrite(gpio13Led, HIGH); //turns LED OFF
- digitalWrite(gpio12Relay, LOW); //deenergizes relay
- Serial.print("Off");
- client.publish("home/office/sonoff02X/state","off");//sends "OFF" message to NR
- }
- Serial.println();
- }
- // This functions reconnects your ESP8266 to your MQTT broker
- // Change the function below if you want to subscribe to more topics with your ESP8266
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- /*
- YOU MIGHT NEED TO CHANGE THIS LINE, IF YOU'RE HAVING
- PROBLEMS WITH MQTT MULTIPLE CONNECTIONS
- To change the ESP device ID, you will have
- to give a new name to the ESP8266.
- Here's how it looks:
- if (client.connect("ESP8266Client")) {
- You can do it like this:
- if (client.connect("ESP1_Office")) {
- Then, for the other ESP:
- if (client.connect("ESP2_Garage")) {
- That should solve your MQTT multiple connections problem
- */
- if (client.connect("sonoff02X")) {
- Serial.println("connected");
- // Subscribe or resubscribe to a topic
- // You can subscribe to more topics (to control more sonoffxxXs)
- client.subscribe("home/office/sonoff02X");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- // The setup function sets your ESP GPIOs to Outputs,
- // starts the serial communication at a baud rate of 115200
- // Sets your mqtt broker and sets the callback function
- // The callback function is what receives messages
- // and actually controls the LEDs
- void setup() {
- // preparing GPIOs
- pinMode(buttonPin, INPUT);
- pinMode(gpio13Led, OUTPUT);
- digitalWrite(gpio13Led, HIGH);
- pinMode(gpio12Relay, OUTPUT);
- digitalWrite(gpio12Relay, HIGH);
- Serial.begin(115200);
- setup_wifi();
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- }
- // For this project, you don't need to change anything in the loop function.
- // Basically it ensures that you ESP is connected to your broker
- void loop() {
- // read the state of the switch into a local variable:
- int reading = digitalRead(buttonPin);
- // check to see if you just pressed the button
- // (i.e. the input went from LOW to HIGH), and you've waited long enough
- // since the last press to ignore any noise:
- // If the switch changed, due to noise or pressing:
- if (reading != lastButtonState) {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- // whatever the reading is at, it's been there for longer than the debounce
- // delay, so take it as the actual current state:
- // if the button state has changed:
- if (reading != buttonState) {
- buttonState = reading;
- // only toggle the LED if the new button state is HIGH
- if (buttonState == HIGH) {
- ledState = !ledState;
- }
- }
- }
- // set the LED:
- digitalWrite(gpio13Led, ledState);
- // save the reading. Next time through the loop, it'll be the lastButtonState:
- lastButtonState = reading;
- if (!client.connected()) {
- reconnect();
- }
- if(!client.loop())
- client.connect("sonoff02X");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement