running with two sensors
This commit is contained in:
parent
c8f4baf984
commit
f556189a0b
|
|
@ -0,0 +1,34 @@
|
||||||
|
import network
|
||||||
|
|
||||||
|
|
||||||
|
class DeviceInfo:
|
||||||
|
|
||||||
|
name = "Dev Device 1"
|
||||||
|
|
||||||
|
token = "PC]-0Bmp83h7F5#U!D6KJ(A&"
|
||||||
|
|
||||||
|
wlan = network.WLAN(network.STA_IF)
|
||||||
|
|
||||||
|
def get_macaddress(self):
|
||||||
|
return self._format_mac(self.wlan.config('mac').hex())
|
||||||
|
|
||||||
|
def get_ipaddress(self):
|
||||||
|
return self.wlan.ifconfig()[0]
|
||||||
|
|
||||||
|
def get_all_device_infos(self):
|
||||||
|
return {
|
||||||
|
'name': self.name,
|
||||||
|
'mac_address': self.get_macaddress(),
|
||||||
|
'ip_address': self.get_ipaddress(),
|
||||||
|
'token': self.token}
|
||||||
|
|
||||||
|
def _format_mac(self, mac):
|
||||||
|
# Split the MAC address into pairs of two characters each
|
||||||
|
pairs = [mac[i:i+2] for i in range(0, len(mac), 2)]
|
||||||
|
# Join the pairs with colons to create the formatted MAC address
|
||||||
|
formatted_mac = ":".join(pairs)
|
||||||
|
return formatted_mac
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
15
dht22.py
15
dht22.py
|
|
@ -7,7 +7,7 @@ class TemperatureHumiditySensor:
|
||||||
|
|
||||||
dht22_sensor = None
|
dht22_sensor = None
|
||||||
|
|
||||||
most_recent_values = {}
|
most_recent_values = []
|
||||||
|
|
||||||
def __init__(self, settings):
|
def __init__(self, settings):
|
||||||
print("Hello from dht22 sensor class")
|
print("Hello from dht22 sensor class")
|
||||||
|
|
@ -18,15 +18,18 @@ class TemperatureHumiditySensor:
|
||||||
def read(self):
|
def read(self):
|
||||||
try:
|
try:
|
||||||
self.dht22_sensor.measure()
|
self.dht22_sensor.measure()
|
||||||
self.most_recent_values = {
|
self.most_recent_values = [
|
||||||
'temperature': {
|
{
|
||||||
|
'type': 'temperature',
|
||||||
'value': self.dht22_sensor.temperature(),
|
'value': self.dht22_sensor.temperature(),
|
||||||
'unit': '°C'},
|
'unit': 'C'
|
||||||
'humidity': {
|
},
|
||||||
|
{
|
||||||
|
'type': 'humidity',
|
||||||
'value': self.dht22_sensor.humidity(),
|
'value': self.dht22_sensor.humidity(),
|
||||||
'unit': '%'
|
'unit': '%'
|
||||||
}
|
}
|
||||||
}
|
]
|
||||||
except OSError:
|
except OSError:
|
||||||
print('DHT22 Error reading temperature/humidity. Check wires')
|
print('DHT22 Error reading temperature/humidity. Check wires')
|
||||||
print()
|
print()
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,24 @@
|
||||||
import time
|
import time
|
||||||
from moisture_sensor import MoistureSensor
|
from moisture_sensor import MoistureSensor
|
||||||
from dht22 import TemperatureHumiditySensor
|
from dht22 import TemperatureHumiditySensor
|
||||||
|
from sensor_data_manager import SensorDataManager
|
||||||
|
from grow_system_api import GrowSystemApi
|
||||||
|
|
||||||
|
|
||||||
class GrowSystem:
|
class GrowSystem:
|
||||||
|
|
||||||
|
grow_system_api = GrowSystemApi()
|
||||||
|
|
||||||
moisture_sensor = None
|
moisture_sensor = None
|
||||||
temperature_humidity_sensor = None
|
temperature_humidity_sensor = None
|
||||||
|
|
||||||
most_recent_values = {}
|
most_recent_values = []
|
||||||
|
|
||||||
|
sensor_data_manager = None
|
||||||
|
|
||||||
|
device_id = None
|
||||||
|
|
||||||
def __init__(self, settings):
|
def __init__(self, settings):
|
||||||
print("hello from GrowSystem")
|
|
||||||
print(settings)
|
|
||||||
if not self.moisture_sensor:
|
if not self.moisture_sensor:
|
||||||
self.moisture_sensor = MoistureSensor(settings['moisture_sensor'])
|
self.moisture_sensor = MoistureSensor(settings['moisture_sensor'])
|
||||||
|
|
||||||
|
|
@ -20,16 +26,33 @@ class GrowSystem:
|
||||||
self.temperature_humidity_sensor = TemperatureHumiditySensor(settings['temperature_humidity_sensor'])
|
self.temperature_humidity_sensor = TemperatureHumiditySensor(settings['temperature_humidity_sensor'])
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
|
print("Say the server hello...")
|
||||||
|
result = self.grow_system_api.say_hello()
|
||||||
|
message = result['message']
|
||||||
|
|
||||||
|
if message != 'OK':
|
||||||
|
print("Device not activated. Stopping")
|
||||||
|
return
|
||||||
|
|
||||||
|
self.device_id = result['data']['device_id']
|
||||||
|
self.sensor_data_manager = SensorDataManager(self.device_id)
|
||||||
|
|
||||||
print("Start reading sensors ...")
|
print("Start reading sensors ...")
|
||||||
while True:
|
while True:
|
||||||
|
# Reset data
|
||||||
|
self.most_recent_values = []
|
||||||
# Moisture Sensor
|
# Moisture Sensor
|
||||||
self.moisture_sensor.read()
|
self.moisture_sensor.read()
|
||||||
self.most_recent_values['moisture_sensor'] = self.moisture_sensor.most_recent_value
|
self.most_recent_values = self.most_recent_values + self.moisture_sensor.most_recent_value
|
||||||
|
|
||||||
# Temperature and Humidity Sensor
|
# Temperature and Humidity Sensor
|
||||||
self.temperature_humidity_sensor.read()
|
self.temperature_humidity_sensor.read()
|
||||||
self.most_recent_values['temperature_humidity_sensor'] = self.temperature_humidity_sensor.most_recent_values
|
self.most_recent_values = self.most_recent_values + self.temperature_humidity_sensor.most_recent_values
|
||||||
|
|
||||||
|
print("Most recent bla")
|
||||||
print(self.most_recent_values)
|
print(self.most_recent_values)
|
||||||
time.sleep(1)
|
|
||||||
|
self.sensor_data_manager.handleData(self.most_recent_values)
|
||||||
|
|
||||||
|
time.sleep(5)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
from http_client import HTTPClient
|
||||||
|
from device_info import DeviceInfo
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
class GrowSystemApi:
|
||||||
|
|
||||||
|
http_client = HTTPClient()
|
||||||
|
|
||||||
|
device_info = DeviceInfo()
|
||||||
|
|
||||||
|
base_url = 'api.growsystem.muellerdev.kozow.com'
|
||||||
|
|
||||||
|
def say_hello(self):
|
||||||
|
data = self._get_device_data()
|
||||||
|
response = self.http_client.post(self.base_url + "/api/device", data)
|
||||||
|
jsonResult = json.loads(response.text)
|
||||||
|
print(jsonResult)
|
||||||
|
return jsonResult;
|
||||||
|
|
||||||
|
def send_measurements(self, device_id, data):
|
||||||
|
url = self.base_url + "/api/device/" + str(device_id) + "/sensor-log"
|
||||||
|
print(url)
|
||||||
|
response = self.http_client.post(url, data)
|
||||||
|
return json.loads(response.text)
|
||||||
|
|
||||||
|
def _get_device_data(self):
|
||||||
|
return self.device_info.get_all_device_infos()
|
||||||
|
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
import urequests
|
||||||
|
import json
|
||||||
|
|
||||||
|
class HTTPClient:
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get(self, url):
|
||||||
|
url = 'https://' + url
|
||||||
|
try:
|
||||||
|
# headers = {'Content-Type': 'application/json'}
|
||||||
|
response = urequests.get(url)
|
||||||
|
if response.status_code == 200:
|
||||||
|
print("Data sent, got response")
|
||||||
|
return response
|
||||||
|
else:
|
||||||
|
print("Failed to get data. Status code:", response.status_code)
|
||||||
|
except Exception as e:
|
||||||
|
print("Exception occurred:", e)
|
||||||
|
|
||||||
|
def post(self, url, data):
|
||||||
|
url = 'https://' + url
|
||||||
|
try:
|
||||||
|
headers = {'Content-Type': 'application/json', 'Accept': 'application/json, text/plain, */*'}
|
||||||
|
json_data = json.dumps(data)
|
||||||
|
print("Send post request to: " + url)
|
||||||
|
response = urequests.post(url, data=json_data, headers=headers)
|
||||||
|
if response.status_code == 200:
|
||||||
|
return response
|
||||||
|
else:
|
||||||
|
print("Failed to send data. Status code:", response.status_code)
|
||||||
|
print(response.text)
|
||||||
|
except Exception as e:
|
||||||
|
print("Exception occurred:", e)
|
||||||
|
|
||||||
32
main.py
32
main.py
|
|
@ -5,8 +5,15 @@
|
||||||
# This file should do only:
|
# This file should do only:
|
||||||
# - provide constants for settings
|
# - provide constants for settings
|
||||||
# - eventually necessary system settings
|
# - eventually necessary system settings
|
||||||
|
# - init wlan connection
|
||||||
# - Call base class, permitting the configured constants
|
# - Call base class, permitting the configured constants
|
||||||
|
import network
|
||||||
|
import urequests
|
||||||
from grow_system import GrowSystem
|
from grow_system import GrowSystem
|
||||||
|
from wlan import WlanClient
|
||||||
|
from http_client import HTTPClient
|
||||||
|
from device_info import DeviceInfo
|
||||||
|
|
||||||
|
|
||||||
settings = {
|
settings = {
|
||||||
'wlan_ssid': 'Oppa-95.lan',
|
'wlan_ssid': 'Oppa-95.lan',
|
||||||
|
|
@ -20,10 +27,33 @@ settings = {
|
||||||
'pump_pin_int': 24
|
'pump_pin_int': 24
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def wlan_scan():
|
||||||
|
# Client-Betrieb
|
||||||
|
wlan = network.WLAN(network.STA_IF)
|
||||||
|
# WLAN-Interface aktivieren
|
||||||
|
wlan.active(True)
|
||||||
|
# WLANs ausgeben
|
||||||
|
found_wlans = wlan.scan()
|
||||||
|
return found_wlans
|
||||||
|
|
||||||
|
|
||||||
# Press the green button in the gutter to run the script.
|
# Press the green button in the gutter to run the script.
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
#print(wlan_scan())
|
||||||
|
print("Connect WLAN")
|
||||||
|
wlanClient = WlanClient(settings['wlan_ssid'], settings['wlan_pw'])
|
||||||
|
wlanClient.connect()
|
||||||
|
print("---------------------------------------")
|
||||||
|
print("")
|
||||||
|
|
||||||
|
di = DeviceInfo()
|
||||||
|
print("Device Infos:")
|
||||||
|
print(di.get_all_device_infos())
|
||||||
|
print("---------------------------------------")
|
||||||
|
print("")
|
||||||
|
|
||||||
print("Start grow system")
|
print("Start grow system")
|
||||||
gs = GrowSystem(settings)
|
gs = GrowSystem(settings)
|
||||||
gs.start()
|
gs.start()
|
||||||
|
|
||||||
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ class MoistureSensor:
|
||||||
|
|
||||||
moisture_sensor = None
|
moisture_sensor = None
|
||||||
|
|
||||||
most_recent_value = -1
|
most_recent_value = []
|
||||||
|
|
||||||
def __init__(self, moisture_sensor_data):
|
def __init__(self, moisture_sensor_data):
|
||||||
self.moisture_sensor_pin_int = moisture_sensor_data['pin_int']
|
self.moisture_sensor_pin_int = moisture_sensor_data['pin_int']
|
||||||
|
|
@ -15,7 +15,10 @@ class MoistureSensor:
|
||||||
self.moisture_sensor = ADC(Pin(self.moisture_sensor_pin_int))
|
self.moisture_sensor = ADC(Pin(self.moisture_sensor_pin_int))
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
self.most_recent_value = self.moisture_sensor.read_u16()
|
self.most_recent_value = [
|
||||||
|
{
|
||||||
|
'type': 'moisture',
|
||||||
|
'value': self.moisture_sensor.read_u16(),
|
||||||
|
'unit': 'unknown'
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
from grow_system_api import GrowSystemApi
|
||||||
|
|
||||||
|
|
||||||
|
class SensorDataManager:
|
||||||
|
|
||||||
|
grow_system_api = None
|
||||||
|
|
||||||
|
base_url = 'api.growsystem.muellerdev.kozow.com'
|
||||||
|
|
||||||
|
device_id = None
|
||||||
|
|
||||||
|
def __init__(self, device_id):
|
||||||
|
self.grow_system_api = GrowSystemApi()
|
||||||
|
self.device_id = device_id
|
||||||
|
|
||||||
|
def handleData(self, data):
|
||||||
|
jsonResponse = self.grow_system_api.send_measurements(self.device_id, data)
|
||||||
|
print("---- Response: -----")
|
||||||
|
print(jsonResponse)
|
||||||
20
wlan.py
20
wlan.py
|
|
@ -1,7 +1,6 @@
|
||||||
import machine
|
import machine
|
||||||
import network
|
import network
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# network.country('DE')
|
# network.country('DE')
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -9,25 +8,32 @@ class WlanClient:
|
||||||
|
|
||||||
ssid = ''
|
ssid = ''
|
||||||
pw = ''
|
pw = ''
|
||||||
wlan = network.WLAN(network.STA_IF)
|
wlan = None
|
||||||
# Status-LED
|
# Status-LED
|
||||||
led_onboard = machine.Pin('LED', machine.Pin.OUT)
|
led_onboard = machine.Pin('LED', machine.Pin.OUT)
|
||||||
led_onboard.value(False)
|
led_onboard.value(False)
|
||||||
|
|
||||||
def __init__(self, ssid, pw):
|
def __init__(self, ssid, pw):
|
||||||
|
# print("Hello from wlan class")
|
||||||
self.ssid = ssid
|
self.ssid = ssid
|
||||||
self.pw = pw
|
self.pw = pw
|
||||||
|
self.wlan = network.WLAN(network.STA_IF)
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
if not self.isConnected():
|
if not self.is_connected():
|
||||||
print('No WLAN connected. Connecting ...')
|
print('No WLAN connected. Connecting ...' + self.ssid + ' ' + self.pw)
|
||||||
self.wlan.active(True)
|
self.wlan.active(True)
|
||||||
self.wlan.connect(self.ssid, self.pw)
|
self.wlan.connect(self.ssid, self.pw)
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
if self.wlan.status() < 0 or self.wlan.status() >= 3:
|
if self.wlan.status() < 0 or self.wlan.status() >= 3:
|
||||||
break
|
break
|
||||||
led_value = self.led_onboard.value() == 1
|
time.sleep(1)
|
||||||
self.led_onboard.value(led_value)
|
# led_value = self.led_onboard.value() == 1
|
||||||
|
# self.led_onboard.value(led_value)
|
||||||
|
if self.wlan.isconnected():
|
||||||
|
net_config = self.wlan.ifconfig()
|
||||||
|
print("NetConfig:")
|
||||||
|
print(net_config)
|
||||||
|
|
||||||
def isConnected(self):
|
def is_connected(self):
|
||||||
return self.wlan.isconnected()
|
return self.wlan.isconnected()
|
||||||
Loading…
Reference in New Issue