Code runs differently through Thonny vs battery powered? #10185
-
Hi, I have a project with two Raspberrypi Picos and Grove LoRa E5 transmitters on both of them. I want them to be able to communicate with each other wirelessly. The transmitter device (original Pico) has a motion sensor, so when something moves, it sends a signal to the receiver device. The receiver is a Pico W, and it creates a web server with a very simple site basically telling me when the signal was received (when something passed the motion sensor). I managed to achieve this, but the problem is that it only works when run through Thonny. More specifically, the transmitter works as it should when I press run in Thonny, but not when I plug it into a battery. Judging by the Grove E5 LED blinks, it still detects motion and sends a signal, but it is never received on the receiver device. The receiver works the same whether it it run by battery or by pressing run in Thonny. Again, the problem goes away when the transmitter is connected to a computer and I manually press run in Thonny. Motion detected -> Grove E5 blinks on the transmitter -> Grove E5 blinks on the receiver -> The website is updated with the correct time. Edit. If it helps, the power sources I tried are 3xAA, power bank, 5V from wall outlet and usb port from computer. Here is the transmitter code: import time
from machine import UART, Pin
uart = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1), bits=8, parity=None, stop=1)
pir = Pin(16, Pin.IN, Pin.PULL_DOWN)
msg = 'AT+TEST=TXLRPKT "ABCD"\r\n'
uart.write('AT+MODE=TEST\r\n')
time.sleep(1)
while True:
if pir.value() == 1:
uart.write(msg)
time.sleep(10) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
How are you saving the code onto the device? Is it being copied on as main.py? (Please see the welcome post for hints about how to format code snippets -- https://github.com/orgs/micropython/discussions/9111) |
Beta Was this translation helpful? Give feedback.
-
Have you checked that the transmitter is receiving power when running from battery? How is it wired to the Pico? |
Beta Was this translation helpful? Give feedback.
-
Okay, I got it working. Thank you @karfas for the Putty suggestion. import time
from machine import UART, Pin
time.sleep(2) # wait a little bit to make sure everything is ready
uart = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1), bits=8, parity=None, stop=1)
pir = Pin(16, Pin.IN, Pin.PULL_DOWN)
msg = 'AT+TEST=TXLRPKT "ABCD"\r\n' # this is sent when motion is detected
uart.write('AT+MODE=TEST\r\n') # mode setup for the Grove E5
time.sleep(1)
# if pir detects motion, send the msg
while True:
if pir.value() == 1:
uart.write(msg)
time.sleep(10) When plugging into a battery, the program starts to run immediately and sends the first UART AT message to the transmitter. The problem is, that the Grove transmitter is still half asleep, so it doesn't register it. I added a short wait at the beginning, and now it works. I guess the reason it worked in Thonny is that before pressing run, it has been plugged in for several seconds or even minutes already. Next, let's see how much distance I can get between the devices and still be able to send messages :) |
Beta Was this translation helpful? Give feedback.
Okay, I got it working. Thank you @karfas for the Putty suggestion.
When plugging into a battery, the program starts to run immediately and sends the first UART AT message to the transmitter.…