Skip to main content

Raspberry bye, hello


Let us make this very clear from the onset of this exotic excursion.

This is not a case of Raspberry Bye. Our relationship with our favourite single-board computer hasn't at all soured. In fact, we've become wholly inseparable. There's been many months of undeniable fun that's been had with the venerable computer strangely named after an edible fruit. To the extent that our relationship requires a healthy break. And quite frankly, our Pis require a well earned summer holiday to do whatever it is that robots and computers do during their time off. Crash. Burn. Refuel (with questionable toxins). Not at all unlike their human counterparts. And ultimately, it would be nice if they could return to a brand new, adorable pet waiting for them at home, a likeable little companion that they can just get along with.

Well, we visited a pet shop, but couldn't find anything as small and smart as this adorable pup we stumbled up on while searching the Internet for a new, miniature companion.  So when we eventually laid eyes on this Wi-Fi enabled microchip - a favourite of IoT tinkerers - staring back at us through its shiny shrink wrap, we knew we had stumbled up on the perfect match.

As such, it is here that we begin our disorganised day trip with the ESP8266 module.  That is, before our beloved Pis return from their much deserved vacation from a sunny beach retreat on The Continent.  After all, with a little house training, they should all get along swimmingly - should they not?

You don't want to be the tool without the tooling:

  • ESP8266 is the microcontroller module we've decided to use, although ESP32 is its more recent successor.  We're using the ESP-01S variant which is the equivalent of a 99p burger from Burger King, compared to the premium steak sandwich overloaded with stupendous gherkins and lettuce. Which should tell you: ESP-01S is as basic as basic can be (compared to its many siblings). For example, it is equipped with only the 2 GPIO pins. Has an amazing (!) 1MB of flash storage.  All perfectly acceptable for throwaway IoT devices, but perhaps not so good for an actual robot brain. Incidentally, you can buy a full breakout board equipped with the ESP chip with enhanced features and connectivity options from the usual suspects such as SparkFun and Adafruit.  Here, we're using just the base ESP8266 module. No frills. Just a bun with the wilted cheese and (what looks like) horse meat beef.
  • ...And that's why the use of a breadboard is highly recommended - and not the type that you saw through last week's baguettes on. Without the breakout board, you will end up with a lot of rat's nest cabling between your power supply, ESP pins, and any other sensors you decide to connect.
  • All input / output voltage related to the ESP8266 device operates at 3.3V (including the logic signals). Use a multimeter to verify that at no point you are introducing 5V or more. Bad things will happen at 5V.
  • A 3.3V DC power supply is required. We're using a popular breadboard power supply module called MB V2 - which is specially designed to provide power to the positive / negative rails of a breadboard (in other words, it slots right in). The other end connects to regular mains, which means we don't have to muck around with batteries (for now).
  • USB to (TTL) serial converter is required to communicate with your ESP device (before Wi-Fi networking is fully established). We're using a device based on the CP2102 chip which conveniently slots into the USB port of our Windows 10 PC and allows us to communicate with the ESP module over a COM port. Other end connects to three pins - Transmit (TX), Receive (RX) and Ground (0V) of the ESP module. More on this later.
  • ...Which is also why a PC with a serial client is required.  A Windows 10 PC with Putty works well enough. Providing that you have installed the CP2102 drivers in Windows. You could choose to use the Pi instead of a PC, but ours is on holiday, remember???
  • A Raspberry Pi 3, running Raspbian OS. Wait? What? We just said it was on holiday? Yes, but we'll use it only at the very end of this tour so that we have something that the networked ESP8266 can communicate with. We just couldn't help it, could we? Besides, it's Internet of Things, rather than Thing.
  • We'll be using a DS18B20 digital temperature sensor - like we did before - to take readings on the ESP8266. It'll end up being a bit like a cut-down, bargain weather station. That's if by weather station we mean something that just records temperatures and sends this data elsewhere. Don't forget the pull-up resistor, if you have just the naked DS18B20 sensor.

Chequered history:

Building Rosie 2.0 - naturally - assumes that there is a Rosie 1.0.  For that reason, casting your eyes over the organised chaos that was Rosie and Rosie Patrol is highly recommended.

