Added new examples

pull/1/head
Jan Gromeš 2018-07-03 12:01:37 +02:00
rodzic 09ebf94490
commit 8a8f58c08b
8 zmienionych plików z 485 dodań i 0 usunięć

Wyświetl plik

@ -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 <KiteLib.h>
// 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);
}

Wyświetl plik

@ -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 <KiteLib.h>
// 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());
}
}

Wyświetl plik

@ -0,0 +1,66 @@
/*
* KiteLib RF69 Receive Example
*
* This example receives packets using RF69 FSK radio module.
*/
// include the library
#include <KiteLib.h>
// 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!"));
}
}

Wyświetl plik

@ -0,0 +1,50 @@
/*
* KiteLib RF69 Transmit Example
*
* This example transmits packets using RF69 FSK radio module.
*/
// include the library
#include <KiteLib.h>
// 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);
}

Wyświetl plik

@ -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 <KiteLib.h>
// 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!"));
}
}

Wyświetl plik

@ -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 <KiteLib.h>
// 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);
}

Wyświetl plik

@ -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 <KiteLib.h>
// 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());
}
}

Wyświetl plik

@ -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 <KiteLib.h>
// 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());
}
}