This commit is contained in:
Max 2020-12-23 21:11:46 +00:00
parent 7042f79d81
commit 8cf5bc9031
4 changed files with 60 additions and 23 deletions

View File

@ -19,7 +19,9 @@ class DataCollector:
__API_KEY_PATH = "secrets/weather_api_key.txt"
__CITY = "London"
__API_ENDPOINT = "api.openweathermap.org/data/2.5/weather"
__TESTING = False
__NODE_T_ENDPOINT = "http://ss.maxhunt.design:3000/temp"
__NODE_H_ENDPOINT = "http://ss.maxhunt.design:3000/hmdt"
__TESTING = True
def __init__(self):
self.init_firebase()
@ -53,6 +55,20 @@ class DataCollector:
# or push previous data
return {"data": "TODO"}
try:
rsp = api_get(self.__NODE_H_ENDPOINT)
rsp_json = rsp.json()
local_hmdt = rsp_json.get('value', False)
except Exception:
local_hmdt = 0
try:
rsp = api_get(self.__NODE_T_ENDPOINT)
rsp_json = rsp.json()
local_temp = rsp_json.get('value', False)
except Exception:
local_temp = 0
weather_data = api_data.json()
station_data = weather_data.get('main', False)
if station_data:
@ -74,6 +90,8 @@ class DataCollector:
"cloud": current_cloud_pct,
"wind": current_wind_ms,
"rain_1h": rain_mm_1h,
"local_soil_humidity": local_hmdt,
"local_soil_temperature": local_temp,
"is_test": self.__TESTING
}
return relevant_data

View File

@ -0,0 +1,7 @@
FROM python:3.8
WORKDIR /code
COPY . .
RUN pip install requests
RUN pip install -r requirements.txt
EXPOSE 3333
CMD "/code/main.py"

View File

@ -3,7 +3,7 @@ import board
import busio
import digitalio
import logging
import time
from flask import Flask
import adafruit_rfm69
@ -18,8 +18,8 @@ class LoRa:
'cs': board.D22,
'rst': board.D27
}
__PACKET_WAIT = 3
__MAX_ATTEMPTS = 3
__PACKET_WAIT = 2
__MAX_ATTEMPTS = 5
def __init__(self):
spi = busio.SPI(self.__PINS['sck'],
@ -74,19 +74,46 @@ class Master:
def get_temp(self):
temp = self.com.send_and_wait('iot_g_temp|')
logging.info(f'Got temp from slave: {temp}ºC')
return temp
return float("{:.1f}".format(float(temp.decode())))
def get_hmdt(self):
hmdt = self.com.send_and_wait('iot_g_hmdt|')
logging.info(f'Got hmdt from slave: {hmdt}')
return hmdt
return int(hmdt.decode())
def run_pump(self, volume):
status = self.com.send_and_wait(f'iot_pmp_ctrl|{str(volume)}')
logging.info(status)
return status
return status.decode()
if __name__ == "__main__":
logging.root.setLevel(logging.DEBUG)
master = Master()
node = Flask(__name__)
prev_temp = master.get_temp()
prev_hmdt = master.get_hmdt()
@node.route("/")
def root():
return "IoT-ICL DE Weateher Master Node running..."
@node.route("/hmdt")
def humidity():
humidity = master.get_hmdt()
if humidity:
prev_hmdt = humidity
return {'success': True, 'value': humidity}
return {'success': False, 'value': prev_hmdt}
@node.route("/temp")
def temp():
temp = master.get_temp()
if temp:
prev_temp = temp
return {'success': True, 'value': temp}
return {'success': False, 'value': prev_temp}
node.run(host='0.0.0.0', port='3333', use_reloader=False)

View File

@ -3,20 +3,5 @@ adafruit-circuitpython-busdevice==5.0.1
adafruit-circuitpython-rfm69==2.1.0
Adafruit-PlatformDetect==2.23.0
Adafruit-PureIO==1.1.8
asn1crypto==0.24.0
certifi==2018.8.24
chardet==3.0.4
cryptography==2.6.1
entrypoints==0.3
idna==2.6
keyring==17.1.1
keyrings.alt==3.1.1
pycrypto==2.6.1
pyftdi==0.52.0
requests==2.21.0
RPi.GPIO==0.7.0
SecretStorage==2.3.1
six==1.12.0
ssh-import-id==5.7
sysv-ipc==1.0.1
urllib3==1.24.1
Flask==1.1.2