Avatar for jfox

jfox

Member since Jul 2016 • Last active Nov 2016
  • 2 conversations
  • 17 comments

I write code and solder stuff.

Most recent activity

  • started
    Improving Enterprises 5445 Legacy Dr., Plano, TX
    • 8 comments
    • 6,130 views
  • in Projects
    Avatar for jfox

    Sounds great. Email is on the way...

  • in Projects
    Avatar for jfox

    https://docs.google.com/presentation/d/1­66dAUcRsvvksKnAZph-sg_5bjgyt_eXKhB9IQolW­nAA/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.

  • in Projects
    Avatar for jfox

    I am presenting on Javascript and the IoT.
    I will be using Espruino running on esp8266s, Johnny Five, Node JS, and Node Red.

    • 10 comments
    • 6,698 views
  • in JavaScript
    Avatar for jfox

    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.

  • in JavaScript
    Avatar for jfox

    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).
    
  • in JavaScript
    Avatar for jfox

    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.

  • in JavaScript
    Avatar for jfox

    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.

  • in JavaScript
    Avatar for jfox

    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.clie­nt_id))...
                                  ^
    in function "e" called from line 3 col 353
    ..."close")});b.client=a};a?e():a=requir­e("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 }
    
Actions