from machine import Pin, ADC
import time
import network
import ujson
from umqtt.simple import MQTTClient
# MQTT Server Parameters
MQTT_CLIENT_ID = "testwaterneighborhood"
MQTT_BROKER = "broker.mqttdashboard.com"
MQTT_USER = "TestWater"
MQTT_PASSWORD = "#testwat3rpassWord"
MQTT_TOPIC1 = "neighborhood/1/parameter/solids"
MQTT_TOPIC2 = "neighborhood/1/parameter/turbidity"
MQTT_TOPIC3 = "neighborhood/1/parameter/organic_carbon"
MQTT_TOPIC4 = "neighborhood/1/parameter/ph"
mock = {
"solids": "100mg/L",
"turbidity": "5.2NTU",
"organic_carbon": "10mg/L",
"ph": "7",
}
print("Conectando ao WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print("Conectado!")
try:
print("Conectando ao servidor MQTT... ", end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
client.connect()
print("Conectado!")
while True:
message1 = ujson.dumps({"Solids": mock["solids"]})
client.publish(MQTT_TOPIC1, message1)
message2 = ujson.dumps({"turbidity": mock["turbidity"]})
client.publish(MQTT_TOPIC2, message2)
message3 = ujson.dumps({"organic_carbon": mock["organic_carbon"]})
client.publish(MQTT_TOPIC3, message3)
message4 = ujson.dumps({"ph": mock["ph"]})
client.publish(MQTT_TOPIC4, message4)
time.sleep(3)
except Exception as e:
print(f"Erro: {e}")
finally:
if 'client' in locals():
client.disconnect()