And then, there's also been the first few episodes of Rosie 2.0:
  1. PiAMI HEAT
  2. Crawly creepy
  3. 2 crawly 2 creepy
  4. Balancing the books
  5. Shaken not steered
  6. El-oh-wire 

Bit by byte:

  • Unbox / unwrap / unravel the ESP8266 device. Marvel at its diminutive size (and wonder whether it is worth the Great British pound or two you paid for it - answer: it is). Yes, the ESP-01S model really is around the size of a 10p coin. Now, don't lose it behind the sofa.
  • Connect the devices up. [We repeat again] using the breadboard. And by devices we actually mean the power supply, USB to serial cable, and the mandatory pins of the ESP module. 
  • Install the USB to serial cable drivers, and make sure your PC is able to see it appear as a serial port (in Windows, shown as a "COM<number>" in Device Manager)
  • Use Putty to open a serial session to the ESP8266. Run a few "AT" commands and get excited about minimalist text showing on a black background.Wow. How marvellous.
  • Then flash the ESP8266.  Afterwards, install new firmware using esptool. We chose MicroPython as our firmware platform.
  • Connect the ESP8266 to an available Wi-Fi network, using MicroPython commands
  • Write a bunch of Python code to take measurements from a connected DS18B20 temperature sensor and send its readings back to the Pi (over Wi-Fi) using a REST API
  • Write some more Python code to receive the readings on the Pi, using Flask
  • Claim to now know something about IoT. There's a seat on the bandwagon with your name on it.

You ain't seen nothing yet:

As we've come to discover through out the course of our never ending adventures with Rosie (or Rosie Patrol, Rosie 2.0, Mr. Ted-E U, Cambridge Troublitica or any of our other nonsensical detours), Raspberry Pis are super useful.  They are essentially a small computer that allows you to run an Operating System (such as a Linux variant like Raspbian) and enables you to interact with a full array of both onboard and external hardware.  It's powerful.  Flexible (not in a bendy Yoga sort of way).  Everything seems quite intuitive once you get into the groove.  And all in the world is fine and dandy.

Except, if you just want to do one thing, cheaply and cheerfully, a Raspberry Pi is probably over-equipped. And not very cost effective. Too bulky.  Perhaps even a tad power hungry. You see; every little "smart" device these days is connected to a little known piece of technology called... the Internet. Fridges. Watches. Toothpicks (maybe not just yet). This obsession to interconnect stuff (however pointless at times) is aptly called the Internet of Things, and it's all the rage these days.

So if not a Raspberry Pi, what else? We've picked the hugely popular ESP8266, made by Espressif.  At £1 or £2 a piece, it's cheap enough that we won't lose sleep if our IoT flush-o-meter is.. erm... flushed down the loo.  Most importantly, it has Wi-Fi connectivity. It has minimal power requirements. It's ridiculously small. And might just have enough GPIO connections to attach a sensor or two, and send some readings back to a bigger computer, the Pi, or even the Cloud.  No wonder they are popular with IoT journeymen and women looking to patch obscure household items into The Matrix. Loo roll dispenser detects device in need of more toilet paper? No problem. You can know about it from your business trip on the other side of the world - on a smartphone app.

Finally, how is this relevant to a robotics project like Rosie, we hear you (not) ask? Perhaps as we equip our friendly superhero with more sensors, ones that are maybe not so critical to its operation, further and further away from the brain, we should be using distributed microcontroller modules like the ESP to handle less important work on behalf of the Pi. And leave the Pi to concentrate on being the central device that orchestrates everything. Bit like the brain.

In any case, our new acquisition is patiently waiting for us to do something useful with it. Let's get IoT-ing.

Detail: 

There's a very good reason why the ESP8266 ESP-01S model is so small, and cheap (in the nicest possible way).  It's not your typical "plug in and play" type of computer. No, it's definitely not that.  Because, if we're being entirely truthful, it requires a bit of hardware and software wizardry to get it to do something even remotely useful. We never said it will be easy!

As usual, we do need to bore ourselves first by reading the official datasheet, provided in this instance by the chip's manufacturer - Espressif.  And once we're quietly impressed by all the acronyms related to the technologies that the chip claims to be able to support... well... it's time to put this to the test.


First off: we're not using a ready-made breakout board with the bells and whistles. Let's deal with the most obvious obstacle that we encounter first as a result: power.

