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

Fork of Sigfox_Com by Adrien Chapelet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sigfox.cpp Source File

sigfox.cpp

00001 #include "mbed.h"
00002 #include "millis.h"
00003 #include "sigfox.h"
00004 
00005 Sigfox_ Sigfox;
00006 
00007 Sigfox_::Sigfox_()  {
00008     Serial device(PIN_RX, PIN_TX);
00009     _lastSend=-1;
00010 }
00011 
00012 uint8_t Sigfox_::_nextReturn() {
00013     Serial device(PIN_RX, PIN_TX);     
00014     char fstChar = device.getc();
00015     while(device.getc() != ';');
00016     return fstChar;
00017 }
00018 
00019 void Sigfox_::begin () {
00020     Serial device(PIN_RX, PIN_TX); 
00021     device.putc((uint8_t)'\0');
00022     device.putc((uint8_t)';');
00023     
00024     //while(device.available() < 3);
00025 
00026     device.getc(); //'K'
00027     device.getc(); //'O'
00028     device.getc(); //';'
00029 }
00030 
00031 bool Sigfox_::isReady() {
00032     Serial device(PIN_RX, PIN_TX);
00033 
00034     unsigned long currentTime = millis();
00035     if(currentTime >= _lastSend && (currentTime - _lastSend) <= 600000) {
00036         return false;
00037     }
00038     // Time is ok, ask the modem's status
00039     device.putc((uint8_t)'\0');
00040     device.putc((uint8_t)'A');
00041     device.putc((uint8_t)'T');
00042     device.putc('\r');
00043     //device.putc((uint8_t)';');
00044 
00045     //return _nextReturn() == OK;
00046     return true;
00047 }
00048  
00049 bool Sigfox_::send(const void* data, uint8_t len) {
00050     uint8_t* bytes = (uint8_t*)data;
00051     _lastSend = millis();
00052     
00053     Serial device(PIN_RX, PIN_TX);
00054     device.putc((uint8_t)'\0');
00055     device.putc((uint8_t)'A');
00056     device.putc((uint8_t)'T');
00057     device.putc((uint8_t)'$');
00058     device.putc((uint8_t)'S');
00059     device.putc((uint8_t)'F');
00060     device.putc((uint8_t)'=');
00061     
00062     for(uint8_t i = 0; i < len; ++i) {
00063         device.putc(bytes[i]);
00064     }
00065     device.putc('\r');
00066 
00067     uint8_t ok = _nextReturn();
00068     if(ok == OK) {
00069         _nextReturn(); //SENT
00070         return true;
00071     }
00072     return false;
00073 }
00074  
00075 unsigned long Sigfox_::getID() {
00076     unsigned long id = 0;
00077     Serial device(PIN_RX, PIN_TX);
00078     device.putc((uint8_t)'\0');
00079     device.putc((uint8_t)'A');
00080     device.putc((uint8_t)'T');
00081     device.putc((uint8_t)'$');
00082     device.putc((uint8_t)'I');
00083     device.putc((uint8_t)'=');
00084     device.putc((uint8_t)'1');
00085     device.putc((uint8_t)'0');
00086     device.putc('\r');
00087     
00088     uint8_t response[8] = {0};
00089     uint8_t i = 0;
00090     for (i = 0; i<7;++i) {
00091         response[i] = device.getc();
00092         ++i;
00093     }
00094     /*uint8_t response[8] = {0};
00095     while(device.getc() != '\0') {
00096         response[i] = device.getc();
00097         while(!device.readable());
00098         ++i;
00099     }*/
00100     device.getc(); //';'
00101     for(uint8_t j = 0; j < i; ++j) {
00102         id += response[j] << ((i-3-j) * 8);
00103     }
00104     return id;
00105 }
00106 
00107 unsigned long Sigfox_::getPAC() {
00108     unsigned long id = 0;
00109     Serial device(PIN_RX, PIN_TX);
00110     device.putc((uint8_t)'\0');
00111     device.putc((uint8_t)'A');
00112     device.putc((uint8_t)'T');
00113     device.putc((uint8_t)'$');
00114     device.putc((uint8_t)'I');
00115     device.putc((uint8_t)'=');
00116     device.putc((uint8_t)'1');
00117     device.putc((uint8_t)'1');
00118     device.putc('\r');
00119     
00120     uint8_t response[8] = {0};
00121     uint8_t i = 0;
00122     for (i = 0; i<7;++i) {
00123         response[i] = device.getc();
00124         ++i;
00125     }
00126     
00127     for(uint8_t j = 0; j < i; ++j) {
00128         id += response[j] << ((i-3-j) * 16);
00129     }
00130     return true;
00131 }
00132 
00133 bool Sigfox_::setPowerMode(uint8_t power) {
00134     Serial device(PIN_RX, PIN_TX);
00135     device.putc((uint8_t)'\0');
00136     device.putc((uint8_t)'A');
00137     device.putc((uint8_t)'T');
00138     device.putc((uint8_t)'$');
00139     device.putc((uint8_t)'I');
00140     device.putc((uint8_t)'=');
00141     device.putc(power);
00142     device.putc('\r');
00143 
00144     //return _nextReturn() == OK;
00145     return 1;
00146 }
00147 
00148 //------------------------------------------------------------------------------------------------//
00149 //Destructors
00150 
00151 /*sigfox::~sf() {
00152 }*/