📜 ⬆️ ⬇️

My smart switch or how I made a device for a smart home without electronics development experience, living in a village in Indonesia



They say that laziness is the mother of progress. In my case, it was so. I now live in sunny Indonesia, where people light their homes at night to scare off thieves (and ghosts). It so happened that my order was to turn this lighting on and off. Every morning and evening you have to go around the house and click all the external switches. It is especially difficult not to forget about this in the morning, since the included light bulbs are not noticeable in daylight. After a week of such walking, I wanted a better life. I decided to make a remote switch and automate the control of the light. Thus, laying the first virtual bricks of his smart home. There was only one little problem - I had no idea how to design electronics and dilute the board.

In the title photo you see the result of my work. The switch consists of two inserts in the wall. In one there is a physical switch, and in the other its “brain”. The virtual switch works in OpenHab2, launched in turn on the RaspberryPi2.
')


Design / Wishes

So, how do you start developing such a device? I wanted the virtual switch to work together, not instead of the physical one, and that turning off the physical switch does not turn off the virtual one. Here, such a design is called a "hotel switch".



And what about the body? In no case should my device be a “wall wart”. On the contrary, it should smoothly merge with the physical switch without giving out its existence. This means that the board should be placed in one of the widespread wall boxes. Below, the photo shows a copy of such a box, it is from the line of switches and sockets of Panasonic. On its basis, I developed my device.



Ok, but what about “smart” features? I wanted a switch that knows whether it is on or not. I didn’t just know the status of rela, but I really knew if the current flows through the system. I also wanted this state to persist for a long time, and for example, in the event of a power failure, the light did not turn on / off suddenly after power was restored.

Finally, I wanted a beautiful control panel for the virtual switch. I needed a good solution to control the smart home where I could connect. I didn’t want to write another cool smartphone app, damn it!



First Prototype

So, I wanted to build an electronic device, but did not know how to do it at all. I was familiar with microcontrollers, knew how to use a multi-mitter and soldered a couple of non-residential boards of someone else's design. But I could not design my scheme at all. Therefore, at first I decided to assemble my switch from ready-made modules. Here is what I have collected:



To reduce 220V to 5V, I used a charger from the phone. Immediately take a good charger, for example the one that was attached to the phone with the purchase. Do not buy Chinese aftermarket in the market, they are usually curves. I took mine from some old Samsung.

I picked it up as a whale (“beefcake” kit from SparkFun). It was large, with a loud click and had all the necessary strapping (transistor, protective diode, etc.) to wield the microcontroller’s foot.

To measure the current, I took one of the Allegro ACS712 modules on a small board. There was all the kit, it was only necessary to connect the power and read the analog output.

The above made up my power module. For the logical part, I took:

Dev board on PIC24F from Circuits @ Home . It was made to create USB Host devices, but I only needed the giant PIC that stood on it. Also, I took a PickIt3 programmer / debager for it. In hell there is a reserved place for the man who, for the first time, dared to call this crap a debager.

The wifi module was originally a CC3000 on the breakout board. But not for long. The CC3000 could have been the very workhorse for internet irons, but only before TI priced it up (from $ 10 to $ 30) and stopped producing samples. Just as I was about to dive into his study, another chip came out about which you might have heard - ESP8266. Ultra cheap wifi module driven by weird AT commands.

AT commands for a long time did not give me rest. Who in their right mind can pull on the construction of such a strange syntax? Then, of course, I realized that these AT commands were not invented by Espressif (the company producing ESP8266), but were used because the same AT commands in the same performance were used earlier in other similar products, such as GSM modems, for example. And even earlier, GSM modems, someone else used them, and so on to the very first dialup modem. It was expected that people who had previously written under the AT command will be able to quickly connect the ESP8266. Only it was not me;)

Software for the microcontroller.

Programming is another thing, I can! Using my kompashku modules I wrote most of the firmware for the PIC. I decided, somewhat arbitrarily, that my device would be available via JSON RPC over TCP. RPC (remote procedure call) seemed to me the most logical way to organize communication between the central server and my device. The device will open a connection to the server, and the server will use the established channel to call the methods “on”, “off”, “status” and receive some data in response. And I took JSON in order not to invent my syntax and parser for it.

I found a little C for JSON parsing - JSMN . It is easy to use, but there were no easy ways to walk through a JSON tree. No problem, the necessary functionality was easily added . Over JSMN, I wrote my JSONRPC server . The module is initialized by an array of function pointers and string function names (approximately how JNI methods are registered in Android). Thus, the server knows which function to run when it encounters its name in a JSONRPC request. When a call is made, an automatic check of the type of arguments takes place, a check of the type of return will be added in the future. Take a look if you're interested.

