KmanOz/Sonoff-HomeAssistant

Not an issue more of a question

nbittmann opened this issue · 4 comments

Do you think it's possible to change the code so that we can add an external switch?

I'm just thinking of the scenario that I might want to use a switch to control the lifts as well I could just reuse the switch that's on the board but I was wondering if there is any way to use the GPIO 14 which you use for the temp sensor for it?

Thanks

Have a look at the discussion forum here https://community.home-assistant.io/t/sonoff-homeassistant-alternative-firmware-for-sonoff-switches-for-use-with-mqtt-ha You can literally parallel a switch over the current switch if you want external control. Although it would take little to no time to do what you're suggesting, it's an extra version I would need to maintain when it's almost not necessary if you do what that person did.

30e7b189e1a158bcbd027c3f80c34e16b4639fea_1_690x388

@KmanOz Thanks a lot actually saw that earlier on as well and that will serve my purpose fine as well one more question.

I'm in process of making a remote light switch with the ESP8266 and was thinking to reuse your code since it seems rock solid not clutter in here and it works but I'd just want to use a button and an led can I comment out the

pinMode(RELAY, OUTPUT);

to deactivate action on the relay GPIO pin?

If yes then I could pretty much just use your code as a drop in and connect the same way as the Sonoff relay button to GPIO 0 and the Led to GPIO 13 if I'm not mistaken.

Thanks a lot and apologies for asking these questions :)

EDIT:

So I would make it look like this to comment out all relay related lines this should avoid turning on pin 12 as far as I can see the state is determined by the LED?

Would the below work if I connect a switch between GPIO 0 and GND and a LED to GPIO 13?

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Ticker.h>

#define BUTTON          0                                    // (Don't Change for Sonoff)
//#define RELAY           12                                   // (Don't Change for Sonoff)
#define LED             13                                   // (Don't Change for Sonoff)

#define MQTT_CLIENT     "ESP_12F_SWITCH1_v1.0p"              // mqtt client_id (Must be unique for each Sonoff)
#define MQTT_SERVER     "192.168.20.10"                      // mqtt server
#define MQTT_PORT       1883                                 // mqtt port
#define MQTT_TOPIC      "home/switch/living_room/1"          // mqtt topic (Must be unique for each Sonoff)
#define MQTT_USER       "USER"                      // mqtt user
#define MQTT_PASS       "PW"                        // mqtt password

#define WIFI_SSID       "WIFI"                // wifi ssid
#define WIFI_PASS       "homepass"                           // wifi password

#define VERSION    "\n\n------------------  Sonoff Powerpoint v1.0p  -------------------"

extern "C" { 
  #include "user_interface.h" 
}

bool sendStatus = false;
bool requestRestart = false;

int kUpdFreq = 1;
int kRetries = 10;

unsigned long TTasks;
unsigned long count = 0;

WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient, MQTT_SERVER, MQTT_PORT);
Ticker btn_timer;

void callback(const MQTT::Publish& pub) {
  if (pub.payload_string() == "stat") {
  }
  else if (pub.payload_string() == "on") {
    digitalWrite(LED, LOW);
    //digitalWrite(RELAY, HIGH);
  }
  else if (pub.payload_string() == "off") {
    digitalWrite(LED, HIGH);
    //digitalWrite(RELAY, LOW);
  }
  else if (pub.payload_string() == "reset") {
    requestRestart = true;
  }
  sendStatus = true;
}

