You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My main.py will connect to my mqtt server and Blynk 2 with Thonny on my Mac and also on a stand alone battery,
but will not connect on my ubuntu 22.04 machine.
main.py
import required modules
import time
import network
from machine import ADC, Pin
import utime
import BlynkLib
from machine import RTC
from umqttsimple import MQTTClient
import ubinascii
rtc = RTC()
import ntptime
ntptime.settime()
time.localtime()
UTC_OFFSET = -7 * 60 * 60
use variables instead of numbers:
soil = ADC(Pin(26)) # Soil moisture PIN reference
volt = ADC(Pin(27, mode=Pin.IN)) # Voltage reference
#Calibraton values
min_moisture=19200
max_moisture=49300
def connect_mqtt():
global client_id, mqtt_server
client = MQTTClient(client_id, mqtt_server)
client.connect()
print('Connected to %s MQTT broker' % (mqtt_server))
return client
def restart_and_reconnect():
print('Failed to connect to MQTT broker. Reconnecting...')
time.sleep()
machine.reset()
try:
client = connect_mqtt()
except OSError as e:
restart_and_reconnect()
Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)
Run the main loop
def tod():
t = rtc.datetime()
if t[6] == 30:
x = 1
print (x)
return '{:02d}:{:02d}:{:02d} Alarm!'.format(t[4], t[5], t[6])
else:
x = 0
print (x)
return '{:02d}:{:02d}:{:02d}'.format(t[4], t[5], t[6])
while True:
# read moisture value and convert to percentage into the calibration range
moisture = (max_moisture-soil.read_u16())*100/(max_moisture-min_moisture)
# print values
print("moisture: " + "%.2f" % moisture +"% (adc: "+str(soil.read_u16())+")")
V = (3.3/(volt.read_u16()) * volt.read_u16())
print ("Voltage: ",(V))
print(tod())
# Send sensor data to Blynk
blynk.virtual_write(0, moisture) # virtual pin 1 for temperature
blynk.virtual_write(1, V) # Supply voltage
time.sleep(1)
# Run Blynk
blynk.run()
utime.sleep(readDelay) # set a delay between readings
This is the error I receive on my Ubumtu machine:
Failed to connect to MQTT broker. Reconnecting...
PROBLEM IN THONNY'S BACK-END: Exception while handling 'Run' (ConnectionError: device reports readiness to read but returned no data (device disconnected or multiple access on port?)).
See Thonny's backend.log for more info.
You may need to press "Stop/Restart" or hard-reset your MicroPython device and try again.
Process ended with exit code 1.
Couldn't find the device automatically.
Check the connection (making sure the device is not in bootloader mode) or choose
"Configure interpreter" in the interpreter menu (bottom-right corner of the window)
to select specific port or another interpreter.
I have other Raspberry pi pico w divices that work with mqtt and Blynk 2 with micropython and the.arduino ide
Any ideas would be helpful.
I am thinking that it is the port but I don't understand why this pi pico w has its issues.
Thanks, Joe
Sorry that my main.py program isn't professional.
I am still learning micropython.
The text was updated successfully, but these errors were encountered:
Never mind, I reverted back to a v1.21.0.uf2 file for Micropython and now it works.
I think that the v1.22.2.uf2 file contains stuff for Bluetooth that causes a conflict with mqtt.
My main.py will connect to my mqtt server and Blynk 2 with Thonny on my Mac and also on a stand alone battery,
but will not connect on my ubuntu 22.04 machine.
main.py
import required modules
import time
import network
from machine import ADC, Pin
import utime
import BlynkLib
from machine import RTC
from umqttsimple import MQTTClient
import ubinascii
rtc = RTC()
import ntptime
ntptime.settime()
time.localtime()
UTC_OFFSET = -7 * 60 * 60
use variables instead of numbers:
soil = ADC(Pin(26)) # Soil moisture PIN reference
volt = ADC(Pin(27, mode=Pin.IN)) # Voltage reference
#Calibraton values
min_moisture=19200
max_moisture=49300
readDelay = 2 # delay between readings
ssid = '#####'
password = '#####'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
mqtt_server = "#####"
while wlan.isconnected() == False:
pass
print('Connection successful')
print(wlan.ifconfig())
client_id = ubinascii.hexlify(machine.unique_id())
TOPIC_PUB_MOISTURE = b'pico/moisture'
TOPIC_PUB_VOLTAGE = b'pico/voltage'
Blynk authentication token
BLYNK_AUTH = "######"
#define BLYNK_TEMPLATE_ID "TMPL2XYmqjMLt"
#define BLYNK_TEMPLATE_NAME "Pico W Moisture"
#define BLYNK_AUTH_TOKEN "P-m947k1LnSh9Gxjeawob1INE2MKBajE"
connect the network
wait = 10
while wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
wait -= 1
print('waiting for connection...')
time.sleep(1)
Handle connection error
if wlan.status() != 3:
raise RuntimeError('wifi connection failed')
else:
print('connected')
ip=wlan.ifconfig()[0]
print('IP: ', ip)
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
print('Connection successful')
print(wlan.ifconfig())
def connect_mqtt():
global client_id, mqtt_server
client = MQTTClient(client_id, mqtt_server)
client.connect()
print('Connected to %s MQTT broker' % (mqtt_server))
return client
def restart_and_reconnect():
print('Failed to connect to MQTT broker. Reconnecting...')
time.sleep()
machine.reset()
try:
client = connect_mqtt()
except OSError as e:
restart_and_reconnect()
Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)
Run the main loop
def tod():
t = rtc.datetime()
if t[6] == 30:
x = 1
print (x)
return '{:02d}:{:02d}:{:02d} Alarm!'.format(t[4], t[5], t[6])
while True:
# read moisture value and convert to percentage into the calibration range
moisture = (max_moisture-soil.read_u16())*100/(max_moisture-min_moisture)
# print values
print("moisture: " + "%.2f" % moisture +"% (adc: "+str(soil.read_u16())+")")
This is the error I receive on my Ubumtu machine:
Failed to connect to MQTT broker. Reconnecting...
PROBLEM IN THONNY'S BACK-END: Exception while handling 'Run' (ConnectionError: device reports readiness to read but returned no data (device disconnected or multiple access on port?)).
See Thonny's backend.log for more info.
You may need to press "Stop/Restart" or hard-reset your MicroPython device and try again.
Process ended with exit code 1.
Couldn't find the device automatically.
Check the connection (making sure the device is not in bootloader mode) or choose
"Configure interpreter" in the interpreter menu (bottom-right corner of the window)
to select specific port or another interpreter.
I have other Raspberry pi pico w divices that work with mqtt and Blynk 2 with micropython and the.arduino ide
Any ideas would be helpful.
I am thinking that it is the port but I don't understand why this pi pico w has its issues.
Thanks, Joe
Sorry that my main.py program isn't professional.
I am still learning micropython.
The text was updated successfully, but these errors were encountered: