Application Note: AN101
Connecting Arduino to Thingspeak using python as an middle ware

Author: Manoj Reviewer: Mithun Revision: 1.1 Tenet Technetronics 7/15/2011

Introduction to Arduino connected to Thingspeak

Hardware Overview

Software Overview
Flow Chart Pseudo code Code

Demo Application References and further reading


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
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


Software overview
For python:

For Arduino:

Serial communication and baud rate initialization

Void setup Serial communication initialization

While true

Void loop Read analog values from A0

Read the data from the potentiometer End Print the previous value


If new data

print the analog values and update the same data to thingspeak



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

Condition Description Body

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


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

/* 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

Fig3: Arduino with potentiometer

The Arduino sketch for reading the analog values from the potentiometer

Fig4: Arduino with potentiometer


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


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


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


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


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/


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 : info@tenettech.com


Sign up to vote on this title
UsefulNot useful