I'm just teaching myself MicoPython.
At the very basic level; I have had some successful projects using the ESP8266/MQTT with Arduino.ide.
Disclaimer: I am a Hobbyist, not a coder.
I am using THONNY to program, communicate with the ESP8266
I have had success getting the onboard led to blink. Although "On" turns the LED off and vice versa. I can live with that
I have gotten the ESP8266 to connect to my home WiFi, "Print" router assigned IP.
I would like some MORE assistance with Micropython/MQTT.
I would like some MORE assistance with Micropython/MQTT.
Last edited by marine_hm on Fri Dec 17, 2021 11:02 pm, edited 2 times in total.
Re: I would like some assistance with Micropython/MQTT.
I was finally (with lots of help) able to publish and subscribe to an MQTT Topic using a LOLIN ESP8266.
Now I'm working on a script to fetch temp data from an Adafruit MCP9808 sensor. Once complete, I should be able to merge the temp sensor with the "Publish to MQTT" script.
After that; I will see if I can do something with the "Subscribe to MQTT" script. Like turn on an LED if the temp goes within a specific range or maybe trigger a relay to turn on a fan.
Keeping it super simple FOR NOW!
Now I'm working on a script to fetch temp data from an Adafruit MCP9808 sensor. Once complete, I should be able to merge the temp sensor with the "Publish to MQTT" script.
After that; I will see if I can do something with the "Subscribe to MQTT" script. Like turn on an LED if the temp goes within a specific range or maybe trigger a relay to turn on a fan.
Keeping it super simple FOR NOW!
I would like some MORE assistance with Micropython/MQTT.
Everything is working well. BUT, Id like to learn how to format the decimal places when "publishing" to MQTT. Or should I worry about that on the subscribing/working end? As in my sensor script, (with help) I got it to display the temp with just one decimal space in the REPL 87.7
But, when it publishes the temp to MQTT. The MQTT log shows the temp with 4 decimal spaces ie... 84.7875
But, when it publishes the temp to MQTT. The MQTT log shows the temp with 4 decimal spaces ie... 84.7875
Code: Select all
# **************************************#
# GET TEMP SENSOR DATA, INITIAL PRINT SO WE KNOW IT'S WORKING
i2c.readfrom_mem_into(24, 5, byte_data)
value = byte_data[0] << 8 | byte_data[1]
tempC = (value & 0xFFF) / 16.0 # converts "byte_data" to C
if value & 0x1000:
tempC -= 256.0
tempF = tempC * 9 / 5 + 32 #converts C to F
_send_str = (tempF, 2)
# **************************************#
# TRYING TO MAKE THE PAYLOAD WITH 1 OR ZERO DECIMAL PLACES
payload = str(tempF) #WORKS BUT, CANNOT FORMAT DECIMALS
# payload = _send_str(tempF) # DOES NOT WORK
# payload = (tempF) # DOES NOT WORK
client.publish(topic, payload)
print( "MCP9808 temp is {:.0f}F".format(tempF) )
# **************************************#
# Main loop; Print/Publish the Temperature every 60 seconds
while True:
if time.ticks_ms() - last_update >= UPDATE_TIME_INTERVAL:
# FETCH NEW TEMP EACH LOOP
i2c.readfrom_mem_into(24, 5, byte_data)
value = byte_data[0] << 8 | byte_data[1]
tempC = (value & 0xFFF) / 16.0 # converts "byte_data" to C
if value & 0x1000:
tempC -= 256.0
tempF = tempC * 9 / 5 + 32 #converts C to F
# PUBLISH/PRINT TEMP EACH LOOP
payload = str(tempF)
client.publish(topic, payload)
print( "MCP9808 temp is {:.1f}F".format(tempF) )
last_update = time.ticks_ms()
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: I would like some MORE assistance with Micropython/MQTT.
I'm not sure what you're after here. If you want to publish with 1 decimal place precision you can write
Code: Select all
payload = "{:.1f}".format(tempF)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: I would like some MORE assistance with Micropython/MQTT.
Peter;
Thank you so very much. That was what I was looking for.
Without my Wifi login, here's the final script. I just got in an OLED display that plugs right on top of the ESP8266. Header pins soldered, a little bit of fiddling to get the code just right... Came out pretty nice.
Thank you so very much. That was what I was looking for.
Without my Wifi login, here's the final script. I just got in an OLED display that plugs right on top of the ESP8266. Header pins soldered, a little bit of fiddling to get the code just right... Came out pretty nice.
Code: Select all
#############################################################
# #
# This script publishes the temp over WiFi via MQTT. #
# Today, we are using an Adafruit MCP9808 temp sensor, #
# LOLIN ESP8266 (Wemos D-1 mini) #
# SSD1306 64 X 48 oled I2C display addr: 0x3c #
# #
# SCL D1 #
# SDA D2 #
# VDD 5v #
# #
# By: Nick Sebring #
# 12-22-21 #
# #
#############################################################
import machine
import network
import time
import ssd1306
from ssd1306 import SSD1306_I2C
from machine import I2C, Pin
i2c = I2C(scl=Pin(5), sda=Pin(4))
display = SSD1306_I2C(64, 48, i2c)
from umqtt.simple import MQTTClient
import machine
from time import sleep
i2c = I2C(scl=Pin(5), sda=Pin(4))
byte_data = bytearray(2)
display.text("Micro F", 3, 0) # 1st No. is the left/right, 2nd No. is the top/bottom
display.text("By", 26, 20)
display.text("Mr. Nick", 0, 40)
display.show()
time.sleep(3)
display.fill(0)
display.show()
# **************************************#
# Connect to Network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.isconnected()
print("Connecting to router...")
display.text("Connect", 6, 0)
display.text("to", 26, 20)
display.text("WiFi", 20, 40)
display.show()
time.sleep(3)
display.fill(0)
display.show()
wlan.connect('MySSID', 'MYPASSWORD')
sleep(2)
print("Connected to WiFi")
print('network config:', wlan.ifconfig())
# **************************************#
# Configure the ESP8266 wifi as STAtion.
sta = network.WLAN(network.STA_IF)
if not sta.isconnected():
sta.active(True)
# **************************************#
# Global variables and constants:
SERVER = "192.168.1.13"
client = MQTTClient("ESP8266", SERVER)
client.connect()
topic = "Playroom/Temp" # MCP9808 temp
UPDATE_TIME_INTERVAL = 60000 # in ms unit # 60000 = 60 seconds
last_update = time.ticks_ms()
print("Connected to MQTT Server")
# **************************************#
# GET TEMP SENSOR DATA, INITIAL PRINT SO WE KNOW IT'S WORKING
i2c.readfrom_mem_into(24, 5, byte_data)
value = byte_data[0] << 8 | byte_data[1]
tempC = (value & 0xFFF) / 16.0 # converts "byte_data" to C
if value & 0x1000:
tempC -= 256.0
tempF = tempC * 9 / 5 + 32 #converts C to F
payload = "{:.1f}".format(tempF)
client.publish(topic, payload)
print( "Playroom temp is {:.1f}F".format(tempF) ) # {:.#f} = decimal places
# ******** CLEAR THE SCREEN ********
display.fill(0)
display.show()
# ******** Send data to the oled display ********
display.text("Playroom", 0,0)
display.text(payload, 20,33)
display.show()
# **************************************#
# Main loop; Print/Publish the Temperature every 60 seconds
while True:
if time.ticks_ms() - last_update >= UPDATE_TIME_INTERVAL:
# FETCH NEW TEMP EACH LOOP
i2c.readfrom_mem_into(24, 5, byte_data)
value = byte_data[0] << 8 | byte_data[1]
tempC = (value & 0xFFF) / 16.0 # converts "byte_data" to C
if value & 0x1000:
tempC -= 256.0
tempF = tempC * 9 / 5 + 32 #converts C to F
# PUBLISH/PRINT TEMP EACH LOOP
payload = "{:.1f}".format(tempF)
client.publish(topic, payload)
print( "MCP9808 temp is {:.1f}F".format(tempF) )
display.fill(0)
display.show()
display.text("Playroom", 0,0)
display.text(payload, 20,33)
display.show()
last_update = time.ticks_ms()
Re: I would like some MORE assistance with Micropython/MQTT.
I'm pretty sure I don't have to re-write to the entire screen just to update the temp data.
How would I go about keeping the top line "Playroom" and just refresh the temperature?
Ideally, I shouldn't need to update the temp as frequently as I have here. Probably every 15 minutes would be more realistic. But during this design phase, for now, I'm going to leave it. Afterward, I would like to learn other ways of "Time" module. Something more efficient with less power consumption? Maybe even a vibration sensor to tap the screen, print the temp to the screen for 10 seconds so we don't burn out the OLED display?
Moderators: Should I open a new subject for this or leave as is?
How would I go about keeping the top line "Playroom" and just refresh the temperature?
Ideally, I shouldn't need to update the temp as frequently as I have here. Probably every 15 minutes would be more realistic. But during this design phase, for now, I'm going to leave it. Afterward, I would like to learn other ways of "Time" module. Something more efficient with less power consumption? Maybe even a vibration sensor to tap the screen, print the temp to the screen for 10 seconds so we don't burn out the OLED display?
Moderators: Should I open a new subject for this or leave as is?
Re: I would like some MORE assistance with Micropython/MQTT.
Thanks for posting your example ... it kickstarted me on MQTT. I had a closer look at trying to erase one line but couldn't find anything useful.