microbit radio comms
I am now working on a project that uses two microbits that are using radio communication. I have some simple code, but not ready to post it.
The big problem that I am having is, I think, is a timing solution between the radio.send and the radio.receive(). When I get this settled then I will post.
Ray
Comments
This particular microbit will be outside gathering info upon request. The light and sound are values, I will try to add some info too the values, as it pertains to the location.
I am still working on the ther program.
Ray
Below is untested code of a general concept program. I still have not worked out the timing issue between the two programs. This a manual edition of getting some values from the other microbit, just to see what is really occurring. the next step would be to automate the logging procedure of that data while still show a scroll of the local temperature.
The hard part will be to add the gately RTC code, so I could have date and time added to the log file.
Ray
The problem that I am seeing is, in m_b_master, when I request a value (temp,sound,light), sometimes I get a value and most of the time I get nothing. I think it is a timing problem, but not sure how to resolve this.
Now I am thinking that on the module that is sending the requested value, maybe I should set that up as a separate task. So, basically I would have two tasks running, one task that handles the requests and the other task would handle the scrolling. I am not sure if the microbit can do this correctly. I also want to add some of the other sensor data to the request list.
Ray
I could be mistaken, but I think that in the "Get the temp" code, the value of inbuff will always be empty... Tabbing/spacing is crucial in micropython. That section of code needs an additional TAB or spacing, aligning it to the "if inbuff" above it.
Just my thought on this...
dgately
Sorry, I was doing some multiple copy and paste to get it into the post, and something got corrupted.
The one thing that I am really starting to dislike about Python is that indentation stuff. On a small program it is fairly easy to catch and fix, but on a larger more complex program, you will be pulling your hair out. Maybe a Raspberry Perl is something to think about.
Ray
What editor are you using? I've never had a problem with indentation in any of the editors I've used, but I wouldn't want to try writing Python (or anything else, for that matter) in e.g. Notepad.
I was using Geany then I switched over to Thonny, I also tried MU for micropython. Because I work between Win 10 and Linux, I have ti find something that works in both systems.
Ray
I have a Raspberry 5 and I have connected a micro:bit. I am using Thonny with the latest interpreter update for micro:bit. I am having a couple of issues, maybe if somebody has an RPi 5, if you could run the program below and see what kind of issues it will have, if any.
First issue, the quit function, not sure why it is taking such a long time for the command to respond.
Second issue, I am using '@run_every(s=5)', this seems to be bogging down the program. Not sure what the next problem would be, if I expanded the program.
This program, when run on my Win 10 machine, it does not have any of these issues. I get the feeling that Thonny runs just a little bit different on the different machines. This is making Thonny all most impossible to use, if you will be switching between different machines.
Ray
Don't use @run_every(s=5) in this particular code... I ran the code without '@run_every(s=5)' on an RPi5, in Thonny with a Micro:Bit and it responded fairly well. It did on several attempts require some time for a 'quit' to complete. I also got some garbage characters after the code printed the temperature, but it would send the appropriate '>>' and I was able to continue sending 'temp1' and a final 'quit' to the Micro:Bit.
Why use @run_every(s=5) ? You already have a 'while True:' loop. @run_every(s=5) will just restart the whole code every 5 seconds. And, that may interrupt the running loop. Short loops in micropython (I guess with any language, possibly) also have an issue with being interrupted by Thonny or Control-C or Control-F, as the loop (& constant run_every 5 second restarts) may be just giving too short of a period to allow serial input from an app or terminal. (In my very simple-minded view)...
There can also be a problem when an existing 'main.py' (with a very short loop) has been saved to the Micro:Bit. It runs automatically on activating or restart and may become un-interruptable (see: https://forums.raspberrypi.com/viewtopic.php?t=321332 )...
BTW: What makes your while loop so small??? Answer: Your sleep(100) is actually not inside the while loop & does not execute, ever!
dgately
Thanks, ggately.
"Why use @run_every(s=5)". I wanted to have the temp shown via the display.scroll() constantly. In the 'while True:' loop, it only displays when the uart.read() is satisfied. I was trying to create a two task system. This is interesting, when you use Win 10 Thonny, this seems to run just fine. Not sure why the RPi 5 Thonny falters.
Ray
Ah, remove the 'sleep(5000)' in loc_temp(). And, place the sleep(100) inside you while loop so it executes everytime through the loop. The loop needs some time to allow serial input to buffer (my guess). You may even need to make the loop sleep more than 100 milliseconds. Try 100, 200, 300 and see if that helps.
Different processor speeds, different OS... Also, I would expect there are differences in micropython on WIN and RPiOS.
dgately
I am replacing my RPi 5 with a Win 10 NUC to run Thonny and a micro:bit. If Thonny does not run correctly, then I might have to try MU on the NUC box. This project is starting to be more of a PIA than it is worth. Hopefully I will stumble over a solution that I think will be a satisfactory one.
Still not giving up, yet.
Ray