Sigfox Communication library, allowing you to use any kind of UART able Sigfox Transmitter

Dependencies:   SoftSerial

Dependents:   RUCHE2-CODES RUCHE2-CODES_correctionpoids RUCHE2-CODES

Committer:
RB62680
Date:
Sat Mar 12 16:00:57 2016 +0000
Revision:
2:975b82a3cde0
Parent:
1:39a0558a3a3a
Child:
3:7625fe7cd1b6
Added Sigfox_n Sigfox;;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adrien3d 0:996eb84c895e 1 #include "mbed.h"
adrien3d 0:996eb84c895e 2 #include "millis.h"
adrien3d 0:996eb84c895e 3 #include "sigfox.h"
adrien3d 0:996eb84c895e 4
RB62680 2:975b82a3cde0 5 Sigfox_ Sigfox;
RB62680 2:975b82a3cde0 6
adrien3d 0:996eb84c895e 7 Sigfox_::Sigfox_() {
adrien3d 0:996eb84c895e 8 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 9 _lastSend=-1;
adrien3d 0:996eb84c895e 10 }
adrien3d 0:996eb84c895e 11
adrien3d 0:996eb84c895e 12 uint8_t Sigfox_::_nextReturn() {
adrien3d 0:996eb84c895e 13 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 14 char fstChar = device.getc();
adrien3d 0:996eb84c895e 15 while(device.getc() != ';');
adrien3d 0:996eb84c895e 16 return fstChar;
adrien3d 0:996eb84c895e 17 }
adrien3d 0:996eb84c895e 18
adrien3d 0:996eb84c895e 19 void Sigfox_::begin () {
adrien3d 0:996eb84c895e 20 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 21 device.putc((uint8_t)'\0');
adrien3d 0:996eb84c895e 22 device.putc((uint8_t)';');
adrien3d 0:996eb84c895e 23
adrien3d 0:996eb84c895e 24 //while(device.available() < 3);
adrien3d 0:996eb84c895e 25
adrien3d 0:996eb84c895e 26 device.getc(); //'K'
adrien3d 0:996eb84c895e 27 device.getc(); //'O'
adrien3d 0:996eb84c895e 28 device.getc(); //';'
adrien3d 0:996eb84c895e 29 }
adrien3d 0:996eb84c895e 30
adrien3d 0:996eb84c895e 31 bool Sigfox_::isReady() {
adrien3d 0:996eb84c895e 32 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 33
adrien3d 0:996eb84c895e 34 unsigned long currentTime = millis();
adrien3d 0:996eb84c895e 35 if(currentTime >= _lastSend && (currentTime - _lastSend) <= 600000) {
adrien3d 0:996eb84c895e 36 return false;
adrien3d 0:996eb84c895e 37 }
adrien3d 0:996eb84c895e 38 // Time is ok, ask the modem's status
adrien3d 0:996eb84c895e 39 device.putc((uint8_t)'\0');
adrien3d 0:996eb84c895e 40 device.putc((uint8_t)'A');
adrien3d 0:996eb84c895e 41 device.putc((uint8_t)'T');
adrien3d 0:996eb84c895e 42 device.putc('\r');
adrien3d 0:996eb84c895e 43 //device.putc((uint8_t)';');
adrien3d 0:996eb84c895e 44
adrien3d 0:996eb84c895e 45 //return _nextReturn() == OK;
adrien3d 0:996eb84c895e 46 return true;
adrien3d 0:996eb84c895e 47 }
adrien3d 0:996eb84c895e 48
adrien3d 0:996eb84c895e 49 bool Sigfox_::send(const void* data, uint8_t len) {
adrien3d 0:996eb84c895e 50 uint8_t* bytes = (uint8_t*)data;
adrien3d 0:996eb84c895e 51 _lastSend = millis();
adrien3d 0:996eb84c895e 52
adrien3d 0:996eb84c895e 53 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 54 device.putc((uint8_t)'\0');
adrien3d 0:996eb84c895e 55 device.putc((uint8_t)'A');
adrien3d 0:996eb84c895e 56 device.putc((uint8_t)'T');
adrien3d 0:996eb84c895e 57 device.putc((uint8_t)'$');
adrien3d 0:996eb84c895e 58 device.putc((uint8_t)'S');
adrien3d 0:996eb84c895e 59 device.putc((uint8_t)'F');
adrien3d 0:996eb84c895e 60 device.putc((uint8_t)'=');
adrien3d 0:996eb84c895e 61
adrien3d 0:996eb84c895e 62 for(uint8_t i = 0; i < len; ++i) {
adrien3d 0:996eb84c895e 63 device.putc(bytes[i]);
adrien3d 0:996eb84c895e 64 }
adrien3d 0:996eb84c895e 65 device.putc('\r');
adrien3d 0:996eb84c895e 66
adrien3d 0:996eb84c895e 67 uint8_t ok = _nextReturn();
adrien3d 0:996eb84c895e 68 if(ok == OK) {
adrien3d 0:996eb84c895e 69 _nextReturn(); //SENT
adrien3d 0:996eb84c895e 70 return true;
adrien3d 0:996eb84c895e 71 }
adrien3d 0:996eb84c895e 72 return false;
adrien3d 0:996eb84c895e 73 }
adrien3d 0:996eb84c895e 74
adrien3d 0:996eb84c895e 75 unsigned long Sigfox_::getID() {
adrien3d 0:996eb84c895e 76 unsigned long id = 0;
adrien3d 0:996eb84c895e 77 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 78 device.putc((uint8_t)'\0');
adrien3d 0:996eb84c895e 79 device.putc((uint8_t)'A');
adrien3d 0:996eb84c895e 80 device.putc((uint8_t)'T');
adrien3d 0:996eb84c895e 81 device.putc((uint8_t)'$');
adrien3d 0:996eb84c895e 82 device.putc((uint8_t)'I');
adrien3d 0:996eb84c895e 83 device.putc((uint8_t)'=');
adrien3d 0:996eb84c895e 84 device.putc((uint8_t)'1');
adrien3d 0:996eb84c895e 85 device.putc((uint8_t)'0');
adrien3d 0:996eb84c895e 86 device.putc('\r');
adrien3d 0:996eb84c895e 87
adrien3d 0:996eb84c895e 88 uint8_t response[8] = {0};
adrien3d 0:996eb84c895e 89 uint8_t i = 0;
adrien3d 0:996eb84c895e 90 for (i = 0; i<7;++i) {
adrien3d 0:996eb84c895e 91 response[i] = device.getc();
adrien3d 0:996eb84c895e 92 ++i;
adrien3d 0:996eb84c895e 93 }
adrien3d 0:996eb84c895e 94 /*uint8_t response[8] = {0};
adrien3d 0:996eb84c895e 95 while(device.getc() != '\0') {
adrien3d 0:996eb84c895e 96 response[i] = device.getc();
adrien3d 0:996eb84c895e 97 while(!device.readable());
adrien3d 0:996eb84c895e 98 ++i;
adrien3d 0:996eb84c895e 99 }*/
adrien3d 0:996eb84c895e 100 device.getc(); //';'
adrien3d 0:996eb84c895e 101 for(uint8_t j = 0; j < i; ++j) {
adrien3d 0:996eb84c895e 102 id += response[j] << ((i-3-j) * 8);
adrien3d 0:996eb84c895e 103 }
adrien3d 0:996eb84c895e 104 return id;
adrien3d 0:996eb84c895e 105 }
adrien3d 0:996eb84c895e 106
adrien3d 0:996eb84c895e 107 unsigned long Sigfox_::getPAC() {
adrien3d 0:996eb84c895e 108 unsigned long id = 0;
adrien3d 0:996eb84c895e 109 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 110 device.putc((uint8_t)'\0');
adrien3d 0:996eb84c895e 111 device.putc((uint8_t)'A');
adrien3d 0:996eb84c895e 112 device.putc((uint8_t)'T');
adrien3d 0:996eb84c895e 113 device.putc((uint8_t)'$');
adrien3d 0:996eb84c895e 114 device.putc((uint8_t)'I');
adrien3d 0:996eb84c895e 115 device.putc((uint8_t)'=');
adrien3d 0:996eb84c895e 116 device.putc((uint8_t)'1');
adrien3d 0:996eb84c895e 117 device.putc((uint8_t)'1');
adrien3d 0:996eb84c895e 118 device.putc('\r');
adrien3d 0:996eb84c895e 119
adrien3d 0:996eb84c895e 120 uint8_t response[8] = {0};
adrien3d 0:996eb84c895e 121 uint8_t i = 0;
adrien3d 0:996eb84c895e 122 for (i = 0; i<7;++i) {
adrien3d 0:996eb84c895e 123 response[i] = device.getc();
adrien3d 0:996eb84c895e 124 ++i;
adrien3d 0:996eb84c895e 125 }
adrien3d 0:996eb84c895e 126
adrien3d 0:996eb84c895e 127 for(uint8_t j = 0; j < i; ++j) {
adrien3d 0:996eb84c895e 128 id += response[j] << ((i-3-j) * 16);
adrien3d 0:996eb84c895e 129 }
adrien3d 0:996eb84c895e 130 return true;
adrien3d 0:996eb84c895e 131 }
adrien3d 0:996eb84c895e 132
adrien3d 0:996eb84c895e 133 bool Sigfox_::setPowerMode(uint8_t power) {
adrien3d 0:996eb84c895e 134 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 135 device.putc((uint8_t)'\0');
adrien3d 0:996eb84c895e 136 device.putc((uint8_t)'A');
adrien3d 0:996eb84c895e 137 device.putc((uint8_t)'T');
adrien3d 0:996eb84c895e 138 device.putc((uint8_t)'$');
adrien3d 0:996eb84c895e 139 device.putc((uint8_t)'I');
adrien3d 0:996eb84c895e 140 device.putc((uint8_t)'=');
adrien3d 0:996eb84c895e 141 device.putc(power);
adrien3d 0:996eb84c895e 142 device.putc('\r');
adrien3d 0:996eb84c895e 143
adrien3d 0:996eb84c895e 144 //return _nextReturn() == OK;
adrien3d 0:996eb84c895e 145 return 1;
adrien3d 0:996eb84c895e 146 }
adrien3d 0:996eb84c895e 147
adrien3d 0:996eb84c895e 148 //------------------------------------------------------------------------------------------------//
adrien3d 0:996eb84c895e 149 //Destructors
adrien3d 0:996eb84c895e 150
adrien3d 0:996eb84c895e 151 /*sigfox::~sf() {
adrien3d 0:996eb84c895e 152 }*/