mirror of
https://github.com/KevinMidboe/ESP-Plant-Logger.git
synced 2025-10-29 17:40:20 +00:00
v0.1 🙆♀️ Chewie, we're home
This commit is contained in:
29
boot.py
Normal file
29
boot.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# This file is executed on every boot (including wake-boot from deepsleep)
|
||||||
|
#import esp
|
||||||
|
#esp.osdebug(None)
|
||||||
|
import uos, machine
|
||||||
|
#uos.dupterm(None, 1) # disable REPL on UART(0)
|
||||||
|
import gc
|
||||||
|
#import webrepl
|
||||||
|
#webrepl.start()
|
||||||
|
gc.collect()
|
||||||
|
|
||||||
|
# - - - NETWORKING - - -
|
||||||
|
import network
|
||||||
|
sta_if = network.WLAN(network.STA_IF)
|
||||||
|
|
||||||
|
def connectWifi():
|
||||||
|
sta_if.active(True)
|
||||||
|
|
||||||
|
# PSID and password for wifi
|
||||||
|
sta_if.connect('', '')
|
||||||
|
return sta_if
|
||||||
|
def disconnectWifi():
|
||||||
|
sta_if.active(False)
|
||||||
|
|
||||||
|
if not sta_if.isconnected():
|
||||||
|
print('connecting to network...')
|
||||||
|
connectWifi()
|
||||||
|
while not sta_if.isconnected():
|
||||||
|
pass
|
||||||
|
print('network config:', sta_if.ifconfig())
|
||||||
16
elasticLog.py
Normal file
16
elasticLog.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import json
|
||||||
|
import urequests
|
||||||
|
|
||||||
|
headers = { 'Content-Type': 'application/json' }
|
||||||
|
url = 'http://localhost.com:9200'
|
||||||
|
index = 'botany'
|
||||||
|
|
||||||
|
def logger(msg, data=None):
|
||||||
|
uri = '/'.join([url, index, '_doc'])
|
||||||
|
r = urequests.post(uri, data=json.dumps(data), headers=headers)
|
||||||
|
|
||||||
|
print(r.status_code)
|
||||||
|
text = r.text
|
||||||
|
r.close()
|
||||||
|
|
||||||
|
return text
|
||||||
15
main.py
Normal file
15
main.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
from watercontent import watercontent
|
||||||
|
from elasticLog import logger
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print('main loaded')
|
||||||
|
try:
|
||||||
|
watercontent()
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
logger('Error thrown', {'message': 'Error thrown', 'error': e})
|
||||||
|
print('starting again')
|
||||||
|
watercontent()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
42
watercontent.py
Normal file
42
watercontent.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import ntptime
|
||||||
|
from time import sleep
|
||||||
|
from machine import ADC, deepsleep, DEEPSLEEP, RTC, Timer
|
||||||
|
from boot import connectWifi, disconnectWifi
|
||||||
|
from elasticLog import logger
|
||||||
|
|
||||||
|
signalPin = ADC(0)
|
||||||
|
NTP_OFFSET = 946684800
|
||||||
|
DEEPSLEEP_TIME=30 # in seconds
|
||||||
|
|
||||||
|
def logMoisture(reading):
|
||||||
|
try:
|
||||||
|
time = ntptime.time()
|
||||||
|
time = time + NTP_OFFSET
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'@timestamp': int(time*1000),
|
||||||
|
'moisture': reading,
|
||||||
|
'message': 'Moisture reading',
|
||||||
|
'plant_id': 2,
|
||||||
|
'plant_name': 'Gulrorbambus'
|
||||||
|
}
|
||||||
|
return logger('Moisture reading', data=data)
|
||||||
|
except OSError as e:
|
||||||
|
logger('Error', data={'message': 'Error fetching ntp time', 'error': e})
|
||||||
|
|
||||||
|
|
||||||
|
def watercontent():
|
||||||
|
moistureReading = signalPin.read()
|
||||||
|
|
||||||
|
print(logMoisture(moistureReading))
|
||||||
|
# A little extra time alive for file transfere
|
||||||
|
print('3 second upload window before deepsleep')
|
||||||
|
sleepTriggerTimer = Timer(-1)
|
||||||
|
sleepTriggerTimer.init(period=3000, mode=Timer.ONE_SHOT, callback=_sleep)
|
||||||
|
|
||||||
|
def _sleep(_):
|
||||||
|
print('deepsleep starting for {} seconds'.format(DEEPSLEEP_TIME))
|
||||||
|
rtc = RTC()
|
||||||
|
rtc.irq(trigger=rtc.ALARM0, wake=DEEPSLEEP)
|
||||||
|
rtc.alarm(rtc.ALARM0, DEEPSLEEP_TIME * 1000)
|
||||||
|
deepsleep()
|
||||||
Reference in New Issue
Block a user