HOW TO: Make Udoo Neo an IoT Gateway of Sensors

Discussion in 'UDOO NEO' started by waltervl, Nov 13, 2016.

  1. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    I have no experience with services on Linux. On windows I know it doesn't like print to console but I don't know what other conditions Linux has on services. Perhaps someone else here could assist?
     
  2. Maurice

    Maurice Active Member

    Joined:
    Oct 13, 2015
    Messages:
    394
    Likes Received:
    87
    I've created multiple services on Linux. I always start out by copying a simple script from another service. Not that it has to be executable (as root) and should be registered before it is automatically started.

    Here is a tutorial on Upstart, hope that helps.
     
    waltervl likes this.
  3. Gla

    Gla New Member

    Joined:
    Oct 25, 2016
    Messages:
    25
    Likes Received:
    4
    Please check the following code, because it produces "strange" feedback. I use 4 different sensors in udoo neo, which produces 5 outcomes and should "fill" the 5 correspondive thingspeak fields of the same channel.
    The code returns results that fill only the two first fields, but the problem is that all the printed serial values appear in the first two filelds

    2017-01-03 21:17:13+0200,
    1084,
    "1050.00,25.00,18.00,55,705.00", "1050.00,33.00,18.00,54,705.00", 0,0,0

    2017-01-03 21:17:33 +0200,
    1085,
    "1350.00,33.00,18.00,55,705.00",0,0,0,0

    2017-01-03 21:17:54+0200,
    1086,
    "1700.00,25.00,18.00,55,705.00",0,0,0,0

    2017-01-03 21:18:17 +0200,
    1087,
    "1900.00,33.00,18.00,55,705.00","1900.00,24.00,18.00,55,705.00",0,0,0

    My code follows

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

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

    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',9600,timeout=0)
    arduino.flushOutput()
    arduino.flushInput()
    serial_buffer = ""
    line = ""

    # sensor coefficient name
    sensorValue1 = '0'
    temp1 = 0
    sensorValue2 = '0'
    temp2 = 0
    sensorValue3 = '0'
    temp3 = 0
    #sensorValue4='0'
    #temp4=0
    #sensorValue5 = '0'
    #temp5=0

    #init for arduino so Arduino knows it can send data.
    arduino.write("I\n")

    def ReadArduino(arduino_out):
    global serial_buffer, line
    arduino_out = ""
    serial_data = arduino.read() # 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 = arduino.read()
    # if there is a Line string then finish reading the Line
    if line:
    # strip string for unwanted characters
    line = line.replace("\r\n", "")
    #line = line.strip(",")
    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 temp1, sensorValue1,temp2, sensorValue2,temp3, sensorValue3,temp4, sensorValue4,temp5, sensorValue5
    global temp1, sensorValue1,temp2, sensorValue2,temp3, sensorValue3
    while True:
    #read arduino data and make it a integer value
    sensorValue1 = ReadArduino(sensorValue1)
    if sensorValue1 is None:
    sensorValue1 = '0'
    temp1 = (sensorValue1)
    sensorValue2 = ReadArduino(sensorValue2)
    if sensorValue2 is None:
    sensorValue2 = '0'
    temp2 = (sensorValue2)
    sensorValue3 = ReadArduino(sensorValue3)
    if sensorValue3 is None:
    sensorValue3 = '0'
    temp3 = (sensorValue3)
    #sensorValue4 = ReadArduino(sensorValue4)
    #if sensorValue4 is None:
    # sensorValue4 = '0'
    #temp4 = (sensorValue4)
    #sensorValue5 = ReadArduino(sensorValue5)
    #if sensorValue5 is None:
    # sensorValue5 = '0'
    #temp5 = (sensorValue5)
    params = urllib.urlencode({'field1': temp1, 'field2': temp2,'field3': temp3,'key':key })
    headers = {"Content-typZZe": "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 temp1
    print temp2
    print temp3
    print response.status, response.reason
    data = response.read()
    conn.close()
    except:
    print "connection failed"
    break
    #sleep for desired amount of time
    if __name__ == "__main__":
    while True:
    writesensordata()
    time.sleep(sleep)

    Please help!!!
     
  4. Gla

    Gla New Member

    Joined:
    Oct 25, 2016
    Messages:
    25
    Likes Received:
    4
    sorry the previous uploaded code is for three sensor values, but my problem is the same
     
  5. Gla

    Gla New Member

    Joined:
    Oct 25, 2016
    Messages:
    25
    Likes Received:
    4
    my main problem is that the sensors values printed in the /dev/ttyMCC port (seperated with commas) cannot be distinguished and be assigned to the suitable field of my thingspeak channel.
     
  6. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    I suppose it goes wrong in the line starting with params = urllib.urlencode.........

    It looks like it is conflicting with strings, dots and quotes or something like that. I made integers of all the values, what if you do that too? Just to check if it works.

    You could print params to the console to check that it is the root cause of your problem.
     
  7. Gla

    Gla New Member

    Joined:
    Oct 25, 2016
    Messages:
    25
    Likes Received:
    4
    no changing to integers does not help. i have tried it.
    there is not any problem when i check the values in the serial monitor
     
  8. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Please print the value of variable params. And check it.

    I still believe you have a declaration issue:
    You initialize for examples temp3 as an integer (=0) but you later make it a string as sensorvalue3 is a string.
     
  9. Gla

    Gla New Member

    Joined:
    Oct 25, 2016
    Messages:
    25
    Likes Received:
    4
    surely, i declare sth wrong in the code, thats the reason i have wrong type results.
    sensorvalues are declared as integer values in my arduino code.
    please check the following python code

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

    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',9600,timeout=0)
    arduino.flushOutput()
    arduino.flushInput()
    serial_buffer = ""
    line = ""

    # sensor coefficient name
    sensorValue1 = 0
    temp1 = 0
    sensorValue2 = 0
    temp2 = 0
    sensorValue3 = 0
    temp3 = 0


    #init for arduino so Arduino knows it can send data.
    arduino.write("I\n")

    def ReadArduino(arduino_out):
    global serial_buffer, line
    arduino_out = ""
    serial_data = arduino.read() # 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 = arduino.read()
    # if there is a Line string then finish reading the Line
    if line:
    # strip string for unwanted characters
    line = line.replace("\r\n", "")
    #line = line.strip(",")
    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 temp1, sensorValue1,temp2, sensorValue2,temp3, sensorValue3,temp4, sensorValue4,temp5, sensorValue5
    global temp1, sensorValue1,temp2, sensorValue2,temp3, sensorValue3
    while True:
    #read arduino data and make it a integer value
    sensorValue1 = ReadArduino(sensorValue1)
    if sensorValue1 is None:
    sensorValue1 = '0'
    #temp1 = sensorValue1
    sensorValue2 = ReadArduino(sensorValue2)
    if sensorValue2 is None:
    sensorValue2 = '0'
    #temp2 = sensorValue2
    sensorValue3 = ReadArduino(sensorValue3)
    if sensorValue3 is None:
    sensorValue3 = '0'
    #temp3 = sensorValue3
    params = urllib.urlencode({'field1': sensorValue1, 'field2': sensorValue2,'field3': sensorValue3,'key':key })
    headers = {"Content-typZZe": "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 sensorValue1
    print sensorValue2
    print sensorValue3
    print response.status, response.reason
    data = response.read()
    conn.close()
    except:
    print "connection failed"
    break
    #sleep for desired amount of time
    if __name__ == "__main__":
    while True:
    writesensordata()
    time.sleep(sleep)

    there is no temp values, affecting the code and still i have the same results

    {"channel":{"id":207669,"name":"MyUdooAllSensors","description":"
    Udoo Neo Project that contains Light, Humidity, Temperature,

    "field1":"sensorValue1","field2":"sensorValue2","field3":"sensorValue3",

    "created_at":"2016-12-28T13:21:53Z",
    "updated_at":"2017-01-04T13:55:01Z",
    "entry_id":1391,"field1":"150,33,19","field2":"150,22,17","field3":"0"}]}
    "entry_id":1480,"field1":"150,23,18","field2":"150,22,18","field3":"0"},
    {"created_at":"2017-01-04T14:33:06Z",
    "entry_id":1481,"field1":"150,34,18","field2":"0","field3":"0"}]}

    the problem is still the same.................... the thingspeak field1 and field2 are field with all the data [sensorValue1,sensorValue2,sensorValue3] as a string
    and there is no data in field3.

    I dont know how to split the string in the corresponding fields, and from my google searching there is no profound solution.
    Any ideas how to solve it?
    Does anoyne test a similar experiment with UDOO NEO to upload 2 different sensor field data in Thingspeak?
     
  10. Gla

    Gla New Member

    Joined:
    Oct 25, 2016
    Messages:
    25
    Likes Received:
    4
    and i have printed the params
    some of the results are

    field2=150%2C35%2C19&field3=0&field1=150%2C32%2C19&key=J***********

    field2=150%2C33%2C18&field3=0&field1=150%2C30%2C19&key=J*************

    field2=0&field3=0&field1=150%2C29%2C18&key=J***********

    field2=150%2C34%2C18&field3=0&field1=150%2C28%2C18&key=J**********
     
  11. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    If you use Serial.println() in the Arduino sketch to send data to the /dev/ttyMCC you are receiving a string with python ReadArduino(sensorValuexxx).
    How do you write the data to Serial? I think you do all values in one line like Serial.println(sensorValue1,sensorValue2,sensorValue3);

    Then you have to split it into different values in Python:
    Code:
    sensorValues = ReadArduino(sensorValues)
    
    sensorValue1,sensorValue2,sensorValue3 = sensorValues.split(",")
    
    params = urllib.urlencode({'field1': sensorValue1, 'field2': sensorValue2,'field3': sensorValue3,'key':key })
    perhaps you have to convert the string Sensorvaluexxx into a float or integer:
     
  12. Gla

    Gla New Member

    Joined:
    Oct 25, 2016
    Messages:
    25
    Likes Received:
    4
    thanks again for your feedback.
    This was exactly the solution and i have discovered it 3 hours ago.
    My worked code is the following:

    def writesensordata():
    global temp1, sensorValue1,value1, value2, value3
    while True:
    sensorValue1 = ReadArduino(sensorValue1)
    if sensorValue1 is None:
    print "No serial Data"
    else:
    temp1 = sensorValue1.split(',')
    value1=temp1[0]
    value2=temp1[1]
    value3=temp1[2]
    params = urllib.urlencode({'field1': value1, 'field2': value2,'field3': value3,'key':key })
    #params = urllib.urlencode({'field1': value1, 'key':key })
    headers = {"Content-typZZe": "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 value1
    print value2
    print value3
    print response.status, response.reason
    data = response.read()
    conn.close()
    except:
    print "connection failed"
    break
    #sleep for desired amount of time
    if __name__ == "__main__":
    while True:
    writesensordata()
    time.sleep(sleep)
     
    waltervl likes this.
  13. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Good! For documentation purposes: Can you share the Arduino serial.println code lines in which you created the combined sensor output?
     
  14. Gla

    Gla New Member

    Joined:
    Oct 25, 2016
    Messages:
    25
    Likes Received:
    4
    Surely!!! Thanks again for your feedback!

    Serial.print(sensorValue1);
    Serial.print(",");
    Serial.print(sensorValue2);
    Serial.print(",");
    Serial.print(sensorValue3);
    Serial.println();
     
    waltervl likes this.
  15. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Thanks, I made an update of the first post referring to your solution.
     
    Andrea Rovai likes this.

Share This Page