Sunday, June 16, 2019

Easy IOT with ESP8266 and MicroPython using sockets instead of HTML

The problem I've always had with the ESP8266 was that all of the examples showed wrapping the controls in HTML. So - you'll start up a small web server, then decode the GET from the client and run your control logic from there. I don't like that.

I can see certain advantages, if you want to manually control your outputs from your browser, and you want to actually see the state of your inputs, it's great. Have at it. But I don't want to do that - at least not all  of the time. I want an easy way to read the inputs and control the outputs without having to log in with a browser or use some funky CURL.



My idea is to have my control application running on a central server, like a Raspberry Pi, and I want to communicate directly with my peripheral (IOT) devices. Most of the logic will happen on the Pi. I want to be able to control one IOT device based on sensors located on a different IOT device.

So I started researching MicroPython sockets. I've used them before for different projects. It seems so much cleaner. The code much easier to write. The client connects to the socket server and sends a message. The server checks the message and reacts, then sends a reply. Like I said, much easier to implement.

You can see the initial project on github. It includes a sample client.

The server programming is as simple as reacting immediately to the response, or defining functions and triggering them from the client message. For example:
from_client = str(incoming_data)
if from_client == '6on':
    led(0)
    reply = "Relay On"
elif from_client == '6off':
    led(1)
    reply = "Relay Off"
elif from_client == "info":
    reply = DEVICE
elif from_client == "voltage":
    reply = str(get_voltage(adc, 12, 5))
Where DEVICE is a global string with a device description and get_voltage is a function. Check out the github repo.

This is just the start of the thing. There is still a lot of work to be done. It is not very robust, and breaks pretty easily. It is not secure. But I am very happy with it so far, and I am rarely happy with my own stuff. It is a good start.

Hopefully I'll get some help along the way and this turns into something really cool. It has a lot of potential.

Resources:
MicroPython on the ESP8266
Python Sockets
Wemos D1 Mini

UPDATE:
I added a sample ruby client, in case that's your preferred language.

UPDATE 2:
Added a C++ client, same reason.

UPDATE 3:
This works, and it's good as it is, but using it, I realize that the server is blocking, and will only react when the client requests it. It should not be blocking, and be able to perform a normal program - yet still be able to react to a client request. This means threading. Guess I know where to go next :)

UPDATE 4:
Crap - threading isn't included in the Wemos D1 Mini Micropython build. That really would have been too easy. I think I see a way around it. Still digging.

No comments:

Post a Comment