2019-03-25

Reprogramming a light switch

I started in computers in the late '70s when my high school got an RML 380Z. I never imagined I would ever be re-programming a light switch. Yes, a light switch, and one that is only £14.99 from amazon.

But yes, that is what I am doing. I was very impressed with this youtube video with step by step instructions on loading the Tasmota code using a Mac. It worked exactly as expected. That never happens!

I purchased a serial adapter from Amazon (here) which came with leads, and a simple molex header. The good news is you don't actually have to solder a header in the holes, you can just hold it there whilst programming. You only ever have to do this once (per device).


I now have a web page I can use to control it and configure it and so on. I had to do very little tweaking to the config, but was able to set up so it connected to the right SSID by default. I can even upgrade the s/w over the air (web interface). Impressive.

The only complication was the T1 (light switches) which needed more messing to get in to flash mode (see here). But you only need to flash once like this obviously and it is pretty simple.

Note: one other issue - edit the my_user_config.h and turn off some things you don't need (add // to start of the #define line). Test compiling Sketch->Verify/Compile to confirm under 500k size. This means you can have two copies in memory and hence upgrade the firmware over the air via its web page. If not, you have to take apart and re-flash to change code.

Next step MQTT... This is the messaging broker used to control and report button pushes...

It seems a simple apt install mosquitto moquitto-clients is a good start. And just like that I have an MQTT broker just working. Wow. Running in debug shows the events and everything, but it just works.

By setting the MQTT topic on a device, in this case to pixar, I can do commands like mosquitto_pub -t cmnd/pixar/Power -m 1 to turn the lamp on!

It allows me to send commands to the sonoff devices, and allows me to collect messages from them - cool. Next step I expect is to set up some scripts to do what I want.

That, or play with something like home-assistant.



P.S. I did tidy the wiring - the cover now clamps on the sleeving as it should.



15 comments:

  1. Can highly recommend Home-Assistant and Tasmota auto-discovery. SetOption19 1 to turn on autodiscovery, and SetOption56 1 and SetOption57 1 to turn on strongest signal selection and periodic re-scans.

    I'm using Tasmota in a lot of places, and my Sonoff Touch and T1s are next on the list, instead of my buggy homebrew Homie based firmware. Just need to figure out how to decouple the switch from the relay, and put the logic into Home-Assistant

    ReplyDelete
    Replies
    1. There seem to be loads of switch modes, but I could not see an obvious setting for making the switch not work the light - or ideally only work the light if MQTT is not active (as a fallback).

      Delete
    2. That's exactly the use case I'm after. Let me know how your research goes!

      Delete
    3. Well, if all else fails, there we can always "use the source" :-)
      I'll be playing more with this tomorrow, and I'll post if/when I find a way.

      Delete
    4. Did you look at:
      Configure -> Configure Module

      I don't have T1, but on other devices, you can map the GPIO ports to various inputs/functions. If you set to None, then this would unmap its function I think.

      But why do you want to do that? You can set PowerRetain 1, it publishes the state to mqtt, so HA knows you turned it on from the switch anyway. See https://github.com/arendst/Sonoff-Tasmota/wiki/PowerOnState-Configuration

      I think if mqtt is down, then it publishes the state next time it connects.

      So unless you really want to disable the switch for some reason, I don't get why you would? Some of the LED strip controllers I have have IR sensors, and they work fine publishing the data into mqtt.

      Delete
    5. Looks like there is a way to do exactly this. https://github.com/arendst/Sonoff-Tasmota/wiki/Buttons-and-Switches

      Delete
    6. There is a function to have the buttons uncouoled from the relays. Then you use the backend processor to control the relays based on the buttons.
      I did this with mine when setting up the switches on the stairs so the upstairs button controlled the relay upstairs etc

      Delete
    7. I'm beginning to think there's no way of decoupling the button from the relay entirely. You can swap behaviour of single and double presses, but in either of the cases at least one of them must be linked to the relay behaviour.

      In addition to that, the 'hold' feature only emits the message once per contact. Hardly ideal for determining the duration of the hold!

      I'll stick with my Homie firmware for now on the touch switches. Which kinda sucks cos my code doesn't support the dual T1 switches.

      Delete
  2. After flashing with Tasmota does the RF on these still work? I've need using ESPEasy on Sonoff devices with much success however RF and Wifi doesn't appear to be supported.

    ReplyDelete
    Replies
    1. Not using that, but the code seems to have config for it, so I assume so.

      Delete
    2. Yeah the RF remotes still work with tasmota

      Delete
  3. For anyone that is interested look at Domoticz. I wanted to integrate a number of different IOT systems locally, for instance LightwaveRF which relies on a remote server. Now been running for 2 years on a Pi with zero outages and a pretty sophisticated environment controlling lights, power and heating. Fully programmable and free.

    ReplyDelete
  4. Bonus points if you can automate your Pixar lamp stomping the sonoff flat :)

    ReplyDelete
  5. Catching up here... I have the same T1 unit and FT232 USB/serial bridge that the Rev linked to. My logic board looks just like this one. But I seem to be having trouble getting into flashing mode. Rev, if you don't mind: how specifically did you achieve this?

    ReplyDelete
    Replies
    1. Hold GND to the right of R9, then connect power, wait a couple of second (it seems to help), then disconnect GND from R9, then flash.

      Delete

Comments are moderated purely to filter out obvious spam, but it means they may not show immediately.