void setup() {
  pinMode(LED, OUTPUT);
  //pinMode(RELAY, OUTPUT);
  pinMode(BUTTON, INPUT);

  digitalWrite(LED, HIGH);
  //digitalWrite(RELAY, LOW);
  
  btn_timer.attach(0.05, button);
  
  mqttClient.set_callback(callback);
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  Serial.begin(115200);
  Serial.println(VERSION);
  Serial.print("\nESP ChipID: ");
  Serial.print(ESP.getChipId(), HEX);
  Serial.print("\nConnecting to "); Serial.print(WIFI_SSID); Serial.print(" Wifi"); 
  while ((WiFi.status() != WL_CONNECTED) && kRetries --) {
    delay(500);
    Serial.print(" .");
  }
  if (WiFi.status() == WL_CONNECTED) {  
    Serial.println(" DONE");
    Serial.print("IP Address is: "); Serial.println(WiFi.localIP());
    Serial.print("Connecting to ");Serial.print(MQTT_SERVER);Serial.print(" Broker . .");
    delay(500);
    while (!mqttClient.connect(MQTT::Connect(MQTT_CLIENT).set_keepalive(90).set_auth(MQTT_USER, MQTT_PASS)) && kRetries --) {
      Serial.print(" .");
      delay(1000);
    }
    if(mqttClient.connected()) {
      Serial.println(" DONE");
      Serial.println("\n----------------------------  Logs  ----------------------------");
      Serial.println();
      mqttClient.subscribe(MQTT_TOPIC);
      blinkLED(LED, 40, 8);
      digitalWrite(LED, HIGH);
    }
    else {
      Serial.println(" FAILED!");
      Serial.println("\n----------------------------------------------------------------");
      Serial.println();
    }
  }
  else {
    Serial.println(" WiFi FAILED!");
    Serial.println("\n----------------------------------------------------------------");
    Serial.println();
  }
}

void loop() { 
  mqttClient.loop();
  timedTasks();
  checkStatus();
}

void blinkLED(int pin, int duration, int n) {             
  for(int i=0; i<n; i++)  {  
    digitalWrite(pin, HIGH);        
    delay(duration);
    digitalWrite(pin, LOW);
    delay(duration);
  }
}

void button() {
  if (!digitalRead(BUTTON)) {
    count++;
  } 
  else {
    if (count > 1 && count <= 40) {   
      digitalWrite(LED, !digitalRead(LED));
      //digitalWrite(RELAY, !digitalRead(RELAY));
      sendStatus = true;
    } 
    else if (count >40){
      Serial.println("\n\nSonoff Rebooting . . . . . . . . Please Wait"); 
      requestRestart = true;
    } 
    count=0;
  }
}

void checkConnection() {
  if (WiFi.status() == WL_CONNECTED)  {
    if (mqttClient.connected()) {
      Serial.println("mqtt broker connection . . . . . . . . . . OK");
    } 
    else {
      Serial.println("mqtt broker connection . . . . . . . . . . LOST");
      requestRestart = true;
    }
  }
  else { 
    Serial.println("WiFi connection . . . . . . . . . . LOST");
    requestRestart = true;
  }
}

void checkStatus() {
  if (sendStatus) {
    if(digitalRead(LED) == LOW)  {
      mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "on").set_retain().set_qos(1));
      Serial.println("Switch . . . . . . . . . . . . . . . . . . ON");
    } else {
      mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "off").set_retain().set_qos(1));
      Serial.println("Switch . . . . . . . . . . . . . . . . . . OFF");
    }
    sendStatus = false;
  }
  if (requestRestart) {
    blinkLED(LED, 400, 4);
    ESP.restart();
  }
}

void timedTasks() {
  if ((millis() > TTasks + (kUpdFreq*60000)) || (millis() < TTasks)) { 
    TTasks = millis();
    checkConnection();
  }
}

Thanks a lot for helping out I'm still a newbie and still waiting for my ESP modules to arrive I've ordered a bunch ESP12F

Yep no problems. You pretty much got it. As long as your switch is on GPIO 0 (so you can send code to it) you can pretty much put your LED on any of the GPIO pins. What you have will work fine. In fact I had ESP's doing what you're doing well before Sonoff introduced their switch. I just adapted that code for the Sonoff's and cleaned it up so it was easy to follow for all. So you're good to go. Good luck.

Awesome this makes my live much easier and I can start using it as a very cheap switch :)

Thanks a lot for your help I'll close this out!!