Summary: Getting the arduino to read the temperature
My Arduino has been sitting on my desk for some time and I thought it was about time to get it working again. I figured I would get it to do something simple: read the temperature from an LM35 sensor.
My setup consists of:
- An Arduino Duemilenove,
- An LM35 connected to the Arduino
- A Computer running Linux Mint,
- An account at ThingSpeak for storing and plotting the data.
The Arduino
To get the temperature reading from the LM35 I programmed the arduino to:
- Read from the analog port (A0).
- Convert the 0-1023 (10bit) to a value between 0 and 5V (Vsupply).
- Convert that voltage to temperature based on the LM35 outputing 10mV/C.
/* Reads the voltage output from an LM35
and converts the voltage to a temperature C.
The analog input: 0 - 1023
Vs: 5V
LM35 output: 10mV/C
*/
int inputPin = A0; // LM35 output connected to Analog pin0
int inputValue = 0; // analog sensor value 0 to 1023
float temperature = 0; //initialize temperature variable
char tempValue[]="";
void setup() {
// inputPin is an INPUT
pinMode(inputPin, INPUT);
Serial.begin(9600);
}
void loop() {
// read the voltage from the LM35
inputValue = analogRead(inputPin);
//map the analog reading of the LM35 based on a Vs: 5V and Vin: 10mV/C
temperature=(float(inputValue)/1023.0)*500.0;
//print to USB
Serial.print("Temperature: ");
Serial.println(temperature,1);
}
The output from the Arduino looks like:
Temperature: 25
Read the Arduino output and send to ThingSpeak
The output from the Arduino was read by a python script on my laptop. It received the temperature from the Arduino and then posted it to my account at ThingSpeak. You could just as easily write the temperatures to a file, but I like how easy it is to get a nice chart from ThingSpeak.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import serial
import re
import requests
import ConfigParser
def sendToThingSpeak(temperature, api_key):
Parameters={"key":api_key, "field1":temperature.group(1)}
response=requests.post("https://api.thingspeak.com/update", params=Parameters)
return (response)
print "Starting ..."
config = ConfigParser.ConfigParser()
config.read('config.cfg')
api_key = config.get('default','api_key')
#Search pattern for the temperature value
dataForm = r'Temperature: (\d+\.\d*)'
repeat = True
#set up the USB port
print "Setting up serial ..."
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=None)
while(repeat):
#read in the temperature from the Arduino
dataLine=ser.readline()
if dataLine: #if the reading was successful
temperature=re.search(dataForm,dataLine) #extract the temperature from the reading
if temperature:
print temperature.group(1)
repeat=False
response = sendToThingSpeak(temperature, api_key)
print response
serial.close()
And the config.cfg file is just
[default]
api_key:your thingspeak key
I used crontab to run the python script every 20 minutes. Since I used a config file to keep my api_key I had to get crontab cd into the python scripts directory then run the script. As well since the script prints output, the output needs to be directed to a log file.
crontab
*/20 * * * * cd path/to/python/script.py %% ./pythonscript.py >> script.log
I then can call the chart from thingspeak and place it on my page using
<iframe width="620" height="370" style="border: 1px solid #cccccc;" src="https://thingspeak.com/channels/8220/charts/1?width=600&height=350&results=20"></iframe>
The values are then plotted using the ThingSpeaks chart api.