Skip to content

Commit ebd9b36

Browse files
author
brentru
committed
modifications for requests library
1 parent 4beb004 commit ebd9b36

File tree

3 files changed

+102
-10
lines changed

3 files changed

+102
-10
lines changed

adafruit_fona/adafruit_fona.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@
6868
FONA_HTTP_HEAD = const(0x02)
6969

7070
# TCP/IP
71-
FONA_TCP_MODE = const(0)
72-
FONA_UDP_MODE = const(1)
71+
TCP_MODE = const(0)
72+
UDP_MODE = const(1)
73+
7374
FONA_MAX_SOCKETS = const(6)
75+
7476
# pylint: enable=bad-whitespace
7577

7678
# pylint: disable=too-many-instance-attributes
@@ -343,6 +345,9 @@ def _set_gprs(self, gprs_on=True):
343345
if not self._send_check_reply(b"AT+CIICR", reply=REPLY_OK, timeout=10000):
344346
return False
345347

348+
# Query local IP
349+
if not self.local_ip:
350+
return False
346351
else:
347352
# reset PDP state
348353
if not self._send_check_reply(
@@ -504,18 +509,24 @@ def get_host_by_name(self, hostname):
504509
"""
505510
self._read_line()
506511
if self._debug:
507-
print("*** get_host_by_name")
512+
print("*** get_host_by_name: ", hostname)
508513
if isinstance(hostname, str):
509514
hostname = bytes(hostname, "utf-8")
510515

511-
self._uart.write(b'AT+CDNSGIP="' + hostname + b'"\r\n')
516+
self._uart.write(b'AT+CDNSGIP=')
517+
self._uart.write(b'"')
518+
self._uart.write(hostname)
519+
self._uart.write(b'"')
520+
self._uart.write(b"\r\n")
512521

513522
if not self._expect_reply(REPLY_OK):
523+
print("retin")
514524
return False
515525
# eat the blank line
516526
self._read_line()
517527
# parse the 3rd line
518528
self._read_line()
529+
print(self._buf)
519530

520531
self._parse_reply(b"+CDNSGIP:", idx=2)
521532
return self._buf
@@ -609,9 +620,9 @@ def socket_available(self, sock_num):
609620

610621
return self._buf
611622

612-
def socket_connect(self, sock_num, dest, port, conn_mode=FONA_TCP_MODE):
623+
def socket_connect(self, sock_num, dest, port, conn_mode=TCP_MODE):
613624
"""Connects to a destination IP address or hostname.
614-
By default, we use conn_mode FONA_TCP_MODE but we may also use FONA_UDP_MODE.
625+
By default, we use conn_mode TCP_MODE but we may also use UDP_MODE.
615626
:param int sock_num: Desired socket number
616627
:param str dest: Destination dest address.
617628
:param int port: Destination dest port.
@@ -640,7 +651,7 @@ def socket_connect(self, sock_num, dest, port, conn_mode=FONA_TCP_MODE):
640651
# Start connection
641652
self._uart.write(b"AT+CIPSTART=")
642653
self._uart.write(str(sock_num).encode())
643-
if conn_mode == FONA_TCP_MODE:
654+
if conn_mode == TCP_MODE:
644655
if self._debug:
645656
print('\t--->AT+CIPSTART="TCP","{}",{}'.format(dest, port))
646657
self._uart.write(b',"TCP","')

adafruit_fona/adafruit_fona_socket.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535

3636
_the_interface = None # pylint: disable=invalid-name
3737

38-
3938
def set_interface(iface):
4039
"""Helper to set the global internet interface."""
4140
global _the_interface # pylint: disable=global-statement, invalid-name
@@ -84,8 +83,7 @@ def gethostbyname(hostname):
8483
:param str hostname: Desired hostname.
8584
"""
8685
addr = _the_interface.get_host_by_name(hostname)
87-
return addr
88-
86+
return addr.strip('\"')
8987

9088
# pylint: disable=invalid-name, redefined-builtin
9189
class socket:
@@ -109,20 +107,24 @@ def __init__(
109107
SOCKETS.append(self._socknum)
110108
self.settimeout(self._timeout)
111109

110+
112111
@property
113112
def socknum(self):
114113
"""Returns the socket object's socket number."""
115114
return self._socknum
116115

116+
117117
@property
118118
def connected(self):
119119
"""Returns whether or not we are connected to the socket."""
120120
return _the_interface.socket_status(self.socknum)
121121

122+
122123
def getpeername(self):
123124
"""Return the remote address to which the socket is connected."""
124125
return _the_interface.remote_ip(self.socknum)
125126

127+
126128
def inet_aton(self, ip_string):
127129
"""Convert an IPv4 address from dotted-quad string format.
128130
:param str ip_string: IP Address, as a dotted-quad string.
@@ -133,6 +135,7 @@ def inet_aton(self, ip_string):
133135
self._buffer = bytearray(self._buffer)
134136
return self._buffer
135137

138+
136139
def connect(self, address, conn_mode=None):
137140
"""Connect to a remote socket at address. (The format of address depends
138141
on the address family — see above.)

examples/fona_simpletest_requests.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import time
2+
import board
3+
import busio
4+
import digitalio
5+
from adafruit_fona.adafruit_fona import FONA
6+
import adafruit_fona.adafruit_fona_socket as cellular_socket
7+
import adafruit_requests as requests
8+
9+
SERVER_ADDRESS = ("wifitest.adafruit.com", 80)
10+
11+
# Get GPRS details and more from a secrets.py file
12+
try:
13+
from secrets import secrets
14+
except ImportError:
15+
print("GPRS secrets are kept in secrets.py, please add them there!")
16+
raise
17+
18+
# Create a serial connection for the FONA connection using 4800 baud.
19+
# These are the defaults you should use for the FONA Shield.
20+
# For other boards set RX = GPS module TX, and TX = GPS module RX pins.
21+
uart = busio.UART(board.TX, board.RX, baudrate=4800)
22+
rst = digitalio.DigitalInOut(board.D4)
23+
24+
# Initialize FONA module (this may take a few seconds)
25+
fona = FONA(uart, rst, debug=True)
26+
27+
print("Adafruit FONA WebClient Test")
28+
29+
# Enable GPS
30+
fona.gps = True
31+
32+
# Bring up cellular connection
33+
fona.configure_gprs((secrets["apn"], secrets["apn_username"], secrets["apn_password"]))
34+
35+
# Bring up GPRS
36+
fona.gprs = True
37+
38+
39+
# Initialize a requests object with a socket and cellular interface
40+
requests.set_socket(cellular_socket, fona)
41+
42+
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
43+
JSON_GET_URL = "http://httpbin.org/get"
44+
JSON_POST_URL = "http://httpbin.org/post"
45+
46+
47+
print("Fetching text from %s" % TEXT_URL)
48+
response = requests.get(TEXT_URL)
49+
print("-" * 40)
50+
51+
print("Text Response: ", response.text)
52+
print("-" * 40)
53+
response.close()
54+
55+
"""
56+
print("Connecting to: ", SERVER_ADDRESS[0])
57+
sock.connect(SERVER_ADDRESS)
58+
59+
print("Connected to:", sock.getpeername())
60+
61+
# Make a HTTP Request
62+
sock.send(b"GET /testwifi/index.html HTTP/1.1\n")
63+
sock.send(b"Host: 104.236.193.178")
64+
sock.send(b"Connection: close\n\n")
65+
66+
bytes_avail = 0
67+
while not bytes_avail:
68+
bytes_avail = sock.available()
69+
if bytes_avail > 0:
70+
print("bytes_avail: ", bytes_avail)
71+
data = sock.recv(bytes_avail)
72+
print(data.decode())
73+
break
74+
time.sleep(0.05)
75+
76+
sock.close()
77+
print("Socket connected: ", sock.connected)
78+
"""

0 commit comments

Comments
 (0)