HOW TO: Make UDOO Neo an IoT Gateway of Sensors

5 Flares 5 Flares ×

This HOW TO is the first of two. This one is meant for beginners who just bought a Neo and want to connect it to the Internet and want to see their sensor data online. The second HOW TO will be about controlling the Neo’s Arduino in and ouputs through a local website on your Neo, so you can control it from everywhere in your house and from every connected device (smartphone, laptop, desktop) that has a web browser.


So this HOW TO is about sending sensor data to an IoT dashboard cloud supplier like Thingspeak. But there are more like for example

1: Create a Thingspeak channel so we can upload data to this IoT cloud hosting.
2: Connect the Neo to the Internet so it can connect to Thingspeak.
3: Create a Python program on the Linux side to send the data to Thingspeak.
4: Create an Arduino sketch for reading sensor data and sending it to the Linux side.
5. Check your Thingspeak channel and confirm data transfer.
6. Read multiple sensors and send to Thingspeak.

Let’s begin! If you have any questions on this How To you are free to ask, just comment this thread.

Step 1: Create a Thingspeak channel.
Create Thingspeak channel with 1 temp field. You can use the following instruction:

Step 2:
Connect the Neo to the Internet.
For connecting the Neo to the internet you can use the following instruction: Use Neo as a headless IoT Device

Step 3: Create a Python program on the Linux side to send the data to Thingspeak.
Copy this Python code, save it as in your home folder on the Neo.
Replace the key value with your own Thingspeak Write API Key
Python is preinstalled on the Neo so you dont have to install anything for this.
After saving this Python code you can run it ín a linux terminal console: sudo python
We are using the Linux /dev/ttyMCC serial device that is implemented on the Neo for communicating between the Linux and Arduino side.
Tip: To copy the code open the Neo desktop through VNC Remote Desktop so you can copy/paste it from your web browser on your PC to the text editor (leafpad) on the Neo with the standard Ctrl-C Ctrl-V commands.


#!/usr/bin/env python
# This program logs a Arduino Sensor on an Udoo Neo to a Thingspeak Channel
# To use, get a account, set up a channel, and capture the Channel Key at
# Then paste your channel ID in the code for the value of "key" below.
# Then run as sudo python

import httplib, urllib
import time
import serial
sleep = 16 # how many seconds to sleep between posts to the channel, API limit min 15 sec
key = 'Put your Thingspeak Channel Key here'  # Thingspeak channel to update

DEBUG = False # set to True to  get more debug prints in the Python console

#Create serial device for reading serial
arduino = serial.Serial('/dev/ttyMCC',115200,timeout=0)
serial_buffer = ""
line = ""

temp_str = '0'
temp = 0

#init for arduino so Arduino knows it can send data.

def ReadArduino(arduino_out):
    global serial_buffer, line
    arduino_out = ""
    serial_data =                 # Read 1 character
    while serial_data:                             # If there is a character proceed
        serial_buffer += serial_data            # Add character to buffer
        if DEBUG: print "ReadArduino:Serial buffer:",(repr(serial_buffer))
        # if there is a new line in it, then create Line string
        if "\r\n" in serial_buffer:
            if DEBUG: print "ReadArduino:New Line:",(repr(serial_buffer))
            line = serial_buffer
            serial_buffer = "" # empty Serial buffer
        serial_data =
        # if there is a Line string then finish reading the Line
        if line:
                # strip string for unwanted characters
                line = line.replace("\r\n", "")
                arduino_out = line
                if DEBUG: print "ReadArduino:arduino_out:", arduino_out
                line = ""
                if arduino_out == "": arduino_out = None
                # add the last character to the new buffer
                serial_buffer += serial_data
                return arduino_out

#Report Udoo Neo Arduino Sensor data to Thingspeak Channel
def writesensordata():
    global temp, temp_str
    while True:
        #read arduino data and make it a integer value
        temp_str = ReadArduino(temp_str)
        if temp_str is None:
            temp_str = '0'
        temp = int(temp_str)
        params = urllib.urlencode({'field1': temp, 'key':key })
        headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept": "text/plain"}
        conn = httplib.HTTPConnection("")
            conn.request("POST", "/update", params, headers)
            response = conn.getresponse()
            print temp
            print response.status, response.reason
            data =
            print "connection failed"
#sleep for desired amount of time
if __name__ == "__main__":
        while True:

Step 4: Create Arduino sketch for reading sensor and sending it to Serial
Use the following code to read an Arduino analog (temperature) sensor on pin A0 and send it to serial (see also for more info). I modified the code a bit to slow down the read frequency and to be sure the data is send to the Linux side of the Neo.
Copy the code in a new sketch (as a start use the IDE on the Neo) and save it. Compile and upload it to the Arduino side of the Neo.
You can check the results of this sketch with the Serial Monitor of the Arduino IDE.
You first have to send a character to the Arduino in order to get the sensor data displayed (every 16 seconds). Be sure the Python program is not running, else you will get a reading conflict of the /dev/ttytMCC serial device.

void setup() {
  // initialize serial communication:
  pinMode(13, OUTPUT);

// the loop routine runs over and over again forever:
void loop() {
  while ( Serial.available() > 0 ) {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  digitalWrite(13, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);  // wait for a second.
  Serial.println(sensorValue); // If LED stays high longer you have serial timeout
  digitalWrite(13, LOW);  // turn the LED off by making the voltage LOW
  delay(10000);  // wait for a 10 seconds to prevent buffer overflow

Step 5: Check your Thingspeak channel and confirm data transfer
Be sure the Python program and the Arduino sketch are running.
Open your Thingspeak channel and you should see the temperature uploading into Thingspeak website.

You can check my data here:
I did not change the value of the sensor so this is a very straight line :)
You can download the values and there is an update every 16 seconds:
2016-08-10 23:22:00 UTC,12,685
2016-08-10 23:22:17 UTC,13,684
2016-08-10 23:22:33 UTC,14,685
2016-08-10 23:22:49 UTC,15,684
2016-08-10 23:23:06 UTC,16,684
2016-08-10 23:23:22 UTC,17,685

Step 6: Read multiple sensors and send to Thingspeak
If you have more sensors you can send all data in one string (use Serial.print() and end with a Serial.println() ) to the A9 and split this string into multiple values again in Python.
I will add an example for that later.

Use Neo as a headless IoT Device
UDOO NEO Serial Communication Examples
Python programming for beginners

Disclaimer: I am not a programmer, just a computer enthusiast. The code provided is tested but is not guaranteed to be the best in class. :) The code and How To is written for beginners to give a start in using the Neo. At the moment this How To is work in progress so updates are foreseen.

Originally written by: waltervl

If you got any question, suggestion or idea, just visit our forum and comment the following topic:

5 Flares Twitter 0 Facebook 0 Google+ 2 Reddit 0 LinkedIn 3 Pin It Share 0 5 Flares ×