diff --git a/examples/ESP8266_MQTT_Publish/ESP8266_MQTT_Publish.ino b/examples/ESP8266_MQTT_Publish/ESP8266_MQTT_Publish.ino new file mode 100644 index 00000000..9c046790 --- /dev/null +++ b/examples/ESP8266_MQTT_Publish/ESP8266_MQTT_Publish.ino @@ -0,0 +1,70 @@ +/* + * 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 upolading 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; + +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("SSID", "password"); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + 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"); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + 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"); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + } + + // wait for a second before publishing again + delay(1000); +} + diff --git a/examples/HC05/HC05.ino b/examples/HC05/HC05.ino new file mode 100644 index 00000000..2d5021e4 --- /dev/null +++ b/examples/HC05/HC05.ino @@ -0,0 +1,28 @@ +/* + * KiteLib HC05 Example + * + * This example sends data using HC05 Bluetooth module. + * HC05 works exactly like a Serial line, data are sent to the paired device. + * The default pairing code for HC05 is 1234. + */ + +// include the library +#include + +// HC05 module is in slot A on the shield +HC05 bluetooth = Kite.ModuleA; + +void setup() { + Serial.begin(9600); + + // initialize HC05 with baudrate 9600 baud + bluetooth.begin(9600); +} + +void loop() { + // read data incoming from Serial port and write them to bluetooth + // HC05 supports all methods of the Serial class + while(Serial.available() > 0) { + bluetooth.write(Serial.read()); + } +} diff --git a/examples/RF69_Receive/RF69_Receive.ino b/examples/RF69_Receive/RF69_Receive.ino new file mode 100644 index 00000000..536659e4 --- /dev/null +++ b/examples/RF69_Receive/RF69_Receive.ino @@ -0,0 +1,66 @@ +/* + * KiteLib RF69 Receive Example + * + * This example receives packets using RF69 FSK radio module. + */ + +// include the library +#include + +// RF69 module is in slot A on the shield +RF69 rf = Kite.ModuleA; + +// create empty instance of Packet class +Packet pack; + +void setup() { + Serial.begin(9600); + + // initialize RF69 with default settings + Serial.print(F("[RF69] Initializing ... ")); + byte state = rf.begin(); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } +} + +void loop() { + Serial.print(F("[RF69] Waiting for incoming transmission ... ")); + + // wait for single packet + byte state = rf.receive(pack); + + if(state == ERR_NONE) { + // packet was successfully received + Serial.println(F("success!")); + + // print the source of the packet + Serial.print(F("[RF69] Source:\t\t")); + Serial.println(pack.getSourceStr()); + + // print the destination of the packet + Serial.print(F("[RF69] Destination:\t")); + Serial.println(pack.getDestinationStr()); + + // print the length of the packet + Serial.print(F("[RF69] Length:\t\t")); + Serial.println(pack.length); + + // print the data of the packet + Serial.print(F("[RF69] Data:\t\t")); + Serial.println(pack.data); + + } else if(state == ERR_RX_TIMEOUT) { + // timeout occurred while waiting for a packet + Serial.println(F("timeout!")); + + } else if(state == ERR_CRC_MISMATCH) { + // packet was received, but is malformed + Serial.println(F("CRC error!")); + + } +} diff --git a/examples/RF69_Transmit/RF69_Transmit.ino b/examples/RF69_Transmit/RF69_Transmit.ino new file mode 100644 index 00000000..67f14fdd --- /dev/null +++ b/examples/RF69_Transmit/RF69_Transmit.ino @@ -0,0 +1,50 @@ +/* + * KiteLib RF69 Transmit Example + * + * This example transmits packets using RF69 FSK radio module. + */ + +// include the library +#include + +// RF69 module is in slot A on the shield +RF69 rf = Kite.ModuleA; + +// create instance of Packet class with destination address +// "01:23:45:67:89:AB:CD:EF" and data "Hello World !" +Packet pack("01:23:45:67:89:AB:CD:EF", "Hello World!"); + +void setup() { + Serial.begin(9600); + + // initialize RF69 with default settings + Serial.print(F("[RF69] Initializing ... ")); + byte state = rf.begin(); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } +} + +void loop() { + Serial.print(F("[RF69] Transmitting packet ... ")); + + // start transmitting the packet + byte state = rf.transmit(pack); + + if(state == ERR_NONE) { + // the packet was successfully transmitted + Serial.println(" success!"); + + } else if(state == ERR_PACKET_TOO_LONG) { + // the supplied packet was longer than 256 bytes + Serial.println(" too long!"); + + } + + // wait for a second before transmitting again + delay(1000); +} diff --git a/examples/SX127x_Receive/SX127x_Receive.ino b/examples/SX127x_Receive/SX127x_Receive.ino new file mode 100644 index 00000000..7c28a898 --- /dev/null +++ b/examples/SX127x_Receive/SX127x_Receive.ino @@ -0,0 +1,88 @@ +/* + * KiteLib SX127x Receive Example + * + * This example receives packets using SX1278 LoRa radio module. + * + * Other modules from SX127x family can also be used. + * SX1272 lora = Kite.ModuleA; + * SX1273 lora = Kite.ModuleA; + * SX1276 lora = Kite.ModuleA; + * SX1277 lora = Kite.ModuleA; + * SX1279 lora = Kite.ModuleA; + */ + +// include the library +#include + +// SX1278 module is in slot A on the shield +SX1278 lora = Kite.ModuleA; + +// create empty instance of Packet class +Packet pack; + +void setup() { + Serial.begin(9600); + + // initialize SX1278 with default settings + Serial.print(F("[SX1278] Initializing ... ")); + byte state = lora.begin(); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } +} + +void loop() { + Serial.print(F("[SX1278] Waiting for incoming transmission ... ")); + + // wait for single packet + byte state = lora.receive(pack); + + if(state == ERR_NONE) { + // packet was successfully received + Serial.println(F("success!")); + + // print the source of the packet + Serial.print(F("[SX1278] Source:\t\t")); + Serial.println(pack.getSourceStr()); + + // print the destination of the packet + Serial.print(F("[SX1278] Destination:\t")); + Serial.println(pack.getDestinationStr()); + + // print the length of the packet + Serial.print(F("[SX1278] Length:\t\t")); + Serial.println(pack.length); + + // print the data of the packet + Serial.print(F("[SX1278] Data:\t\t")); + Serial.println(pack.data); + + //print the measured data rate + Serial.print("[SX1278] Datarate:\t"); + Serial.print(lora.dataRate); + Serial.println(" bps"); + + //print the RSSI (Received Signal Strength Indicator) of the last received packet + Serial.print("[SX1278] RSSI:\t\t"); + Serial.print(lora.lastPacketRSSI); + Serial.println(" dBm"); + + //print the SNR (Signal-to-Noise Ratio) of the last received packet + Serial.print("[SX1278] SNR:\t\t"); + Serial.print(lora.lastPacketSNR); + Serial.println(" dBm"); + + } else if(state == ERR_RX_TIMEOUT) { + // timeout occurred while waiting for a packet + Serial.println(F("timeout!")); + + } else if(state == ERR_CRC_MISMATCH) { + // packet was received, but is malformed + Serial.println(F("CRC error!")); + + } +} diff --git a/examples/SX127x_Transmit/SX127x_Transmit.ino b/examples/SX127x_Transmit/SX127x_Transmit.ino new file mode 100644 index 00000000..0a754e31 --- /dev/null +++ b/examples/SX127x_Transmit/SX127x_Transmit.ino @@ -0,0 +1,61 @@ +/* + * KiteLib SX127x Transmit Example + * + * This example transmits packets using SX1278 LoRa radio module. + * + * Other modules from SX127x family can also be used. + * SX1272 lora = Kite.ModuleA; + * SX1273 lora = Kite.ModuleA; + * SX1276 lora = Kite.ModuleA; + * SX1277 lora = Kite.ModuleA; + * SX1279 lora = Kite.ModuleA; + */ + +// include the library +#include + +// SX1278 module is in slot A on the shield +SX1278 lora = Kite.ModuleA; + +// create instance of Packet class with destination address +// "01:23:45:67:89:AB:CD:EF" and data "Hello World !" +Packet pack("01:23:45:67:89:AB:CD:EF", "Hello World!"); + +void setup() { + Serial.begin(9600); + + // initialize SX1278 with default settings + Serial.print(F("[SX1278] Initializing ... ")); + byte state = lora.begin(); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } +} + +void loop() { + Serial.print(F("[SX1278] Transmitting packet ... ")); + + // start transmitting the packet + byte state = lora.transmit(pack); + + if(state == ERR_NONE) { + // the packet was successfully transmitted + Serial.println(" success!"); + + } else if(state == ERR_PACKET_TOO_LONG) { + // the supplied packet was longer than 256 bytes + Serial.println(" too long!"); + + } else if(state == ERR_TX_TIMEOUT) { + // timeout occured while transmitting packet + Serial.println(" timeout!"); + + } + + // wait for a second before transmitting again + delay(1000); +} diff --git a/examples/XBee_AT_Receive/XBee_AT_Receive.ino b/examples/XBee_AT_Receive/XBee_AT_Receive.ino new file mode 100644 index 00000000..de421237 --- /dev/null +++ b/examples/XBee_AT_Receive/XBee_AT_Receive.ino @@ -0,0 +1,61 @@ +/* + * KiteLib XBee AT Receive Example + * + * This example receives packets using XBee AT mode. + * In AT mode, two XBee modules act like a Serial line. Both modules must have + * the same PAN ID, and the destination addresses have to be set properly. + * + * IMPORTANT: Before uplolading this example, make sure that the XBee module is running + * AT ROUTER firmware! + */ + +// include the library +#include + +// XBee module is in slot A on the shield +XBee bee = Kite.ModuleA; + +void setup() { + Serial.begin(9600); + + // initialize XBee module with baudrate 9600 + Serial.print(F("[XBee] Initializing ... ")); + byte state = bee.begin(9600); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } + + // set PAN ID to 0123456789ABCDEF + Serial.print(F("[XBee] Setting PAN ID ... ")); + state = bee.setPanId("0123456789ABCDEF"); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } + + // set destination address to the address of the second module + Serial.print(F("[XBee] Setting destination address ... ")); + state = bee.setDestinationAddress("0013A200", "40A58A52"); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } +} + +void loop() { + // read data incoming from XBee and write them to Serial + // XBee supports all methods of the Serial class + while(bee.available() > 0) { + Serial.write(bee.read()); + } +} diff --git a/examples/XBee_AT_Transmit/XBee_AT_Transmit.ino b/examples/XBee_AT_Transmit/XBee_AT_Transmit.ino new file mode 100644 index 00000000..8e863303 --- /dev/null +++ b/examples/XBee_AT_Transmit/XBee_AT_Transmit.ino @@ -0,0 +1,61 @@ +/* + * KiteLib XBee AT Transmit Example + * + * This example transmits packets using XBee AT mode. + * In AT mode, two XBee modules act like a Serial line. Both modules must have + * the same PAN ID, and the destination addresses have to be set properly. + * + * IMPORTANT: Before uplolading this example, make sure that the XBee module is running + * AT COORDINATOR firmware! + */ + +// include the library +#include + +// XBee module is in slot A on the shield +XBee bee = Kite.ModuleA; + +void setup() { + Serial.begin(9600); + + // initialize XBee module with baudrate 9600 + Serial.print(F("[XBee] Initializing ... ")); + byte state = bee.begin(9600); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } + + // set PAN ID to 0123456789ABCDEF + Serial.print(F("[XBee] Setting PAN ID ... ")); + state = bee.setPanId("0123456789ABCDEF"); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } + + // set destination address to the address of the second module + Serial.print(F("[XBee] Setting destination address ... ")); + state = bee.setDestinationAddress("0013A200", "40A58A5D"); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } +} + +void loop() { + // read data incoming from Serial port and write them to XBee + // XBee supports all methods of the Serial class + while(Serial.available() > 0) { + bee.write(Serial.read()); + } +}