2

Not sure if this is possible but I need to write data from a sensor into a text file using Javascript. This data will then be displayed in an app hence why I need it from the sensor.

I'm using a BBC micro:bit as my microcontroller and this only supports Javascript and micro python, ideally I would use micro python however this does not support BLE yet and I also need this. Any help is appreciated.

rosie_hyde
  • 133
  • 1
  • 4
  • 12

2 Answers2

0

This is long-- there's a summary at the end :)

If you are using MicroPython, then you can use micro:bit's 30k file system -

with open("filename.txt", "w") as file_object:
    file_object.write("[your data here]")

To manage files on your micro:bit, you can use MicroFS or "ufs" for short. To install: $ pip install microfs. Your four commands are:

  1. ufs ls to see all files on your micro

  2. ufs rm filename.txt to delete a file on your micro

  3. ufs put path/to/your/file.txt optional_target_filename.txt to copy a file onto your micro from your computer

  4. ufs get filename.txt optional/path/to/target/file.txt to copy a file from your micro to your computer.

    For more information, type ufs --help.

NOTES: Flashing you micro:bit will delete all your information; however, turning it off will not. Also, micro:bit's file system is flat, meaning it has no directories; everything is stored at the top level.

As per your request for BLE, I can't really help in that area, but I would like to point out the radio MicroPython module, used with import radio followed by a radio.on()
(for conserving battery; there's also a radio.off())

  • send data with radio.send("[your data here]")
  • recieve data with radio.recieve() which takes from the top of a message queue.

The radio module sends radio waves at a channel that's between 0 and 100, set in a default. I'm not sure exactly how to change this, but there should be documentation somewhere :) I'm sorry that this isn't a great solution, but, hopefully, it'll point you in the right direction.

EDIT: To change the radio channel, set radio.channel to your desired channel (default=7). It can 0-100 inclusive, which in reality is 2400MHz-2500MHz. For more info, go here.

SUMMARY

main.py on the micro

import microbit
import radio

with open("datalog.txt") as logfile:
    logfile.write(microbit.temperature()) #or whatever data you had in mind

radio.channel = 47 #2447MHz, feel free to use something else
radio.send(microbit.temperature()) #or whatever

app pseudocode

radio.listenOn(2447MHz) #or whatever

@event.recieveRadioMessage()
void function(evt) {
    #tell user the data
    screen.display(evt.msg)
}
Community
  • 1
  • 1
Ryan
  • 123
  • 1
  • 12
0

using the blocks editor which converts to java you can do the following:

  1. in the blocks menu choose the advanced block at the bottom
  2. scroll to the bottom and click add package
  3. in the search box enter file
  4. this finds a package called files beta, select it

You now have the relevant blocks to write data to the microbit. As mentioned above you will need the microfs utility to retrieve the data from the microbit using the usb cable. WARNING do not flash a script (copy/download a .hex file) to your microbit without retrieving the data it will get wiped

phil
  • 561
  • 3
  • 10