from time import sleep, sleep_ms from machine import Pin from board_manager import * from wlan_manager import WLAN_Manager from mqtt_manager import MQTT_Manager from json import dumps, loads wlan_client = WLAN_Manager() mqtt_client = MQTT_Manager() def reconnect(): print("WiFi Connection") wlan_client.start() for i in range(30): if wlan_client.check(): break print(".", end="") sleep(1) sleep(5) success = wlan_client.check() and mqtt_client.check() if success: mqtt_client.broker.subscribe(TOPIC_SUB) return success def mqtt_callback(topic, msg): global status print('Mensagem! Topic: {};\nData: {}'.format(topic.decode(), msg.decode())) # Get the number 0 to 8 after /control/#
import machine, time, json from wlan_manager import WLAN_Manager # Wireless Connection wlan_client = WLAN_Manager() wlan_client.start() from mqtt_manager import MQTT_Manager # MQTT Connection mqtt_client = MQTT_Manager() mqtt_client.check() # Open connection to broker TOPIC_SUB = mqtt_client.get_topic('control') TOPIC_PUB = mqtt_client.get_topic('status') def mqtt_callback(topic, msg): print('MSG! Topic:{}; Data{}'.format(topic, msg)) mqtt_client.broker.set_callback(mqtt_callback) mqtt_client.broker.subscribe(TOPIC_SUB) # Change this to your sensor from board_manager import D1 from sensor_manager import Sensor_DS18B20 sensor = Sensor_DS18B20(D1) # Pin 5 = D1 DELAY = 5 * 1000 # DELAY in milliseconds while True: sensor.read() msg = sensor.values_dict print(msg) mqtt_client.send(TOPIC_PUB, json.dumps(msg))
from machine import Pin, I2C from time import sleep from json import dumps, loads # Modulos de Ligação á Internet from wlan_manager import WLAN_Manager from mqtt_manager import MQTT_Manager # Modulos de Ligação ao Hardware from board_manager import * from sensor_manager import Sensor_BME280 from sensor_manager import Sensor_BH1750 # Ligação à Internet wlan_client = WLAN_Manager() # Cliente para ligar ao Wireless #wlan_client.setup("ATLANTICO", "oceno12") mqtt_client = MQTT_Manager() # Cliente para ligar ao Servidor de Mensagens # Ligação ao Hardware i2c = I2C(scl=Pin(D1), sda=Pin(D2)) # Protocolo de Comunicação I2C sensor1 = Sensor_BME280(i2c, 118) # Ligação ao sensor 1 sensor2 = Sensor_BH1750(i2c, 35) # Ligação ao sensor 2 # Função para tratar das quedas de ligação à Internet def reconnect(): print("WiFi Connection") wlan_client.start() for i in range(30): if wlan_client.check(): break print(".", end="")
from machine import RTC, Pin, I2C import utime from wlan_manager import WLAN_Manager from board_manager import D1, D2 from ssd1306 import SSD1306_I2C wlan_client = WLAN_Manager() #wlan_client.setup('YOUR SSID', 'your password') #wlan_client.start() #import upip #upip.install("urequests") import urequests web_query_delay = 3600000 retry_delay = 5000 i2c = I2C(scl=Pin(D1), sda=Pin(D2)) oled = SSD1306_I2C(128, 64, i2c, 0x3c) rtc = RTC() oled.fill(0) oled.text('Loading Clock', 8, 40) oled.show() utime.sleep(1) wlan_client.start()