Archive for the ‘Embedded’ Category

ESP32 Clock – Introduction

Ok, let’s start our big little project based on the ESP32.

First of all, we provide some guidelines on the project, which will be followed for the realization of the first prototype:

  1. ESP32 board has no many way to interact with a user, so at first glance our project will have only one purpose: connect to a NTP server and get the correct time;
  2. Neither it has a real RTC clock peripheral, so at the moment an RTC peripheral has to be emulated by software; when power is lost, also clock is lost, so we need to restart the board and reconnect to the NTP server to synchronize time again;
  3. To connect to the network, we have to provide a network configuration and WiFi configuration; as we don’t have a way to configure it (yet), we will hardcode the access point name and password, we will use DHCP and we will put at least one NTP server in the firmware.

So basically speaking, this would be just a proof of concept about the building of an ESP32 clock. Then we will start adding features to make this project more usable.

I decided to create a public GitHub repository to host all of the code needed so it will be easier to track down issues, bugs and improvements. You can find it at this address.

Advertisements

My ESP32

Several time ago I bought on Banggood an ESP32 dev board equipped with a OLED display. At a fancy 10,50€ including shipping I received:

ESP32
  1. the board itself, which consist on
    1. the main control chip using Le Xin ESP32, Tensilica LX6 dual core processor, clocked at 240MHz, computing capacity of up to 600DMIPS,
    2. 520 SRAM
    3. 802.11 KB chip b/g/n HT40 Wi-Fi transceiver, baseband, and LWIP protocol stack, Bluetooth (Bluetooth dual-mode integrated traditional and BLE low-power Bluetooth) and Wi-Fi antennas
    4. onboard 16 Mt bytes (128 Mt bit), Flash
    5. 0.96 inch White OLED display
    6. lithium battery charging circuit and interface
    7. CP2102 USB to serial chip, perfect support for Arduino development environment
  2. a couple of pin strip
  3. a heatsink for the main processor
  4. a cable to power the board with a lithium battery

Even though I don’t know what Mt means… it’s a great board. As ESP32 is widely used at the moment, I thought that it would be nice to have one to play with, especially at that price and with a display.

I started today to play with it, and thanks to Banggod comments and Q&A section I was able to get the original demo sketch, load it, modify it and make some changes. Now all I have to do is just find something to do with it. As it has WiFi and a display, a neat application could be to create a digital clock synchronized via NTP. Maybe I’ll post the code in the following days…

… and it’s finally here

I was waiting my new gadget, and it’s finally here.

A couple of Onion Omega 2 plus board, a couple of prototyping expansion board and a power dock. I also have a Onion Omega (the previous version) with a couple of power dock, so I can power up all the three boards at the same time.

Now it’s time to think about what to do… Ideas are welcome!

Waiting for my new gadget…

About a year ago I received my Onion Omega board, and it’still here on my shelf, waiting for something cool to do.

But then Onion released Omega2, a more powerful device, which doubles RAM and ROM available at a little cost. So I backed the project on Kickstarter, and my new Omega2 is on track now. I’m waiting it to be here in a couple of weeks, and in the meantime I’ll think about something to do with this new board and with the old one. I have some expansion boards (relays, OLED, Ethernet) that will be divided between the two boards to build some IoT projects.

Stay tuned!

Firsts steps into the IoT world…

Ok, it’s time to enter the IoT world, because IoT is cool.
With my Udoo Neo board I decided to do something usefull  (at least for me): trace down temperature and humidity data from my garage, that is the place in which almost all of my equipment lives (or try to do it).

First of all you have to think how to collect and how to show data. Many options are available, starting from DIY, and finishing to online services. Many of them are available up to now, but one focused my attention: ThingSpeak.

ThingSpeak is a platform for IoT data collection and analysis, driven by Mathworks (yes, that Matworks that publish Matlab). Within certain limits, you can use it for free (see their statements here).

