-
https://docs.google.com/presentation/d/166dAUcRsvvksKnAZph-sg_5bjgyt_eXKhB9IQolWnAA/edit?usp=sharing
IMHO it needs work. The live presentation went well. I got good audience feedback and participation. I know at least 2 people are using Espurino now.
I'd like to do the presentation a couple of more times to sharpen it. Maybe I can get a video shot next time.
-
-
I found the cause. WHITESPACE! Yep, if I change the number of spaces at the end of the line of code, after the ;
mqtt.connect();
and upload/flash, I will get a different client_id from Mosquitto.
Other changes to whitespace may have the same result. Further experiments will follow.I am using the Web IDE. I have my source code in Atom (because I edit everything in Atom) and then copy/paste the code into the Web IDE. I use the 'Send to Espruino' icon to upload/flash the code to the esp8266. I connect to the esp8266 over wifi to upload/flash. I reset() before every upload/flash and save() after.
In WebIDE under Communications I have Reset before Send set, and Set Current Time set.
Minification Settings:
Minification and Module Minification are both set to 'Esprima (offline)'
All check boxes are checked.Later I may toggle some settings and see if I get different results.
-
I tried two other things to get Mosquitto to use totally unique client_ids.
First I tried setting a different host name for each esp8266 with wifi.setHostname(hostname)
This made no difference.
Then I tried setting the time with wifi.setSNTP(server, tz_offset)
This made no difference.Then I took out the code that set the hostname and time and uploaded to the sonoff again.
Now I get a different client_id for each device. I have no idea why. I LOVE Heisenbugs!1477452360: New connection from 192.168.2.11 on port 1883. 1477452360: New client connected from 192.168.2.11 as b0a38163976b (c1, k60). 1477452386: New connection from 192.168.2.12 on port 1883. 1477452386: New client connected from 192.168.2.12 as 8e0660e041fa (c1, k60). 1477452398: New connection from 192.168.2.16 on port 1883. 1477452398: New client connected from 192.168.2.16 as e14751c9fb06 (c1, k60).
-
I thought that maybe the client_id that Mosquitto provided to anonymous clients may somehow be tied to the hardware, MAC address, or IP address. I tried loading the code on to yet another esp8266 that I have. When it does the mqtt.connect() it gets the same client_id from Mosquitto as the other two.
I have my router configured to assign each esp8266 (I have six of them so far) a different IP address. But each device gets the same IP each time it connects. Apparently Mosquitto does not pay attention to the IP address of the clients.
-
My MQTT broker is Mosquitto running on a Raspberry Pi. I have three different esp8266s running that use the same wifi and MQTT connection code. 2 of the 3 devices get a different client_id assigned from Mosquitto. The third device ( a Sonoff wifi switch) gets the same client_id as one of the other devices, which disconnects the first device with that client_id to be connected.
I understand that this is default and desirable configuration for Mosquitto. I looked at the documentation for the conf file to see if there was a way to have it generate other types of client_ids if the clients are anonymous. I found no way to do this. It looks like my only hope is to be able to set the client_id on the es8266 when the mqtt.connect() happens.
I thought that maybe I would have luck with a different MQTT broker. I tried http://andsel.github.io/moquette/. Which runs and appears to work just fine, but it behaves exactly like Mosquitto.
-
Here is the code that works without error:
var led = NodeMCU.D7, relay = NodeMCU.D6; pinMode(led, "output"); pinMode(relay, "output"); function onInit() { setupWifi(); setLed(led, 0); } function setupWifi() { var ssid = "ssid", options = { password: "password" }, wifi = require("Wifi"); wifi.connect(ssid, options, function() { print(wifi.getIP()); server = "192.168.2.10"; mqtt = require("MQTT").create(server); mqtt.connect(); mqtt.on('connected', function() { mqtt.publish('esp8266/status', 'sonoff01 start'); mqtt.subscribe("esp8266/sonoff01"); }); mqtt.on('publish', function(pub) { mqtt.publish('esp8266/status', 'sonoff01 ' + pub.message); switch (pub.message) { case 'on': setLed(led, 1); setRelay(relay, 1); break; case 'off': setLed(led, 0); setRelay(relay, 0); break; } }); }); } function setLed(led, state) { state = !state; digitalWrite(led, state); } function setRelay(relay, state) { digitalWrite(relay, state); }
Here is what process.memory() shows:
process.memory(); ={ "free": 849, "usage": 851, "total": 1700, "history": 434 }
Change line 31 from:
mqtt.connect();
to:
mqtt.connect({client_id:"booger"});
This is the result:
Running onInit()... { "ip": "192.168.2.16", "netmask": "255.255.255.0", "gw": "192.168.2.1", "mac": "60:01:94:07:ab:1e" } Client connected Uncaught Error: Function "write" not found! at line 1 col 35 ....log("Client connected");a.write(b.mqttConnect(b.client_id))... ^ in function "e" called from line 3 col 353 ..."close")});b.client=a};a?e():a=require("net").connect({host:... ^ in function "connect" called from line 1 col 109 ...onnect({client_id:'booger'}),mqtt.on('connected',function(){... ^ in function called from system
Here is what process.memory() shows:
process.memory(); ={ "free": 1120, "usage": 580, "total": 1700, "history": 436 }
-
Well, I believe that I have proved one of my assumptions wrong.
I decided to try it 'one more time', and it worked.
Last night I had one esp8266 setup to publish to the 'led' topic, and the other to subscribe.
The idea was that I could change the LED color on webmos01 and it would publish the colour as a message. The other es8266, aka webmos02 would subscribe to the led topic and then change it's LED colour appropriately.
The problem was, when I would turn on the second of the two the first would no longer be able to publish or subscribe. It did not matter which one I powered up first.So for my one last test I removed the connection options from my code and powered them both up for a test. And now they work as expected. I've done several power cycles on both and cannot get a failure. I don't know what I did right, but I like the result.
I would still like to know how to set a client_id and/or set connection options.
-
-
I am running v1.87 on an esp8266. I am able to subscribe to an MQTT broker running on my local network. I can publish and subscribe successfully.
I am attempting to publish from one esp8266 and subscribe from another. I believe that both boards use the same default client_id, because when I connect the second one, the first is disconnected.
I would expect to be able to work around this by setting a client_id in the MQTT connection options, as described here: http://www.espruino.com/MQTT
My code looks like this:
var server = "192.168.2.10"; mqtt = require("MQTT").create(server); mqtt.connect({ client_id: 'webmos02' }); mqtt.on('connected', function() { mqtt.publish('led', 'webmos02 start'); mqtt.subscribe("led"); print('subscribed to led'); }); mqtt.on('publish', function(pub) { print("topic: " + pub.topic); print("message: " + pub.message); });
I get these errors:
Uncaught Error: Function "write" not found! at line 1 col 35 ....log("Client connected");a.write(b.mqttConnect(b.client_id))... ^ in function "e" called from line 3 col 353 ..."close")});b.client=a};a?e():a=require("net").connect({host:... ^ in function "connect" called from line 1 col 131 ...nect({client_id:'webmos02'}),mqtt.on('connected',function(){... ^ in function called from system
I have tried putting in other things in the connection options, like _protocolname: "MQTT" and get the same result. I get errors regardless of what I put in as a connection option.
If I leave out the connection option I am able to connect and both publish and subscribe.
- Am I correct in assuming that two different esp8266s without a client_id set will use the same client_id and thus collide?
- How do I set a client_id?
- Is there possible another way to resolve this issue?
- Am I correct in assuming that two different esp8266s without a client_id set will use the same client_id and thus collide?
-
-
-
| |_ ___ ___ _ ||___ ___
| |_ -| . | _| | | | | . |
||| || |_|||_|_||_| http://espruino.com
1v86 Copyright 2016 G.Williams
Espruino is Open Source. Our work is supported
only by sales of official boards and donations:
http://espruino.com/Donate
Flash map 4MB:512/512, manuf 0xe0 chip 0x4016require("Wifi").getDetails()
={
"status": "connected",
"ssid": "someSsid",
"password": "somePassword",
"rssi": -61, "savedSsid": null } -
Right, I've done that. But after trying to upload/flash some code that I enter into Web IDE there is a reboot and the wifi connection never comes back up. After I connect with screen over USB I see that the wifi configuration is missing, just like right after a fresh OS flash.
Same thing will happen if I setup the wifi connection and then cycle the power.
In both cases the wifi configuration is lost. -
I have a HiLetgo NodeMCU LUA WiFi Internet ESP8266 Development Board.
I was able to flash espruino_1v86_esp8266_combined_512.bin onto it.
I have followed instructions found here: http://www.espruino.com/ESP8266_FlashingI am able to connect and setup wifi and establish a connection to my network. I can ping the IP address and connect via telnet, screen, and Web IDE. I can connect via USB or wifi.
When I attempt to save any script or power cycle, the wifi settings are lost and I have to reconnect. I have tried this multiple times.
-
Sounds great. Email is on the way...