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