diff --git a/examples/ESP8266_HTTP_Get/ESP8266_HTTP_Get.ino b/examples/ESP8266_HTTP_Get/ESP8266_HTTP_Get.ino new file mode 100644 index 00000000..25489ec9 --- /dev/null +++ b/examples/ESP8266_HTTP_Get/ESP8266_HTTP_Get.ino @@ -0,0 +1,68 @@ +/* + * 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 + +// ESP8266 module is in slot A on the shield +ESP8266 wifi = Kite.ModuleA; + +// create HTTP client instance using the wifi module +HTTPClient http(&wifi); + +void setup() { + Serial.begin(9600); + + // initialize ESP8266 with baudrate 9600 + Serial.print(F("[ESP8266] Initializing ... ")); + byte 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); + } + + // join AP named "SSID" using the password "password" + Serial.print(F("[ESP8266] Joining AP ... ")); + state = wifi.join("Tenda", "Student20-X13"); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } + +} + +void loop() { + // send HTTP GET request to www.httpbin.org/ip + // the response will contain origin IP address of the request + String response; + Serial.print(F("[ESP8266] Sending HTTP GET request ... ")); + int http_code = http.get("www.httpbin.org/ip", response); + if(http_code == 200) { + Serial.println(F("success!")); + 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.println(http_code); + } + + // wait for a second before sending new request + delay(1000); +} diff --git a/examples/ESP8266_HTTP_Post/ESP8266_HTTP_Post.ino b/examples/ESP8266_HTTP_Post/ESP8266_HTTP_Post.ino new file mode 100644 index 00000000..3f9f0d0a --- /dev/null +++ b/examples/ESP8266_HTTP_Post/ESP8266_HTTP_Post.ino @@ -0,0 +1,71 @@ +/* + * KiteLib ESP8266 HTTP POST Example + * + * This example sends HTTP POST 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 + +// ESP8266 module is in slot A on the shield +ESP8266 wifi = Kite.ModuleA; + +// create HTTP client instance using the wifi module +HTTPClient http(&wifi); + +void setup() { + Serial.begin(9600); + + // initialize ESP8266 with baudrate 9600 + Serial.print(F("[ESP8266] Initializing ... ")); + byte 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); + } + + // join AP named "SSID" using the password "password" + Serial.print(F("[ESP8266] Joining AP ... ")); + state = wifi.join("Tenda", "Student20-X13"); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } + +} + +void loop() { + // send HTTP POST request to www.httpbin.org/status/200 + // Content: str + // Content-Type: text/plain + String response; + Serial.print(F("[ESP8266] Sending HTTP POST request ... ")); + int http_code = http.post("www.httpbin.org/status/404", "str", response); + if(http_code >= 100) { + 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, code 0x")); + Serial.println(http_code, HEX); + } + + // wait for a second before sending new request + delay(1000); +} + diff --git a/examples/ESP8266_MQTT_Publish/ESP8266_MQTT_Publish.ino b/examples/ESP8266_MQTT_Publish/ESP8266_MQTT_Publish.ino index 9c046790..cf161b39 100644 --- a/examples/ESP8266_MQTT_Publish/ESP8266_MQTT_Publish.ino +++ b/examples/ESP8266_MQTT_Publish/ESP8266_MQTT_Publish.ino @@ -6,7 +6,7 @@ * 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 upolading this example, make sure that the ESP8266 module is running + * 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)! */ @@ -16,6 +16,9 @@ // ESP8266 module is in slot A on the shield ESP8266 wifi = Kite.ModuleA; +// create MQTT client instance using the wifi module +MQTTClient mqtt(&wifi); + void setup() { Serial.begin(9600); @@ -41,9 +44,9 @@ void setup() { while(true); } - // connect to MQTT broker using client ID "arduino", username "try" and password "try" - Serial.print(F("[ESP8266] Connecting to MQTT broker ... ")); - state = wifi.MqttConnect("broker.shiftr.io", "arduino", "try", "try"); + // connect to MQTT server using client ID "arduino", username "try" and password "try" + Serial.print(F("[ESP8266] Connecting to MQTT server ... ")); + state = mqtt.connect("broker.shiftr.io", "arduino", "try", "try"); if(state == ERR_NONE) { Serial.println(F("success!")); } else { @@ -56,7 +59,7 @@ void setup() { void loop() { // publish MQTT message to the topic "hello" with content "world" Serial.print(F("[ESP8266] Publishing MQTT message ... ")); - byte state = wifi.MqttPublish("hello", "world"); + byte state = mqtt.publish("hello", "world"); if(state == ERR_NONE) { Serial.println(F("success!")); } else { diff --git a/examples/ESP8266_MQTT_Subscribe/ESP8266_MQTT_Subscribe.ino b/examples/ESP8266_MQTT_Subscribe/ESP8266_MQTT_Subscribe.ino index 9c046790..15941896 100644 --- a/examples/ESP8266_MQTT_Subscribe/ESP8266_MQTT_Subscribe.ino +++ b/examples/ESP8266_MQTT_Subscribe/ESP8266_MQTT_Subscribe.ino @@ -1,12 +1,12 @@ /* - * KiteLib ESP8266 MQTT Publish Example + * KiteLib ESP8266 MQTT Subscribe Example * - * This example publishes MQTT messages using ESP8266 WiFi module. + * This example subscribes to MQTT topic using ESP8266 WiFi module. * - * The messages are published to https://shiftr.io/try. You can use this namespace + * 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 upolading this example, make sure that the ESP8266 module is running + * 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)! */ @@ -16,6 +16,9 @@ // ESP8266 module is in slot A on the shield ESP8266 wifi = Kite.ModuleA; +// create MQTT client instance using the wifi module +MQTTClient mqtt(&wifi); + void setup() { Serial.begin(9600); @@ -43,7 +46,7 @@ void setup() { // connect to MQTT broker using client ID "arduino", username "try" and password "try" Serial.print(F("[ESP8266] Connecting to MQTT broker ... ")); - state = wifi.MqttConnect("broker.shiftr.io", "arduino", "try", "try"); + state = mqtt.connect("broker.shiftr.io", "arduino", "try", "try"); if(state == ERR_NONE) { Serial.println(F("success!")); } else { @@ -51,20 +54,57 @@ void setup() { Serial.println(state, HEX); while(true); } -} -void loop() { - // publish MQTT message to the topic "hello" with content "world" - Serial.print(F("[ESP8266] Publishing MQTT message ... ")); - byte state = wifi.MqttPublish("hello", "world"); + // subscribe to the topic "hello" + // after calling this method, server will send PUBLISH packets + // to this client each time a new message was published at the topic + Serial.print(F("[ESP8266] Subscribing to MQTT topic ... ")); + state = wifi.subscribe("hello"); if(state == ERR_NONE) { Serial.println(F("success!")); - } else { + } else { Serial.print(F("failed, code 0x")); Serial.println(state, HEX); } - // wait for a second before publishing again - delay(1000); + // unsubscribe from topic "hello" + // after calling this method, server will stop sending PUBLISH packets + Serial.print(F("[ESP8266] Unsubscribing from MQTT topic ... ")); + state = wifi.unsubscribe("hello"); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + } +} + +// create a function that will be called when a new PUBLISH packet +// arrives from the server +// +// IMPORTANT: This function MUST have two C-strings as arguments! +void onPublish(const char* topic, const char* message) { + Serial.println("[ESP8266] Received packet from MQTT server: "); + Serial.print("[ESP8266] Topic:\t"); + Serial.println(topic); + Serial.print("[ESP8266] Message:\t"); + Serial.println(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); + Serial.print("[ESP8266] MQTT check "); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + } + + // the rest of your loop() code goes here + // make sure that the maximum time the loop() runs is less than 1.5x keep alive, + // otherwise the server will close the network connection }