Webserver (or similar) as backend for microcontroller

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Zamfir
I built a novelty castle, the irony was lost on some.
Posts: 7312
Joined: Wed Aug 27, 2008 2:43 pm UTC
Location: Nederland

Webserver (or similar) as backend for microcontroller

Postby Zamfir » Mon Feb 23, 2015 7:52 pm UTC

I have ordered some ESP8266 modules, cheap units that combine WiFi with a microcontroller and some gpio pins. I want to play around a bit adding networking to electronic projects.

One thing I want try is offloading calculations to a PC. The microcontroller sends local sensor data over the WiFi to a PC acting as server, the PC does the thinking, sends back a command for local actuators.

I am not very knowledgeable here, especially about the server side of things, so I am looking for some advice on options. I am looking for simple and robust, but I have no clue if there are simpler and/or robuster options. Here's my current plan, based more on what I accidentally know than on wisdom.

The esp8266 has a custom firmware to run lua scripts, avoiding some low level hassle. It has simple commands to act as http client. I can send GET or POST requests to my PCs IP address on the local network, with sensor data

On the PC, I can run python, using the http.server library.This should allow me to receive the request, pass it on to other python code, and send a response with commands to the ESP8266.

Alternatively, the firmware implements something called MQTT, a publish/subscribe protocol that seems designed for my use case. I'd have to figure out the central server side of that, if it has advantages/avoids disadvantages that I am not yet aware of...

KnightExemplar
Posts: 5492
Joined: Sun Dec 26, 2010 1:58 pm UTC

Re: Webserver (or similar) as backend for microcontroller

Postby KnightExemplar » Mon Feb 23, 2015 9:44 pm UTC

Zamfir wrote:The esp8266 has a custom firmware to run lua scripts, avoiding some low level hassle. It has simple commands to act as http client. I can send GET or POST requests to my PCs IP address on the local network, with sensor data

On the PC, I can run python, using the http.server library.This should allow me to receive the request, pass it on to other python code, and send a response with commands to the ESP8266.


This is called "REST" and is the current go-to standard way of implementing things right now. It really won't take much processing power to create a GET (or POST) request and then parse the response (I've done something like that on a PIC back in College).

You'll want to design your web server with some thought. Here's an example Bitcoin REST api you can study:
https://btc-e.com/api/documentation

Once you get the server set up and logical, the client (aka: the esp8266) will have a very easy time communicating. There's a touch of theory with RESTful APIs, but its mostly common sense. I do suggest studying the methodology on Wikipedia (just a little bit) and then going at it.

The pros / cons of REST is that things are set up like a normal web server. Web Servers already have solved the major scaling issues (parallelism to multiple clients, etc. etc.). But the HTTP protocol is ASCII-based and pretty verbose. I can imagine that a very active sensor on a weak microcontroller could be too slow to send it all... but unless speed were an explicit concern I wouldn't be too worried about it.

MQTT might be a more efficient protocol, but I dunno much about it. Otherwise, I'd probably write some simple protocol in raw TCP if I were worried about speed / processing issues. But if a GET / POST library were already available, I'd at least test out the RESTful implementation, due to how easy those are to set up.

Premature optimization is evil, etc. etc.
First Strike +1/+1 and Indestructible.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 10 guests