This is just a piece of a greater project that I am currently working on regarding a home automation system aka domotic. In this post I will demonstrate a part of it; serving as an internet switch. Yet it is somehow limited it could be built for as low as 10$. The system is based on a very popular and trendy -among hobbyists- WiFi module, the ESP8266. Communication between the uC and the REST server is achieved via MQTT; a lightweight pub/sub messaging protocol developed in the labs of IBM. The web application was built on the MEAN stack – MongoDB Express Angular 2 Node.js.
The uC was programmed within arduino IDE which supports C/C++, instead of LUA which is also available but I personally do not prefer. In order to program the ESP with arduino IDE one should install the esp arduino addon.
The circuit is pretty simple to follow. From what you can see there is a standard relay driver delivering power to the connected devices (later on I added another 2 relays). There’s also a Dallas 1-wire temperature sensor DS18b20 which I found accidentally in the drawer. Furthermore there is an HD44780 driven 16×2 LCD, connected through a shift register (74HC595N) in order to use half GPIO pins of the uC.
The host of MQTT broker was chosen to be mosquitto. There are a couple of other free brokers available online, but one could probably use mosca to implement his own. I have tested it recently and it works like a charm.
The code in C is actually pretty straightforward. Restful API was built on top of express and the connection with the MongoDB is made via mongoose. The web application was built in Angular 2 which has a rather steep learning curve and I wouldn’t suggest it to new programmers. Though angular CLI makes things a lot simpler, I still faced several difficulties while compiling the application ahead of time (aot). Both Rest API and angular application were deployed to heroku. For authentication I made use of jwt -pronounced /dʒɒt/–, which I for one believe is safe enough even though the token is stored @ localStore (but only for some limited time, which one is free to choose). To enhance security I wrote a middleware to force redirection of HTTP requests to HTTPS.
Link to github