init converting project from micro python to c
This commit is contained in:
parent
4edd5b1cc2
commit
512e041717
32 changed files with 372 additions and 21 deletions
3
old_micro_python_code/CIRCUITPY/boot_out.txt
Executable file
3
old_micro_python_code/CIRCUITPY/boot_out.txt
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
Adafruit CircuitPython 8.2.0 on 2023-07-05; Raspberry Pi Pico with rp2040
|
||||
Board ID:raspberry_pi_pico
|
||||
UID:E6605838837E9433
|
||||
27
old_micro_python_code/CIRCUITPY/code.py
Executable file
27
old_micro_python_code/CIRCUITPY/code.py
Executable file
|
|
@ -0,0 +1,27 @@
|
|||
import time
|
||||
from weather_station.pubsub import PubSub
|
||||
from weather_station.timer import Timer
|
||||
from weather_station.bme280 import BME280
|
||||
from weather_station.airlift import AirLift
|
||||
|
||||
pubsub = PubSub()
|
||||
airlift = AirLift(pubsub)
|
||||
bme280 = BME280(pubsub)
|
||||
timers = [
|
||||
Timer(pubsub, 120),
|
||||
]
|
||||
|
||||
while True:
|
||||
try:
|
||||
airlift.mqtt_client.loop()
|
||||
for timer in timers:
|
||||
t = time.time()
|
||||
timer.tick(t)
|
||||
except (ValueError, RuntimeError, ConnectionError) as e:
|
||||
print("Ran into problem\n", e)
|
||||
airlift.esp.reset()
|
||||
time.sleep(5)
|
||||
airlift.connect()
|
||||
airlift.mqtt_client.reconnect()
|
||||
time.sleep(5)
|
||||
continue
|
||||
0
old_micro_python_code/CIRCUITPY/lib/adafruit_bme280/__init__.py
Executable file
0
old_micro_python_code/CIRCUITPY/lib/adafruit_bme280/__init__.py
Executable file
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_bme280/basic.mpy
Executable file
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_bme280/basic.mpy
Executable file
Binary file not shown.
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_bme280/protocol.mpy
Executable file
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_bme280/protocol.mpy
Executable file
Binary file not shown.
0
old_micro_python_code/CIRCUITPY/lib/adafruit_bus_device/__init__.py
Executable file
0
old_micro_python_code/CIRCUITPY/lib/adafruit_bus_device/__init__.py
Executable file
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_bus_device/i2c_device.mpy
Executable file
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_bus_device/i2c_device.mpy
Executable file
Binary file not shown.
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_bus_device/spi_device.mpy
Executable file
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_bus_device/spi_device.mpy
Executable file
Binary file not shown.
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_esp32spi/PWMOut.mpy
Executable file
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_esp32spi/PWMOut.mpy
Executable file
Binary file not shown.
0
old_micro_python_code/CIRCUITPY/lib/adafruit_esp32spi/__init__.py
Executable file
0
old_micro_python_code/CIRCUITPY/lib/adafruit_esp32spi/__init__.py
Executable file
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_esp32spi/adafruit_esp32spi.mpy
Executable file
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_esp32spi/adafruit_esp32spi.mpy
Executable file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_esp32spi/digitalio.mpy
Executable file
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_esp32spi/digitalio.mpy
Executable file
Binary file not shown.
0
old_micro_python_code/CIRCUITPY/lib/adafruit_minimqtt/__init__.py
Executable file
0
old_micro_python_code/CIRCUITPY/lib/adafruit_minimqtt/__init__.py
Executable file
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_minimqtt/adafruit_minimqtt.mpy
Executable file
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_minimqtt/adafruit_minimqtt.mpy
Executable file
Binary file not shown.
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_minimqtt/matcher.mpy
Executable file
BIN
old_micro_python_code/CIRCUITPY/lib/adafruit_minimqtt/matcher.mpy
Executable file
Binary file not shown.
0
old_micro_python_code/CIRCUITPY/weather_station/__init__.py
Executable file
0
old_micro_python_code/CIRCUITPY/weather_station/__init__.py
Executable file
82
old_micro_python_code/CIRCUITPY/weather_station/airlift.py
Executable file
82
old_micro_python_code/CIRCUITPY/weather_station/airlift.py
Executable file
|
|
@ -0,0 +1,82 @@
|
|||
import os
|
||||
import board
|
||||
import busio
|
||||
import time
|
||||
from digitalio import DigitalInOut
|
||||
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
|
||||
from adafruit_esp32spi import adafruit_esp32spi
|
||||
import adafruit_minimqtt.adafruit_minimqtt as MQTT
|
||||
|
||||
class AirLift():
|
||||
def __init__(self, pubsub):
|
||||
self.ssid = os.getenv("WIFI_SSID")
|
||||
self.wifi_pass = os.getenv("WIFI_PASSWORD")
|
||||
self._setup()
|
||||
pubsub.subscribe("mqtt_pub", self._publish)
|
||||
|
||||
def _publish(self, body):
|
||||
self.mqtt_client.publish(body['topic'], body['msg'])
|
||||
|
||||
def connect(self):
|
||||
while not self.esp.is_connected:
|
||||
try:
|
||||
print(f"Trying to connect to {self.ssid}")
|
||||
self.esp.connect_AP(self.ssid, self.wifi_pass)
|
||||
except (RuntimeError, ConnectionError) as e:
|
||||
print("Ran into problem connecting to AP", e)
|
||||
time.sleep(5)
|
||||
self.esp.reset()
|
||||
time.sleep(5)
|
||||
continue
|
||||
print("Connected to", str(self.esp.ssid, "utf-8"), "\tRSSI:", self.esp.rssi)
|
||||
|
||||
def _setup(self):
|
||||
esp32_cs = DigitalInOut(board.GP21)
|
||||
esp32_ready = DigitalInOut(board.GP22)
|
||||
esp32_reset = DigitalInOut(board.GP17)
|
||||
spi = busio.SPI(clock=board.GP18, MOSI=board.GP19, MISO=board.GP20)
|
||||
self.esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
|
||||
if self.esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
|
||||
print("ESP32 found and in idle mode")
|
||||
print("Firmware vers.", self.esp.firmware_version)
|
||||
print("MAC addr:", [hex(i) for i in self.esp.MAC_address])
|
||||
self.connect()
|
||||
|
||||
# MQTT inspiration from https://docs.circuitpython.org/projects/minimqtt/en/latest/examples.html
|
||||
def connect(mqtt_client, userdata, flags, rc):
|
||||
print("Connected to MQTT Broker!")
|
||||
print("Flags: {0}\n RC: {1}".format(flags, rc))
|
||||
|
||||
def disconnect(mqtt_client, userdata, rc):
|
||||
print("Disconnected from MQTT Broker!")
|
||||
|
||||
def subscribe(mqtt_client, userdata, topic, granted_qos):
|
||||
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
|
||||
|
||||
def unsubscribe(mqtt_client, userdata, topic, pid):
|
||||
print("Unsubscribed from {0} with PID {1}".format(topic, pid))
|
||||
|
||||
def publish(mqtt_client, userdata, topic, pid):
|
||||
print("Published to {0} with PID {1}".format(topic, pid))
|
||||
|
||||
def message(client, topic, message):
|
||||
print("New message on topic {0}: {1}".format(topic, message))
|
||||
|
||||
socket.set_interface(self.esp)
|
||||
MQTT.set_socket(socket, self.esp)
|
||||
self.mqtt_client = MQTT.MQTT(
|
||||
broker=os.getenv("MQTT_HOST"),
|
||||
username=os.getenv("MQTT_USER"),
|
||||
password=os.getenv("MQTT_PASSWORD"),
|
||||
client_id='balcony_weather_station'
|
||||
)
|
||||
|
||||
self.mqtt_client.on_connect = connect
|
||||
self.mqtt_client.on_disconnect = disconnect
|
||||
self.mqtt_client.on_subscribe = subscribe
|
||||
self.mqtt_client.on_unsubscribe = unsubscribe
|
||||
self.mqtt_client.on_publish = publish
|
||||
self.mqtt_client.on_message = message
|
||||
|
||||
print("Attempting to connect to %s" % self.mqtt_client.broker)
|
||||
self.mqtt_client.connect()
|
||||
73
old_micro_python_code/CIRCUITPY/weather_station/bme280.py
Executable file
73
old_micro_python_code/CIRCUITPY/weather_station/bme280.py
Executable file
|
|
@ -0,0 +1,73 @@
|
|||
import board
|
||||
import busio
|
||||
from adafruit_bme280 import basic as adafruit_bme280
|
||||
|
||||
class BME280():
|
||||
mqtt_state_topic = "homeassistant/sensor/balcony_weather_station/bme_280_001/state"
|
||||
mqtt_config_topic_temperature = "homeassistant/sensor/balcony_weather_station/bme_280_001_temp/config"
|
||||
mqtt_config_topic_humidity = "homeassistant/sensor/balcony_weather_station/bme_280_001_humi/config"
|
||||
mqtt_config_topic_pressure = "homeassistant/sensor/balcony_weather_station/bme_280_001_pres/config"
|
||||
config_msg_temperature = """{
|
||||
"name": "BEM 280 Temperature",
|
||||
"device_class": "temperature",
|
||||
"unit_of_measurement": "°C",
|
||||
"value_template": "{{ value_json.temperature}}",
|
||||
"state_topic": "homeassistant/sensor/balcony_weather_station/bme_280_001/state",
|
||||
"unique_id": "balcony_weather_station_bme_280_001_temperature",
|
||||
"device": {
|
||||
"identifiers": "balcony_weather_station_001",
|
||||
"name": "Balcony Weather Station"
|
||||
}
|
||||
}"""
|
||||
config_msg_pressure = """{
|
||||
"name": "BEM 280 Pressure",
|
||||
"device_class": "pressure",
|
||||
"unit_of_measurement": "hPa",
|
||||
"value_template": "{{ value_json.pressure}}",
|
||||
"state_topic": "homeassistant/sensor/balcony_weather_station/bme_280_001/state",
|
||||
"unique_id": "balcony_weather_station_bme_280_001_pressure",
|
||||
"device": {
|
||||
"identifiers": "balcony_weather_station_001",
|
||||
"name": "Balcony Weather Station"
|
||||
}
|
||||
}"""
|
||||
config_msg_humidity = """{
|
||||
"name": "BEM 280 Humidity",
|
||||
"device_class": "humidity",
|
||||
"unit_of_measurement": "%",
|
||||
"value_template": "{{ value_json.humidity}}",
|
||||
"state_topic": "homeassistant/sensor/balcony_weather_station/bme_280_001/state",
|
||||
"unique_id": "balcony_weather_station_bme_280_001_humidity",
|
||||
"device": {
|
||||
"identifiers": "balcony_weather_station_001",
|
||||
"name": "Balcony Weather Station"
|
||||
}
|
||||
}"""
|
||||
|
||||
def __init__(self, pubsub):
|
||||
self.pubsub = pubsub
|
||||
i2c = busio.I2C(scl=board.GP15, sda=board.GP14)
|
||||
self.bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, 0x76)
|
||||
self.bme280.sea_level_pressure = 1016 # value from https://metar-taf.com/EDDM
|
||||
self.pubsub.publish('mqtt_pub', {
|
||||
'topic': self.mqtt_config_topic_temperature,
|
||||
'msg': self.config_msg_temperature
|
||||
})
|
||||
self.pubsub.publish('mqtt_pub', {
|
||||
'topic': self.mqtt_config_topic_humidity,
|
||||
'msg': self.config_msg_humidity
|
||||
})
|
||||
self.pubsub.publish('mqtt_pub', {
|
||||
'topic': self.mqtt_config_topic_pressure,
|
||||
'msg': self.config_msg_pressure
|
||||
})
|
||||
self.pubsub.subscribe('tick 120', self._send)
|
||||
|
||||
def _send(self, _):
|
||||
msg = f"""{{
|
||||
"temperature": {self.bme280.temperature:.1f},
|
||||
"humidity": {self.bme280.relative_humidity:.1f},
|
||||
"pressure": {self.bme280.pressure:.1f}
|
||||
}}"""
|
||||
self.pubsub.publish('mqtt_pub', {'topic': self.mqtt_state_topic, 'msg': msg})
|
||||
|
||||
16
old_micro_python_code/CIRCUITPY/weather_station/pubsub.py
Executable file
16
old_micro_python_code/CIRCUITPY/weather_station/pubsub.py
Executable file
|
|
@ -0,0 +1,16 @@
|
|||
class PubSub:
|
||||
def __init__(self):
|
||||
self.cbs = {}
|
||||
def _check_msg_type(self, msg_type):
|
||||
if msg_type not in self.cbs:
|
||||
self.cbs[msg_type] = []
|
||||
|
||||
def subscribe(self, msg_type, cb):
|
||||
self._check_msg_type(msg_type)
|
||||
self.cbs[msg_type].append(cb)
|
||||
|
||||
def publish(self, msg_type, body):
|
||||
self._check_msg_type(msg_type)
|
||||
print(f"publishing msg {msg_type}")
|
||||
for cb in self.cbs[msg_type]:
|
||||
cb(body)
|
||||
10
old_micro_python_code/CIRCUITPY/weather_station/timer.py
Executable file
10
old_micro_python_code/CIRCUITPY/weather_station/timer.py
Executable file
|
|
@ -0,0 +1,10 @@
|
|||
class Timer:
|
||||
def __init__(self, pubsub, timer_length):
|
||||
self.timer_length = timer_length
|
||||
self.last_tick = 0
|
||||
self.pubsub = pubsub
|
||||
self.topic = f"tick {self.timer_length}"
|
||||
def tick(self, now):
|
||||
if (now - self.last_tick) > self.timer_length:
|
||||
self.last_tick = now
|
||||
self.pubsub.publish(self.topic, {})
|
||||
4
old_micro_python_code/rsync-exclude.txt
Normal file
4
old_micro_python_code/rsync-exclude.txt
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
.Trashes
|
||||
.fseventsd
|
||||
.metadata_never_index
|
||||
._code.py
|
||||
7
old_micro_python_code/settings.sample.toml
Executable file
7
old_micro_python_code/settings.sample.toml
Executable file
|
|
@ -0,0 +1,7 @@
|
|||
WIFI_SSID = "xxxxxxxxxxxx"
|
||||
WIFI_PASSWORD = "xxxxxxxxxxxx"
|
||||
|
||||
MQTT_HOST = "192.168.1.xx"
|
||||
MQTT_PORT = "1883"
|
||||
MQTT_USER = "homeassistant"
|
||||
MQTT_PASSWORD = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||
3
old_micro_python_code/sync.sh
Executable file
3
old_micro_python_code/sync.sh
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh
|
||||
|
||||
rsync -avc --exclude-from=./rsync-exclude.txt /Volumes/CIRCUITPY/ ./CIRCUITPY/
|
||||
Loading…
Add table
Add a link
Reference in a new issue