Wifi controlled switching - hacking a Sonoff device
Feb 26, 2021 05:42 · 3594 words · 17 minute read
starting off today with none such brewings baltic porter uh they don’t have any interesting tasting notes on the can but i’ve been having this one quite a bit and i’m liking the sort of a little bit of dark chocolate overtones and malt roast stuff like that it’s just a very nice drinking porter i’m pretty pleased with them and i’m hoping once all this pandemic stuff’s over i can actually get back down to their tasting room which is also a very nice little environment so today i am going to be playing with this little sonoff basic wi-fi switch thing that my buddy another maker sent a very long time ago and i just found it again when i was doing some cleaning up around here um that’s actually going to be a theme going forward for the next few weeks is messing around with things that i found while i was cleaning up because as you might expect there’s a lot of them so what this is is oh as it says in the box a wi-fi controlled switch it is designed for controlling line voltage anywhere from 90 to 250 volts ac 50 or 60 hertz doesn’t matter it can control up to a 10 amp load and you just wire up your ac mains coming in and your load going out and then you do some magic with it using their proprietary uh app and stuff like that and you can make it to just control whatever it is you’ve got on there they’ve got a whole ecosystem of these things and similar so as you can see there’s a bunch of different uh wi-fi things that uh in sonoff’s line but the basic is the one that we’re interested in but they all sort of work in their closed ecosystem with their proprietary app and i mean if you’re into that kind of thing great i prefer not to i prefer to have a little bit more control over the stuff that’s on my wi-fi so i am going for a bit more homebrew sort of an approach a very common option uh especially with uh with makers and diy-ers and hardware hackers and stuff is to use it with home assistant which is a sort of an open source and community run type of uh home automation system and it’s really cool but that is way deeper than i want to go i’m probably going to get there eventually but for right now that’s that means setting up servers and a bunch of other stuff and i’m just you know not into doing that right now i’m going for something a little bit more easy if you were going to use it with the with the home assistant stuff tesmota is a firmware that had that the community has developed and it’s very straightforward and it works pretty well with that the environment but again that’s not the direction that i’m going today however that doesn’t mean that i can’t learn from their experiments and discoveries uh there is a basic the uh one of the basic units there similar to the one that i’ve got and it shows those pins there which are the programming header 3 volts rx tx and ground and there’s also a gpio input there too which we will play with and i think make use of let’s pop the little module out here and you can see there’s not a whole a bunch to it um this is the input side there is essentially well there’s a fuse and uh he’s a little power supply in there nothing too fancy just pretty much a little switching power supply to power the chip um and then we have a relay there which connects to these huge wires and goes over to the load side nothing too bizarre going on there um you can see the wi-fi antenna over here got a push button led and the one chip that is doing the magic and that chip in this case is an esp8285 which is the brother of the esp8266 which a lot of us are familiar with it it’s a chip in a lot of these iot products uh the d1 mini the little low-end board the full-size d1 the 8285 is a little bit less common in maker world but it still gets its uses and as a bonus it can also be programmed with the arduino ide the same as the 8266 so that is a benefit to this because lots of smart people have gone before me and created uh youtube videos and web pages and instructables and stuff like that on how to reprogram this guy which is what i intend to do i hope to do um because i don’t want to use their proprietary app and their proprietary software but regardless there is that programming header ground tx rx 3.
3 volts and this one the fifth pin is marked o two so i’m gonna have to keep that in mind if i plan on using it the button on this is connected to gp0 which is the button that you or which is basically the flash line if you hold that low when this thing powers up it is supposed to put the chip into flash mode which is one of the uh important things to remember about the esp chips is that you have to manually put them into flash mode you can’t just uh send the some data to them and accept expect them to accept it so in order to experiment with this just to get software up and running properly i’ve put a d1 mini which you got the esp8266 on it on a breadboard and i’ve connected a relay and an led to what the research tells me this led and this really are connected to and i’ve got a button that i need to connect to that gpio i’ve just got it connected to a random one right now i’d initially planned to use the d1 mini on this little wemos tripler base which just basically parallels three things in its plug-in format and then use one of these really shields with it to do this and you know plug in led and stuff like that just to make it really easy rather than using a breadboard but then i noticed that this relay is a 12 volt relay which is really bizarre that’s a 12 volt coil but there’s no 12 volts available anywhere on any of the input pins any power pins there’s 3.
3 volts there it’s 5 volts there and the rest of it is all you know the 3. 3 volt logic there is no 12 volts anywhere this connector is on the contacts which is nothing to do with the coil that can be anything so i couldn’t see any way of using these little modules which is really kind of annoying because i don’t know um did the chinese sellers screw up and just assemble these wrong it should have a 5 volt really on it like this one over here which is just you know a standard arduino type module but whatever we’ll uh we’ll persevere with uh with loose components like that it’s good to have options so now that i got some experimental hardware sorted out let’s see if we can throw some code at this thing now this is using a much more complex piece of code that i found someplace on the internet and for reasons that i can’t quite explain to myself or to you i can’t find it again with some google searching if i do i’ll put a link down below but it it’s not important this is just a a step anyway i’ve got the led and the relay defined led still on 13 the relay is on gpio 12 and i’ve just got them going on and off together anyway it just looks like this when i turn the relay off it goes off when i turn the relay on it goes on for whatever reason i programmed them backwards but there you go you can see that as i’m clicking it’s changing states so one more step in my experimentation but after further messing around with that code trying to expand it into something that i could use for what i wanted to do um it uh i don’t know it proved to be way too advanced for me to wrap my brain around so i did some more digging around and found another really basic piece of code basically just to create a wi-fi led and then i expanded it from there it just uses the wi-fi library ssid and password i’ve chosen to create static ip just so i know where to find it on my network eventually rather than having to figure it out with dhcp all the time uh define the relay and the led thing as before the push button i’ve got it on gpio 14 right now which is d5 and then set up a few states the idea how to create things set some initial conditions set up the wi-fi this is the part that i added here just to read the button in the in the wi-fi client loop um i still don’t understand exactly it looks like it’s using stuff in the library mostly and just has a loop constantly running with some uh calling some stuff that’s down here that’s defined but in that loop while it’s just sitting there waiting for a wi-fi client i’ve got it reading the button um checking what the relay state is toggling the relay state and the led with it if the relay state is the other you know just basically toggle the thing it’s it’s probably not the most well i can guarantee it’s not the most efficient code because i wrote it but it seems to work and this is from the example that i found as is all this stuff um i added relay state in there just so that i could read it up in that loop and that is all of this all of the uh the loop stuff that that is basically doing it all and then down here is again the web server stuff that i don’t really understand fully because i just copy and pasted this from somebody else’s example and again if i can find that basic example i will link to it but no guarantees because i spent a lot of time hunting for this and i didn’t keep good notes so same functionality as before led on led off notice with the static ip address that i created the difference is i’ve added a push button with a pull up resistor so i can also control it from here led is on there i can turn it off from the website turn it on from the website and i’m going to turn it off from there so that is the functionality that i want um if i didn’t want a local push button to turn this thing on and off independent or yeah without having to go back to computer then i could have just used this thing as it is but i want that push button so now i have to uh figure out how to get that code onto there and also figure out that io2 and just quickly solder a header on there just to make life easier when i’m programming this thing unfortunately the fifth pin isn’t on the header uh it’s not drilled through it just shows up there only so only be able to put the programming and power on there but that’s not a big hardship the more annoying thing is that i’ve only got in four p and female hitters have only got these really long ones i’ll have to remedy that in a future mailbag it’s not a huge hardship but it’s nice to have the right tool for the job sometimes i’m also just going to tack a wire onto this one that’s labeled io2 here just so that i can experiment with it and find out what it really is because that doesn’t match anything that i found on any of the stuff online that doesn’t have to be a high current wear because of course it’s not going to be carrying any current up to speak of now to talk to this thing we need a uart board that can do 3.
3 volts there’s one there so this button here i think i mentioned it earlier is supposed to be gpio 0 on the chip and so you hold that down when you’re powering the thing on and it should just work hopefully now that another thing that is warned about everywhere is that you can’t trust that the low voltage side isn’t ground reference to the means it shouldn’t be but there’s probably going to be some leakage through there so when you’re programming it never have it connected up to means just connected to your computer potential for all kinds of disastrousness on your computer and i need to hold that down while i’m plugging it in so that should theoretically put it in flash mode you should be able to yet we’re on usb 0.
all this stuff is still default oh wait a minute no i need to change it i need to change the board to esp8285 module this is legit the first time i’ve tried this so we’ll see what happens connecting hey it’s uploading it’s actually uploading i shouldn’t sound so surprised because i’m following somebody else’s instructions but i’m always impressed when a microcontroller thing works the first try okay huh i hate it when something sort of works i’ve got it working on my own hardware now i’m working on this little wi-fi module here and i’ve got it i mean it’s it’s inverted but that doesn’t matter yeah but the relay on there is also supposed to click at the same time and it doesn’t and this wire is connected to a switch on the back which is supposed to work when i ground it but it puts it into that mode which i don’t know what that is that could be panic mode i’m not sure the reason that the um things started flashing and acting weird when i uh when i effectively pushed my push button is because i don’t have a pull-up resistor on that i o so i’ll just take my uh my 10k resistor and put it in there i think i’m going to need some heat shrink sleeving too that ought to do it and it’s still in the low voltage side so it’s not going to cause any issues i hope well a great deal of troubleshooting has gone on and it was all in the code which doesn’t surprise me because as i’ve said many many times i’m horrible at code but i finally persevered and i think i’ve got it so if i push the button hmm why did it take so long anyway you can see that’s come on the lights come on okay maybe i just wasn’t pushing it well enough these are cheap crappy buttons so there’s that and then over here we have this i can turn it on there they can turn it off there hmm let me de-bounce that maybe anyway i can turn it on here and i can turn it off over there i’ve just got some very primitive debouncing happening in the software and i may need to adjust that slightly but i think it’s working and it’s working well enough for my application anyway next i guess i should package this thing up well things are progressing along in fits and starts here i’ve got a push button wired up to that digital input and onto the ground pin i’ve just got it hot snotted in place just gently so it doesn’t fall out but i can still remove it should i want to reprogram this thing and i’ve got a couple of notches cut into the case for the wiring so hopefully that should just snap together properly like that right now then i have a plastic electrical box uh see in the bottom there it is csa rated it’s even made in canada uh plastic electrical boxes are not especially common in canada they’re very commonly used in the states but typically unless it’s in a waterproof situation in conduit in canada most of the boxes are still metallic they’re less expensive which is i think part of the reason and the electricians are just comfortable with them i don’t know if there’s any code reason or not but uh regardless this is a proper box so i can have the connections made in there and then that will just fit in there and i’ll put a cover on it with the button coming through and run some wiring up through there good wiring so the wire i’m going to use is this extension cord it’s outdoor rated i don’t need it to be rated for this application but it is out there rated anyway this used to be the extension cord that i used to plug in the block heater on my car except for the rabbits chewed through it so i’ve had to chop a piece out of the middle it’s fortunate for this project because i’ll just put this in the middle more time has passed i’ve got the live and neutral come from the input and the output connected i’ve got the two grounds uh aka earth the green wire coming from the cables connected to the grounding strap on the back there which comes up to the front here to ground the face plate if it was metal but it isn’t again i think i mentioned this isn’t exactly the correct box for this but it’s as close as i could find if i can find something better in the future i may replace it but i’ve got the button on there and the faceplate just screws on with the supplied faceplate screws in a color coordinating sort of a color either so the reason that i didn’t want to use standard blocks the more common one which are also much less expensive is that they are all metal and if i’m putting a radio device such as a wi-fi uh switch inside of a box i can’t be having it inside a faraday cage that wouldn’t work at all this is probably overkill but i just want this thing to be protected and safe just in case a dog sniffs around it or something uh the kids are old enough to know better than to dick around with electricity that’s my job but i just want it to be safe and also look safe okay so i’ll just give it one final test i’ve got it plugged in off the side there that works and from the web interface works i think we’re golden now the purpose that i’ve created this whole thing for okay it’s all hooked up here’s what it does it turns on the 3d printer remotely because as you may recall i’ve got this thing hooked up to a octoprint on a raspberry pi so this turns on the printer and it turns on the little raspberry pi yes i know the pi zero is not recommended for octoprint yes i know i’m not doing things properly so far it’s working for me this is not an endorsement so i’m pretty sure my main application for this is going to be when i’m monitoring a print from upstairs using octoprint and i notice on the camera that print is done i can shut down the raspberry pi gracefully like that because of course like any computer it doesn’t like being shut down unceremoniously once it is shut down and offline and they can go over there and just shut everything else off you see the lights have gone off the printer’s off and nothing gets damaged knife is good and of course with my local button on it i can still turn it on locally without the computer should i choose to do it manually well that was a a long and winding road for me and even though this is probably only going to look like about 15 or 20 minutes to you this was the product of multiple evenings even before i sat down here and did the introduction and it was a long day of software messing around the hardware only took me about half an hour really it’s the software because as i’m sure i’ve told you many many times i’m not that good at the software part if you want to see how not good i am the link to to the code that i wrote is down in the description along with links to a few of the resources that i found at least the ones that i didn’t forget about the ones i actually bookmarked um yeah questions and comments uh down below as usual thanks for watching and i will go get myself another beer i’ll talk to you later.