We will use a DC power supply to power the positive and negative breadboard rails.  For this, we used a MB V2 breadboard power supply module. Does it actually supply 3.3V? Apparently so, let's attach it to the breadboard, connect the unit to a mains wall socket, and see what our multimeter tell us.  Notably, our MB V2 has a jumper which allows us to set the output to 3.3V or 5V. We've already established: 5V is bad. How bad? Cambridge Troublitica bad.  As if we needed one, it's the reason why we want to dust off our multimeter to take some readings. The fact that using one also makes you look like you are doing some actual electronics, is a bonus.

We used a 5V DC power supply with a barrel connector to supply the MB V2 to begin with, but it didn't give us our desired 3.3V output.  In fact, it only reached 2.9V. Just as well we used our multimeter to discover this.


Well, it transpires that we should have read the manual for the MB V2 power supply module.  Because it quite clearly states that the input voltage from the mains needs to be 6.5-12V (not 5V). We changed the DC wall plug to one that supplies 9V and, surprise surprise, we have a stable voltage being supplied that is just shy of 3.3V. Good enough by us!

Connect the 3.3V Vcc and 0V (Ground) connections on the ESP8266 device to the power rails in the breadboard and it should now have the required power to operate.


Power.  ✔ (done).

But we kid you not.  This is where it all starts to get a little fiddly, and tad messy. And it's why using a breadboard is a must.

Firstly: it's best to have 8 shortened Dupont cables on-hand (perhaps by chopping and crimping a few spare cables), so that your ESP8266 module can be neatly placed in at the heart of the breadboard.

These are the physical connections we ended up with:

