Descrição
O sketch foi obtido de https://sites.google.com/site/lucidguppy2/dataloggeridea e modificado a partir de exemplos contidos na seção Examples do Processing. Aqui a leitura de umidade (e temperatura) medidas pelo sensor DTH11 e são lidas pelo Arduino e enviadas para a porta serial( via usb). O script em Python, intercepta esses dados seriais e atualiza uma planilha Excel que "mora" nas nuvens.
Material :
1. Arduino Atmega 2560
2. Sensor de temperatura e umidade DTH11 comprado da Iteadstudio.
3. Prot-o-board
4. Paciência e imaginação.
Sketch (lado do Arduino):
//
// FILE: dht11_test1.pde
// PURPOSE: DHT11 library test sketch for Arduino
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}
//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}
// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}
// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 2
void setup()
{
Serial.begin(115200);
}
void loop()
{
int chk = DHT11.read(DHT11PIN);
Serial.println((float)DHT11.humidity, 2);
delay(2000);
}
//
// END OF FILE
//
Script do Python
Script do Python:
import gdata.spreadsheet.service
import gdata.service
import atom.service
import gdata.spreadsheet
import atom
import getopt
import sys
import string
import serial
import time
fmt = '%m/%d/%Y %H:%M:%S'
# Code gathered and arranged from arduino python examples and
# google documentation by lucidguppy.
# http://www.lucidguppy.com
# from getting started - google
# functions copied from example in the #documentation
# http://code.google.com/p/gdata-python-#client/
# #http://code.google.com/apis/spreadsheets/d#evelopers_guide_python.html
# Please read the code and documentation #from the links above...it goes a
# long way to answering your questions.
def StringToDictionary(row_data):
dict = {}
for param in row_data:
temp = param.split('=')
dict[temp[0]] = temp[1]
return dict
def _CellsUpdateAction(row,col,inputValue,sheet_key, work_key):
entry = spr_client.UpdateCell(row=row, col=col, inputValue=inputValue,
sheet_key=_key, work_key=curr_wksht_id)
if isinstance(entry, gdata.spreadsheet.SpreadsheetsCell):
print 'Updated!'
def PrintFeed(feed):
for i, entry in enumerate(feed.entry):
if isinstance(feed, gdata.spreadsheet.SpreadsheetsCellsFeed):
print '%s %s\n' % (entry.title.text, entry.content.text)
elif isinstance(feed, gdata.spreadsheet.SpreadsheetsListFeed):
print '%s %s %s' % (i, entry.title.text, entry.content.text)
# Print this row's value for each column (the custom dictionary is
# built from the gsx: elements in the entry.) See the description of
# gsx elements in the protocol guide.
print 'Contents:'
for key in entry.custom:
print ' %s: %s' % (key, entry.custom[key].text)
print '\n',
else:
print '%s %s\n' % (i, entry.title.text)
def ListInsertAction(gd_client, key, wksht_id, row_data):
entry = gd_client.InsertRow(row_data,
key, wksht_id)
if isinstance(entry, gdata.spreadsheet.SpreadsheetsCell):
print 'Inserted!'
def PromptForWorksheet(gd_client, key):
# Get the list of worksheets
feed = gd_client.GetWorksheetsFeed(key)
PrintFeed(feed)
input = raw_input('\nSelection: ')
return feed.entry[string.atoi(input)].id.text.rsplit('/', 1)[1]
def PromptForSpreadsheet(gd_client):
# Get the list of spreadsheets
feed = gd_client.GetSpreadsheetsFeed()
PrintFeed(feed)
input = raw_input('\nSelection: ')
return feed.entry[string.atoi(input)].id.text.rsplit('/', 1)[1]
##### heres the beginning of the script ##########
gd_client = gdata.spreadsheet.service.SpreadsheetsService()
#Keep in mind to make your own new gmail #datalogger account, so you protect your #password here.
gd_client.email = 'youremail@gmail.com' #Put your email here
gd_client.password = 'xxxxxxxx' #Put the login password here
gd_client.source = 'test3'
gd_client.ProgrammaticLogin()
#serial stuff taken from arduino website
#http://www.arduino.cc/playground/Interfacin#g/Python
ser = serial.Serial()
ser.port = "/dev/ttyACM0" # may be called something different
ser.baudrate = 115200
ser.open()
while 1:
# get the reading then append it to the end of #the file up on google
print 'waiting'
val = ser.readline()
t=ser.readline()
print 'uploading %s' % (val)
feed = gd_client.GetSpreadsheetsFeed()
sheet_key = feed.entry[0].id.text.rsplit('/', 1)[1]
work_feed = gd_client.GetWorksheetsFeed(sheet_key)
work_key = work_feed.entry[0].id.text.rsplit('/', 1)[1]
ListInsertAction(gd_client, sheet_key, work_key, {'time':time.strftime(fmt),'data':str(val),'temp':str(t)})
_CellsUpdateAction(2, 2, str(val), sheet_key, work_key)
_CellsUpdateAction(2, 3, str(t), sheet_key, work_key)