Page 1 of 1

8266 to send MQTT data

Posted: Thu Aug 04, 2022 12:25 am
by pythonpete
I am very new to python and a bit stumped on how to do something. I am trying to setup a 8266 ESP-12 to send sensor data from an analog moisture sensor to a MQTT server. At this point I have been able to get the 8266 online and can display the sensor reading in the console but I don’t know how to get that data back to the MQTT server. I can send commands from the MQTT server to the 8266 and turn the LED on and off and even get the water sensor to display in the console. Just not sure how to get data back form the 8266 to the MQTT server. I think I am just missing what command I should use to send the data back to the MQTT server but I have no idea what command would be.

How would one go about doing this? So far here is the code I am using from what I figured out from various websites.

import machine
import network
import time
import gc
import machine
from time import sleep
from umqtt.simple import MQTTClient
from machine import Pin
gc.collect()


#Variables to be used in the script
WIFI_SSID = "MySSID"
WIFI_PASSWORD = "MyPassword"

#Analog-to-digital conversion
adc = machine.ADC (0)
acdread = adc.read ()

#MQTT Server info
MQTT_SERVER = "172.16.1.41"
CLIENT_ID = "wattering01"
MQTT_TOPIC = b"TEST"

#MQTT_SERVER = "172.16.1.41"
#CLIENT_ID = "wattering01"
#MQTT_TOPIC = b"TEST"
#WIFI_SSID = "jcllcIOT"
#WIFI_PASSWORD = "peterjordan1"

led = Pin(2, Pin.OUT)

# Connect to Network
def connectWIFI():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
print("")
print("")
print("Connecting to router...")
sleep (1)
wlan.connect(WIFI_SSID,WIFI_PASSWORD)
sleep(2)
print("Connected to WiFi",WIFI_SSID)
sleep (1)
print('network config:',wlan.ifconfig())
sleep (1)
while not wlan.isconnected():
pass
#This will turn on the light on the 8266
print ("Turning on 8266 LED")
led = Pin(2, Pin.OUT)
led.value(0)

#This will give us the sensor data
print ("Watering sensor data is",acdread)
#MQTT callback service function
def sub_cb(topic, msg):
print(topic, msg)
if topic.decode() == MQTT_TOPIC.decode() and msg.decode() == 'ON':
led.value(0)
print("LED is ON")
if topic.decode() == MQTT_TOPIC.decode() and msg.decode() == 'OFF':
led.value(1)
print("LED is OFF")
if topic.decode() == MQTT_TOPIC.decode() and msg.decode() == 'watering':
print(adc.read ())

try:
led.value(1)

connectWIFI()
c = MQTTClient(CLIENT_ID,MQTT_SERVER)
c.set_callback(sub_cb)
c.connect()
c.subscribe(MQTT_TOPIC)

print("MQTT connected and ready to receive message")
while True:
c.wait_msg()
finally:
c.disconnect()

Re: 8266 to send MQTT data

Posted: Thu Aug 04, 2022 8:31 am
by pythoncoder
MQTT uses a publish/subscribe model. As you've discovered you use subscribe to receive data. You use publish to send data to any client that has subscribed to that topic.

Note that you don't really send and receive to the server (broker): you exchange data with other clients. The server acts as a middleman passing messages around.

Re: 8266 to send MQTT data

Posted: Thu Aug 04, 2022 11:37 pm
by pythonpete
Thanks for the info! Am I correct in thinking that I would be better off to have the 8266 publish the data at regular timed intervolves? Have it publish every 30 minutes or something?

I did some searching online and I looks like the command I would I want to use would be the
def publish():

is that correct?

Re: 8266 to send MQTT data

Posted: Fri Aug 05, 2022 10:46 am
by pythoncoder
The publish method is here.