0

I have a v2 microbit attached to a Raspberry Pi via USB (/dev/ttyACM0), which is running Node-Red. I want to show a weather icon on the Microbit, based on the 'weather' icon returned from the openweathermap node-red module. I can extract the icon (e.g. "03d') using the node-red flow and a 'serial' module, but am finding it impossible to get it to display on the microbit, which shows as 'connected' in the node-red flow. I'll eventually use a (probably really ugly) set of 'if' statements to get the icon from the returned code, but that seems a million miles away rn.

My code on the Microbit (which was built using Blocks) is:

serial.set_baud_rate(BaudRate.BAUD_RATE115200)
serial.set_rx_buffer_size(128)

def on_forever():
    basic.show_string(serial.read_string())
    basic.pause(5000)
basic.forever(on_forever)

That is just one version I have tried. The closest I have got is to receive the following:

[Object Object Object]

...on the microbit, which is odd because the debug message for the flow shows that the right payload is being passed to the serial node by the Pi.

I know this is probably obvious and is likely my misunderstanding of 'string' and 'number'. I have successfully written to a different microbit with a different pi, but that was a number, not a string. At the end of my tether. All advice greatly appreciated.

TIA, Warwick

ps. in an additional frustrating twist, I cannot use Mu to 'borrow' others code, as the microbit is a version that Mu 1.02 can't work with, and I've failed to update that (mu) too!

[EDIT to include Node-Red flow]

[
   {
      "id":"6ed3221a.52c58c",
      "type":"inject",
      "z":"1696e88b.74d287",
      "name":".#.#.#.#.#.#.#.#.#.#.#.#.",
      "props":[
         {
            "p":"payload"
         }
      ],
      "repeat":"10",
      "crontab":"",
      "once":false,
      "onceDelay":0.1,
      "topic":"",
      "payload":".#.#.#.#.#.#.#.#.#.#.#.#.",
      "payloadType":"str",
      "x":560,
      "y":120,
      "wires":[
         [
            "af95ece7.418f2"
         ]
      ]
   },
   {
      "id":"af95ece7.418f2",
      "type":"serial out",
      "z":"1696e88b.74d287",
      "name":"",
      "serial":"92c2150.d6fbee8",
      "x":860,
      "y":120,
      "wires":[
         
      ]
   },
   {
      "id":"92c2150.d6fbee8",
      "type":"serial-port",
      "serialport":"/dev/ttyACM0",
      "serialbaud":"115200",
      "databits":"8",
      "parity":"none",
      "stopbits":"1",
      "waitfor":"",
      "dtr":"none",
      "rts":"none",
      "cts":"none",
      "dsr":"none",
      "newline":"200",
      "bin":"false",
      "out":"time",
      "addchar":"",
      "responsetimeout":"10000"
   }
]
WarwickHW
  • 1
  • 1
  • Can you clarify what you are expecting `show_string` to be showing? It can't show a png file. Only a string. – ukBaz Jun 06 '21 at 18:16
  • I'm not looking to show a png. As mentioned I'll add the code to control the 5x5 mtrix afterwards. Right now, I need the microbit to get the 3 character string which represents the weather from the Pi. – WarwickHW Jun 07 '21 at 08:03
  • I suspect the error is on the other end of the connection. I suspect the Pi/node-red is not sending the string value that you think. Are you able to do a simple node-red that only sends a string? Or show some debug information in your question of why you are sure it is correct? – ukBaz Jun 07 '21 at 09:48
  • Hi @ukBaz yep - i sent the string as per the below (which was a great starter for getting this right), however nothing I'm afraid. It's definitely connected in Node-Red. – WarwickHW Jun 08 '21 at 14:27
  • You might need to update the question with some information about the node-red setup and debug you have done. – ukBaz Jun 08 '21 at 16:27
  • @ukBaz I've added that... – WarwickHW Jun 09 '21 at 12:43
  • You might need to give a little more context to that data... Is that what you are sending over the serial connection? There are three dictionaries in a list; Is that why you are getting `[Object Object Object]` on the micro:bit? Should it only a be string (for example: `.#.#.#.#.#.#.#.#.#.#.#.#.$`) that is sent? – ukBaz Jun 09 '21 at 13:04

1 Answers1

1

Rather than sending the icon name, you might want to send an encoding of what you would like to display so the code on the micro:bit could be more generic.

For example: .#.#.#.#.#.#.#.#.#.#.#.#. would display:

enter image description here

This was done with:

enter image description here

When sending "packets" of data over serial I typically put some known delimiter in there. For example:

enter image description here

ukBaz
  • 6,985
  • 2
  • 8
  • 31
  • I will try that now. Seems a much more elegant solution and the work is done on the pi in node-red. Thank you so much for giving this a looksee. I really appreciate any effort someone puts in on my behalf. Your suggestion will, no doubt, help me, as handling strings and numbers seems to be the bit I need most help with. – WarwickHW Jun 07 '21 at 16:01