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

Dependents:   TestVirtualisation

Committer:
adrien3d
Date:
Fri Mar 11 08:24:52 2016 +0000
Revision:
0:996eb84c895e
Child:
1:39a0558a3a3a
First version

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
adrien3d 0:996eb84c895e 5 /* sigfox library
adrien3d 0:996eb84c895e 6 *
adrien3d 0:996eb84c895e 7 * Copyright (c) Adrien Chapelet for IoTech.fr
adrien3d 0:996eb84c895e 8 *
adrien3d 0:996eb84c895e 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
adrien3d 0:996eb84c895e 10 * of this software and associated documentation files (the "Software"), to deal
adrien3d 0:996eb84c895e 11 * in the Software without restriction, including without limitation the rights
adrien3d 0:996eb84c895e 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
adrien3d 0:996eb84c895e 13 * copies of the Software, and to permit persons to whom the Software is
adrien3d 0:996eb84c895e 14 * furnished to do so, subject to the following conditions:
adrien3d 0:996eb84c895e 15 *
adrien3d 0:996eb84c895e 16 * The above copyright notice and this permission notice shall be included in
adrien3d 0:996eb84c895e 17 * all copies or substantial portions of the Software.
adrien3d 0:996eb84c895e 18 *
adrien3d 0:996eb84c895e 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
adrien3d 0:996eb84c895e 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
adrien3d 0:996eb84c895e 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
adrien3d 0:996eb84c895e 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
adrien3d 0:996eb84c895e 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
adrien3d 0:996eb84c895e 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
adrien3d 0:996eb84c895e 25 * THE SOFTWARE.
adrien3d 0:996eb84c895e 26 */
adrien3d 0:996eb84c895e 27
adrien3d 0:996eb84c895e 28 /*
adrien3d 0:996eb84c895e 29 This library allows you to use any kind of Sigfox Transmitter, all you have to do is configure pins and baudrate, and check syntax is conform to your datasheet.
adrien3d 0:996eb84c895e 30 */
adrien3d 0:996eb84c895e 31
adrien3d 0:996eb84c895e 32 //---------------------------------------------------------------------------------------------//
adrien3d 0:996eb84c895e 33 //Constructors
adrien3d 0:996eb84c895e 34 Sigfox_::Sigfox_() {
adrien3d 0:996eb84c895e 35 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 36 _lastSend=-1;
adrien3d 0:996eb84c895e 37 }
adrien3d 0:996eb84c895e 38 /*Sigfox_::Sigfox_(PinMode TX, PinMode RX) {
adrien3d 0:996eb84c895e 39 Serial device(p8, p7);
adrien3d 0:996eb84c895e 40 }*/
adrien3d 0:996eb84c895e 41
adrien3d 0:996eb84c895e 42 //Functions
adrien3d 0:996eb84c895e 43 uint8_t Sigfox_::_nextReturn() {
adrien3d 0:996eb84c895e 44 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 45 char fstChar = device.getc();
adrien3d 0:996eb84c895e 46 while(device.getc() != ';');
adrien3d 0:996eb84c895e 47 return fstChar;
adrien3d 0:996eb84c895e 48 }
adrien3d 0:996eb84c895e 49
adrien3d 0:996eb84c895e 50 void Sigfox_::begin () {
adrien3d 0:996eb84c895e 51 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 52 device.putc((uint8_t)'\0');
adrien3d 0:996eb84c895e 53 device.putc((uint8_t)';');
adrien3d 0:996eb84c895e 54
adrien3d 0:996eb84c895e 55 //while(device.available() < 3);
adrien3d 0:996eb84c895e 56
adrien3d 0:996eb84c895e 57 device.getc(); //'K'
adrien3d 0:996eb84c895e 58 device.getc(); //'O'
adrien3d 0:996eb84c895e 59 device.getc(); //';'
adrien3d 0:996eb84c895e 60 }
adrien3d 0:996eb84c895e 61
adrien3d 0:996eb84c895e 62 bool Sigfox_::isReady() {
adrien3d 0:996eb84c895e 63 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 64
adrien3d 0:996eb84c895e 65 unsigned long currentTime = millis();
adrien3d 0:996eb84c895e 66 if(currentTime >= _lastSend && (currentTime - _lastSend) <= 600000) {
adrien3d 0:996eb84c895e 67 return false;
adrien3d 0:996eb84c895e 68 }
adrien3d 0:996eb84c895e 69 // Time is ok, ask the modem's status
adrien3d 0:996eb84c895e 70 device.putc((uint8_t)'\0');
adrien3d 0:996eb84c895e 71 device.putc((uint8_t)'A');
adrien3d 0:996eb84c895e 72 device.putc((uint8_t)'T');
adrien3d 0:996eb84c895e 73 device.putc('\r');
adrien3d 0:996eb84c895e 74 //device.putc((uint8_t)';');
adrien3d 0:996eb84c895e 75
adrien3d 0:996eb84c895e 76 //return _nextReturn() == OK;
adrien3d 0:996eb84c895e 77 return true;
adrien3d 0:996eb84c895e 78 /*
adrien3d 0:996eb84c895e 79 device.putc((uint8_t)'A');
adrien3d 0:996eb84c895e 80 device.putc((uint8_t)'T');
adrien3d 0:996eb84c895e 81
adrien3d 0:996eb84c895e 82 uint8_t response[8] = {0};
adrien3d 0:996eb84c895e 83 uint8_t i = 0;
adrien3d 0:996eb84c895e 84 //while(!device.available());
adrien3d 0:996eb84c895e 85 while(device.peek() != ';') {
adrien3d 0:996eb84c895e 86 response[i] = device.read();
adrien3d 0:996eb84c895e 87 while(!device.available());
adrien3d 0:996eb84c895e 88 ++i;
adrien3d 0:996eb84c895e 89 }
adrien3d 0:996eb84c895e 90 response[0]=device.getc(); //'K'
adrien3d 0:996eb84c895e 91 response[1]=device.getc(); //'O'
adrien3d 0:996eb84c895e 92 response[2]=device.getc(); //';'
adrien3d 0:996eb84c895e 93 if ((response[0]=='O') && (response[1]=='K'))
adrien3d 0:996eb84c895e 94 return OK;*/
adrien3d 0:996eb84c895e 95 }
adrien3d 0:996eb84c895e 96
adrien3d 0:996eb84c895e 97 bool Sigfox_::send(const void* data, uint8_t len) {
adrien3d 0:996eb84c895e 98 uint8_t* bytes = (uint8_t*)data;
adrien3d 0:996eb84c895e 99 _lastSend = millis();
adrien3d 0:996eb84c895e 100
adrien3d 0:996eb84c895e 101 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 102 device.putc((uint8_t)'\0');
adrien3d 0:996eb84c895e 103 device.putc((uint8_t)'A');
adrien3d 0:996eb84c895e 104 device.putc((uint8_t)'T');
adrien3d 0:996eb84c895e 105 device.putc((uint8_t)'$');
adrien3d 0:996eb84c895e 106 device.putc((uint8_t)'S');
adrien3d 0:996eb84c895e 107 device.putc((uint8_t)'F');
adrien3d 0:996eb84c895e 108 device.putc((uint8_t)'=');
adrien3d 0:996eb84c895e 109
adrien3d 0:996eb84c895e 110 for(uint8_t i = 0; i < len; ++i) {
adrien3d 0:996eb84c895e 111 device.putc(bytes[i]);
adrien3d 0:996eb84c895e 112 }
adrien3d 0:996eb84c895e 113 device.putc('\r');
adrien3d 0:996eb84c895e 114
adrien3d 0:996eb84c895e 115 uint8_t ok = _nextReturn();
adrien3d 0:996eb84c895e 116 if(ok == OK) {
adrien3d 0:996eb84c895e 117 _nextReturn(); //SENT
adrien3d 0:996eb84c895e 118 return true;
adrien3d 0:996eb84c895e 119 }
adrien3d 0:996eb84c895e 120 return false;
adrien3d 0:996eb84c895e 121 }
adrien3d 0:996eb84c895e 122
adrien3d 0:996eb84c895e 123 unsigned long Sigfox_::getID() {
adrien3d 0:996eb84c895e 124 unsigned long id = 0;
adrien3d 0:996eb84c895e 125 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 126 device.putc((uint8_t)'\0');
adrien3d 0:996eb84c895e 127 device.putc((uint8_t)'A');
adrien3d 0:996eb84c895e 128 device.putc((uint8_t)'T');
adrien3d 0:996eb84c895e 129 device.putc((uint8_t)'$');
adrien3d 0:996eb84c895e 130 device.putc((uint8_t)'I');
adrien3d 0:996eb84c895e 131 device.putc((uint8_t)'=');
adrien3d 0:996eb84c895e 132 device.putc((uint8_t)'1');
adrien3d 0:996eb84c895e 133 device.putc((uint8_t)'0');
adrien3d 0:996eb84c895e 134 device.putc('\r');
adrien3d 0:996eb84c895e 135
adrien3d 0:996eb84c895e 136 uint8_t response[8] = {0};
adrien3d 0:996eb84c895e 137 uint8_t i = 0;
adrien3d 0:996eb84c895e 138 for (i = 0; i<7;++i) {
adrien3d 0:996eb84c895e 139 response[i] = device.getc();
adrien3d 0:996eb84c895e 140 ++i;
adrien3d 0:996eb84c895e 141 }
adrien3d 0:996eb84c895e 142 /*uint8_t response[8] = {0};
adrien3d 0:996eb84c895e 143 while(device.getc() != '\0') {
adrien3d 0:996eb84c895e 144 response[i] = device.getc();
adrien3d 0:996eb84c895e 145 while(!device.readable());
adrien3d 0:996eb84c895e 146 ++i;
adrien3d 0:996eb84c895e 147 }*/
adrien3d 0:996eb84c895e 148 device.getc(); //';'
adrien3d 0:996eb84c895e 149 for(uint8_t j = 0; j < i; ++j) {
adrien3d 0:996eb84c895e 150 id += response[j] << ((i-3-j) * 8);
adrien3d 0:996eb84c895e 151 }
adrien3d 0:996eb84c895e 152 return id;
adrien3d 0:996eb84c895e 153 }
adrien3d 0:996eb84c895e 154
adrien3d 0:996eb84c895e 155 unsigned long Sigfox_::getPAC() {
adrien3d 0:996eb84c895e 156 unsigned long id = 0;
adrien3d 0:996eb84c895e 157 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 158 device.putc((uint8_t)'\0');
adrien3d 0:996eb84c895e 159 device.putc((uint8_t)'A');
adrien3d 0:996eb84c895e 160 device.putc((uint8_t)'T');
adrien3d 0:996eb84c895e 161 device.putc((uint8_t)'$');
adrien3d 0:996eb84c895e 162 device.putc((uint8_t)'I');
adrien3d 0:996eb84c895e 163 device.putc((uint8_t)'=');
adrien3d 0:996eb84c895e 164 device.putc((uint8_t)'1');
adrien3d 0:996eb84c895e 165 device.putc((uint8_t)'1');
adrien3d 0:996eb84c895e 166 device.putc('\r');
adrien3d 0:996eb84c895e 167
adrien3d 0:996eb84c895e 168 uint8_t response[8] = {0};
adrien3d 0:996eb84c895e 169 uint8_t i = 0;
adrien3d 0:996eb84c895e 170 for (i = 0; i<7;++i) {
adrien3d 0:996eb84c895e 171 response[i] = device.getc();
adrien3d 0:996eb84c895e 172 ++i;
adrien3d 0:996eb84c895e 173 }
adrien3d 0:996eb84c895e 174
adrien3d 0:996eb84c895e 175 for(uint8_t j = 0; j < i; ++j) {
adrien3d 0:996eb84c895e 176 id += response[j] << ((i-3-j) * 16);
adrien3d 0:996eb84c895e 177 }
adrien3d 0:996eb84c895e 178 return true;
adrien3d 0:996eb84c895e 179 }
adrien3d 0:996eb84c895e 180
adrien3d 0:996eb84c895e 181 bool Sigfox_::setPowerMode(uint8_t power) {
adrien3d 0:996eb84c895e 182 Serial device(PIN_RX, PIN_TX);
adrien3d 0:996eb84c895e 183 device.putc((uint8_t)'\0');
adrien3d 0:996eb84c895e 184 device.putc((uint8_t)'A');
adrien3d 0:996eb84c895e 185 device.putc((uint8_t)'T');
adrien3d 0:996eb84c895e 186 device.putc((uint8_t)'$');
adrien3d 0:996eb84c895e 187 device.putc((uint8_t)'I');
adrien3d 0:996eb84c895e 188 device.putc((uint8_t)'=');
adrien3d 0:996eb84c895e 189 device.putc(power);
adrien3d 0:996eb84c895e 190 device.putc('\r');
adrien3d 0:996eb84c895e 191
adrien3d 0:996eb84c895e 192 //return _nextReturn() == OK;
adrien3d 0:996eb84c895e 193 return 1;
adrien3d 0:996eb84c895e 194 }
adrien3d 0:996eb84c895e 195
adrien3d 0:996eb84c895e 196 //------------------------------------------------------------------------------------------------//
adrien3d 0:996eb84c895e 197 //Destructors
adrien3d 0:996eb84c895e 198
adrien3d 0:996eb84c895e 199 /*sigfox::~sf() {
adrien3d 0:996eb84c895e 200 }*/