Connecting Arduino To Thingspeak Using Python As An Middle Ware
Connecting Arduino To Thingspeak Using Python As An Middle Ware
Connecting Arduino To Thingspeak Using Python As An Middle Ware
Contents
Introduction
Introduction to Arduino connected to Thingspeak
Hardware Overview
Schematic
Software Overview
Flow Chart Pseudo code Code
Introduction
Here the Arduino is connected to Thingspeak through python and python is communicating to Arduino through Pyserial. Pyserial supports two way communications with file handles. You can read and write strait to serial. I am using it to read potentiometer data which is connected to Arduino and uploading the same data to Thingspeak.
Hardware required
Arduino/Comet Potentiometer (10k)
You can buy the products and other electronic components at www.tenettech.com
Software required
Arduino IDE Python Emac editor
You can download the Arduino software, Emac editor, python, and pyserial package through the following links www.arduino.cc http://www.gnu.org/software/emacs/ http://python.org/download/ http://linux.softpedia.com/get/Programming/Libraries/pySerial-51222.shtml
Hardware Overview
Schematic
Here we are connecting variable potentiometer (1k) to Arduino analog pin A0.
Fig1: schematic
Software overview
Flowchart
For python:
Start
For Arduino:
Start
While true
Read the data from the potentiometer End Print the previous value
NO
If new data
YES
print the analog values and update the same data to thingspeak
End
Pseudo code
For python:
Include header files for SERIAL and HTTP Global declaration: For Serial port selection For setting baud rate. For variable to store serial data. Loop Description Body Condition Description Body : While : infinite loop : reading analog values continuously : if : if condition true control enters the body : If there is newly available serial data then print the value and store it in a new variable Set up a connection : else : if all the conditions fails control enters the body : store the previously read value
For Arduino:
Function Return type Description Body Function Return type Description Body : setup : void : initialization : serial communication initialization : loop : void : infinite loop : read analog values from Arduino analog pin A0
code
PYTHON CODE:
import httplib2 import serial // for HTTP connection // for serial communication
ser = serial.Serial('/dev/ttyACM0') // serial port selected as /dev/ttyACM0 in arduino ser.baudrate = 9600 // baud rate is set to 9600 temp = '' a=0 while True: ch = ser.read(1) if ch == '\n': print temp a = int(temp) conn = httplib2.Http() // include API KEY for your channel conn.request("http://api.thingspeak.com/update?key=VQ927LPM9PNYMTQP&field1=%d" %(a)) temp = '' else: temp += ch
ARDUINO CODE:
/* AnalogReadSerial Reads an analog input on pin 0, prints the result to the serial monitor */ void setup() { Serial.begin(9600); //baud rate is set to 9600 } void loop() { int sensorValue = analogRead(A0); // reading analog values from A0 of arduino Serial.println(sensorValue); // printing the read values }
Demo Application
Through this demo application we are going to read the analog values from the Potentiometer and the same data we are uploading to the Thing speak using an Arduino and an internet connection from the PC Here i have connected a potentiometer to the Arduino
The Arduino sketch for reading the analog values from the potentiometer
Python code on emac editor which tell to read and upload the same Data to Thingspeak
Here I have called python code, and we can see the data from the potentiometer on the terminal is same as the Arduino serial monitor data.
Here I have signed in and you can see the channels given to me.
Through this link u can fill up the chart characteristics as per the need http://community.thingspeak.com/documentation/api/#charts
10
11
Contact details
Tenet Technetronics,
No 8/14, Third Floor, MN Chambers, P.T.Street, Opp to Pai Vista Convention Hall, Basavangudi, Bangalore -560004, India
12