-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_client.ino
More file actions
108 lines (90 loc) · 3.3 KB
/
Copy pathweb_client.ino
File metadata and controls
108 lines (90 loc) · 3.3 KB
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
108
#include <Adafruit_ESP8266.h>
#include <SoftwareSerial.h>
#define ARD_RX_ESP_TX 2
#define ARD_TX_ESP_RX 3
#define ESP_RST 4
SoftwareSerial softser(ARD_RX_ESP_TX, ARD_TX_ESP_RX); // Arduino RX = ESP TX, Arduino TX = ESP RX
// Must declare output stream before Adafruit_ESP8266 constructor; can be
// a SoftwareSerial stream, or Serial/Serial1/etc. for UART.
Adafruit_ESP8266 wifi(&softser, &Serial, ESP_RST);
// Must call begin() on the stream(s) before using Adafruit_ESP8266 object.
#define ESP_SSID "SSIDNAME" // Your network name here
#define ESP_PASS "PASSWORD" // Your network password here
#define HOST "wifitest.adafruit.com" // Host to contact
#define PAGE "/testwifi/index.html" // Web page to request
#define PORT 80 // 80 = HTTP default port
#define LED_PIN 13
void setup() {
char buffer[50];
// Flash LED on power-up
pinMode(LED_PIN, OUTPUT);
for(uint8_t i=0; i<3; i++) {
digitalWrite(LED_PIN, HIGH); delay(50);
digitalWrite(LED_PIN, LOW); delay(100);
}
// This might work with other firmware versions (no guarantees)
// by providing a string to ID the tail end of the boot message:
// comment/replace this if you are using something other than v 0.9.2.4!
wifi.setBootMarker(F("Version:0.9.2.4]\r\n\r\nready"));
softser.begin(9600); // Soft serial connection to ESP8266
Serial.begin(57600); while(!Serial); // UART serial debug
Serial.println(F("Adafruit ESP8266 Demo"));
// Test if module is ready
Serial.print(F("Hard reset..."));
if(!wifi.hardReset()) {
Serial.println(F("no response from module."));
for(;;);
}
Serial.println(F("OK."));
Serial.print(F("Soft reset..."));
if(!wifi.softReset()) {
Serial.println(F("no response from module."));
for(;;);
}
Serial.println(F("OK."));
Serial.print(F("Checking firmware version..."));
wifi.println(F("AT+GMR"));
if(wifi.readLine(buffer, sizeof(buffer))) {
Serial.println(buffer);
wifi.find(); // Discard the 'OK' that follows
} else {
Serial.println(F("error"));
}
Serial.print(F("Connecting to WiFi..."));
if(wifi.connectToAP(F(ESP_SSID), F(ESP_PASS))) {
// IP addr check isn't part of library yet, but
// we can manually request and place in a string.
Serial.print(F("OK\nChecking IP addr..."));
wifi.println(F("AT+CIFSR"));
if(wifi.readLine(buffer, sizeof(buffer))) {
Serial.println(buffer);
wifi.find(); // Discard the 'OK' that follows
Serial.print(F("Connecting to host..."));
if(wifi.connectTCP(F(HOST), PORT)) {
Serial.print(F("OK\nRequesting page..."));
if(wifi.requestURL(F(PAGE))) {
Serial.println("OK\nSearching for string...");
// Search for a phrase in the open stream.
// Must be a flash-resident string (F()).
if(wifi.find(F("working"), true)) {
Serial.println(F("found!"));
} else {
Serial.println(F("not found."));
}
} else { // URL request failed
Serial.println(F("error"));
}
wifi.closeTCP();
} else { // TCP connect failed
Serial.println(F("D'oh!"));
}
} else { // IP addr check failed
Serial.println(F("error"));
}
wifi.closeAP();
} else { // WiFi connection failed
Serial.println(F("FAIL"));
}
}
void loop() {
}