Advertisement
Masoko

esp mqtt and relay

Feb 27th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1.  
  2. /*
  3.  
  4.  It connects to an MQTT server then:
  5.   - on 0 switches off relay
  6.   - on 1 switches on relay
  7.   - on 2 switches the state of the relay
  8.  
  9.   - sends 0 on off relay
  10.   - sends 1 on on relay
  11.  
  12.  It will reconnect to the server if the connection is lost using a blocking
  13.  reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
  14.  achieve the same result without blocking the main loop.
  15.  
  16.  The current state is stored in EEPROM and restored on bootup
  17.  
  18. */
  19.  
  20. #include <ESP8266WiFi.h>
  21. #include <PubSubClient.h>
  22. #include <Bounce2.h>
  23. #include <EEPROM.h>
  24.  
  25.  
  26. const char* ssid = "xxxx";
  27. const char* password = "xxxx";
  28. const char* mqtt_server = "192.168.0.103";
  29.  
  30. WiFiClient espClient;
  31. PubSubClient client(espClient);
  32. long lastMsg = 0;
  33. char msg[50];
  34. int value = 0;
  35.  
  36. const char* outTopic = "Sonoff1out";
  37. const char* inTopic = "Sonoff1in";
  38.  
  39. int relay_pin = 12;
  40. int button_pin = 0;
  41. bool relayState = LOW;
  42.  
  43. // Instantiate a Bounce object :
  44. Bounce debouncer = Bounce();
  45.  
  46.  
  47. void setup_wifi() {
  48.  
  49.   delay(10);
  50.   // We start by connecting to a WiFi network
  51.   Serial.println();
  52.   Serial.print("Connecting to ");
  53.   Serial.println(ssid);
  54.  
  55.   WiFi.begin(ssid, password);
  56.  
  57.   while (WiFi.status() != WL_CONNECTED) {
  58.     extButton();
  59.     for(int i = 0; i<500; i++){
  60.       extButton();
  61.       delay(1);
  62.     }
  63.     Serial.print(".");
  64.   }
  65.   digitalWrite(13, LOW);
  66.   delay(500);
  67.   digitalWrite(13, HIGH);
  68.   delay(500);
  69.   digitalWrite(13, LOW);
  70.   delay(500);
  71.   digitalWrite(13, HIGH);
  72.   Serial.println("");
  73.   Serial.println("WiFi connected");
  74.   Serial.println("IP address: ");
  75.   Serial.println(WiFi.localIP());
  76. }
  77.  
  78. void callback(char* topic, byte* payload, unsigned int length) {
  79.   Serial.print("Message arrived [");
  80.   Serial.print(topic);
  81.   Serial.print("] ");
  82.   for (int i = 0; i < length; i++) {
  83.     Serial.print((char)payload[i]);
  84.   }
  85.   Serial.println();
  86.  
  87.   // Switch on the LED if an 1 was received as first character
  88.   if ((char)payload[0] == '0') {
  89.     digitalWrite(relay_pin, LOW);   // Turn the LED on (Note that LOW is the voltage level
  90.     Serial.println("relay_pin -> LOW");
  91.     relayState = LOW;
  92.     EEPROM.write(0, relayState);    // Write state to EEPROM
  93.     EEPROM.commit();
  94.   client.publish(outTopic, "0");
  95.   } else if ((char)payload[0] == '1') {
  96.     digitalWrite(relay_pin, HIGH);  // Turn the LED off by making the voltage HIGH
  97.     Serial.println("relay_pin -> HIGH");
  98.     relayState = HIGH;
  99.     EEPROM.write(0, relayState);    // Write state to EEPROM
  100.     EEPROM.commit();
  101.   client.publish(outTopic, "1");
  102.   } else if ((char)payload[0] == '2') {
  103.     relayState = !relayState;
  104.     digitalWrite(relay_pin, relayState);  // Turn the LED off by making the voltage HIGH
  105.     Serial.print("relay_pin -> switched to ");
  106.     Serial.println(relayState);
  107.     EEPROM.write(0, relayState);    // Write state to EEPROM
  108.     EEPROM.commit();
  109.  
  110.     if (relayState == 1){
  111.       client.publish(outTopic, "1");
  112.      } else if (relayState == 0){
  113.       client.publish(outTopic, "0");
  114.     }
  115.      
  116.   }
  117. }
  118.  
  119. void reconnect() {
  120.   // Loop until we're reconnected
  121.   while (!client.connected()) {
  122.     Serial.print("Attempting MQTT connection...");
  123.     // Attempt to connect
  124.     if (client.connect("ESP8266Client")) {
  125.       Serial.println("connected");
  126.       // Once connected, publish an announcement...
  127.       client.publish(outTopic, "Sonoff1 booted");
  128.       // ... and resubscribe
  129.       client.subscribe(inTopic);
  130.     } else {
  131.       Serial.print("failed, rc=");
  132.       Serial.print(client.state());
  133.       Serial.println(" try again in 5 seconds");
  134.       // Wait 5 seconds before retrying
  135.       for(int i = 0; i<5000; i++){
  136.         extButton();
  137.         delay(1);
  138.       }
  139.     }
  140.   }
  141. }
  142.  
  143. void extButton() {
  144.   debouncer.update();
  145.    
  146.    // Call code if Bounce fell (transition from HIGH to LOW) :
  147.    if ( debouncer.fell() ) {
  148.      Serial.println("Debouncer fell");
  149.      // Toggle relay state :
  150.      relayState = !relayState;
  151.      digitalWrite(relay_pin,relayState);
  152.      EEPROM.write(0, relayState);    // Write state to EEPROM
  153.      if (relayState == 1){
  154.       client.publish(outTopic, "1");
  155.      }
  156.      else if (relayState == 0){
  157.       client.publish(outTopic, "0");
  158.      }
  159.    }
  160. }
  161.  
  162. void setup() {
  163.   EEPROM.begin(512);              // Begin eeprom to store on/off state
  164.   pinMode(relay_pin, OUTPUT);     // Initialize the relay pin as an output
  165.   pinMode(button_pin, INPUT);     // Initialize the relay pin as an output
  166.   pinMode(13, OUTPUT);
  167.   relayState = EEPROM.read(0);
  168.   digitalWrite(relay_pin,relayState);
  169.  
  170.   debouncer.attach(button_pin);   // Use the bounce2 library to debounce the built in button
  171.   debouncer.interval(50);         // Input must be low for 50 ms
  172.  
  173.   digitalWrite(13, LOW);          // Blink to indicate setup
  174.   delay(500);
  175.   digitalWrite(13, HIGH);
  176.   delay(500);
  177.  
  178.   Serial.begin(115200);
  179.   setup_wifi();                   // Connect to wifi
  180.   client.setServer(mqtt_server, 1883);
  181.   client.setCallback(callback);
  182. }
  183.  
  184. void loop() {
  185.  
  186.   if (!client.connected()) {
  187.     reconnect();
  188.   }
  189.   client.loop();
  190.   extButton();
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement