Skip to content

Commit 31a05e5

Browse files
committed
Initial core lib working.
1 parent dc3f259 commit 31a05e5

File tree

6 files changed

+1126
-0
lines changed

6 files changed

+1126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
#define ESP8266_SW_RX 9
2+
#define ESP8266_SW_TX 8
3+
4+
#include "esp8266_lib.h"
5+
6+
SoftwareSerial swSerial(8,9);
7+
Esp8266 esp8266(&swSerial);
8+
9+
const char mySSID[] = "tzuming";
10+
const char myPSK[] = "5102991912";
11+
12+
const char server[] = "example.com";
13+
const char httpRequest[] = "GET / HTTP/1.1\r\n"
14+
"Host: example.com\r\n"
15+
"Connection: close\r\n\r\n";
16+
const char httpRequest2[] = "GET /hello.html HTTP/1.1\r\n"
17+
"Host: junsun.net\r\n"
18+
"Connection: close\r\n\r\n";
19+
20+
#define TEST(x,y) do { \
21+
Serial.print(F("test ")); \
22+
Serial.print(#x); \
23+
Serial.print(F(" .... ")); \
24+
if (x y) Serial.println(F("pass")); \
25+
else { Serial.println(F("fail")); for(;;); } \
26+
} while (0)
27+
28+
char buf[64];
29+
void setup() {
30+
31+
Serial.begin(115200);
32+
33+
TEST(esp8266.begin(), >= 0);
34+
35+
// raw_test();
36+
37+
general_test();
38+
39+
server_test();
40+
41+
client_test();
42+
43+
Serial.println(F("All done! Entering loop ..."));
44+
}
45+
46+
void loop() {
47+
// Serial.println(F("We are in loop"));
48+
delay(2000);
49+
}
50+
51+
void raw_test() {
52+
esp8266.rawTest("AT+CIPSTART=0,\"TCP\",\"example.com\",80,60", 5000);
53+
Serial.println(strlen(httpRequest));
54+
esp8266.rawTest("AT+CIPSENDEX=0,56", 5000);
55+
esp8266.rawTest(httpRequest, 5000);
56+
}
57+
58+
void general_test() {
59+
serialTrigger(F("Press any key to begin general test."));
60+
61+
TEST(esp8266.getVersion(buf,NULL,NULL), >= 0);
62+
Serial.print(F("AT version is ")); Serial.println(buf);
63+
64+
TEST(esp8266.getVersion(NULL, buf,NULL), >= 0);
65+
Serial.print(F("SDK version is ")); Serial.println(buf);
66+
67+
TEST(esp8266.getVersion(NULL, NULL, buf), >= 0);
68+
Serial.print(F("Compile time is ")); Serial.println(buf);
69+
70+
TEST(esp8266.connectAP(mySSID, myPSK), >0);
71+
72+
TEST(esp8266.getAP(buf), >=0);
73+
Serial.print(F("getAP()=")); Serial.println(buf);
74+
TEST(strcmp(buf, mySSID), == 0);
75+
76+
TEST(esp8266.getLocalMAC(buf), >=0);
77+
Serial.print(F("getLocalMAC()=")); Serial.println(buf);
78+
79+
IPAddress ip;
80+
TEST(esp8266.getLocalIP(ip), >=0);
81+
Serial.print(F("getLocalIP()=")); Serial.println(ip);
82+
83+
Serial.println(F("disconnecting AP ..."));
84+
TEST(esp8266.disconnectAP(), >=0);
85+
86+
delay(5000);
87+
88+
TEST(esp8266.getAP(buf), >=0);
89+
TEST(strcmp(buf, ""), == 0);
90+
}
91+
92+
void server_test()
93+
{
94+
serialTrigger(F("Press any key to begin TCP SERVER test."));
95+
96+
TEST(esp8266.connectAP(mySSID, myPSK), >0);
97+
IPAddress ip;
98+
TEST(esp8266.getLocalIP(ip), >=0);
99+
Serial.print(F("getLocalIP()=")); Serial.println(ip);
100+
101+
TEST(esp8266.tcpServerStart(80), >= 0);
102+
103+
Serial.println(F("Open browser at server ... waiting for client"));
104+
for (;;) {
105+
if (esp8266.tcpConnected()) break;
106+
}
107+
Serial.println(F("client connected!"));
108+
109+
// read data
110+
Serial.println(F("Now dump received data:"));
111+
while(!esp8266.tcpAvailable());
112+
while(esp8266.tcpAvailable())
113+
Serial.write(esp8266.tcpRead());
114+
Serial.println(F("\n===END OF TCP DATA===\n"));
115+
116+
// send something back
117+
TEST(esp8266.tcpWrite("<h1>Hello, there</h1>"), >= 0);
118+
119+
TEST(esp8266.tcpClose(), >=0);
120+
121+
// chrome browser sends two requests
122+
while(!esp8266.tcpConnected());
123+
Serial.println(F("Now dump received data:"));
124+
while(!esp8266.tcpAvailable());
125+
while(esp8266.tcpAvailable())
126+
Serial.write(esp8266.tcpRead());
127+
Serial.println(F("\n===END OF TCP DATA===\n"));
128+
129+
// send something back
130+
TEST(esp8266.tcpWrite("<h1>Hello, there</h1>"), >= 0);
131+
132+
TEST(esp8266.tcpClose(), >=0);
133+
134+
// stop server
135+
TEST(esp8266.tcpServerStop(), >= 0);
136+
137+
/*
138+
Serial.println(F("Restart server and re-stop it ..."));
139+
TEST(esp8266.tcpServerStart(80), >= 0);
140+
TEST(esp8266.tcpServerStop(), >= 0);
141+
*/
142+
}
143+
144+
void client_test()
145+
{
146+
serialTrigger(F("Press any key to begin TCP client test."));
147+
148+
// TEST(esp8266.connectAP(mySSID, myPSK), >0);
149+
150+
TEST(esp8266.tcpConnect(server, 80, 30000), >=0);
151+
152+
while (!esp8266.tcpConnected()) ;
153+
TEST(esp8266.tcpWrite(httpRequest), >=0);
154+
155+
Serial.println(F("Now dump received data:"));
156+
while(!esp8266.tcpAvailable());
157+
while(esp8266.tcpAvailable())
158+
Serial.write(esp8266.tcpRead());
159+
Serial.println(F("\n===END OF TCP DATA===\n"));
160+
161+
// server disconnects
162+
delay(1000);
163+
TEST(esp8266.tcpConnected(), ==false);
164+
165+
// now open a second session
166+
TEST(esp8266.tcpConnect("junsun.net", 80, 30000), >=0);
167+
while (!esp8266.tcpConnected()) ;
168+
TEST(esp8266.tcpWrite(httpRequest2), >=0);
169+
170+
Serial.println(F("Now dump received data:"));
171+
while(!esp8266.tcpAvailable());
172+
while(esp8266.tcpAvailable())
173+
Serial.write(esp8266.tcpRead());
174+
Serial.println(F("\n===END OF TCP DATA===\n"));
175+
176+
// server disconnects
177+
delay(1000);
178+
TEST(esp8266.tcpConnected(), ==false);
179+
}
180+
181+
void serialTrigger(String message)
182+
{
183+
Serial.println();
184+
Serial.println(message);
185+
Serial.println();
186+
while (!Serial.available())
187+
;
188+
while (Serial.available()) {
189+
Serial.read();
190+
delay(2); // deal with slow serial case, 2ms is good to cover 9600
191+
}
192+
}

library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=ESP8266 AT CMD WI-FI Library
2+
version=1.0.0
3+
author=Jun Sun
4+
maintainer=Jun Sun <jsun@junsun.net>
5+
sentence=Driver library for SparkFun's ESP8266 WiFi Shield, Adafruit Huzzah and other ESP8266 breakout boards.
6+
paragraph=Simple API to connect to WiFi, TCP server or TCP client
7+
category=Communication
8+
url=http://github.com/sparkfun/SparkFun_ESP8266_AT_Arduino_Library
9+
architectures=*

src/esp8266_const.h

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/******************************************************************************
2+
******************************************************************************/
3+
4+
#ifndef __esp8266_const_h__
5+
#define __esp8266_const_h__
6+
7+
//////////////////////
8+
// Common Responses //
9+
//////////////////////
10+
const char RESPONSE_OK[] = "OK\r\n";
11+
const char RESPONSE_ERROR[] = "ERROR\r\n";
12+
const char RESPONSE_FAIL[] = "FAIL";
13+
const char RESPONSE_READY[] = "READY!";
14+
const char RESPONSE_IPD[] = "+IPD,";
15+
const char RESPONSE_CONNECT_0[] = "0,CONNECT\r\n";
16+
const char RESPONSE_CONNECT[]=",CONNECT\r\n";
17+
const char RESPONSE_CLOSED_0[] = "0,CLOSED\r\n";
18+
19+
///////////////////////
20+
// Basic AT Commands //
21+
///////////////////////
22+
const char ESP8266_TEST[] = ""; // Test AT startup
23+
//const char ESP8266_RESET[] = "+RST"; // Restart module
24+
const char ESP8266_VERSION[] = "+GMR"; // View version info
25+
//!const char ESP8266_SLEEP[] = "+GSLP"; // Enter deep-sleep mode
26+
const char ESP8266_ECHO_ENABLE[] = "E1"; // AT commands echo
27+
const char ESP8266_ECHO_DISABLE[] = "E0"; // AT commands echo
28+
//!const char ESP8266_RESTORE[] = "+RESTORE"; // Factory reset
29+
//const char ESP8266_UART[] = "+UART"; // UART configuration
30+
31+
////////////////////
32+
// WiFi Functions //
33+
////////////////////
34+
//const char ESP8266_WIFI_MODE[] = "+CWMODE"; // WiFi mode (sta/AP/sta+AP)
35+
const char ESP8266_CONNECT_AP[] = "+CWJAP"; // Connect to AP
36+
//!const char ESP8266_LIST_AP[] = "+CWLAP"; // List available AP's
37+
const char ESP8266_DISCONNECT[] = "+CWQAP"; // Disconnect from AP
38+
//!const char ESP8266_AP_CONFIG[] = "+CWSAP"; // Set softAP configuration
39+
//!const char ESP8266_STATION_IP[] = "+CWLIF"; // List station IP's connected to softAP
40+
//!const char ESP8266_DHCP_EN[] = "+CWDHCP"; // Enable/disable DHCP
41+
//!const char ESP8266_AUTO_CONNECT[] = "+CWAUTOCONN"; // Connect to AP automatically
42+
//!const char ESP8266_SET_STA_MAC[] = "+CIPSTAMAC"; // Set MAC address of station
43+
const char ESP8266_GET_STA_MAC[] = "+CIPSTAMAC"; // Get MAC address of station
44+
//!const char ESP8266_SET_AP_MAC[] = "+CIPAPMAC"; // Set MAC address of softAP
45+
//!const char ESP8266_SET_STA_IP[] = "+CIPSTA"; // Set IP address of ESP8266 station
46+
//!const char ESP8266_SET_AP_IP[] = "+CIPAP"; // Set IP address of ESP8266 softAP
47+
48+
/////////////////////
49+
// TCP/IP Commands //
50+
/////////////////////
51+
const char ESP8266_TCP_STATUS[] = "+CIPSTATUS"; // Get connection status
52+
const char ESP8266_TCP_CONNECT[] = "+CIPSTART"; // Establish TCP connection or register UDP port
53+
const char ESP8266_TCP_SEND[] = "+CIPSEND"; // Send Data
54+
const char ESP8266_TCP_CLOSE[] = "+CIPCLOSE"; // Close TCP/UDP connection
55+
const char ESP8266_GET_LOCAL_IP[] = "+CIFSR"; // Get local IP address
56+
const char ESP8266_TCP_MULTIPLE[] = "+CIPMUX"; // Set multiple connections mode
57+
const char ESP8266_SERVER_CONFIG[] = "+CIPSERVER"; // Configure as server
58+
//const char ESP8266_TRANSMISSION_MODE[] = "+CIPMODE"; // Set transmission mode
59+
//!const char ESP8266_SET_SERVER_TIMEOUT[] = "+CIPSTO"; // Set timeout when ESP8266 runs as TCP server
60+
const char ESP8266_PING[] = "+PING"; // Function PING
61+
62+
//////////////////////////
63+
// Custom GPIO Commands //
64+
//////////////////////////
65+
//const char ESP8266_PINMODE[] = "+PINMODE"; // Set GPIO mode (input/output)
66+
//const char ESP8266_PINWRITE[] = "+PINWRITE"; // Write GPIO (high/low)
67+
//const char ESP8266_PINREAD[] = "+PINREAD"; // Read GPIO digital value
68+
69+
#endif /* __esp8266_const_h__ */