Oh yes, there is one more thing worth mentioning before we go any further. Each microcontroller project should start with a good UART console. Well, it's actually quite simple to send a couple of bytes over the UART and then spin in a loop, waiting for an answer, did you mean that? No, this is a bad UART console. A good one would be non-blocking, using interrupts, ring buffers and all that. Unfortunately, I did not find the open version of the sorta version, and was forced to port one of the vendor-specific ones to fit my needs. I am going to write my own, from scratch, and let it out, but my hands still cannot reach.

Also, we need an interface to the ESP8266. I decided to write a module that called 'at-socket'. The user interface is socket-like: open / close / send / receive / joinap / quitap / etc, but under the hood these functions perform sequences of valid AT commands and return binary statuses. It was much harder than it might seem. Facing AT teams for the first time, I didn’t know if there were any established conventions, and if the authors of this AT implemented them. For example, nothing was said about synchronization (is it possible to read an asynchronous AT command in the middle of a synchronous call?). The control words sometimes change in different commands (eg ERROR and FAIL), and sometimes single characters are used without any EOL markers (eg '>'), and therefore one cannot simply read and read line by line from the buffer. Do not forget to parse the size of incoming data from the string in the int. Taken together, these little things strongly interfere. In the end, by the method of experiments, I got something good, but you can probably imagine how the code written in this way looks from the inside. Therefore, I will not offer it to your attention. Maybe after I rewrite it, knowing the main pitfalls, but not earlier. One thing is not clear to me - where are all these legacy AT commands that keep this syntax afloat?

The second run on the iron / make boards

Up to this point, I had successfully compensated for my ignorance of electronics using ready-made modules, but now it looked right into my face. The alpha version of the firmware for the micra was ready and exploded for new hardware. I understood that the production of boards would take time, so I wanted to start the process as quickly as possible, and at this time pick the central server of the smart home.

My attempts to get an electronic friend interested were not successful. I was alone. I decided that at least I would draw a scheme, in the end, I already had all the modules of a loner and a scheme was usually attached to them, or it was in a datasheet. All that was required of me was to combine everything into one big scheme, and with it it was already possible to competently ask for help.

With the help of YouTube and search engines I learned how to use electronic CAD (I started with Eagle, and yes, it is terrible). As each module is added to my master drawing, I just could not help but wonder how it works. Prior to this, electronic circuits were misleading. They were big and I didn’t even know which way to start looking at them. And here, everything was elegantly broken into small pieces, and you could think about what this or that resistor and capacitor gives. I asked questions on stackoverflow and toster , and gradually began to understand what was happening - current limiting resistors, pull ups, pull downs, filtering caps, protection diodes, voltage dividers, and so on. And I also understood that for each microchip there are usually examples of layout in the datasheet, and even if not everything is clear, you can start using and asking questions. Thus, I even dared to add a few goodies to my large circuit which was not in the modules. And finally, I realized what it is - the minimum binding for the microprocessor. On this topic, I suggest you an interesting story.

How I stupidly burned the first prototype and looked for parts in a local, village shop
As soon as I finished the first pass through the firmware, I had a small incident. I kept my foot in a peroscid treatment bath (tropical fangus, fu, do not ask) and, like a fool, without getting up, with my outstretched hand, I incorrectly plugged the power into the first prototype. These were 12V and not 5. The white dummy spun on the board. I disassembled the unit, and began to check the components. Everything was bad. Everything burned down ... except the programmer, but then I did not know. Now I definitely had to make my pay. While I was finishing the circuit, I decided to try to solder a new virgin board for the PIC, since I had a couple of chips left and I already found the minimum weight in the datasheet. I needed only a few ceramic capacitors, resistors, and maybe a crystal and a linear source. I made a list and headed to the local electronics store.

Let me tell you a little about Indonesian e-shops. This is usually a semi-dark, cramped room with an open wall to the street and minimal windows. Travel products are speakers and amplifiers for numerous mosques and some cheap Chinese LED shny crafts that local people like to mount somewhere on the bumper of their car or moped. All electronic fines lie in dusty boxes behind the counter with yellowed stickers. Do not even think about the catalog, say what you need and we will check whether we have it or not. That evening I went to Digikey, poked into the search box and wept quietly. Once I saw a couple of schoolchildren buying transistors, LEDs and some other trifle. I asked them what they were collecting, because before that I had never seen anyone here interested in collecting electronics. They told me what they are doing flip-flop. Suddenly I had hope! There are still those who are interested in electronics. Maybe I will hire them when they finish school, or arrange a hackathon for them, or ... but not for a long time the music played. It turned out that they were collecting flip-flop for the school assignment, and about this, all the electronics in the school began and ended.

