Sonoff-HomeAssistant (Alternative firmware for Sonoff Switches for use with mqtt/HA)

I use Sonoff POW’s, your firmware is missing those functions. Sorry if I’m hijacking your thread.

\What function exactly is missing?

Current usage, Usage today, Usage yesterday.

Why would you not do that in HA? That’s what my plan is to do when get a chance to install Influx buy yes OK if that is important to you then no problems. Current is available on my firmware but current is a strange thing to measure unless you have a specific reason to know how much current a device is using. Power (WATTS) and voltage are important, because you get charged on wattage and low (or high) voltage is important to trap and report on cause it can damage devices. Anyway each to their own I guess.

No you’re not :smiley:

By current I mean actual usuage (sorry English is not my native language).

Yes I only report current (your english is fine, it’s the same word LOL) and expect HA do do the summing for the day, week, month etc. Anyway enjoy :smiley:

1 Like

The reality is ITEAD have sold thousands of these units. If they were dangerous I’m sure people would be reporting it in threads all over the internet. Do a Google search for Sonoff dangerous and it only appears in this thread and it’s not even a fact. For the price these things cannot be beaten and the fact is yes they could be designed better and to spec but if they did that and them had the units tested and certified by each of the testing houses around the globe they wouldn’t be worth $5 but a whole lot more. A bit of a sensationalist comment from OP

I thoroughly agree, just pointing out the fallacy of equating those CE certs with electrical safety. I am very happy with my units, and am still alive.

1 Like

First, great work KmanOz! Second, Is it possible to use GPIO14 on Sonoff Basic for wall switch? I tried to make same changes like in Sonoff TH10/TH16 and Sonoff POW sketches. But, when I connect switch between GPIO14 and GND reley goes crazy :slight_smile:

I am as well looking for the same was wondering what i have to do to use GPIO14 on my up and down wall switches

Thank you for being interested! At the moment I’m working on your code to support a simple momentary switch mode. I want to use a standard sonoff (no touch version) to link a physical button to a hue bulb (send a POST with payload to HASS services). I connect the bulb (only two wires, phase and neutral) directly bypassing the switch: use the 3.3V output of sonoff, some resistors, GPIO0 and GPIO2, the existing physical wall switch and a modified version of your great sketch. By pushing the physical switch the sonoff sends a POST request to HASS and toggles my bulb. Thats the plan.

At the moment I have some ESP-01 on my desk. I have to add a buckdown AC/DC 230V to 3.3V converter and wire everything. As soon as this works I’ll order some sonoffs. Tinkering around on every switch takes too much time.

You see my plan?

haha!

I did exactly the same and had the same problems. I have this working: GND - 10k resistor - switch - GPIO - 470k resistor - V3.3. I’ll post some schematics if you want. Otherwise if you directly pull down your GPIO to GND it stays low all the time. If you don’t add a pull up resistor (the 470k in my example) your VDD is too strong and pushing the button would not let your GPIO read low.

here it is:

This reads low if pressed an high if not pressed.

I did no calculations at all. Just took some resistors lying around. Perhaps someone can improove!

1 Like

thx! I’ll try this.

Updated the schematics. I forgot the gpio pin.

@KmanOz do you have a sample sketch for sending a POST request to HASS? My google bubble prevents me from finding the right solution it seems. I have this at the moment (I don’t want to add a new library):

wifiClient.println("POST /posts HTTP/1.1");
wifiClient.println("Host: " +String(HASS_URL));
wifiClient.println("Cache-Control: no-cache");
wifiClient.println("x-ha-access: "+ HASS_PWD);
wifiClient.println("Content-Type: application/json");
//wifiClient.print("Content-Length: ");
//wifiClient.println(HASS_SWITCH1_DATA.length());
wifiClient.println(); //this newline defines the end of headers of a post
wifiClient.println(HASS_SWITCH1_DATA);

The code is lifteted from a comment of sukhmeet032795 from https://github.com/esp8266/Arduino/issues/1390

Hmmm, perhaps that’s too much. Perhaps I have to use mqtt for this too. Anyway it woud be nice to know how to send POST requests within a arduino sketch.

He,

ok so i have to check out how this going. Sorry if i did something you don’t like. I’m using Github for a week or two. Maybe i should dive into it a little bit more deeper. I created i fork and upload my File to this. Hopefully I did it right :smile:

I didn’t put it on that sketch for the very reason you are experiencing. There is no pullup resistor on board so the pin is left floating and will cause the issues you’re having. Additionally the sketch doesn’t have debounce code and relies on your mechanical switch to debounce. That’s why you use a proper wall switch which has mechanical debounce in it, otherwise you will still have issues. Remember on a 80Mhz clock, that point in the code executes every 12.5 nanoseconds (actually slower with all the overhead of the Arduino code) so debounce is important.

Why would you send POST when you have mqtt at your disposal. Create a mqtt Binary Sensor on HA and that’s all you need to trigger whatever you want. Also a POST response needs to have the content length including headers etc so it is checked at the other end. You either need to have a fixed length and know it or have a routine that calculates the length and includes the right length in the response or it will be rejected. Cumbersome and unnecessary when you have mqtt.

I flashed a TH10 with the Temperature Humidity Sensor using Theo Arends sketch and placed his HA example in the configuration.yaml. I tried and tried for several hours to make the configuration work and was not able to make HA see the TH10. Apparently I need some more detailed instructions if anyone can help.
I finally gave up and flashed the unit with the easy to use KmanOz sketch and everything was up and working in 15 minutes. I still would like to get the firmware from Theo working because apparently 1 flash works on all Sonoff’s. Thanks for your patience, I am knew at this bit but am persistent.

One sketch does work on all Sonoffs’ but you do need to configure that sketch for the type of Sonoff you’re using. It’s either configure the sketch, or use the specific one with my firmware, it’s actually the same thing. There is not one universal sketch that works on all Sonoffs without user input because the units have slight differences and there is no real way to detect what unit you have so it requires the user to work that out before hand.

I’d say you’re having issues because you haven’t configured the firmware correctly. Maybe you can post a question up on his forum or wherever he supports his firmware and you can get your answers. Or better yet considering there are people that will want to use Tasmota, start a separate thread in this community based specifically on Tasmota so all the answers are in one spot.