ESP8266 - reworked status codes
This commit is contained in:
parent
0e37d600ad
commit
75231e00a3
6 changed files with 111 additions and 110 deletions
examples
ESP8266_HTTP_Get
ESP8266_HTTP_Post
ESP8266_MQTT_Publish
ESP8266_MQTT_Subscribe
src/modules
|
@ -1,14 +1,14 @@
|
|||
/*
|
||||
* KiteLib ESP8266 HTTP GET Example
|
||||
*
|
||||
* This example sends HTTP GET request using ESP8266 WiFi module.
|
||||
*
|
||||
* Please note that the response will be saved including header. HTTP header size
|
||||
* can easily exceed Arduino resources and cause the program to behave erratically.
|
||||
*
|
||||
* IMPORTANT: Before uploading this example, make sure that the ESP8266 module is running
|
||||
* AT firmware (can be found in the /extras folder of the library)!
|
||||
*/
|
||||
KiteLib ESP8266 HTTP GET Example
|
||||
|
||||
This example sends HTTP GET request using ESP8266 WiFi module.
|
||||
|
||||
Please note that the response will be saved including header. HTTP header size
|
||||
can easily exceed Arduino resources and cause the program to behave erratically.
|
||||
|
||||
IMPORTANT: Before uploading this example, make sure that the ESP8266 module is running
|
||||
AT firmware (can be found in the /extras folder of the library)!
|
||||
*/
|
||||
|
||||
// include the library
|
||||
#include <KiteLib.h>
|
||||
|
@ -22,17 +22,17 @@ HTTPClient http(&wifi, 80);
|
|||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
|
||||
// initialize ESP8266
|
||||
Serial.print(F("[ESP8266] Initializing ... "));
|
||||
// baudrate: 9600 baud
|
||||
byte state = wifi.begin(9600);
|
||||
if(state == ERR_NONE) {
|
||||
int state = wifi.begin(9600);
|
||||
if (state == ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(state, HEX);
|
||||
while(true);
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
while (true);
|
||||
}
|
||||
|
||||
// join access point
|
||||
|
@ -40,12 +40,12 @@ void setup() {
|
|||
// name: SSID
|
||||
// password: password
|
||||
state = wifi.join("SSID", "password");
|
||||
if(state == ERR_NONE) {
|
||||
if (state == ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(state, HEX);
|
||||
while(true);
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
while (true);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -57,14 +57,15 @@ void loop() {
|
|||
Serial.print(F("[ESP8266] Sending HTTP GET request ... "));
|
||||
// URL: www.httpbin.org/ip
|
||||
int http_code = http.get("www.httpbin.org/ip", response);
|
||||
if(http_code == 200) {
|
||||
Serial.println(F("success!"));
|
||||
if (http_code > 0) {
|
||||
Serial.print(F("HTTP code "));
|
||||
Serial.println(http_code);
|
||||
Serial.print(F("[ESP8266] Response is "));
|
||||
Serial.print(response.length());
|
||||
Serial.println(F(" bytes long."));
|
||||
Serial.println(response);
|
||||
} else {
|
||||
Serial.print(F("failed, HTTP code "));
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(http_code);
|
||||
}
|
||||
|
||||
|
|
|
@ -26,12 +26,12 @@ void setup() {
|
|||
// initialize ESP8266
|
||||
Serial.print(F("[ESP8266] Initializing ... "));
|
||||
// baudrate: 9600 baud
|
||||
byte state = wifi.begin(9600);
|
||||
int state = wifi.begin(9600);
|
||||
if(state == ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(state, HEX);
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
while(true);
|
||||
}
|
||||
|
||||
|
@ -43,8 +43,8 @@ void setup() {
|
|||
if(state == ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(state, HEX);
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
while(true);
|
||||
}
|
||||
|
||||
|
@ -60,7 +60,7 @@ void loop() {
|
|||
// content: str
|
||||
// content type: text/plain
|
||||
int http_code = http.post("www.httpbin.org/status/404", "str", response);
|
||||
if(http_code >= 100) {
|
||||
if(http_code > 0) {
|
||||
Serial.print(F("HTTP code "));
|
||||
Serial.println(http_code);
|
||||
Serial.print(F("[ESP8266] Response is "));
|
||||
|
@ -68,8 +68,8 @@ void loop() {
|
|||
Serial.println(F(" bytes long."));
|
||||
Serial.println(response);
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(http_code, HEX);
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(http_code);
|
||||
}
|
||||
|
||||
// wait for a second before sending new request
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
/*
|
||||
* KiteLib ESP8266 MQTT Publish Example
|
||||
*
|
||||
* This example publishes MQTT messages using ESP8266 WiFi module.
|
||||
*
|
||||
* The messages are published to https://shiftr.io/try. You can use this namespace
|
||||
* for testing purposes, but remember that it is publicly accessible!
|
||||
*
|
||||
* IMPORTANT: Before uploading this example, make sure that the ESP8266 module is running
|
||||
* AT firmware (can be found in the /extras folder of the library)!
|
||||
*/
|
||||
KiteLib ESP8266 MQTT Publish Example
|
||||
|
||||
This example publishes MQTT messages using ESP8266 WiFi module.
|
||||
|
||||
The messages are published to https://shiftr.io/try. You can use this namespace
|
||||
for testing purposes, but remember that it is publicly accessible!
|
||||
|
||||
IMPORTANT: Before uploading this example, make sure that the ESP8266 module is running
|
||||
AT firmware (can be found in the /extras folder of the library)!
|
||||
*/
|
||||
|
||||
// include the library
|
||||
#include <KiteLib.h>
|
||||
|
@ -26,13 +26,13 @@ void setup() {
|
|||
// initialize ESP8266
|
||||
Serial.print(F("[ESP8266] Initializing ... "));
|
||||
// baudrate: 9600 baud
|
||||
byte state = wifi.begin(9600);
|
||||
if(state == ERR_NONE) {
|
||||
int state = wifi.begin(9600);
|
||||
if (state == ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(state, HEX);
|
||||
while(true);
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
while (true);
|
||||
}
|
||||
|
||||
// join access point
|
||||
|
@ -40,12 +40,12 @@ void setup() {
|
|||
// name: SSID
|
||||
// password: password
|
||||
state = wifi.join("SSID", "password");
|
||||
if(state == ERR_NONE) {
|
||||
if (state == ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(state, HEX);
|
||||
while(true);
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
while (true);
|
||||
}
|
||||
|
||||
// connect to MQTT server
|
||||
|
@ -55,12 +55,12 @@ void setup() {
|
|||
// username: try
|
||||
// password: try
|
||||
state = mqtt.connect("broker.shiftr.io", "arduino", "try", "try");
|
||||
if(state == ERR_NONE) {
|
||||
if (state == ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(state, HEX);
|
||||
while(true);
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
while (true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -69,12 +69,12 @@ void loop() {
|
|||
Serial.print(F("[ESP8266] Publishing MQTT message ... "));
|
||||
// topic name: hello
|
||||
// application message: world
|
||||
byte state = mqtt.publish("hello", "world");
|
||||
if(state == ERR_NONE) {
|
||||
int state = mqtt.publish("hello", "world");
|
||||
if (state == ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(state, HEX);
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
}
|
||||
|
||||
// wait for a second before publishing again
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
/*
|
||||
* KiteLib ESP8266 MQTT Subscribe Example
|
||||
*
|
||||
* This example subscribes to MQTT topic using ESP8266 WiFi module.
|
||||
*
|
||||
* The messages are pulled from https://shiftr.io/try. You can use this namespace
|
||||
* for testing purposes, but remember that it is publicly accessible!
|
||||
*
|
||||
* IMPORTANT: Before uploading this example, make sure that the ESP8266 module is running
|
||||
* AT firmware (can be found in the /extras folder of the library)!
|
||||
*/
|
||||
KiteLib ESP8266 MQTT Subscribe Example
|
||||
|
||||
This example subscribes to MQTT topic using ESP8266 WiFi module.
|
||||
|
||||
The messages are pulled from https://shiftr.io/try. You can use this namespace
|
||||
for testing purposes, but remember that it is publicly accessible!
|
||||
|
||||
IMPORTANT: Before uploading this example, make sure that the ESP8266 module is running
|
||||
AT firmware (can be found in the /extras folder of the library)!
|
||||
*/
|
||||
|
||||
// include the library
|
||||
#include <KiteLib.h>
|
||||
|
@ -26,13 +26,13 @@ void setup() {
|
|||
// initialize ESP8266
|
||||
Serial.print(F("[ESP8266] Initializing ... "));
|
||||
// baudrate: 9600 baud
|
||||
byte state = wifi.begin(9600);
|
||||
if(state == ERR_NONE) {
|
||||
int state = wifi.begin(9600);
|
||||
if (state == ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(state, HEX);
|
||||
while(true);
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
while (true);
|
||||
}
|
||||
|
||||
// join access point
|
||||
|
@ -40,12 +40,12 @@ void setup() {
|
|||
// name: SSID
|
||||
// password: password
|
||||
state = wifi.join("SSID", "password");
|
||||
if(state == ERR_NONE) {
|
||||
if (state == ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(state, HEX);
|
||||
while(true);
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
while (true);
|
||||
}
|
||||
|
||||
// connect to MQTT server
|
||||
|
@ -55,12 +55,12 @@ void setup() {
|
|||
// username: try
|
||||
// password: try
|
||||
state = mqtt.connect("broker.shiftr.io", "arduino", "try", "try");
|
||||
if(state == ERR_NONE) {
|
||||
if (state == ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(state, HEX);
|
||||
while(true);
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
while (true);
|
||||
}
|
||||
|
||||
// subscribe to MQTT topic
|
||||
|
@ -69,11 +69,11 @@ void setup() {
|
|||
Serial.print(F("[ESP8266] Subscribing to MQTT topic ... "));
|
||||
// topic name: hello
|
||||
state = wifi.subscribe("hello");
|
||||
if(state == ERR_NONE) {
|
||||
if (state == ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(state, HEX);
|
||||
} else {
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
}
|
||||
|
||||
// unsubscribe from MQTT topic
|
||||
|
@ -81,11 +81,11 @@ void setup() {
|
|||
Serial.print(F("[ESP8266] Unsubscribing from MQTT topic ... "));
|
||||
// topic filter: hello
|
||||
state = wifi.unsubscribe("hello");
|
||||
if(state == ERR_NONE) {
|
||||
if (state == ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(state, HEX);
|
||||
} else {
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -104,13 +104,13 @@ void onPublish(const char* topic, const char* message) {
|
|||
void loop() {
|
||||
// check for new MQTT packets from server each time the loop() runs
|
||||
// this will also send a PING packet, restarting the keep alive timer
|
||||
byte state = wifi.check(onPublish);
|
||||
int state = wifi.check(onPublish);
|
||||
Serial.print("[ESP8266] MQTT check ");
|
||||
if(state == ERR_NONE) {
|
||||
if (state == ERR_NONE) {
|
||||
Serial.println(F("success!"));
|
||||
} else {
|
||||
Serial.print(F("failed, code 0x"));
|
||||
Serial.println(state, HEX);
|
||||
} else {
|
||||
Serial.print(F("failed, code "));
|
||||
Serial.println(state);
|
||||
}
|
||||
|
||||
// the rest of your loop() code goes here
|
||||
|
|
|
@ -4,7 +4,7 @@ ESP8266::ESP8266(Module* module) {
|
|||
_mod = module;
|
||||
}
|
||||
|
||||
uint8_t ESP8266::begin(long speed) {
|
||||
int16_t ESP8266::begin(long speed) {
|
||||
// set module properties
|
||||
_mod->AtLineFeed = "\r\n";
|
||||
_mod->baudrate = speed;
|
||||
|
@ -21,7 +21,7 @@ uint8_t ESP8266::begin(long speed) {
|
|||
return(ERR_NONE);
|
||||
}
|
||||
|
||||
uint8_t ESP8266::reset() {
|
||||
int16_t ESP8266::reset() {
|
||||
// send the reset command
|
||||
if(!_mod->ATsendCommand("AT+RST")) {
|
||||
return(ERR_AT_FAILED);
|
||||
|
@ -43,14 +43,14 @@ uint8_t ESP8266::reset() {
|
|||
return(ERR_AT_FAILED);
|
||||
}
|
||||
|
||||
uint8_t ESP8266::join(const char* ssid, const char* password) {
|
||||
int16_t ESP8266::join(const char* ssid, const char* password) {
|
||||
// set mode to station + soft AP
|
||||
if(!_mod->ATsendCommand("AT+CWMODE_CUR=3")) {
|
||||
return(ERR_AT_FAILED);
|
||||
}
|
||||
|
||||
// reset the module
|
||||
uint8_t state = reset();
|
||||
int16_t state = reset();
|
||||
if(state != ERR_NONE) {
|
||||
return(state);
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ uint8_t ESP8266::join(const char* ssid, const char* password) {
|
|||
return(ERR_NONE);
|
||||
}
|
||||
|
||||
uint8_t ESP8266::openTransportConnection(const char* host, const char* protocol, uint16_t port, uint16_t tcpKeepAlive) {
|
||||
int16_t ESP8266::openTransportConnection(const char* host, const char* protocol, uint16_t port, uint16_t tcpKeepAlive) {
|
||||
char portStr[6];
|
||||
itoa(port, portStr, 10);
|
||||
char tcpKeepAliveStr[6];
|
||||
|
@ -114,7 +114,7 @@ uint8_t ESP8266::openTransportConnection(const char* host, const char* protocol,
|
|||
return(ERR_NONE);
|
||||
}
|
||||
|
||||
uint8_t ESP8266::closeTransportConnection() {
|
||||
int16_t ESP8266::closeTransportConnection() {
|
||||
// send AT command
|
||||
if(!_mod->ATsendCommand("AT+CIPCLOSE")) {
|
||||
return(ERR_AT_FAILED);
|
||||
|
@ -122,7 +122,7 @@ uint8_t ESP8266::closeTransportConnection() {
|
|||
return(ERR_NONE);
|
||||
}
|
||||
|
||||
uint8_t ESP8266::send(const char* data) {
|
||||
int16_t ESP8266::send(const char* data) {
|
||||
// build AT command
|
||||
char lenStr[8];
|
||||
itoa(strlen(data), lenStr, 10);
|
||||
|
@ -146,7 +146,7 @@ uint8_t ESP8266::send(const char* data) {
|
|||
return(ERR_NONE);
|
||||
}
|
||||
|
||||
uint8_t ESP8266::send(uint8_t* data, uint32_t len) {
|
||||
int16_t ESP8266::send(uint8_t* data, uint32_t len) {
|
||||
// build AT command
|
||||
char lenStr[8];
|
||||
itoa(len, lenStr, 10);
|
||||
|
|
|
@ -11,17 +11,17 @@ class ESP8266: public TransportLayer {
|
|||
ESP8266(Module* module);
|
||||
|
||||
// basic methods
|
||||
uint8_t begin(long speed);
|
||||
uint8_t reset();
|
||||
uint8_t join(const char* ssid, const char* password);
|
||||
int16_t begin(long speed);
|
||||
int16_t reset();
|
||||
int16_t join(const char* ssid, const char* password);
|
||||
|
||||
// transport layer methods (implementations of purely virtual methods in TransportMethod class)
|
||||
uint8_t openTransportConnection(const char* host, const char* protocol, uint16_t port, uint16_t tcpKeepAlive = 0);
|
||||
uint8_t closeTransportConnection();
|
||||
uint8_t send(const char* data);
|
||||
uint8_t send(uint8_t* data, uint32_t len);
|
||||
int16_t openTransportConnection(const char* host, const char* protocol, uint16_t port, uint16_t tcpKeepAlive = 0);
|
||||
int16_t closeTransportConnection();
|
||||
int16_t send(const char* data);
|
||||
int16_t send(uint8_t* data, uint32_t len);
|
||||
size_t receive(uint8_t* data, size_t len, uint32_t timeout = 10000);
|
||||
uint16_t getNumBytes(uint32_t timeout = 10000, size_t minBytes = 10);
|
||||
size_t getNumBytes(uint32_t timeout = 10000, size_t minBytes = 10);
|
||||
|
||||
private:
|
||||
Module* _mod;
|
||||
|
|
Loading…
Add table
Reference in a new issue