-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.py
107 lines (95 loc) · 2.78 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import serial
import time
#Initialized Serial port.
port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=3.0)
def readlineCR(port):
rv = ""
#while 1:
rv = port.read(5096) #Trying to read 5096 bytes max of data.
#rv += ch
return rv
#Checking connection by using AT.
isConnected=True;
while isConnected: #Looping till module is connected
port.write("AT\r\n")
returnedData=readlineCR(port)
print(returnedData)
if "OK" not in returnedData :
print("Module not connected. Connect it and try Again.")
time.sleep(60)
else :
print("Device connected.")
isConnected=False
#Setting up ESP8266 module in STA mode.
port.write("AT+CWMODE=1\r\n")
returnedData=readlineCR(port)
print(returnedData)
if "OK" not in returnedData :
print("Unable config Client.")
else :
print("Confighured as STA.")
#Connecting to local network AP. Replace your SSID and password
port.write("AT+CWJAP=\"oksbwn-world\",\"omisoksbwn\"\r\n")
returnedData=readlineCR(port)
print(returnedData)
if "OK" not in returnedData :
print("WiFi not Connected.")
else :
print("Connected to WiFi.")
#Configuring ESP8266 module to autoconnect to the connected Wi-Fi network.
port.write("AT+CWAUTOCONN=1\r\n")
returnedData=readlineCR(port)
print(returnedData)
if "OK" not in returnedData :
print("Automatically Connection to the AP Disbaled.")
else :
print("Automatically Connection to the AP enabled.")
#Printing out the IP of the module.
port.write("AT+CIFSR\r\n")
returnedData=readlineCR(port)
print(returnedData)
if "OK" not in returnedData :
print("Failed to get IP")
else :
print("Got IP")
#Starting a TCP connection to 192.168.0.1 with port 80.
port.write("AT+CIPSTART=\"TCP\",\"192.168.0.1\",80\r\n")
returnedData=readlineCR(port)
print(returnedData)
if "OK" not in returnedData :
print("Initialization of TCP Connection Failed")
else :
print("TCP Connection Initialized")
#Checking out the TCP connection status.
port.write("AT+CIPSTATUS\r\n")
returnedData=readlineCR(port)
print(returnedData)
if "OK" not in returnedData :
print("Failed to get TCP status.")
else :
pass
#Preparing to send 56 bytes of data through the connected TCP link.
port.write("AT+CIPSEND=56\r\n")
returnedData=readlineCR(port)
print(returnedData)
if ">" not in returnedData :
print("Unable to send")
else :
print("Sending 18 bytes of Data.")
#Setting up the payload for the GET call.
port.write("GET /ESP8266/index.php?NAME=myName /HTTP/1.0\r\n")
port.write("\r\n\r\n\r\n")
returnedData=readlineCR(port)
print(returnedData)
if "OK" not in returnedData :
print("WiFi not Connected.")
else :
print("Connected to WiFi.")
#Closing the TCP connection
port.write("AT+CIPCLOSE\r\n")
returnedData=readlineCR(port)
print(returnedData)
if "OK" not in returnedData :
print("Failed to close TCP Connection")
else :
print("TCP Connection closed sucessfully.")