Close

Starting with the ESP8266

A project log for Wireless remote LCD

Battery powered remote display for logging station

muthMuth 08/02/2016 at 21:540 Comments

This is a big question, where to start with the ESP8266. There is the official SDK path, for the brave. There is the Arduino, Espruino... Well, I tried NodeMCU. Starting with this page https://nodemcu.readthedocs.io/en/master/, there is a lot explained to start from scratch.

First dialog

Adafruit gave a nice schematic to wire the ESP8266 (https://learn.adafruit.com/assets/24745).

You can build online a NodeMCU image : http://nodemcu-build.com/ . Everything is very detailed on the documentation page : https://nodemcu.readthedocs.io/en/master/en/build/

Once the port com is connected, flashing with NodeMCU flasher is done in two clicks.

And here we are, the ESPlorer IDE let you yet start your first LUA scripts.

Wifi connection

I was very impressed how easy it is to configure the ESP as wifi station and connect it to my home network.

wifi.setmode(wifi.STATION)
wifi.sta.config("my_ssid", "the_password", 0)
Transfer an image

The raspberry pi regularly send datasets to an online web server using FTP, a solution could be so send an image as well and retrieve it on the ESP from internet. But it is rather difficult to interpret an image file directly on the ESP. So a PHP script could convert an image file to raw data. I realize the online serve does not like sending real raw bytes, so I had to convert bytes in their hex ascii form.

<?php
$info = getimagesize("pilogger320x240.png");
$im = imagecreatefrompng("pilogger320x240.png");
$width = $info[0];
$height = $info[1];

echo " <br>data:<br> ";

for ($h = 0; $h < $height; $h++){
	$byte = 0;
	for ($w = 0; $w < $width; $w++){
		if (($w%8) == 0) $byte = 0;
		$bit = 0;
		if (imagecolorat($im, $w, $h) < 40) $bit = 1; 
		$byte += ( pow(2, ( 7-($w%8) ) )*  $bit);
		$s = str_pad(dechex($byte), 2, '0', STR_PAD_LEFT);
		if (($w%8) == 7) echo $s . " ";
	}
	//echo "<br>";
}
?>
And this should output something such as :

data:
00 00 00 00 0f ff ff ff ff f7 bf ff ff ff 7f ff ff c3 ff ff ff ff ff ff ff 87 ff ff ff ff ff ff ff ff ff ff ff ff 01 ff ff ff ff ff ff ff ff ff ff eb bf ff ff ff 7f ff ff dd ff ff ff ff ff ff ff bb ff ff ff ff ff ff ff ff ff ff ff fe 01 ff ff ff ff ff ff ff ff ff ff eb 08 47 18 43 0e 31 63 dd 8c 61 0b a3 1f ff ff bb 8f ff ff ff ff ff ff ff ff ff ff fc 00 00 ff ff ff ff ff ff ff ff ff dd bb ba eb dd [...]
Interpret the image data

Now it is time to get these data on the ESP. In LUA there is a high level HTTP client to directly do GET over http. The problem is it is limited to 1400 bytes. At this point I was a bit frustrated, my image is monochrome 320x240 pixels, 9600 bytes.

Fortunately, NodeMCU expose direct socket TCP connection utilities. We can then get our data directly TCP, port 80, GET : (see nodemcu documentation example https://nodemcu.readthedocs.io/en/master/en/modules/net/#netsocketon )

srv = net.createConnection(net.TCP, 0)

srv:on("receive", function(sck, c) 
    print(c)
end)

srv:on("connection", function(sck, c)
  -- Wait for connection before sending.
  sck:send("GET /pilogger/getPNG.php HTTP/1.1\r\nHost: muth.inc.free.fr\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
end)

srv:connect(80,"muth.inc.free.fr")

At this point the ESP receive all the data by blocks of ~1400 bytes ! We can reconstruct the bytes array from the hex chars with the 'encoder' LUA module :

for i in string.gmatch(c, "%S+") do
        array[index] = encoder.fromHex(i)
        print(array[index])
        index = index +1
end    
Now let's connect a LCD screen to see these data.

Discussions