Lord Kelvin said, “If you cannot measure it, you cannot improve it”. In this tutorial, we decide to demonstrate simple IoT project using ESPresso Lite V2.0 board to send digital compass HMC5883L data to ThingSpeak Web. At the same time, three data acquired which are x axis, y axis and the direction angle values also displayed on Arduino IDE serial monitor. Since we want to move the board freely, we are using two Xbee Pro Starter Kits to transmit and received the data wirelessly. We will share the steps taken to transmit data from digital compass to ThingSpeak and serial monitor simultaneously. Hopefully, this simple tutorial can give some idea how to use Espresso Lite V2.0 board to develop other projects related to IoT.
Assemble The Hardware
1. If we refer to Espresso Lite V2.0: Wheeled Robot Basic Movement Control with MQTT Websockets Panel tutorial, OLED display is excluded since we assume the Espresso Lite V2.0 Board already had the internet access. If not, please refer the tutorial before proceed.
2. Referring to the following wiring diagram, do the wiring to connect all the hardware. Power will be supplied to the circuit after all the wiring is done.
3. From the wiring diagram, LM1084 voltage regulator used to supply 5 V to Xbee Pro Starter Kit that send digital compass data since available voltages on the breadboard are 3.3 V and 7.4 V which is came from battery. This regulator is selected since it can provide high output current, up to 5 A.
4. On the other hand, another set of Xbee Starter Kit is connected to computer with USB miniB Type Cable to receive the data in order to display to Arduino IDE serial monitor.
5. After the connection is completed, all the connected hardware should look like the following figure.
Connecting To Web Services
1. ThingSpeak is one of a free web application that allows user to send and retrieve sensor data for logging and visualization purpose.
2. Create new ThingSpeak account by sign up at http://thingspeak.com
3. Creating New Channel.
a) Click on New Channel and enter the necessary information about this channel e.g. Name, Description, Fields etc.
b) For this tutorial, enter x value in the box and select Field 1, y value for Field 2 and direction angle value for Field 3.
4. Get Channel API Key
a) Click on the API Keys tab and copy the key under the section Write API Key.
b) The format of the API Keys are something like this, Pxxxxxxxxxxxxx2
c) API key can be update by clicking Generate New Write API Key to create a new key.
d) For information, the API keys enable the use to write data to a channel or read data from a private channel.
e) API keys are auto-generated when user creates a new channel.
f) Later, be sure to change the API Key in the Arduino sketch to the Write API key indicated in ThingSpeak channel.
Arduino IDE Programming
The HMC5883L is typically available on a small breakout board that run using 3.3V supply voltage. The communication protocol is i2c. It has the ability to provide a 3D heading direction based on the earth magnetic field but in this tutorial we will only be looking at the X and Y components. The directions are marked on the board with a set of axis. The X-axis is forward and gives the heading. Same as if we use compass app on smartphone, the red arrow always point north poles with direction angle of 0˚N. The angle varies if we rotate the phone. Here the procedure to write sample source code for this project:
1. Open the Arduino IDE and please ensure to configure some setting including set the board (under tools) as Espresso Lite 2.0.
2. After that, import the EsPert library’s to enable Esspresso Lite V2.0 communicates through the network.
3. Since the HMC5883L board communicates with Arduino through the I2C protocol, Wire library must also be imported.
4. Don’t forget to change the API Key according to own generated user channel.
5. The I2C is initialized and the communication with digital compass board is open with 0x1E address. hmc5883ModeRegister = 0x02 do a ‘write’ operation to the HMC5883L. The purpose of this ‘write’ operation is to adjust the value in the configuration register of the HMC5883L to tell it to be in continuous operation mode. This enable controller board to continuous reads of the axis data. By default the chip is in single read mode meaning after reading from it once, it will go idle to save power. Once idle, controller must write to it to turn it on before read from it again.
6. The loop() function will run over and over again as long as the board has power. Code espert.loop() used to request sending data for each cycle to the web. For this tutorial, data collected by ThingSpeak quit slow which are around every 15 seconds. Apart from ThingSpeak, user also can try other web services such as freeboard.io or ifttt.com. Since, Espresso Lite V2.0 houses 32-bit 80 Mhz ESP8266, int16_t is used to read x, y and z axis data.
7. Then, we request to read 6 bytes one by one. The chip auto-increments the number of which register we are reading from so we don’t have to select each register manually before we read from it.
8. From x and y value, direction angle is calculated using trigonometry formula. All x, y and angle data then sent back to serial monitor through Xbee with the baudrate set to 9600. Simultaneously, graph for digital compass data will be plot in ThingSpeak web.
Send Digital Compass Data to Network
1. Once the sketch is successfully uploaded to the ESPresso Lite V2, unplugged the board from computer usb port and replace with Xbee Starter Kit. ESPresso Lite V2 should automatically connect to the Internet once power is supplied.
2. There is no OLED display, so serial monitor will show the connection information between the ESPresso board and the network.
3. To visualize digital compass data, click on the Private View tab to display the x, y and direction angle charts at the Channels section.
4. The following figure show all the data displayed on ThingSpeak and serial monitor.
Ok, that’s all. If you have any question, feel free to ask in our technical forum as we seldom check the tutorial’s comment section. Thank you.