Page 1 of 1

temp and humidity data to google docs spreadsheet

Posted: Sun Jul 26, 2015 2:15 pm
by dahunter
Hi Ladies, Hi Guys,

just wanted to share my recent project with you: A HDC1008 temperature and humidity sensor connected via IC2 to a Arduino, connected via USB to a PI2 which uploads the data to google docs.

The time has come to start sharing my stuff, after struggeling with the setup. Anyway, it works.

What you need: Arduino, a HDC1008, a PI2 and the stuff to connect them. Other sensors will work the same way, if you use the Arduino as the sensor backbone.

Step 1
Wire the HDC1008 according specification to PIN4 and 5 of the Arduino. Use the HDC1000 library from http://www.truzzi.me to skip the manual I2C implementation. The sketch must output the temperature and humidity in the format "XX.XX_XX.XX" / "25.00_48.00" to be parsed with regular expressions later on. Save the sketch and test it. [To understand what the HDC1000 library does, have a look at the HDC1000.h and HDC1000.cpp]. You can connect other sensors to the Arduino, this tutorial is more about get data to google docs. This will work with any values, just change the pattern of the regular expressions.

Step 2
Connect the Arduino to the PI2 via USB cable. Install the Arduino IDE. Test the connection by uploading the sketch. That should be no problem, if you had values in Step 1, you likely have the same values here.

Step 3
Install all the necessary libaries to get started with uploading data to google docs. I used gspread to connect to google.

Code: Select all

sudo apt-get update

Code: Select all

sudo apt-get install build-essential python-dev libssl-dev libffi-dev python-pip

Code: Select all

sudo pip install gspread oauth2client pyopenssl 
To set up gspread and oauth, to connect to googles docs API, read this tutorial https://gspread.readthedocs.org/en/latest/oauth2.html. To learn about gspread commands, have a look at this https://gspread.readthedocs.org/en/latest/. The JSON file needs to be in the same folder as the python code of this project. Then go to google docs, create a new spreadsheet, name the sheet temphumi and columns date, time, temp, humi (or whatever values you use). Then share it with the email of the google api you see in the developer console or the JSON file. After that, you should be able to connect to google docs. Before uploading values, delete rows 2-1000. This makes sure, that new lines will not be added at row 1001 but on row 2. Rename the variables in the sourcecode to use it:

Code: Select all

import serial
import re
import time
import json
import gspread


from oauth2client.client import SignedJwtAssertionCredentials

pattern = r"(\d+\.\d*)_(\d+\.\d*)\r\n" #check regual impressions generator at google
port = '/dev/ttyACM1'
port2 = '/dev/ttyACM0' #Select if not on ACM1
baud = 115200
json_key = json.load(open('temphumi-86f9d0378d0f.json'))
scope = ['https://spreadsheets.google.com/feeds']

credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)

s = serial.Serial(port2, baud, timeout = 1)
if s.isOpen():
  print "connected"

  while True:
      line = s.readline()
      if len(line) > 6:
        m = re.match(pattern, line)
	tableline = [str(time.strftime("%d/%m/%Y")), str(time.strftime("%H:%M:%S")), m.group(1), m.group(2)]
        gc = gspread.authorize(credentials)
	wks = gc.open("humitemp").sheet1 #avoids http 401 unauthorized after 15 minutes 
	wks.append_row(tableline)
        #print ("GROUP1: " + m.group(1) + " GROUP2: " + m.group(2))
	print (tableline)
        time.sleep(30)
      else:
        time.sleep(1)

  print "killing"
  s.close()

else:
  print ("Failure opening serial port at " + port)
Save the code in the file google.py and then run it in the console with

Code: Select all

python google.py
The script will output the values it uploaded or show an error message if the serial connection failed.

Step 4
Enjoy sensor values comming in from anywhere in the world

Hope that helps someone out there.

Regards
dahunter

Re: temp and humidity data to google docs spreadsheet

Posted: Mon Jul 23, 2018 9:25 am
by barnabi
Hi there and thanks for sharing your project!

I've tried to do a similar arrangement with a dht22 sensor and everything is working well untill step 4. I receive data from arduino that I can print on terminal. Then I have the json file downloaded, the docs table shared with the email and I can print out the credentials as well also the serial is connected. The problem comes in when I try to run the full code of google.py

I get the following error message:

Traceback (most recent call last):
File "google.py", line 33, in <module>
tableline = [str(time.strftime("%d/%m/%Y")), str(time.strftime("%H:%M:%S")), m.group(1), m.group(2)]
AttributeError: 'NoneType' object has no attribute 'group'

Can you help me figure out what went wrong?

Re: temp and humidity data to google docs spreadsheet

Posted: Mon Jul 23, 2018 9:55 am
by B.Goode
Can you help me figure out what went wrong?

If that was addressed to the author of the initial post you should be aware that user @dahunter has not logged back into these forums since sharing that project 3 years ago.


As to the source of the error -

Maybe the semantics of the re library have changed? Have you checked the documentation?

Maybe the data being returned over the serial port from the Arduino is no longer in the same format, causing the regexp match to fail? Have you examined the serial data being received?

Note that the error is

Code: Select all

AttributeError: 'NoneType' object has no attribute 'group'
My hunch is that the re.match operation failed and returned a value of None, possibly because the line of data read from the serial port was incorrect.