Skip to content

Commit f669ffa

Browse files
authored
Merge pull request #3 from facchinm/send_bit
Implement SendBit functionality and wrap into Stream API
2 parents 2e69c61 + 6deddbf commit f669ffa

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
SigFox Send Boolean tutorial
3+
4+
This sketch demonstrates how to send a simple binary data ( 0 or 1 ) using a MKRFox1200.
5+
If the application only needs to send one bit of information the transmission time
6+
(and thus power consumption) will be much lower than sending a full 12 bytes packet.
7+
8+
This example code is in the public domain.
9+
*/
10+
11+
#include <SigFox.h>
12+
13+
// We want to send a boolean value to signal a binary event
14+
// like open/close or on/off
15+
16+
bool value_to_send = true;
17+
18+
#define DEBUG 1
19+
20+
void setup() {
21+
22+
if (DEBUG){
23+
Serial.begin(9600);
24+
while (!Serial) {};
25+
}
26+
27+
// Initialize the SigFox module
28+
if (!SigFox.begin()) {
29+
if (DEBUG){
30+
Serial.println("Sigfox module unavailable !");
31+
}
32+
return;
33+
}
34+
35+
// If we wanto to debug the application, print the device ID to easily find it in the backend
36+
if (DEBUG){
37+
SigFox.debug();
38+
Serial.println("ID = " + SigFox.ID());
39+
}
40+
41+
delay(100);
42+
43+
// Compose a message as usual; the single bit transmission will be performed transparently
44+
// if the data we want to send is suitable
45+
SigFox.beginPacket();
46+
SigFox.write(value_to_send);
47+
int ret = SigFox.endPacket();
48+
49+
if (DEBUG){
50+
Serial.print("Status : ");
51+
Serial.println(ret);
52+
}
53+
}
54+
55+
void loop(){}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ maintainer=Arduino LLC
55
sentence=Helper library for MKRFox1200 board and ATAB8520E Sigfox module
66
paragraph=This library allows some high level operations on Sigfox module, to ease integration with existing projects
77
category=Device Control
8-
url=http://arduino.cc/libraries/Sigfox
8+
url=https://www.arduino.cc/en/Reference/SigFox
99
architectures=samd

src/SigFox.cpp

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ int SIGFOXClass::begin(SPIClass& spi, int reset, int poweron, int interrupt, int
122122
int SIGFOXClass::send(unsigned char mess[], int len, bool rx)
123123
{
124124
if (len == 0) return -1;
125+
126+
if (rx == false && len == 1 && mess[0] < 2) {
127+
//we can use send_bit command
128+
return sendBit(mess[0]);
129+
}
130+
125131
status();
126132

127133
digitalWrite(chip_select_pin, LOW);
@@ -204,6 +210,49 @@ int SIGFOXClass::send(unsigned char mess[], int len, bool rx)
204210
return sig;
205211
}
206212

213+
int SIGFOXClass::sendBit(bool value){
214+
int ret;
215+
int i = 0;
216+
status();
217+
218+
digitalWrite(chip_select_pin, LOW);
219+
delay(1);
220+
spi_port.beginTransaction(SPICONFIG);
221+
222+
spi_port.transfer(0x0B);
223+
spi_port.transfer(value ? 1 : 0);
224+
spi_port.endTransaction();
225+
delay(1);
226+
digitalWrite(chip_select_pin, HIGH);
227+
228+
int timeout = 7000; //7 seconds
229+
230+
if (!debugging) {
231+
LowPower.attachInterruptWakeup(interrupt_pin, NULL, FALLING);
232+
LowPower.sleep(timeout);
233+
if (digitalRead(interrupt_pin) == 0) {
234+
status();
235+
return statusCode(SIGFOX);
236+
}
237+
}
238+
239+
for (i = 0; i < timeout/10; i++)
240+
{
241+
if (digitalRead(interrupt_pin) == 0) {
242+
status();
243+
return statusCode(SIGFOX);
244+
break;
245+
}
246+
else {
247+
digitalWrite(led_pin, HIGH);
248+
delay(50);
249+
digitalWrite(led_pin, LOW);
250+
delay(50);
251+
}
252+
}
253+
return 99; //default
254+
}
255+
207256
int SIGFOXClass::beginPacket() {
208257
bool ret = (tx_buffer_index == -1);
209258
tx_buffer_index = 0;
@@ -588,4 +637,4 @@ void SIGFOXClass::end()
588637
delay(1);
589638
}
590639

591-
SIGFOXClass SigFox; //singleton
640+
SIGFOXClass SigFox; //singleton

src/SigFox.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ class SIGFOXClass : public Stream
144144
*/
145145
int send(unsigned char mess[], int len = 12, bool rx = false);
146146

147+
/*
148+
* Send a single bit (0 | 1) over the Sigfox network
149+
* Returns the status code from the Atmel Sigfox chipset
150+
**/
151+
int sendBit(bool value);
152+
147153
/*
148154
* Return atm status message
149155
*/

0 commit comments

Comments
 (0)