Espruino WiFi & MDBT42Q

Posted on
  • Hi everyone,
    I would like to create a bluetooth notification system,
    by serial connection of an Espruino WiFi (connected to the internet) to an MDBT42Q.

    When I unplug them from the Web IDE console and power them on batteries (charged)
    MDBT42Q gives me no feedback ...

    I think there is a problem with serial communication,
    I have tested them separately and have never given me hardware / software problems

    Could you help me understand where I'm wrong?

    // Espruino WiFi
    // Web IDE > reset(true);
    
    Serial1.setup(9600, {rx: B7, tx: B6});
    LED2.write(0);
    var WIFI_NAME = "NAME";
    var WIFI_OPTIONS = {password : "PASSWORD"};
    var wifi;
    
    function getString() {
      require("http").get("http://www.pur3.co.­uk/hello.txt", function(res) {
        var doc = "";
        res.on('data', function(d) { 
          doc += d;
        }); // data
        res.on('close', function(d) {
          doc = doc.substring(0, 5); // Hello
          Serial1.println(doc);
          LED2.write(1);
        }); // close
      }); // get
    };
    
    function onInit() {
      clearInterval();
      wifi = require("Wifi");
      wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
          if (err) throw err;
          setInterval(getString, 30000);
          getString();
      });
    };
    
    // Web IDE > save();
    // Web IDE > disconnect
    // Connected to battery
    
    // MDBT42Q
    // Web IDE > reset(true);
    
    global.LED2 = D2;
    
    function updateBT(wifi) {
       var data = E.toString(wifi);
       data = data.toLowerCase();  // hello
       if(data == "hello"){
          global.LED2.write(1);
          NRF.setAdvertising({}, {
             name: "TEST",
             manufacturer: 0x590,
             manufacturerData: data,
             showName: true
          });
       }
       else{};
    };
    
    function onInit() {
       var cmd = "";
       Serial1.setup(9600, {rx: D8, tx: D6});
       Serial1.on('data', function (wifi) {
          global.LED2.write(0);
          cmd += wifi;
          var idx = cmd.indexOf("\r");
          while (idx >= 0) {
             var line = cmd.substr(0, idx);
             cmd = cmd.substr(idx + 1);
             clearInterval();
             setInterval(updateBT, 15000, line);
             updateBT(line);
             idx = cmd.indexOf("\r");
          };
      });
    };
    
    // Web IDE > save();
    // Web IDE > disconnect
    // Connected to battery
    
    // Espruino WiFi > LED green (Hello)
    // MDBT42Q > ???
    

    1 Attachment

    • espruino.jpg
  • Hi! Thanks for the in-depth post and pictures - that really helps!

    I think you might be hitting: http://www.espruino.com/Troubleshooting#­espruino-works-when-connected-to-a-compu­ter-but-stops-when-powered-from-somethin­g-else

    You'll need USB.setConsole(1) in onInit on the WiFi, and Bluetooth.setConsole(1) on the MDBT42

    The other one (as far as I can see) is grounding. While there are 2 wires for RX/TX on serial communications the voltages on the serial lines are relative to GND, so you actually need to connect the two GND pins on MDBT42 and Espruino Wifi together as well in order for it to function properly/reliably.

    Otherwise after that I reckon you'd be sorted!

  • Hi Gordon!
    As soon as I have a moment, I try it and I tell you ...

    Thank you so much!

  • ...no surprise that there are serial communication issues...

    From the pic I do only see RX and TX to TX and RX connections and no ground wire between the two boards... For serial communication the pins are floating and TX and RX even interfere with each other...

    Get a GND to GND wire in and it should work like a charm... (I would put a - Signal- GND wire in even when both are powered by the same source - battery or USB from what ever - and have common/some Ground from power Ground (neg rail).

  • Thanks allObjects, i can't wait to try it!

    :-)

  • OK, now it works!
    Gordon, allObjects thank you very much!

    For whoever needed, here is the code useful for testing ...

    // Espruino WiFi
    // Web IDE > reset(true);
    
    Serial1.setup(9600, {rx: B7, tx: B6});
    LED2.write(0);
    var WIFI_NAME = "NAME";
    var WIFI_OPTIONS = {password: "PASSWORD"};
    var wifi;
    
    function getString() {
      LED2.write(0);
      require("http").get("http://www.pur3.co.­uk/hello.txt", function(res) {
        var doc = "";
        res.on('data', function(d) { 
          doc += d;
        }); // data
        res.on('close', function(d) {
          doc = doc.substring(0, 5);
          Serial1.println(doc);
          LED2.write(1);
          setTimeout(function (){LED2.write(0);}, 15000);
        }); // close
      }); // get
    };
    
    function onInit() {
      USB.setConsole(1);
      clearInterval();
      wifi = require("Wifi");
      wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
          if (err) throw err;
          setInterval(getString, 30000);
          getString();
      });
    };
    
    // Web IDE > save();
    // Web IDE > disconnect
    // Connected to battery
    
    // MDBT42Q
    // Web IDE > reset(true);
    
    global.LED2 = D2;
    global.LED2.write(0);
    var count = 0;
    
    function updateBT(wifi) {
      count = count + 1;
      var data = E.toString(wifi + count);
      global.LED2.write(1);
      setTimeout(function (){global.LED2.write(0);}, 6000);
      NRF.setAdvertising({}, {
        name: "TEST",
        manufacturer: 0x590,
        manufacturerData: data,
        showName: true
      });
    };
    
    function onInit() {
      var cmd = "";
      Bluetooth.setConsole(1);
      Serial1.setup(9600, {rx: D8, tx: D6});
      Serial1.on('data', function (wifi) {
        cmd += wifi;
        var idx = cmd.indexOf("\r");
        while (idx >= 0) {
          var line = cmd.substr(0, idx);
          line = line.substring(0, 5);
          line = line.toLowerCase();
          cmd = cmd.substr(idx + 1);
          clearInterval();
          setInterval(updateBT, 15000, line);
          updateBT(line);
          idx = cmd.indexOf("\r");
        };
      });
    };
    
    // Web IDE > save();
    // Web IDE > disconnect
    // Connected to battery
    

    1 Attachment

    • espruino.jpg
  • Perfect - thanks for posting up the finished code as well!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Espruino WiFi & MDBT42Q

Posted by Avatar for Andrea @Andrea

Actions