So, I was at one of these electronics stores with my list. To my question about ceramic capacitors in response, I got a dull look and clapping eyes. After 15 minutes of explanation, I left the store getting a resistor at 10K, a crystal at 12MHz and a pair of 8nF capacitors to it. They had no other ceramic capacitors, and they didn’t want to accept my descriptions of the ungraved board. Until you learn the local keywords (the non-etched board turned out to be 'PCB polos'), you will be looked upon as a macaque who escaped from the zoo.

My adventures with the search for details were delayed for a couple of weeks before we went to the nearest "metropolis". In it the shops were the same as in the village, but with a little more choice. Knowing the keywords I was able to quickly check the availability of the right products. I got the ceramic capacitors at 0.1uF, but not higher. I also took a tantalum capacitor (last, dusty) at 25uF, which later turned out to be broken and ruined my virgin board. They also had PCB polos! But only one-sided, that life does not seem raspberries.

A few days later I soldered my board using a thin wire (which, in turn, was removed from the burned transformer from the soldering station) for the tracks.



The moment of inclusion was a disappointment. The programmer did not want to recognize the chip. The reason for this was a broken tantalum capacitor, but I did not know this yet, and it was not clear if the programmer burned out or I incorrectly collected the board. To finish the story, I will tell you that later I rebuilt the virgin board on the breadboard with giant (by physical dimensions) thin film capacitors and it all worked. The programmer was alive.


So where did we stay? Oh, yes - I drew the master scheme and pondered the next move. I didn’t want to do the board layout myself. That would be my first pay and jambs were guaranteed. In addition, the power supply module used 220V and the shoals became fire hazardous. I did what any person should do in this situation - I filled the order for freelance . Three performers responded, and so I met Danil . He perfectly set up the power and logic modules, and even lured me to the side of Altium-a (Altium vs. Eagle is just like Photoshop vs. MSPaint).

While Danil worked on the board, I helped to draw footprints and select the main components. I satisfied my impulses of “drawing too” in Altium-e by repeating tutorials from YouTube and official documentation, which is very good. I liked this dude . With my new knowledge, I could look at the options for boards from Daniel, and give a full feedback. In general, learn CAD, it is not as difficult as it seems. Looking ahead, here are the fees:



Having gotten the files into the shaking Gerber pens, I had to find a place to print them and buy the components. I know only two places where you can get a small batch of motherboards at a low price - oshpark and cheapdirtypcbs . I chose oshpark because I really like their web interface and I have already seen their product and it is really solid. The only problem is they are in America and I am in Indonesia. Do you know that there is another big problem? Order delivery to Indenozia, [censored]. There are no local outlets of large distributors like Digikey / Mouser / Element14 / etc. I was thinking of ordering outlets in nearby countries - Mouser has a Malaysian website, and Element14 has Singapore. Happy pricing for shipping. In my opinion, Mouser on their Malaysian website was written with three exclamation marks that - at last they make delivery to Indonesia !!! For a low, low price of $ 70. OK. Go ahead. In the end, I found a re-shipper in America, and they got all my boards and components and sent them to me in one batch. Shipment now cost $ 23. A month later (or another) I had a parcel. Who wants to open a large electronic store inside Indonesia with me? (Seriously)

Before going through the quest “Indiana Johns and a Variable Temperature Soldering Iron” (and do not even dare to ask me about the sting of the correct shape), I proceeded to soldering. A little backward soldered diodes later, I had a power source. There were a few bugs (mine), I will tell only about one interesting / funny. The module with the microcontroller was more difficult. It turned out that I painted the landing pad for the ESP8266 exactly in mirror image (facepalm). It was lucky that he did not burn after such a connection. With the help of a hair dryer, I took it off and turned it face down. It worked.

The next adventure was with a linear regulator. I do not know how I didn’t see it, but it turned out that my 5V was an extreme, low entry value. When I connected the load (read: turned on the ESP8266) 3.3V output sagged to 2.7V, which caused a brown out reset on the PIC-e. I immediately ordered a replacement (MCP1825, I recommend) on Aliexpress. AliExpress is very good for single components - the price is about the same as in America, free shipping with the purchase of such units is ten. But at the same time, I do not see myself ordering the whole BOM in this way.