ESP8266 PinConnection
Ground0V (Ground)
Vcc3.3V used to power the module
TXSerial transmit connection. Connect to receive connection on the USB to serial cable.
RXSerial receive connection. Connect to transmit connection on the USB to serial cable.
CH_PDConnect to 3.3V to enable the chip
GPIO0GPIO0 connection. Connect to 0V (Ground), only when flashing the ESP8266.
GPIO2GPIO2 connection (we'll use this later for the DS18B20)
RESETUsed to reset the module (we're not using this)

So what exactly does this all mean? In summary, we should now have these connections:
  • 3.3V Vcc and 0V (Ground) connections to power the module. Kind of important to power stuff with electricity when it comes to the world of electronics (clue is in the name).
  • TX (transmit) and RX (receive) serial connections are used to control and manage our ESP device, until it can stand on its own two feet
  • CH_PD pin needs to be connected to 3.3V to keep the chip enabled
  • This one's a little different. GPIO0 needs to be connected to 0V (Ground) only when we intend to flash the device with new firmware. We'll leave this pin wired, but disconnected, until we're ready to apply new firmware.
  • GPIO0 and GPIO2 are normally available to use as your input and output pins to do something useful with, involving an external device.  For example, we'll use GPIO2 later with the temperature sensor.
You can start to see why the use of a breadboard is a must.

Here are some pretty diagrams to help alleviate any confusion.


Note that (unlike in the earlier photo) we did eventually end up connecting the Ground (0V) pin of the USB to serial cable to the equivalent 0V rail on the breadboard. This provided a common ground throughout our "system", which was in fact required to reliably communicate with the device (our console messages were garbled when we didn't do this).

Cool. So our device now looks like it's powered. All its mandatory pins appear to be correctly connected. But how do we know that it's doing something? Anything? How do we chat with this thing? Configure it? Is it working? Does P=NP? So many questions in this world. And not enough answers.

Yes, that's right. There's no SSH out of the box. Nor a pretty output by video to a screen. Definitely, not like the Pi in this respect. We'll have to initially resort to using a serial interface to access the module and do stuff with it.

...And this is why we connected a USB to serial cable in anticipation, to access the module from a PC (it could just as easily be a Pi, but remember, ours is still partying to the latest electro dance in Costa del Sol). Remember that the 3.3V rule still continues to apply - the USB to serial circuitry must operate at 3.3V.

Before we go any further, we should download the Windows 10 driver for the CP2102 chip of the USB to serial cable from the vendor.


Complete the driver installation, connect the cable into your USB port and you should see the "COM" port appear in Device Manager as a serial device.  In our case, the device appears as "COM3".  We are ready.  Ready to start eating serial for breakfast.


Fire up Putty, select "Serial", enter "COM3", speed (baud rate) of 115200 and click connect.  If everything is cabled up correctly (but not yet the GPIO0 pin to 0V as we're not yet ready to flash the chip), you should see a nice "ready" message displayed.  You may have missed the ESP booting up (it's pretty much instantaneous).  In which case restart it by removing and reattaching its power.


Here we go.  Some cryptic gobbledygook. Followed by a rather sarcastic "ready" prompt. Let's take this dry acknowledgement at face value, and proceed with the configuration. 


Of course we're currently going commando in serial land, with only a sad looking cursor to guide us.  We only have at our disposal the use of AT commands to get things done.  Bear with us.  Normal service will resume shortly.

There is a full list of AT instruction sets published by Esspressif if you really want to prolong your time on this starkly black screen. Simply to satisfy our curiosity, we'll run one single AT command just to see it work.  Type:

AT+GMR

...then hit the "ENTER" key, which will rather confusingly move the cursor to the start of the line in Putty. Then (don't ask us why) hold down the "CTRL" and "J" keys at the same time. The command appears to have been sent.  And in response, you'll find some information printed on screen about the firmware version currently installed on the ESP8266. Useful, maybe.

That's it. This feels overly geeky (even for us). We won't type any more of these in.


Incidentally, how do we know that "AT+GMR" was the command for this?  Another reason why you should read the AT8266 instruction set.  Next!


We're a bit homesick. We want some of our familiar faces back. And for us, that means having ESP8266 firmware that provides us with a stripped down, minimalist Python 3 interface. Let our holiday romance with MicroPython begin.

First we'll install "esptool" using pip3.  This is a tool that allows us to flash and update the ESP8266 module over the serial port using command line on the PC.

pip3 install esptool


Once the tool has been installed, we want to download the MicroPython for ESP8266 firmware from their website.

We're now ready.  The time has finally arrived to connect the GPIO0 pin of the ESP8266 to Ground (0V) so that the module can boot up in flash mode.  Then we'll flash it using the following command. Wipe it clean.

esptool.py --port COM3 --baud 115200 erase_flash


Looks like that's done.  Let's restart the module and we're ready to flash it again, but this time with our MicroPython firmware we downloaded. Clearly the name of the ".bin" file differs depending on the version you have downloaded.

esptool.py --port COM3 --baud 115200 write_flash --flash_size=detect 0 esp8266-20180511-v1.9.4.bin


Rather than the initial erase, this appears to have uploaded the firmware. Result! Now where's the Python prompt we were promised?

Unplug the cable from GPIO0, and reset the device. What you should now see in Putty (if you reconnect) is a friendly command prompt. One that looks suspiciously familiar. One that accepts... Python 3! Type in some familiar Python commands, and you should see that you're back in the comfort of your own home. No more living out of your suitcase. Your favourite remote control in hand. Feet up. Ready to battle through daily life.

text = "Welcome to Python on ESP8266!"
print(text)


So far we only have the Thing. We need the Internet bit of the IoT now.

We were glad to find out that we can connect the device to the Wi-Fi network using a few Python lines.

import network
sta_if = network.WLAN(network.STA_IF)
sta_if.active()
sta_if.active(True)
sta_if.connect("<SSID>", "<password>")
sta_if.isconnected()
sta_if.ifconfig()
# Disable access point
ap_if = network.WLAN(network.AP_IF)
ap_if.active(False)


Get the Wi-Fi SSID and password correct, and you should see that the ESP has been assigned an IP address by a DHCP service running on your network (most probably your home broadband router). The device's IP address is the first value in the tuple, followed by subnet mask and default gateway.

All set. The device is now officially connected (and somewhat smart).  From the PC you've been using to connect to the module, try a ping test using the module's IP address to see if it's actually available on the network.  You can try this test from the Pi too if it's already back and available on the network.

ping 192.168.43.29


If your PC too is on the same network - we assume it is - then you'll see a response back from the device. 

The I of the IoT is now in place. And the module is still a thing as far as we're concerned. With the ESP8266 now on the Wi-Fi, it should be able to communicate with all other devices on the network, including the Pi.  And, if your network has access to the Internet, it has access to the... dah, dah dah...."World Wide Web".

Now let's put this all to the test by doing something (semi) useful. Let's attach our favourite temperature sensor - a DS18B20 - to the breadboard, use the 1-Wire protocol to obtain readings from it, and send those readings back to the Pi.

We did substantial amounts of "playing around" with the DS18B20 in a previous episode.  Mandatory temperature graphs were produced to make our achievements look more official.  Here, we simply attach the sensor's VDD connection to our 3.3V rail, 0V to the Ground rail, and DQ (DATA) to the ESP8266's GPIO2 pin. Don't forget the pull-up resistor between VDD and DQ (ours is 4.7k Ohms) if you simply have the sensor on its own.

Here's a reminder on how this all ends up being connected:



With the sensor in place, we'll return to MicroPython.  Thankfully, there are already 1-wire and DS18B20 modules available in MicroPython. It is simply a case of importing them, instantiating 1-wire objects, and scanning and obtaining temperature readings from the sensor.

import time
import machine
import onewire
import ds18x20
one_wire_pin = machine.Pin(2)
one_wire_bus = ds18x20.DS18X20(onewire.OneWire(one_wire_pin))
temp_sensors = one_wire_bus.scan()
one_wire_bus.convert_temp()
time.sleep_ms(750)
print(one_wire_bus.read_temp(temp_sensors[0]))


Yay.  That final value looks like a legitimate temperature reading to us.  We now modify the code slightly to send the result, using a HTTP POST request, to another device operating a REST API endpoint (which of course will be our Pi).


Incidentally, the reason why the Python code for the ESP is called "main.py" is because when named in this way, MicroPython automatically starts it when the device is booted - which is handy for IoT devices.  This does mean that a file named "main.py" containing your MicroPython code needs to be uploaded to the ESP8266 device.  You can use something called "WebREPL" to achieve this.

import webrepl_setup

Select "e" for Enable when prompted, and set the password.  Reboot the device and it should now be accessible.


Navigate to http://micropython.org/webrepl/ using your browser, replace the IP in the top left corner of the screen and "connect".  After entering the password set earlier, it should now be possible to see the MicroPython console.

Not only that, you can now select a file - your "main.py" file with code - and upload it using the "browse" and "send to device" buttons in the top right corner.


Reboot the device, and your Python application should auto start.  It's also best to disable WebREPL again once you're done using webrepl_setup.

Lastly, we create a very crude API endpoint using Flask on the Pi to receive the incoming REST APIs requests.  Being Flask, there is little code required to setup a basic POST API endpoint, that listens for our readings from the ESP module. It'll use its internal development web server for now.


Start the Python application on the Raspberry Pi, and if main.py is auto-running on the ESP module, it should start to receive the REST API POST requests from the ESP device, over the Wi-Fi network.  How do we know? Because we can quite clearly see the POST requests arriving in Flask on the Pi, and the data received contains the temperature and device name fields sent by the ESP.

What you do with this data, of course, is totally up to you.


That's about it. Our baby pup is now playing nicely with our freshly recharged, resident Raspberry Pi. It took a while, but we now have this tiny (yet powerful) device doing something, and telling others that it is doing that something. Since they are now both definitely online, and they are things, we will lay claim to our first Internet of Things implementation.

And, more than likely, you will find yourself paying a few quid more for a ESP8266 with more connectivity options, like the slightly brawnier ESP-12E model as a development board (like the NodeMCU).

Update 1 - February 2019

Is it always frowned upon to trade your dearest for a younger, trendier model?  One with more features?  Actually, don't answer that.


...Because although it took a while, we did eventually get our hands on ESP8266's successor - the ESP32.  There's plenty of sites out there on the Wild Wild Web (like this) that beautifully describe the differences between the ESP32 and its now forlorn predecessor so we won't bore you with the gory details in this addendum.  Nonetheless, it goes without saying, that the ESP32 has considerably more features: such as Bluetooth, more GPIO connections, a couple of built in sensors, and is generally considered more "capable".  It supports MicroPython firmware just like the ESP8266 (although the download is different), so we didn't find any significant differences in the steps to set one up.

Here's what we did.

When our ESP32 development board was booted up in flash mode (entered using a fiddly combination of the BOOT and EN push buttons being pressed together), it showed a slightly puzzling screen... something about waiting for download. Downloading what? The latest film featuring Vin Diesel? A USB drive full of (illegal) music? Should we be worried?  No, let's just flash the device back in to obscurity.


esptool.py --port COM3 --baud 115200 erase_flash


It's been cleared of all the muck.  In other words, it's ready for the ESP32 MicroPython firmware to be loaded.

esptool.py --port COM3 --baud 115200 write_flash -z 0x1000 esp32-20190207-v1.10-54-g43a894fb4.bin 


And that's about it.  Once it boots up, it looks and feels just like the ESP32.  With the austere MicroPython prompt, and everything else that makes these microprocessors from Espressif so venerable.  Except, it's better (in all ways except the price). Now, now.  Don't be jealous of our new companion, our beloved ESP8266. It takes more than two microprocessors to fandango.

Go and see the doc:

ESP8266 datasheet by Espressif can be found here:
AT instruction set for the ESP8266 might be worth a read if you are intending to spend time using AT commands:
Here is a great tutorial by Adafruit on how to wire up the ESP8266.  Note that we only used this tutorial for the physical wiring. Once the device boots up, we move onto the MicroPython documentation.
We relied on this little article on how to communicate with the device, and send AT commands using Putty:
CP2012 USB to serial converter documentation can be found here:
Since we are using MicroPython for ESP8266 firmware, its documentation makes essential reading:
Currently, a copy of the MB v2 power supply module documentation can be found here:
If the ESP8266 simply isn't good enough for you, you can swap it for the more recent ESP32, also by Espressif:

Comments

MOST VISITED (APPARENTLY)

LoRa-Wan Kenobi

In the regurgitated words of Michael BublĂ©: It's a new dawn .  It's a new day .  It's a new Star Wars film .  For me .  And I'm (George Lucas, and I'm) feeling good .  Unfortunately for Canadian Mike, the Grammy that year was won by the novelty disco classic with the famous refrain: We love IoT, even in Planet Tatooine * . *Not true. Clearly, the Star Wars producers didn't sincerely mean the last Jedi the previous time around.  Return of the Jedi, released during the decade that spearheaded cultural renaissance 2.0 with the mullet and hair-metal , was less economic with the truth.  Either way, we're going to take inspiration from the impressive longevity of the money-spinning space-opera and reboot our franchise with some Jedi mind tricks.  Except this particular flick doesn't require an ever-growing cast of unrecognisable characters, unless ASCII or UTF counts.  In place of an ensemble gathering of Hollywood stars and starlets, we will b

Battle of BLEtain

The trolling . The doxing . An army of perplexing emojis. And endless links to the same - supposedly funny - viral video of a cat confusing a reflection from a dangling key for a golden hamster, while taking part in the mice bucket challenge. Has social media really been this immense force for good? Has it actually contributed significantly to the continued enlightenment of the human (or feline) race? In order to answer these poignant existential questions about the role of prominent platforms such as Critter, StinkedIn and Binterest, employing exceptional scientific rigour equal to that demonstrated by Theranos , we're going to set up a ground-breaking experiment using the Bluetooth Low Energy feature of MicroPython v1.12, and two ESP32 development boards with inexplicable hatred for one another.  And let them hurl quintessentially British expressions (others call them abuse) at each other like two Wiltshire residents who have had their internet access curbed by the co

Hard grapht

You would all be forgiven for assuming that bar , pie and queue line are favourite pastimes of the British .  Yet, in fact – yes, we did learn this back in GCSE maths – they are also mechanisms through which meaningless, mundane data of suspect origin can be given a Gok Wan -grade makeover, with the prime objective of padding out biblical 187-page PowerPoint presentations and 871-page Word reports (*other Microsoft productivity tools are available).  In other words, documents that nobody has the intention of ever reading.  But it becomes apparent over the years; this is perhaps the one skill which serves you well for a lifetime in certain careers.  In sales.  Consultancy.  Politics.  Or any other profession in which the only known entry requirement is the ability to chat loudly over a whizzy graph of dubious quality and value, preferably while frantically waving your arms around. Nevertheless, we are acutely conscious of the fact that we have spent an inordinate amount