Native Development for the ESP8266 Using Sming

In previous posts, I've talked about using the ESP8266 module to add WiFi to hardware projects. The ESP8266 is a neat little chip, and recently I've spent some time learning how to write native programs for it, ie - code that runs directly on the ESP8266 itself without having to connect it to an Arduino.

I've been using Sming, an open source framework built on top of the official Espressif SDK. Sming makes writing native applications a breeze, and supports most existing Arduino libraries which means that you can connect and use almost any sensor/device that works with Arduino!

Although documentation is a bit sparse, I followed the Windows Quickstart for setting it up and had no problems at all. Where it lacks in documentation, it easily makes up in code examples, with dozens of example projects to get you started. The framework, and the applications you build with it, are written in C++. With my C++ being a bit rusty, I found the exmpales a great way to learn the API and brush up on areas of C++ that I am not overly familiar with (mostly templates).

To create a new project, I copied the folder for one of the example projects, renamed it and then imported it into Eclipse. After reading through example uses of the TcpServer class, and modifying the TcpClient example project a little, I was able to successfully write a simple echo-server, which accepts TCP connections on port 123, and sends back any data sent to it. The code is as follows:

#include <user_config.h> #include <SmingCore/SmingCore.h> #define WIFI_SSID "MY_WIFI_SSID" #define WIFI_PWD "MY_WIFI_PASSWORD" TcpServer myTcpServer ; bool onClientData ( TcpClient & client , char * data , int size ) { return client . send ( data , size , false ); } void connectedToWifi () { Serial . println ( "Connected!" ); String ip = WifiStation . getIP (). toString (); Serial . println ( ip ); myTcpServer . listen ( 123 ); } void connectFail () { Serial . println ( "Connect failed, retrying" ); WifiStation . waitConnection ( connectedToWifi , 10 , connectFail ); } void init () { Serial . begin ( 115200 ); myTcpServer = TcpServer ( onClientData ); WifiAccessPoint . enable ( false ); WifiStation . config ( WIFI_SSID , WIFI_PWD ); WifiStation . enable ( true ); WifiStation . waitConnection ( connectedToWifi , 30 , connectFail ); }

It consists of four simple methods: init() runs once after the device has started up, and initialises the connection to my local wifi network. connectedToWifi() gets called when the WiFi connection is established, starts the TCP server listening on port 123, and prints out the IP address to the serial connection. connectFail() gets called on failure to connect to the WiFi and attempts to re-connect. onClientData() get calls when a connected client sends us data over the TCP socket which we send directly back to the conected client.

Getting the code onto the device itself is a bit fiddly, you'll need a USB to TTL adapter, and then it's just a matter of wiring everything up and flashing it. To flash code onto the ESP8266 you'll need to either run make flash from the command line or select the flash option from the Make Targets menu in Eclipse.

With the basics sorted, it should be easy enough to port some of my existing Arduino projects to native ESP8266. Doing so for the sous-vide project would be really cool, as it would open up the possiblity of controlling it from a smartphone via WiFi. I'm also looking into clustering several ESP8266 to see if I can implement some interesting distributed algorithms, more on that soon.

Please enable JavaScript to view the comments powered by Disqus.