class sms: def __init__(self, rx, tx): self._uart = UART(baudrate=9600, rx=rx, tx=tx) # Perform self check if not self._check(): ... # TODO replace this with a buzz or something self._report() # TODO indicate sim setup complete def _send(self, cmd: str) -> str: self._uart.write(bytes((cmd+"\r\n").encode("ascii"))) sleep_ms(SEND_DELAY_MS) resp = self._uart.read() if resp is None: return "" return resp.decode("ascii") def _check(self) -> bool: if "ok" not in self._send("at"): return False if "error" in self._send("at+ccid"): return False if "error" in self._send("at+creg?"): return False return True # Report will send commands to the sim800l and collect the responses. # Then the responses will be sent via sms to the "owner" def _report(self): rep = "" rep += self._send("at+csq") # signal strength rep += self._send("AT+COPS?") # connected network rep += self._send("AT+CBC") # lipo state self.send_msg(rep) def send_msg(self, msg: str): self._send("AT+CMGF=1") # Text message mode self._send(f"AT+CMGS=\"{OWNER_NUMBER}\"") self._send(msg) self._uart.write(bytes(chr(26))) def recv_msg(self) -> str: self._send("AT+CMGF=1") # Text message mode self._send("AT+CNMI=1,2,0,0,0") # Send text message over uart resp = "" while resp == "" or "+CMT" not in resp: resp = self._uart.read() if resp == None: resp = "" msg = resp.decode("ascii") words = msg.split("\r\n") return words[2]
class UARTSource(Observable): def __init__(self): super().__init__() self.uart = UART(board.TX, board.RX, baudrate=9600) def tick(self): self.emit(self.uart.read(self.uart.in_waiting))
''' https://circuitpython.readthedocs.io/en/2.x/shared-bindings/busio/UART.html connect D3 and D4 to test ''' import board from busio import UART uart = UART(board.D4, board.D3) uart.write('test') uart.read(4) uart.write('test2\n') uart.readline() # reads until a \n character
def sendMeasurement(msg): uart.write(memoryview(bytes([len(msg)]))) uart.write(memoryview(bytes([ord(char) for char in msg]))) # Main loop while True: battValue = getMeasurement(batt, 1.47) currentValue = getMeasurement(current, 1.47) temp2Value = getMeasurement(temp2, 100, -50) # Receive command buf = uart.read(2) if buf is not None: watchdog = 0 data = [i for i in buf] command = data[0] header = data[1] if command == COMMAND_DICT['batt-voltage']: sendMeasurement(str(battValue)) elif command == COMMAND_DICT['current']: sendMeasurement(str(currentValue)) # elif command == COMMAND_DICT['temp1']: # sendMeasurement(str(getTemp(temp1)))
class sms: def __init__(self, rx, tx): self._uart = UART(baudrate=57600, rx=rx, tx=tx) self.pkt_size = 0 def _send(self, cmd: str) -> str: self._uart.write(bytes((cmd + "\r\n").encode("ascii"))) sleep_ms(SEND_DELAY_MS) resp = self._uart.read() if resp is None: return "" return resp.decode("ascii") # Report will send commands to the sim800l and collect the responses. # Then the responses will be sent via sms to the "owner" def report(self): rep = "" rep += self._send("at") rep += self._send("at+ccid") rep += self._send("at+creg?") rep += self._send("at+csq") # signal strength rep += self._send("AT+COPS?") # connected network rep += self._send("AT+CBC") # lipo state self.send_msg(rep) def send_msg(self, msg: str, number=OWNER_NUMBER): self._send("AT+CMGF=1") # Text message mode self._send(f"AT+CMGS=\"{number}\"") self._send(msg) self._uart.write(bytes(chr(26).encode("ascii"))) def send_msg_all(self, msg: str): for num in NUMBERS: self.send_msg(msg, num) sleep_ms(5000) def recv_msg(self, indicator=None) -> str: self._send("AT+CMGF=1") # Text message mode self._send("AT+CNMI=1,2,0,0,0") # Send text message over uart resp = "\r\n\r\n" while "+CMT" not in resp: if indicator is not None: indicator.toggle() resp = self._uart.read() if resp is None: resp = "\r\n\r\n" else: indicator.on() resp = resp.decode("ascii") words = resp.split("\r\n") return words[2] def connect(self): self._send("AT+CFUN=1") self._send("AT+CSTT=\"wap.vodafone.co.uk\",\"wap\",\"wap\"") self._send("AT+CIICR") sleep_ms(200) self._send("AT+CIFSR") self._send("AT+CIPSTART=\"UDP\",35.229.97.111,8080") sleep_ms(2000) #Returns the number of milliseconds that need to be waited #This will have some gaps though, especially when sending a new packet def send_pkt(self, pkt: bytes) -> int: if self.pkt_size == 0: self._uart.write( bytes((f"AT+CIPSEND={MAX_PKT_SIZE}\r\na").encode("ascii"))) return 4 self._uart.write(pkt) self.pkt_size += len(pkt) if self.pkt_size >= MAX_PKT_SIZE: self._uart.write(bytes("\r\n".encode("ascii"))) return 3 return 1 def disconnect(self): self._send("AT+CIPCLOSE") self._send("AT+CIPSHUT")
import board from busio import UART import utime FONA_BAUD = 4800 uart = UART(board.D0, board.D2, baudrate=FONA_BAUD) # for UART2 on esp32 dev board, RX:16, TX:17 uart.init(FONA_BAUD) # clear the buffer for i in range(3): uart.readline() # check replies for i in range(10): message = "AT" uart.write(message + '\r\n') # \r and \n seem necessary print(">>\n" + message) utime.sleep(.1) response = uart.read().decode('ascii') print("<<") print(response.split()) utime.sleep(.1)