!/usr/bin/env python #import required stuff import time import serial import urllib import urllib2 #sample serial data for testing purposes only #serialData = ":03,C,222,CR,LF" #set up serial port on Raspberry Pi port = serial.Serial("/dev/ttyAMA0", baudrate=9600) # our urllib POST function def sendToSandpit(projectName, node, channel, data): #make a string to hold the url of the request url = 'http://sandpit.mtaspiring.school.nz/api/v1/insert.php' #place POST data in a dictionary data = {'project_id' : projectName, 'node' : node, 'channel' : channel, 'data' : data} #encode the POST data to be sent in a URL data_encoded = urllib.urlencode(data) #make a request object to hold the POST data and the URL req = urllib2.Request(url, data_encoded) #make the request using the request object as an argument, store response in a variable response = urllib2.urlopen(req) print response.read() while True: serialData = port.readline() #read buffer until cr/lf dataArray = serialData.split(',') # split data into node, channel, data #define variables from dataArray node = dataArray[0] channel = dataArray[1] data = dataArray[2] projectName = "Penguin" #if not null then... if(dataArray): print ("Serial #: " + repr(dataArray)) # Print to screen if ya want to test or debug sendToSandpit(projectName, node, channel, data) # This actions the sendToSandpit function
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question