guild icon
Toit
#Jag Disabled - Attempting to connect to network then do MQTT connection
Thread channel in help
crockedile
crockedile 05/15/2025 05:30 PM
Can someone please try running this code and looking into what's going on? Not sure if there are dfeault network settings I need to bypass or what...The network handling is really throwing me for a loop - I believe the code breaks because of the mqtt handling but thats as far as I got:
crockedile
crockedile 05/15/2025 05:31 PM
error seen here:

[wifi] DEBUG: connecting
[wifi] DEBUG: connected
[wifi] INFO: network address dynamically assigned through dhcp {ip: 10.234.196.5}
[wifi] INFO: dns server address dynamically assigned through dhcp {ip: [128.227.30.254, 8.6.245.30]}
SSID BSSID RSSI Channel Author

uwdevice 78:f1:c6:70:85:44 -66 1 WPA2 PSK

**
Decoding by jag, device has version <2.0.0-alpha.174>
**
EXCEPTION error.
wifi already connected with different credentials
0: WifiServiceProvider.connect.<block> /home/runner/work/envelopes/envelopes/toit/system/extensions/esp32/wifi.toit:97:9
1: WifiServiceProvider.connect /home/runner/work/envelopes/envelopes/toit/system/extensions/esp32/wifi.toit:74:3
2: WifiServiceProvider.connect /home/runner/work/envelopes/envelopes/toit/system/extensions/esp32/wifi.toit:72:12
3: NetworkServiceProviderBase.handle /home/runner/work/envelopes/envelopes/toit/system/extensions/shared/networkbase.toit:27:14
4: WifiServiceProvider.handle /home/runner/work/envelopes/envelopes/toit/system/extensions/esp32/wifi.toit:69:12
5: ServiceManager
.<lambda> <sdk>/system/services.toit:643:15
6: RpcRequest.process.<block> <sdk>/rpc/broker.toit:98:26
7: RpcRequest
.process <sdk>/rpc/broker.toit:95:3
8: RpcRequestQueue.ensure-processing-task.<lambda>.<block>.<block> <sdk>/rpc/broker.toit:214:20
9: RpcRequestQueue.ensure-processing-task.<lambda>.<block> <sdk>/rpc/broker.toit:209:9
10: RpcRequestQueue.ensure-processing-task.<lambda> <sdk>/rpc/broker.toit:204:85
**

[wifi] DEBUG: closing
E (370311) wifi:NAN WiFi stop
[jaguar] ERROR: program f04d6fd8-af2e-e652-a2b2-b7af575d2312 stopped - exit code 1
[wifi] DEBUG: connecting
[wifi] DEBUG: connected
[wifi] INFO: network address dynamically assigned through dhcp {ip: 192.168.137.216}
[wifi] INFO: dns server address dynamically assigned through dhcp {ip: [192.168.137.1]}
floitsch
floitsch 05/16/2025 12:49 PM
The problem is, that you already opened the WiFi with wifi.open.
The mqtt client then tries to open the same WiFi, but this time through the system service.

A few ways to solve this:
- close the network you received with wifi.open and let the the system open the same network again. (basically: connection.close). Note that you can save the WiFi credentials with wifi.open ... --save)
- use the network you received from wifi.open for the MQTT connection.
In the latter case you would set the --net-open lambda of the client:
client := mqtt.Client --net-open=(:: connection) --host=HOST --port=PORT
Note that this isn't completely correct, as net-open can be called multiple times. In theory you would need to track whether this was the first call, and if not, establish a new connection.
Something like:
client := mqtt.Client --host=HOST --port=PORT --net-open=:: if connection.is-closed: connection = wifi.open ... connection
crockedile
crockedile 05/16/2025 08:30 PM
thank! That helped a ton, I am successfuly sending MQTT messages now. Thanks for showing me the -save arg as well. One last question on this:

main:
//connect to uwDevice network:
connection := wifi.open
--ssid=USER-SSID
--password=USER-PASSWORD
--save //saves this connection as the default connection

ap := connection.access-point


print """
$(%-32s "SSID") $(%-18s "BSSID") \
$(%-6s "RSSI") $(%-8s "Channel") \
$(%-8s "Author")\n"""

print """
$(%-32s ap.ssid) $(%-18s ap.bssid-name) \
$(%-6s ap.rssi) $(%-8s ap.channel) \
$(%-8s ap.authmode-name)"""

//set up proper time keeping:
result ::= ntp.synchronize
if result:
print "attempting to synchronize time with NTP server"
print "ntp: $result.adjustment ±$result.accuracy"
esp32.adjust_real_time_clock result.adjustment


connection.close //close connection before it is opened again by MQTT
// Initialize MQTT client and secure certificates

The ntp sync is not working at this point. I dont see the console print ""attempting to synchronize time with NTP server". Its as if result does not equate to anything
(edited)
crockedileOPcrockedile
thank! That helped a ton, I am successfuly sending MQTT messages now. Thanks for showing me the -save arg as well. One last question on this: main: //connect to uwDevice network...(edited)
bitphlipphar
bitphlipphar 05/17/2025 03:14 PM
You may want to pass the network connection to ntp.synchronize like this ntp.synchronize --network=connection.
nas2011
nas2011 05/20/2025 04:59 PM
Is there a way to close the connection that is started with the flashed credentials? My understanding of the sequence is that the device connects on power up with the flashed credentials at that time I would like to close that connection, but how can that be accessed? Is it an implicit variable that can be accessed for example 'default_fonnection.close'
nas2011nas2011
Is there a way to close the connection that is started with the flashed credentials? My understanding of the sequence is that the device connects on power up with the flashed crede...
bitphlipphar
bitphlipphar 05/20/2025 05:00 PM
Are you using jaguar?
bitphlipphar
bitphlipphar 05/20/2025 05:01 PM
The base image doesn't connect to the network on its own, but the jaguar service does - unless asked to not do it while running a specific container.
nas2011
nas2011 05/20/2025 05:32 PM
In explaining step by step I think I realized the issue. The process is flash with credentials for network1. Connect to network1 with jag. Issue run command with jag disabled. First part of code connects to network2. Then start MQTT client. That client fails because the wifi is already in use by the ap connection to network2. As mention e earlier, we can save the credentials to network2, close the connection then start the client. The issue is there isn't a way to revert to network1. Because of network isolation we can't send new code so we have to reflash and start over since network2 credentials are now saved. I see now it is not from the jag connection but from MQTT using the default net.open call. If we used a full client and passed the network2 connection I believe we could fail back to network1 after timeout. Does that sound right?
floitsch
floitsch 05/20/2025 05:35 PM
I think so.
For the mqtt: the library has already support for it. In fact there is a --net-open lambda where you can provide the code of how to open the network.
If you don't want to reflash, you can also have a small container installed, that just looks at a specific pin, and opens (with --save) on network1, when the pin is pulled to ground. That should make development much easier.
nas2011
nas2011 05/20/2025 05:38 PM
Copy that. Thanks for all the help as usual. You guys are great.
🙏1
11 messages in total