|
| 1 | +import time |
| 2 | +import board |
| 3 | +import busio |
| 4 | +import digitalio |
| 5 | +from adafruit_fona.adafruit_fona import FONA |
| 6 | +from adafruit_fona.adafruit_fona_gsm import GSM |
| 7 | +import adafruit_fona.adafruit_fona_socket as cellular_socket |
| 8 | +import adafruit_requests as requests |
| 9 | + |
| 10 | +# Get GPRS details and more from a secrets.py file |
| 11 | +try: |
| 12 | + from secrets import secrets |
| 13 | +except ImportError: |
| 14 | + print("GPRS secrets are kept in secrets.py, please add them there!") |
| 15 | + raise |
| 16 | + |
| 17 | +# Create a serial connection for the FONA connection using 4800 baud. |
| 18 | +# These are the defaults you should use for the FONA Shield. |
| 19 | +# For other boards set RX = GPS module TX, and TX = GPS module RX pins. |
| 20 | +uart = busio.UART(board.TX, board.RX, baudrate=4800) |
| 21 | +rst = digitalio.DigitalInOut(board.D4) |
| 22 | + |
| 23 | +# Initialize FONA module (this may take a few seconds) |
| 24 | +fona = FONA(uart, rst) |
| 25 | + |
| 26 | +# initialize gsm |
| 27 | +gsm = GSM(fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"])) |
| 28 | + |
| 29 | +while not gsm.is_attached: |
| 30 | + print("Attaching to network...") |
| 31 | + time.sleep(0.5) |
| 32 | + |
| 33 | +while not gsm.is_connected: |
| 34 | + print("Connecting to network...") |
| 35 | + gsm.connect() |
| 36 | + time.sleep(5) |
| 37 | + |
| 38 | +# Initialize a requests object with a socket and cellular interface |
| 39 | +requests.set_socket(cellular_socket, fona) |
| 40 | + |
| 41 | +TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" |
| 42 | +JSON_GET_URL = "http://httpbin.org/get" |
| 43 | +JSON_POST_URL = "http://httpbin.org/post" |
| 44 | + |
| 45 | +print("Fetching text from %s" % TEXT_URL) |
| 46 | +response = requests.get(TEXT_URL) |
| 47 | +print("-" * 40) |
| 48 | + |
| 49 | +print("Text Response: ", response.text) |
| 50 | +print("-" * 40) |
| 51 | +response.close() |
| 52 | + |
| 53 | +print("Fetching JSON data from %s" % JSON_GET_URL) |
| 54 | +response = requests.get(JSON_GET_URL) |
| 55 | +print("-" * 40) |
| 56 | + |
| 57 | +print("JSON Response: ", response.json()) |
| 58 | +print("-" * 40) |
| 59 | +response.close() |
| 60 | + |
| 61 | +data = "31F" |
| 62 | +print("POSTing data to {0}: {1}".format(JSON_POST_URL, data)) |
| 63 | +response = requests.post(JSON_POST_URL, data=data) |
| 64 | +print("-" * 40) |
| 65 | + |
| 66 | +json_resp = response.json() |
| 67 | +# Parse out the 'data' key from json_resp dict. |
| 68 | +print("Data received from server:", json_resp["data"]) |
| 69 | +print("-" * 40) |
| 70 | +response.close() |
| 71 | + |
| 72 | +json_data = {"Date": "July 25, 2019"} |
| 73 | +print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data)) |
| 74 | +response = requests.post(JSON_POST_URL, json=json_data) |
| 75 | +print("-" * 40) |
| 76 | + |
| 77 | +json_resp = response.json() |
| 78 | +# Parse out the 'json' key from json_resp dict. |
| 79 | +print("JSON Data received from server:", json_resp["json"]) |
| 80 | +print("-" * 40) |
| 81 | +response.close() |
0 commit comments