diff --git a/examples/SendBoolean/SendBoolean.ino b/examples/SendBoolean/SendBoolean.ino new file mode 100644 index 0000000..5dbe5cc --- /dev/null +++ b/examples/SendBoolean/SendBoolean.ino @@ -0,0 +1,55 @@ +/* + SigFox Send Boolean tutorial + + This sketch demonstrates how to send a simple binary data ( 0 or 1 ) using a MKRFox1200. + If the application only needs to send one bit of information the transmission time + (and thus power consumption) will be much lower than sending a full 12 bytes packet. + + This example code is in the public domain. +*/ + +#include + +// We want to send a boolean value to signal a binary event +// like open/close or on/off + +bool value_to_send = true; + +#define DEBUG 1 + +void setup() { + + if (DEBUG){ + Serial.begin(9600); + while (!Serial) {}; + } + + // Initialize the SigFox module + if (!SigFox.begin()) { + if (DEBUG){ + Serial.println("Sigfox module unavailable !"); + } + return; + } + + // If we wanto to debug the application, print the device ID to easily find it in the backend + if (DEBUG){ + SigFox.debug(); + Serial.println("ID = " + SigFox.ID()); + } + + delay(100); + + // Compose a message as usual; the single bit transmission will be performed transparently + // if the data we want to send is suitable + SigFox.beginPacket(); + SigFox.write(value_to_send); + int ret = SigFox.endPacket(); + + if (DEBUG){ + Serial.print("Status : "); + Serial.println(ret); + } +} + +void loop(){} diff --git a/library.properties b/library.properties index 6e8d209..d41a90d 100644 --- a/library.properties +++ b/library.properties @@ -5,5 +5,5 @@ maintainer=Arduino LLC sentence=Helper library for MKRFox1200 board and ATAB8520E Sigfox module paragraph=This library allows some high level operations on Sigfox module, to ease integration with existing projects category=Device Control -url=http://arduino.cc/libraries/Sigfox +url=https://www.arduino.cc/en/Reference/SigFox architectures=samd diff --git a/src/SigFox.cpp b/src/SigFox.cpp index 5cf54b5..f5e3b1e 100644 --- a/src/SigFox.cpp +++ b/src/SigFox.cpp @@ -122,6 +122,12 @@ int SIGFOXClass::begin(SPIClass& spi, int reset, int poweron, int interrupt, int int SIGFOXClass::send(unsigned char mess[], int len, bool rx) { if (len == 0) return -1; + + if (rx == false && len == 1 && mess[0] < 2) { + //we can use send_bit command + return sendBit(mess[0]); + } + status(); digitalWrite(chip_select_pin, LOW); @@ -204,6 +210,49 @@ int SIGFOXClass::send(unsigned char mess[], int len, bool rx) return sig; } +int SIGFOXClass::sendBit(bool value){ + int ret; + int i = 0; + status(); + + digitalWrite(chip_select_pin, LOW); + delay(1); + spi_port.beginTransaction(SPICONFIG); + + spi_port.transfer(0x0B); + spi_port.transfer(value ? 1 : 0); + spi_port.endTransaction(); + delay(1); + digitalWrite(chip_select_pin, HIGH); + + int timeout = 7000; //7 seconds + + if (!debugging) { + LowPower.attachInterruptWakeup(interrupt_pin, NULL, FALLING); + LowPower.sleep(timeout); + if (digitalRead(interrupt_pin) == 0) { + status(); + return statusCode(SIGFOX); + } + } + + for (i = 0; i < timeout/10; i++) + { + if (digitalRead(interrupt_pin) == 0) { + status(); + return statusCode(SIGFOX); + break; + } + else { + digitalWrite(led_pin, HIGH); + delay(50); + digitalWrite(led_pin, LOW); + delay(50); + } + } + return 99; //default +} + int SIGFOXClass::beginPacket() { bool ret = (tx_buffer_index == -1); tx_buffer_index = 0; @@ -588,4 +637,4 @@ void SIGFOXClass::end() delay(1); } -SIGFOXClass SigFox; //singleton \ No newline at end of file +SIGFOXClass SigFox; //singleton diff --git a/src/SigFox.h b/src/SigFox.h index f1197ae..18759a5 100644 --- a/src/SigFox.h +++ b/src/SigFox.h @@ -144,6 +144,12 @@ class SIGFOXClass : public Stream */ int send(unsigned char mess[], int len = 12, bool rx = false); + /* + * Send a single bit (0 | 1) over the Sigfox network + * Returns the status code from the Atmel Sigfox chipset + **/ + int sendBit(bool value); + /* * Return atm status message */