2015년 7월 17일 금요일

[4th Week] Simple Example - ThingSpeak API

I could send sensor data from Arduino to Raspberry Pi with serial connection. It would be better that we can check our sensor data as time changes. Open source API like ThingSpeak can be a simple solution. Then, the design can be described like this.

Sensor -> Arduino -> Raspberry Pi -> ThingSpeak

We can sign up in this website(https://thingspeak.com/), and easily use the platform. I sent data from temperature sensor that I used before. Here's python code on RPi below.

import sys
impotr serial
import urllib2

port = "/dev/ttyACM0"

serialFromArduino = serial.Serial(port, 9600)
serialFromArduino.flushInput()

def getSensorData():

    input = serialFromArduino.read(5)
    print(input + 'C')
    return str(input)

def main():

    if len(sys.argv) < 2:
        print('Usage: python test.py PRIVATE.KEY')
        exit(0)
    print('starting...')
    baseURL = 'httpsL//api.thingspeak.com/update?key=%s' % sys.argv[1]

    while True:

        if(serialFromArduino.inWaiting() > 0):
            tem = getSensorData()
            f = urllib2.urlopen(baseURL + "&field1=%s" % tem)
            f.close()

if __name__ == '__main__':

    main()


We can compile it with sudo python test.py PRIVATEAPIKEY command. Just put API key in that black box.

Result looks like this. Code on Arduino is same with the one I used before, but it seems it works well with over 15 sec delay. When I set the delay 10 seconds, the graph captures data in every 20 second.

댓글 없음:

댓글 쓰기