Python/MicroPython Sensor Logger: Seamlessly Store Sensor Data in Google Sheets
A robust system for recording sensor values directly into a Google Sheet. It uses simple HTTP requests to bridge your micro‑controller with a lightweight server, and the gspread library to write the data.
Before you begin, follow the OAuth2 setup instructions on the gspread documentation to grant your script access to the target spreadsheet.
Hardware connections
- D0 to RST: Required to wake the board from deep‑sleep.
- + to 3.3 V on the Wemos.
- – to GND on the Wemos.
- Signal to A0 on the Wemos.
If you run into any issues, drop a comment and I’ll help you troubleshoot.
Schematics
Code
import machine
import urequests
import time
# RTC handles deep‑sleep wake‑ups
rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
# ADC pin for sensor voltage
adc = machine.ADC(0)
######################
# Sensor calibration #
######################
# Reference values (inverse ×1000)
# Dry air = 759 (0 %) → 1.31752305665349143610013175231
# Water = 382 (100 %) → 2.61780104712041884816753926702
# Difference = 1.30027799046692741206740751471
# 1 % relative humidity = 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 = f"{hours}:{mins}:{secs}"
# Convert ADC reading to relative humidity (%)
variable = (((1 / adc.read()) * 1000) / 0.0130027799046692741206740751471) - 101
variable = max(0, min(100, variable))
# Send data to local endpoint
url = 'https://192.168.1.2:8000/solomon'
headers = {'content-type': 'application/json'}
data = f'{{"Value":"{variable}","Time":"{timestr}"}}'
resp = urequests.post(url, data=data, headers=headers)
print(resp.json())
# Schedule next reading after 25 s and enter deep‑sleep
rtc.alarm(rtc.ALARM0, 25000)
machine.deepsleep()
Source: Python/MicroPython Sensor Logger for Google Sheets
Manufacturing process
- Build a Raspberry Pi Temperature Logger with a $5 I2C Sensor
- Build a Remote Temperature Sensor with Raspberry Pi and Python – Step‑by‑Step Guide
- Line Tracking Sensor with Raspberry Pi – Simple KY‑033 Door/Line Detector
- Build a Robust Temperature & Sensor API on Raspberry Pi Using the GY‑91 Module
- Build a Robot with Raspberry Pi and Python: A Complete Guide
- Integrating a Thermocouple Sensor with the Arduino Portenta H7 Using the MAX6675 IC
- Master Python Extension Programming with C: Build Fast, Native Modules
- K30 CO2 Sensor: Real‑Time Indoor Air Quality Monitoring
- Real‑Time IoT Pressure Monitoring with MKR GSM, Arduino Cloud, and Google Sheets
- Build an IR Sensor Project with Arduino UNO – Simple Guide