The last bug in the logic module was the wrong choice of legs under the UART. I was already pricked at using microchip remappable peripherals, but did not notice that many of these pins are suitable only for input. And as one would expect both pins for UART, I chose these. Well, nothing repaired.I cut the tracks and laid them "bypassing" a thin wire. A drop of hot glue to hold, and you're done.



It started! I was able to drive the firmware through all the tests, including current measurement. I sent JSONRPC commands manually and received responses. Wifi and TCP worked. Here is the second prototype: The



central server of the smart home

The central server is the most tasty morsel of the entire smart home ecosystem. Control over it gives full control over the compatibility of devices, you can start the entire metric through your cloud service (and then send ads to diapers when you see that one of the rooms has been repainted), integrate with some Evil Corporation (to improve the experience, of course), or some meanness. So, young Umnodomovtsy - think about who you want to put on this role. I hope your answer will be “ourselves”, but lately I don’t even know what to expect.

I decided to quickly try (install and run) all the home automation solutions I could find. Here is a good list.(at the end), but recently such projects are growing like mushrooms after rain, so it may already be not complete. Of those that I tried, I did not like one. My discontent usually fell into one of these groups: the half-dead status of a project; obscene, unfinished documentation; gross security breaches (project on the basis of a local site — directories with 777 rights belonging to the user under which the web server is running, shell calls), cloud service bindings, and in all cases a disgusting interface. I honestly tried to install and run everything, but it worked only about half the time. From the working half, I stopped at OpenHab- it has the most detailed documentation, active development, a fairly large community and open source. I also liked that it was written in Java. Among the shortcomings is the sophisticated code, the malformed interface (now much better in OpenHab2), and the lack of space for asking questions. Now, OpenHab2 is in the alpha stage, and it offers us a reorganized code (much clearer than the first) and a beautiful PaperUI interface that looks good on both pischki and on phones. In addition, there are all the functions for connecting your interface if you risk writing it.

OpenHab is organized in core, ui and bindings. Binding is a kind of driver for each device. In OpenHab, there are a couple of generalizing buyings, such as http or tcp, and at first I tried to use them, but I quickly realized that their usefulness is extremely limited to the simplest cases. I also have a couple of red words in the direction of “binding notation”. In general, if you need your own logic, then the only way is to write your own binding.

We were lucky, the structure of the banding was much improved between the first and second OpenHabs, and with a little persistence I figured out the binding API and wrote my own. My buying finds new devices, keeps records of connections, disconnects and statuses of all devices. I plan to write a separate post devoted to creating my own binding, so now I will not dwell on this and will describe the architecture in more general terms.

The OpenHab binary runs on any machine with a Java VM and raises the web on the host. Our binding starts the listening TCP socket and waits until a new switch is connected to it. The switch is initially programmed with some parameters from the user - wifi name / password and aypishnik or hostname where to find the central server. That is, as soon as the switch establishes a connection with the AP, it enters the server. When a TCP connection is established, the user is notified that a new device has been found and that he should be added to the control panel. And that's all, no config files and binding configuration strings. In the control panel, the device looks like a normal switch, but hey, this is a smart switch! Its status (ON / OFF) indicates the true state of the system. If you turn it on, and a light bulb burns out there,the status will quickly return back to OFF, because if the current does not flow through the switch, as a result, the light cannot work.



Putting it all together

After several iterations of “straighten the banding-screw up the micro-controller”, the whole system was ready for launch. Debugging takes place on such a portable unit.



The production server I have consists of RaspberryPi2 on which the assembled OpenHab2 is spinning. Raspberry is connected to the home network via LAN, and you can pick up an additional AP, onto which all switches should be installed for separation and additional security.





And now let's play an electrician. Armed with a test screwdriver (lights up red if you touch the "hot" wire) and turning off the breaker, I took up the wiring.



It is quite difficult to wield thick copper wires. We collect.









For three weeks I have been driving this specimen with good results, and I am preparing to install another one on the street.

Goodbye

This is where my story ends. Until I decided what to do next. Heart pulls to send my switch to the hardware incubator, maybe they will take it - home automation is now popular. And the brain says that incubators will never be interested in such a project, too one is unimportant to be turned off in this world - they would be better off if they have something social. It would be very interesting to hear from people with similar experience. And yes, if I apply, I will need a piece of iron and maybe "peoples' person." Who wants with me?

Update

At the request of the viewers, I decided to publish the scheme. Look here .

Source: https://habr.com/ru/post/365841/


All Articles