src/esp8266_debug.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef __esp8266_debug_h__
2+
#define __esp8266_debug_h__
3+
4+
#define ESP8266_DEBUG
5+
//#define ESP8266_DEBUG_VERBOSE
6+
7+
#ifdef ESP8266_DEBUG
8+
9+
#define ASSERT(x) if (!(x)) { \
10+
Serial.print(F("assert failed at ")); \
11+
Serial.print(__func__); \
12+
Serial.print(F("()@")); \
13+
Serial.println(__LINE__); \
14+
for(;;); }
15+
#define WARN(x) if (!(x)) { \
16+
Serial.print(F("warning at ")); \
17+
Serial.print(__func__); \
18+
Serial.print(F("()@")); \
19+
Serial.println(__LINE__);}
20+
#define VERIFY(x, y) ASSERT(x y)
21+
#define DEBUG(x) do { x; } while (0)
22+
#ifdef ESP8266_DEBUG_VERBOSE
23+
#define DEBUG_VERBOSE(x) DEBUG(x)
24+
#else
25+
#define DEBUG_VERBOSE(x)
26+
#endif
27+
28+
#else
29+
30+
#define ASSERT(x)
31+
#define WARN(x)
32+
#define VERIFY(x, y) x
33+
#define DEBUG(x)
34+
#define DEBUG_VERBOSE(x)
35+
36+
#endif
37+
38+
#endif /* __esp8266_debug_h__ */

0 commit comments

Comments
 (0)