Creating a Simple Data Logger with the RIOT (Realtime Internet of Things) Framework - LEKULE

Breaking

27 Mar 2018

Creating a Simple Data Logger with the RIOT (Realtime Internet of Things) Framework

RIOT, Realtime Internet of Things, is a framework designed to make using the ESP8266 with microcontrollers easy. In this project, we will demonstrate the simplicity of RIOT by creating a data logger that will plot levels of light onto a graph on a PC server application.

Introduction

In the last RIOT project, we learned how to create a simple RIOT server on a Windows PC and a simple RIOT client whose LED could be turned on and off by the server. From that project, we could see that RIOT potentially allows for any system with a UART port to be turned into an internet-enabled device.
In this project, we will look at client-server communication where a client will take voltage readings from an LDR/resistor divider and then plot this data onto a graph which can be found on the server (written in VB.net). It is highly recommended that you go through the previous project before attempting this project because portions of code and schematic setup will be recycled but with little explanation.

Note: RIOT is now called RIOTOUS and can be found at http://www.riotousframework.com

Prerequisites and Software for Compiling

For RIOT to function correctly on the client side, a specific AT and SDK version must be used. The AT version must be 1.1.0.0 and the SDK version must be 1.5.4. Earlier versions have bugs when closing connections and later versions have either missing or buggy code. To find out the requirements of the RIOT client side framework, click here.
For compiling the project, you will need:
  • Microchip IDE 8.92
  • Microchip C18 compiler
  • Visual Studio Express 2012
Also, make sure to read through the previous project in this series for a full explanation of what RIOT is.

The Schematic

In this project, we need to utilize the ADC module found on the PIC18F25K20 (PDF), a 3.3V microcontroller. A 3.3V device is used because the ESP8266 itself is a 3.3V device, and thus we don't need to include a logic-level translator.

An extra capacitor has been included on the 3.3V line (C5) near the regulator to help with removal of high-frequency noise on the power line.


The schematic for the project. Click to enlarge

Client-Side Code

The first task for our client-side code is to correctly setup the onboard ADC peripheral. Our analog reading is found on RA0 so we use the following code to get that pin to function as an analog input, turn on the ADC, and configure the ADC. The setup code below also configures the internal oscillator for maximum speed (16MHz) and x4 PLL. This code is found in setup.h.



The next piece of code needed in our client controller is the configuration of RIOT which involves creating the ISR, configuring the UART module, and telling RIOT how to send data over UART. This code (which tells RIOT how to use the UART module) is coded by you and is inserted into IoT_ClientFramework.c.



This ISR code tells RIOT that a new byte has been received and it also updates the state machine in the RIOT framework.



With the client configured, it is time to setup the device outputs, initialize RIOT, connect to a Wi-Fi network, and then connect to a RIOT server.



Now it is time to start data logging! Our client will take readings from RA0 every so often (a few readings per second, though you can change this if you want to) and then transmit the reading to the RIOT server. Timing would be better done using one of the timer module peripherals, but for simplicity, this example will use a simple delay loop. The delay loop can be used here because RIOT is updated using a UART receive interrupt which takes priority over the main running code.

Once the delay has been completed, an analog-to-digital conversion is initiated by setting the GO bit in the ADCON0 register. The ADC module will clear the GO bit once the reading has been taken, so while(ADCON0bits.GO) will loop indefinitely until the ADC result is ready. With the ADC result ready, we transfer the upper 8 bits of the result into the dataBuff array (size 1) and then send that single byte to our server. An array of size 1 is used because data is sent to the RIOT framework using a pointer to an array (this is so a stream of bytes in an array can be sent).



Below are the configuration values for the PIC18F25K20 (improper hardware configuration is a common source of problems in microcontroller projects).



Server-Side Code

Just like in the client side code, we will recycle most of the code from the previous project (specifically, the layout of the application, the server configuration, and the ticker which is used to check for new data from clients and determine which clients are still connected). The difference between this application and the previous project application is the removal of the LED command buttons and a chart object.

But first, a quick note on VB: VB.net (along with the Visual Studio 2012 IDE) has to be one of the most practical languages I have ever used. Before making this project, and not being a natural programmer, I was concerned about how I would create a GUI application with graphing capabilities. As it turns out, VB.net includes a chart object which is absolutely perfect for the job. A simple drag/drop into the main form with two or three lines of setup code gets you charting abilities that would be more challenging in a language such as C or C++ (however, I believe this convenience may be the .net framework itself as opposed to the language as visual C# is also an excellent language when used in Visual Studio).

So the first thing we will do is add a chart to our form which is done by going to toolbox > Data > Chart. From there, you can resize the chart area, change the color scheme, and much more.



One cool thing about objects in VB.net is they automatically initialize and configure in a configuration file that we don’t have to bother with. Instead, we only need to worry about clearing the area, creating a new series of data, and then plotting the raw data as it comes in! Before we get ahead of ourselves and start manipulating the chart object we need to configure the RIOT server class. Future versions of RIOT will allow for all configuration of RIOT to be done using functions but for now, configuration is done by editing the VB files. Only one line needs to be edited in Server.vb, i.e., line 114, which requires the user to enter the machine’s local IP address. For some reason, I could not accomplish this by using localhost.



Getting back to the chart object, clearing the graph and creating a new data series is rather trivial. This will be done when we press the server start button.



Now that the graph is ready to plot data and the server has been initialized, it’s time to get incoming data from clients, clear the data available flag, and plot the data onto the chart.



Quick Note: The visual studio application and video below show the graph as being labeled "loggedLight" where in actual fact the data represents "loggedDarkeness". This is due to the potentiometer configuration and can easily be fixed by subtracting the recorded reading from 256 (max). This would give the logged light instead of the logged darkness.

BOM

  Part
  Schematic Reference
  Quantity
  PIC18F25K20
IC1
1
  AMS1117 3.3V Regulator
IC2
1
  ESP8266 ESP-01
ESP8266
1
  1 kΩ Resistor
R3, R4
2
  5.6 kΩ Resistor
R2
1
  10 kΩ Resistor
R5
1
  LDR
R1
1
  LED
D1, D2
2
  100 nF Capacitor
C1, C2, C4, C5
4
  22 μF Capacitor
C3
1

Getting the Project Running

As in the previous project, we will construct the circuit on a breadboard. You may notice that the AMS1117 is in an IC package that is very unfriendly with breadboards. This is easily solved by cutting off the large tab and then soldering the device to a piece of stripboard. From there, a 3-pin straight header can be connected to create a TO-220-like package.


Converting an unfriendly package to a breadboard friendly version!

Compile the MPLAB project and program the PIC (preferably in Release mode) and run the VB.net program. If all goes well, your server application should not crash when you click “Start Server” and the client should start as soon as power is applied. Eventually, the client should be able to turn on the indicator LEDs when it connects first to the chosen Wi-Fi network and then to the RIOT server. As soon as the client connects, you should see the graph on the server begin to fill up with readings from the LDR.



 

Video of the Project in Action


Summary

This project demonstrates how little effort is needed to create a data logger from a few simple parts and a free software package available to most home computers. This project could easily be expanded for use with different types of sensors (temperature, humidity, etc.). Also, you aren't restricted to merely plotting the data; you also could store it in a file for subsequent analysis.

In the next RIOT project, we will look at inter-device communication where a client will send messages to another client via the RIOT server. 

No comments: