From 3f7ba759710c5b7cff95e1dc0761913da470ada0 Mon Sep 17 00:00:00 2001 From: nicolsc Date: Wed, 17 May 2017 14:37:15 +0200 Subject: [PATCH 1/4] Fix link to Sigfox libary reference Related to arduino-libraries/SigFox#1 --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 9ba098f43a957f8cf0d62ba3b47fd4d1b4b31399 Mon Sep 17 00:00:00 2001 From: nicolsc Date: Wed, 17 May 2017 14:41:31 +0200 Subject: [PATCH 2/4] Add function to send a single bit (true | false) over the Sigfox network This uses a different SPI transaction (`0x0B`), and allows to send empty frames for basic applications such as buttons, door opening systems, ... Added a simple example called _SendBoolean_ Let me know if I need to structure this differently to fit with the lib architecture :) --- examples/SendBoolean/SendBoolean.ino | 29 ++++++++++++++++++++ src/SigFox.cpp | 41 ++++++++++++++++++++++++++++ src/SigFox.h | 6 ++++ 3 files changed, 76 insertions(+) create mode 100644 examples/SendBoolean/SendBoolean.ino diff --git a/examples/SendBoolean/SendBoolean.ino b/examples/SendBoolean/SendBoolean.ino new file mode 100644 index 0000000..4d41f33 --- /dev/null +++ b/examples/SendBoolean/SendBoolean.ino @@ -0,0 +1,29 @@ +#include +#define VALUE_TO_SEND 1 +#define DEBUG 1 +void setup() { + + if (DEBUG){ + Serial.begin(9600); + while (!Serial) {}; + } + + if (!SigFox.begin()) { + if (DEBUG){ + Serial.println("Sigfox module unavailable !"); + } + return; + } + if (DEBUG){ + SigFox.debug(); + Serial.println("ID = " + SigFox.ID()); + } + delay(100); + int ret = SigFox.sendBit(VALUE_TO_SEND); + if (DEBUG){ + Serial.print("Status : "); + Serial.println(ret); + } +} + +void loop(){} diff --git a/src/SigFox.cpp b/src/SigFox.cpp index 5cf54b5..7a0d860 100644 --- a/src/SigFox.cpp +++ b/src/SigFox.cpp @@ -203,7 +203,48 @@ 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; diff --git a/src/SigFox.h b/src/SigFox.h index f1197ae..6e070be 100644 --- a/src/SigFox.h +++ b/src/SigFox.h @@ -82,6 +82,12 @@ class SIGFOXClass : public Stream template inline size_t write(T val) {return write((uint8_t*)&val, sizeof(T));}; using Print::write; + /* + * Send a single bit (0 | 1) over the Sigfox network + * Returns the status code from the Atmel Sigfox chipset + **/ + int sendBit(bool value); + //makes no sense in Sigfox world void flush() {}; From e11db72f875360667f4d2568f2398af6bef510f5 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 19 May 2017 11:42:56 +0200 Subject: [PATCH 3/4] Hide sendBit and use it internally --- src/SigFox.cpp | 10 +++++++++- src/SigFox.h | 12 ++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/SigFox.cpp b/src/SigFox.cpp index 7a0d860..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); @@ -203,6 +209,7 @@ int SIGFOXClass::send(unsigned char mess[], int len, bool rx) return sig; } + int SIGFOXClass::sendBit(bool value){ int ret; int i = 0; @@ -245,6 +252,7 @@ int SIGFOXClass::sendBit(bool value){ } return 99; //default } + int SIGFOXClass::beginPacket() { bool ret = (tx_buffer_index == -1); tx_buffer_index = 0; @@ -629,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 6e070be..18759a5 100644 --- a/src/SigFox.h +++ b/src/SigFox.h @@ -82,12 +82,6 @@ class SIGFOXClass : public Stream template inline size_t write(T val) {return write((uint8_t*)&val, sizeof(T));}; using Print::write; - /* - * Send a single bit (0 | 1) over the Sigfox network - * Returns the status code from the Atmel Sigfox chipset - **/ - int sendBit(bool value); - //makes no sense in Sigfox world void flush() {}; @@ -150,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 */ From 6deddbfa8df0b33e1c5c8bc048045e2f207f90cc Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 19 May 2017 11:59:53 +0200 Subject: [PATCH 4/4] Improve SendBoolean example --- examples/SendBoolean/SendBoolean.ino | 30 ++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/examples/SendBoolean/SendBoolean.ino b/examples/SendBoolean/SendBoolean.ino index 4d41f33..5dbe5cc 100644 --- a/examples/SendBoolean/SendBoolean.ino +++ b/examples/SendBoolean/SendBoolean.ino @@ -1,6 +1,22 @@ +/* + 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 -#define VALUE_TO_SEND 1 + +// 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){ @@ -8,18 +24,28 @@ void setup() { 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); - int ret = SigFox.sendBit(VALUE_TO_SEND); + + // 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);