I then opened a channel and wanted to start publishing data. I had to google around a bit to find a way to publish data, just because the most of the work is done for Arduino and other platforms different from Udoo Neo. But basically publishing data is almost a web service connection exercise, so mixing some script (and having to learn some Python coding), let me to create this script:

import httplib, urllib

def getfcontents(filename):
   "retrieve file contents"
   with open(filename, 'r') as myfile:
      data = myfile.read().replace('\n', '')
   return data

temp_raw = getfcontents('/sys/class/i2c-dev/i2c-1/device/1-0060/iio:device0/in_temp_raw')
temp_scale  = getfcontents('/sys/class/i2c-dev/i2c-1/device/1-0060/iio:device0/in_temp_scale')
baro_raw   = getfcontents('/sys/class/i2c-dev/i2c-1/device/1-0060/iio:device0/in_pressure_raw')
baro_scale  = getfcontents('/sys/class/i2c-dev/i2c-1/device/1-0060/iio:device0/in_pressure_scale')

temperature = float(temp_raw) * float(temp_scale)
barometer = float(baro_raw) * float(baro_scale)

params = urllib.urlencode({'field1': temperature, 'field2': barometer,'key':'<strong><em>your_api_key_goes_here</em></strong>'})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")

try:
        conn.request("POST", "/update", params, headers)
        response = conn.getresponse()
        print response.status, response.reason
        data = response.read()
        print data
        conn.close()
except:
        print "connection failed"

On Udoo Neo, temperature can be read from an external device connected to I2C bus, known as “a brick”. For more information about hardware connection you’d better take a look to the official Udoo Neo brick documentation.

My script does the following:

  1. reads from the I2C device the values for temperature and pressure (they are shadowed into four files on a virtual file system)
  2. calculates the temperature and pressure absolute values
  3. connects to ThingSpeak REST API service and post data

and that’s all. To invoke on a timely-based fashion my script, I simply put the invocation into the crontab file, with the following command:

crontab -e

wich will start your favorite editor, allowing to edit the crontab file. I chose to execute my script once per minute, to see what it happened, but I’ll probably switch the time to once every 5 or 10 minutes, just because these data are not suitable to change so fast. My crontab line so will look like this:

*/1 * * * * python /home/udooer/thing.py

just because my script is in /home/udooer/thing.py file.

Feel free to use the script and modify it; you will have to put your private write API key in the correct place (I put your_api_key_goes_here where you have to do it).

There is something more to do… Did you catch it? At the beginning I said I wanted to record humidity of my garage, but in the following I’ve always spoken about pressure. Why? Because the brick sensor provided is an NXP MPL3115A2, that is a barometer with a temperature sensor included. To measure humidity I need a DHT11-like sensor, but it has to be wired and this will require some more time. To check if all it’s working I’ll go with this setup, then I’ll add a DHT11 sensor later.

And again on the MPL3115A2, it’s actually unclear what is actually the pressure readout supposed to be. This device can output an absolute pressure value, but it can also express an elevation measure depending on its configuration.

udoo neo reconfiguration

Just a quick note on how to reconfigure udoo neo keyboard and timezone from console.

To reconfigure keyboard, from the console issue:

sudo dpkg-reconfigure console-data

give sudo password then select the last option ( ‘Select keymap from full list’). From there you can choose you actual keyboard layout and confirm with enter.

To reconfigure timezone, I think that you can manually edit /etc/timezone with an editor, changing the contents to suit your correct time zone. Otherwise you can simply issue from console:

timedatectl list-timezones

to have a list of available time zones, then

sudo timedatectl set-timezone your/timezone

to set up the correct time zone. For instance I had to issue

sudo timedatectl set-timezone Europe/Rome

and that’s all. For keyboard you should have to restart udoo neo; time zone is immediately set up, and information are update also in lxde.

Previous commands are available also for Ubuntu, from wich udoo Linux distribution comes from.