pimoroni / pimoroni-pico-rp2350

MicroPython builds for Pimoroni Pico 2 / RP2350 boards.
37 stars 6 forks source link

Integration with SMS? #14

Open heavyimage opened 1 month ago

heavyimage commented 1 month ago

Hello,

Is it possible to use the new wireless firmware + the clipper LTE module to not only get a pico online but also receive / send SMS?

Gadgetoid commented 1 month ago

I'm pretty sure that it is, though I'm not sure if there are any relevant limitations with the SIM cards.

The downside is that it would currently involve raw AT commands and I believe you have to set up an additional command channel so that normal commands can still work while in PPP mode. I haven't explored this yet.

https://www.ktron.in/wp-content/uploads/2023/03/A76XX-Series_AT_Command_Manual_V1.06-4.pdf?srsltid=AfmBOorE2r1TGAW2YQfJeWRJXpDzKe2FOL6v9YmGLx-BOVJNm8SRemg5

heavyimage commented 1 month ago

Thanks! I've been playing with this for a bit and using some resources (documenting here if anyone wants to try!)

...but I can't seem to get the anything but a timeout in response to the AT+CMGS command that starts the creation of a text message. I think the modem is supposed to reply with ">" indicating it's waiting for a message?

Maybe is a limitation of the 'data only' SIM that came with the clipper?

Here's some very rough code / lines I've been testing with but I can't seem to get past that. Again, for posterity:

# switch to text mode?
print("switching to text mode")
#con._send_at_command("AT+CMGF=1", 1, 10)
# read text mode values?
#con._send_at_command(b"AT+CSMP?", 1, 10)
#AT+CSMP=17,167,0,16
#print("Sending message")
#con._send_at_command('AT+CMGS="[REDACTED]"', 1, 3)

recipient = "[REDACTED]"
message = "Hello, World!"
#con._send_at_command(b'ATZ',1 , 10)
con._send_at_command(b'AT+CMGF=1',1)
con._send_at_command(b'AT+CMGS="' + recipient.encode() + b'"',1, 20)
con._send_at_command(message.encode() + b"")
con._send_at_command(bytes([26]))
Gadgetoid commented 1 month ago

I think the problem is that _send_at_command will always time out if nothing is returned, but no return value is given until after the message data has been sent.

It also needs a small delay (it would seem) between CMGS and the actual message body, so we can't send it in one transaction.

Try:

con._uart.write(b"AT+CMGS=\"[REDACTED]\"\r")
con._uart.write(b"Hello World\x1a\r")
Gadgetoid commented 1 month ago

Or, to slap that into a function (which I'll add to the lte module I think):

def send_text_message(recipient, body, timeout=5.0):
    con._uart.write("AT+CMGS=\"")
    con._uart.write(recipient.encode("ascii"))
    con._uart.write(b"\"\r")
    time.sleep(0.5)
    con._uart.write(body.encode("ascii"))
    con._uart.write(b"\x1a\r")
    con._uart.flush()

    status, data = con._read_result(1, timeout=timeout)

    if status == "TIMEOUT":
        raise CellularError(f"cellular module timed out for SMS command")

    if status not in ["OK"]:
        raise CellularError(f"non 'OK' result for SMS command")

(Edit: don't need to multiply the timeout by 1000, oof)

heavyimage commented 1 month ago

Hrm, this still isn't working for me :-/

Gadgetoid commented 1 month ago
import gc
import tlte as lte
import time
import requests
con = lte.LTE("APN")

Then just hack around on the repl until something works (in Thonny, at least, the context of an executed Python script is kept and you can use those loaded functions/setup connections etc in the repl).

heavyimage commented 1 month ago

okay thanks! My setup is pretty similar. I'll keep you posted :-)

And thanks for much for the help on this -- happy I was able to nerd snipe you :-)

Gadgetoid commented 1 month ago

It is, indeed, a limitation of the sims we sell- they're data-only!

heavyimage commented 1 month ago

AHA! Okay. Cool. I think I've got another one somewhere for exactly this kind of emergency :eyes:

Gadgetoid commented 1 month ago

Sounds like a job for an SMS -> MQTT gateway. Something like https://www.instructables.com/Two-way-MQTT-SMS-Bridge-Linkit-One/

Would be an interesting project for the LTE Pi HAT 😆