connecting arduino to thingspeak using python as an middle ware
DESCRIPTION
Connecting Arduino to Thingspeak using python as an middle wareTRANSCRIPT
2011
Author: Manoj
Reviewer: Mithun
Revision: 1.1
Tenet Technetronics
7/15/2011
Application Note: AN101 Connecting Arduino to Thingspeak using python as an middle ware
i
Contents
Introduction
Introduction to Arduino connected to Thingspeak
Hardware Overview
Schematic
Software Overview
Flow Chart
Pseudo code
Code
Demo Application
References and further reading
1
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
2
Hardware Overview
Schematic
Here we are connecting variable potentiometer (1k) to Arduino analog pin A0.
Fig1: schematic
NOTE: The pin out for variable potentiometer
Fig2: potentiometer pin out
3
Software overview
Flowchart
For python: For Arduino:
NO
YES
Void setup
Serial communication
initialization
Void loop
Read analog values
from A0
Start
End
print the analog
values and update
the same data to
thingspeak
Serial
communication and
baud rate
initialization
Read the data from
the potentiometer
Start
End
While
true
If new
data
Print the previous
value
4
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 : While
Description : infinite loop
Body : reading analog values continuously
Condition : if
Description : if condition true control enters the body
Body : If there is newly available serial data then print the value and store
it in a new variable
Set up a connection
Condition : else
Description : if all the conditions fails control enters the body
Body : store the previously read value
For Arduino:
Function : setup
Return type : void
Description : initialization
Body : serial communication initialization
Function : loop
Return type : void
Description : infinite loop
Body : read analog values from Arduino analog pin A0
5
code
PYTHON CODE:
import httplib2 // for HTTP connection
import serial // 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
}
6
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
Fig3: Arduino with potentiometer
The Arduino sketch for reading the analog values from the potentiometer
Fig4: Arduino with potentiometer
7
Serial monitor showing the values which is read from potentiometer
Fig5: Arduino serial monitor
Python code on emac editor which tell to read and upload the same Data to Thingspeak
Fig6: python code
8
Open the terminal on the Linux operating system.
Fig7: Linux terminal
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.
Fig8: results on terminal window
9
Things speak home page, we need to log in with our account
Fig9: Thingspeak homepage
Here I have signed in and you can see the channels given to me.
Fig10: Thingspeak channel
10
Here is my channel 873 click for VIEW CARTS .
Fig11: Thingspeak charts screen
Through this link u can fill up the chart characteristics as per the need
http://community.thingspeak.com/documentation/api/#charts
Fig12: chart documentation screen
11
Here is my output on my cart which is same on terminal and serial monitor
Fig13: results on Thingspeak chart
References and Further Reading
www.arduino.cc
www.tenettech.com/comet
http://community.thingspeak.com/documentation/api/#charts
http://community.thingspeak.com
http://pyserial.sourceforge.net/
http://python.org/
12
Contact details
Tenet Technetronics,
No 8/14, Third Floor, MN Chambers,
P.T.Street,
Opp to Pai Vista Convention Hall,
Basavangudi,
Bangalore -560004, India
Ph: 080-42103124
Email : [email protected]