Following up on the general notes, here I some notes on programming for the ESP8266 in C. I use Linux for development, but most of this is OS independent.


  • Prefixing a function with "ICACHE_FLASH_ATTR" places it in flash, not doing so places the function in RAM.
  • The IoT SDK is single tasked and event driven. I've always found event driven programming a pain, but this implementation stinks. There is no way to return to the system, in the middle of processing, to not stall the system tasks. The docs, says that you not may stay in a function for more than 10 ms, without the system code getting behind. If I were to start over I would try the FreeRTOS SDK, unfortunately it seems that Espressif is giving it less love.


  • Some basic code for connecting to a WIFI. GitHub
  • The connect callback returns a pointer to the struct espconn in the arg parameter. It is not the same as the one used by espconn_accept.
  • The disconnect_callbackreceives a pointer to the listening connection, not the active connected one, at least when in TCP server mode.
  • In struct espconn, member reverse, seems to be free to use., except in the disconnect callback, where it suddenly has a new value.


  • It seems the file system of choice is best placed after the code in flash.
  • On suggestion from Dave Hylands, I use a ZIP file as file system image. The file is uncompressed and used as a simple container.


The buggers have been lying around while life happened, and I was waiting for the last one I ordered, which was on a neat breakout board. Well I got tired of waiting, and started playing with the ESP12 version. These are some random notes, of things I have discovered during my experiments.

  • My ESP12's have their GPIO4 & GPIO5 pins swapped on the silkscreen.
  • The toolchain for the Arduino ESP8266 IDE seems to be 64-bit.
  • I cannot reliably program using a PL2303 adaptor, but my Raspberry Pi Model B's serial port works fine.
  • In SoftAP mode, the SDK seems to set up a DHCP server on, all without me doing anything but setting the mode, and the AP config.
  • Baud rate at boot is 74880.
  • Untested Zero-wire auto-reset. ( needs a modification to send the break signal).

I wanted to start working with my ESP8266 and needed a 3.3V regulator to get enough current. I had ordered some 1117 regulators, but went looking through what I had laying about, and "created" this simple circuit.

It is not very efficient, and would benefit from a Darlington in place of Q1, but it gets the job done with enough current for the ESP8266. If the output voltage is just a tad to large, try replacing D1 with a Schottky type.

Adding a 10uF capacitor from 5V to ground is a good idea.

My Acer Aspire One D250, kept going into suspend every minute or so, whenever I was not logged in to a window manager. I run Debian testing and had my first real interaction with systemd (except for systemctl).

Turns out there are some settings in /etc/systemd/logind.conf that configures some of the power management features.

#  This file is part of systemd.
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
# See logind.conf(5) for details


First I asked systemd to ignore everything but the power button, and as I somewhat expected this didn't do the trick. Then I set the IdleActionSec to the actual amount of second (1800), that didn't work either. In the end completely disabling idling, stopped the suspend.


I needed a second screen for my netbook, as the screen real estate was not large enough for working with QT creator comfortably. I did not want (and did not have) a 5 meter VGA cable crossing the living room, but I wanted to use my TV as the second monitor.

I grabbed my Raspberry Pi B, a 2GB SD-card i had in the spares, and set out to configure the Raspberry Pi to mirror the screen on the TV from the network.

I tried using a protocol of X, called XDMX, that is designed to do what I wanted. I used much time, and had inconsistent results. I am sure that the problems where all of the wetware kind, but the documentation seems really sparse, and that does not help.

The solution for now, is less elegant. I installed a basic LXDE environment on the Raspberry Pi. Shared the keyboard and mouse using Synergy, and run the X applications on the netbook, through a ssh tunnel from the Raspberry Pi.

First things first, an OS (Raspberry Pi).

The standard Raspbian image will not fit on the 2GB card I had, but The minimal Raspbian unattended netinstaller comes to the rescue.

Simply download the installer image, write it to the SD-card, and boot the Pi, with it. Instructions for doing this can be found in the README. ua-netinst will start installing a Raspbian system, from the Internet, this takes a while.

I then lost my whits and asked the Pi to install a full lxde environment, it took ages! After starting from scratch, I added myself as a user, and installed X and a minimal LXDE environment.

adduser username

apt-get install keyboard-configuration  
apt-get install xserver-xorg
apt-get install lxde-core
apt-get install xinit

and Synergy

apt-get install synergy

Synergy (control node).

Synergy acting as a server, needs a configuration file in /etc/synergy.conf to setup the layout.

I have the Raspberry Pi (connected to the TV) with the host name pi, and the netbook called ace2 (the alias section). The TV is left of the netbook.

section: screens

section: links
                left = netbook
                right = pi

section: aliases

Start the Synergy server.


Final step (Raspberry Pi).

I needed to run QT creator on the TV, but you can run any X application. Complex things like video and fancy GUIS, are slow. Probably due to missing hardware acceleration and network speed.

I start the Synergy client, after which the netbook mouse and keyboard works on the Pi as well.

synergy (IP address of the controlling computer)

Then I ssh into the the netbook with X tunneling and compression, and start the program that I want to use. I now have the program (qtcreator) running on the TV, but can use the netbook keyboard and mouse to control it.

ssh -CX (IP address of the controlling computer)

With the current setup, if no maintenance is done, the log files from lighhtpd will just keep on growing. OpenWRT itself is configured to rotate its log files once they grow beyond 1MB. This is pretty nice, and since webalizer is configured to cope with disappearing log entries, it would be nice to rotate the web server log as well. To do this, install logrotate.

opkg install logrotate

logrotate has a global configuration file in /etc/logrotate.conf, and for individual programs in the directory /etc/logrotate.d/. Read the man page at Ubuntu Manpage: logrotate on the use of logrotate and the syntax of the configuration files. To rotate the log files of lighttpd, add the file /etc/logrotate.d/lighttpd, with the following contents:

/mnt/data/log/lighttpd/access.log {
    rotate = 53
    su http www-data
        /usr/bin/killall -HUP  lighttpd        

This tells logrotate to rotate lighttpd logs, every week, and keep about a years worth of log files. The log files needs to be accessed as user http, and group www-data, and lighttpd is told to create a new log file by sending it the SIGHUP signal.

logrotate needs to be run daily, so set up a cron job for it. Add the following to run logrotate once a day at 8:00.

0 8 * * * *     /usr/sbin/logrotate -s /mnt/data/tmp/logrotate.state /etc/logrotate.conf

Installing an updated OpenWRT image.

OpenWrt Sysupgrade

OpenWRT has a command, sysupgrade, that is used to upgrade the flash image from an update file. I have not been able to get this to work, and have ended up installing LUCI, the web configuration interface, every time I need to update the root file system.

opkg install luci

This opens another can of worms, since lighttpd is happily serving pages on port 80, where LUCIs web server, uHTTPd, wants to be. To get around this, I told uHTTPd to use some other ports. To do this, change the ports in the lines containing listen in /etc/config/uhttpd, like so:

config uhttpd 'main'
    list listen_http ''
    list listen_http '[::]:8080'
    list listen_https ''
    list listen_https '[::]:4430'

Then restart uHTTPd:

/etc/init.d/uhttpd restart

LUCI will now be available on the current IP address, on port 8080 and encrypted on 4430. Use the root user/password to login in, and use openwrt-oxnas-stg212-ubifs-sysupgrade.tar to update the device.

After flashing the firmware, all packages need to be reinstalled. Opkg will probably complain about changed config files, but this just means our configuration changes have been kept.

Generated on 2015-06-15 07:17:58.721895