Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raspberry pi pico w with Ubuntu 22.04 and Macbook Pro and MQTT #3193

Open
jdgerard opened this issue May 13, 2024 · 1 comment
Open

Raspberry pi pico w with Ubuntu 22.04 and Macbook Pro and MQTT #3193

jdgerard opened this issue May 13, 2024 · 1 comment

Comments

@jdgerard
Copy link

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])

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.

@jdgerard
Copy link
Author

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.

Thanks, Joe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant