# pub.py
import network
import socket
import time
import random
from umqtt.simple import MQTTClient
server="backend.thinger.io"
ClientID = f'mqtt_read'
user = "mijimy"
password = "12345678"
TOPIC = "value"
temp_read=0.00
# Replace with your Wi-Fi credentials
SSID = 'Wokwi-GUEST'
PASSWORD = ''
# Connect to Wi-Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
# Wait for connection
while not wlan.isconnected():
print("Connecting to Wi-Fi...")
time.sleep(1)
print("Connected to Wi-Fi:", wlan.ifconfig())
def connect():
print('Connected to MQTT Broker "%s"' % (server))
client = MQTTClient(ClientID, server, 1883, user, password)
client.connect()
return client
def reconnect():
print('Failed to connect to MQTT broker, Reconnecting...' % (server))
time.sleep(5)
client.reconnect()
try:
client = connect()
except OSError as e:
reconnect()
def msg_received(topicr, msgr):
global temp_read
print("Received:", str(msgr), "Topic:", str(topicr))
import json
# Given byte string
byte_string = msgr
# Step 1: Decode the byte string
json_string = byte_string.decode('utf-8')
# Step 2: Parse the JSON string into a dictionary
data = json.loads(json_string)
print(data)
# Step 3: Extract variables and values
for key, value in data.items():
print("Variable: {}, Value: {}".format(key, value))
#temp_read=float(msgr)
#print (temp_read)
client.set_callback(msg_received)
client.subscribe(TOPIC)
print("Subscribed to", TOPIC)
while 1:
client.ping()
client.wait_msg()
time.sleep(1)
esp:0
esp:1
esp:2
esp:3
esp:4
esp:5
esp:6
esp:7
esp:8
esp:9
esp:10
esp:18
esp:19
esp:GND.1
esp:3V3.1
esp:3V3.2
esp:GND.2
esp:RST
esp:GND.3
esp:GND.4
esp:5V.1
esp:5V.2
esp:GND.5
esp:GND.6
esp:GND.7
esp:GND.8
esp:GND.9
esp:RX
esp:TX
esp:GND.10