Python/MicroPython Sensor Logger with Google Sheets

Story

A system for recording sensor values to a Google Sheet. Making use of HTTP requests to communicate between the micro-controller and the server, and utilising gspread to write data to online spreadsheet.

You will need to follow the instructions on the following link to set up access to your spreadsheet.

http://gspread.readthedocs.io/en/latest/oauth2.html

pin D0 to RST: Must be connected to wake from Deepsleep.

+ : to 3.3v on Wemos
– : to GND on Wemos
Signal: to A0 on Wemos
As usual, if you need help implementing this in your own project, leave me a comment and I’ll get back to you.

Hardware components

Raspberry Pi 3 Model B
Everything ESP Wemos D1 Mini
DFRobot Capacitive Soil Moisture Sensor

Software apps and online services

Google Sheets
python3
micropython

Code

import machine
import urequests
import time

rtc = machine.RTC() # Clock for deepsleep
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)

adc = machine.ADC(0) # Pin to Read sensor voltage

######################
# Sensor calibration #
######################

# values on right are inverse * 1000 values on left
# dry air = 759 (0%) = 1.31752305665349143610013175231
# water = 382 (100%) = 2.61780104712041884816753926702
# The Difference     = 1.30027799046692741206740751471
# 1 %                = 0.0130027799046692741206740751471

hours = str(time.localtime()[3])
mins = str(time.localtime()[4])
secs = str(time.localtime()[5])
if int(secs) < 10:
	secs = '0' + secs
if int(mins) < 10:
	mins = '0' + mins
timestr = hours + ':' + mins + ':' + secs

variable = (((1 / adc.read())* 1000) / 0.0130027799046692741206740751471) - 101
if variable > 100:
	variable = 100
if variable < 0:
	variable = 0

url = 'http://192.168.1.2:8000/solomon'
headers = {'content-type': 'application/json'}
data = '{"Value": "%s", "Time": "%s"}' % (variable, timestr)
resp = urequests.post(url, data=data, headers=headers) # Send the request
print(resp.json())

rtc.alarm(rtc.ALARM0, 25000) # Set alarm for 25 seconds
machine.deepsleep() # Go to sleep ...