Tinkerdoodle DIY
Published © MIT

Low power ESP8266 sensor data logger

This tutorial demonstrates how to make an ultra low power sensor data logger running on 2 AA batteries for more than a year.

BeginnerFull instructions provided1 hour2,332
Low power ESP8266 sensor data logger

Things used in this project

Story

Read more

Schematics

Schematic Diagram

Code

MicroPython program

Python
# This program collects the sensor data every hour, and uploads to a Google Spreadsheet.
# Replace the following parameters before you run the code.
# - ${WIFI_SSID}
# - ${WIFI_PASSWORD}
# - ${COLLECTOR_ID}
# Refer to the Apps Script on how to get the COLLECTOR_ID.

import dht
import machine
import network
import time
import usocket
import ussl

location = b'garden-bed'

def setup():
    ap_if = network.WLAN(network.AP_IF)
    sta_if = network.WLAN(network.STA_IF)
    ap_if.active(False)
    sta_if.active(True)
    sta_if.connect('${WIFI_SSID}', '${WIFI_PASSWORD}')
    for i in range(20):
        if sta_if.isconnected():
            break
        time.sleep(0.25)

def log():
    soil_vcc = machine.Pin(4, machine.Pin.OUT)
    soil_vcc.on()
    led = machine.Pin(2, machine.Pin.OUT)
    for i in range(4):
        led.off()
        time.sleep(0.25)
        led.on()
        time.sleep(0.25)
    dht_sensor = dht.DHT11(machine.Pin(5))
    dht_sensor.measure()
    temp = dht_sensor.temperature()
    humidity = dht_sensor.humidity()
    soil_sensor = machine.ADC(0)
    moisture = soil_sensor.read()
    soil_vcc.off()
    s = usocket.socket()
    ai = usocket.getaddrinfo("script.google.com", 443)
    addr = ai[0][-1]
    s.connect(addr)
    s = ussl.wrap_socket(s)
    req = b"GET /macros/s/"
    req = req + b"${COLLECTOR_ID}"
    req = req + b"/exec?tab=weather&location=" + location + b"&p1="
    req = req + str(temp) + b"&p2=" + str(humidity) + b"&p3=" + str(moisture)
    req = req + b" HTTP/1.1\r\n"
    req = req + b"Host: script.google.com\r\n"
    req = req + b"Connection: close\r\n"
    req = req + b"\r\n"
    s.write(req)
    s.read(100)
    s.close()

try:
    setup()
    log()
except:
    pass

rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
rtc.alarm(rtc.ALARM0, 3592000)
machine.deepsleep()

Apps Script

JavaScript
Create a new Apps Script at https://script.google.com using the following code. Once deployed, look at the URL to get the ID portion to be used as COLLECTOR_ID in the MicroPython program.
function doGet(e) {
  var ssId = "<your_spreadsheet_id>";
  var tab = e.parameter.tab;
  var location = e.parameter.location;
  var p1 = e.parameter.p1;
  var p2 = e.parameter.p2;
  var p3 = e.parameter.p3;
  var p4 = e.parameter.p4;
  var now = new Date();
  var ss = SpreadsheetApp.openById(ssId);
  var sheet = ss.getSheetByName(tab);
  var output = HtmlService.createHtmlOutput();

  sheet.appendRow([now, location, p1, p2, p3, p4]);
  output.append("OK");
  return output;
}

Credits

Tinkerdoodle DIY

Tinkerdoodle DIY

3 projects • 8 followers

Comments