Bluetooth "when connected" command?

Posted on
  • Hi,
    I'm planning to make a datalogger with my espruino with a dht11 sensor.
    I want to make it so that If I connect my mac to my espruino via bluetooth, which will be on my windowsill, the espruino will send me all the logged data through a text file.
    Is this possible?
    Is there a command/keyword that triggers when bluetooth is connected?
    Also a command for sending files by bluetooth?
    I checked the reference page but I can't find any.
    Thanks!
    by the way here's my code so far:

    //Offline weather data logger for the Espruino Javascript microcontroller
    //DHT11 sensor connected to pin c11
    //Made by Eric Min - NO STEALING SOURCE CODE! IF YOU DO SO PLEASE CREDIT ME!!
    
    var dht = require("DHT11").connect(c11); // requires the DHT11 module for temperature/humidity logging
    var files = require("fs").readdirSync(); // requires the Filesystem module for reading, writing, and storing files
    var f = E.openFile("log.txt","w");      // sets variable for file IO library
    var x = 1;
    
    function readTemp() {
        dht.read(function (a) {
            f.write(a.temp.toString() + a.rh.toString());
    
        });
    }
    
    function logTemperature() {
            dht.read(function (a) {
              f.write(a.temp.toString()+a.rh.toString(­));
            });
    }
    
    function logAtIntervals() {
      setInterval(logTemperature, 1800000);
      x++;
    }
    
    if (x <= 24) {
      console.log("Logging done!");
      f.write("Logging done! Entering sleep mode, good night!");
      f.close("log.txt");
    }
    
    
    logAtIntervals();
    

    yeah I'm a noob so please point out any errors in my code please!

  • So this is an Original Espruino board with Bluetooth installed?

    I'm afraid the bluetooth module (HC-05 / HC-06) acts as a simple serial port - so there's no file transfer or even connected/disconnected event.

    You'd have to add a bit of software on the Mac to receive the file. It shouldn't be too bad for example if you have it send the string "\x10sendFile();\n" then it will silently call the sendFile function, which could send the contents.

    You can then make a sendfile function on Espruino -something like:

    function sendFile() {
    var f = E.openFile('log.txt','r');
    var d = f.read(100);
    while (d!==undefined) {
      Serial1.write(d);
      d = f.read(100);
    }
    f.close();
    Serial1.println("<<EOF>>");
    }
    

    Then your code only has to write everything it receives to a file until it gets the characters <<EOF>>

  • Thanks!
    Wait then what is the bluetooth module for?
    can I connect to my board by bluetooth and open up espruino(or screen/picocom/minicom) and program it wirelessly?

  • can I connect to my board by bluetooth and open up espruino(or screen/picocom/minicom) and program it wirelessly?

    Yes, that's the idea.

    You can move the Espruino console away from Serial1 and use it directly to read and write individual bytes - but keeping the console on there is easier, and if there were some kind of error it'd get reported back to you over Bluetooth as well.

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

Bluetooth "when connected" command?

Posted by Avatar for BootySnorkeler @BootySnorkeler

Actions