yann cardaillac
/
Akene-sigfox-projet-iot-tracker
Project IOT Polytech Paris UPMC - XTRM TRACKER
Fork of Akene by
Akene.cpp@2:73eab7323221, 2017-02-08 (annotated)
- Committer:
- ycardaillac
- Date:
- Wed Feb 08 16:46:05 2017 +0000
- Revision:
- 2:73eab7323221
- Parent:
- 0:48c5b5c0395e
???
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Remitte | 0:48c5b5c0395e | 1 | /* |
Remitte | 0:48c5b5c0395e | 2 | This file is a Snootlab's source code file's translation to use Akene on mbed boards instead of Arduino ones. |
Remitte | 0:48c5b5c0395e | 3 | The functions Akene.begin() and Akene.send() work perfectly, but no real checkup has been made for the other functions. |
Remitte | 0:48c5b5c0395e | 4 | Feel free to implement what you want to make your device work on mbed. |
Remitte | 0:48c5b5c0395e | 5 | |
Remitte | 0:48c5b5c0395e | 6 | This translation is due to a Polytech Paris UPMC project led by Rémi Jourdain with the help of Clément Maciejewski. |
Remitte | 0:48c5b5c0395e | 7 | |
Remitte | 0:48c5b5c0395e | 8 | Visit <http://snootlab.com> |
Remitte | 0:48c5b5c0395e | 9 | Copyright (C) 2013-2015 Snootlab. All rights reserved. |
Remitte | 0:48c5b5c0395e | 10 | |
Remitte | 0:48c5b5c0395e | 11 | Akene is free software: you can redistribute it and/or modify |
Remitte | 0:48c5b5c0395e | 12 | it under the terms of the GNU General Public License as published by |
Remitte | 0:48c5b5c0395e | 13 | the Free Software Foundation, either version 3 of the License, or |
Remitte | 0:48c5b5c0395e | 14 | (at your option) any later version. |
Remitte | 0:48c5b5c0395e | 15 | |
Remitte | 0:48c5b5c0395e | 16 | Akene is distributed in the hope that it will be useful, |
Remitte | 0:48c5b5c0395e | 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
Remitte | 0:48c5b5c0395e | 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Remitte | 0:48c5b5c0395e | 19 | GNU General Public License for more details. |
Remitte | 0:48c5b5c0395e | 20 | |
Remitte | 0:48c5b5c0395e | 21 | You should have received a copy of the GNU General Public License |
Remitte | 0:48c5b5c0395e | 22 | along with Akene. If not, see <http://www.gnu.org/licenses/>. |
Remitte | 0:48c5b5c0395e | 23 | */ |
Remitte | 0:48c5b5c0395e | 24 | |
Remitte | 0:48c5b5c0395e | 25 | |
Remitte | 0:48c5b5c0395e | 26 | #include "mbed.h" |
Remitte | 0:48c5b5c0395e | 27 | #include "Akene.h" |
Remitte | 0:48c5b5c0395e | 28 | |
Remitte | 0:48c5b5c0395e | 29 | Akene_ Akene; |
Remitte | 0:48c5b5c0395e | 30 | |
Remitte | 0:48c5b5c0395e | 31 | Akene_::Akene_() : |
ycardaillac | 2:73eab7323221 | 32 | //_serial(P4_28, P4_29) { //P4_25 = Tx P4_29 = Rx |
ycardaillac | 2:73eab7323221 | 33 | _serial(D5, D4) { |
Remitte | 0:48c5b5c0395e | 34 | //Since _lastSend is unsigned, this is infinity |
Remitte | 0:48c5b5c0395e | 35 | _lastSend = -1; |
Remitte | 0:48c5b5c0395e | 36 | _T.start(); |
Remitte | 0:48c5b5c0395e | 37 | } |
Remitte | 0:48c5b5c0395e | 38 | |
Remitte | 0:48c5b5c0395e | 39 | Akene_::~Akene_() { |
Remitte | 0:48c5b5c0395e | 40 | _T.stop(); |
Remitte | 0:48c5b5c0395e | 41 | } |
Remitte | 0:48c5b5c0395e | 42 | |
Remitte | 0:48c5b5c0395e | 43 | void Akene_::begin() { |
Remitte | 0:48c5b5c0395e | 44 | _serial.baud(9600); |
Remitte | 0:48c5b5c0395e | 45 | //Remove un-ended commands from TST's buffer |
Remitte | 0:48c5b5c0395e | 46 | _serial.putc((uint8_t)'\0'); |
Remitte | 0:48c5b5c0395e | 47 | _serial.putc((uint8_t)';'); |
Remitte | 0:48c5b5c0395e | 48 | //Wait for the "KO;" |
Remitte | 0:48c5b5c0395e | 49 | while(_serial.readable() == 0); |
Remitte | 0:48c5b5c0395e | 50 | _serial.getc(); //'K' |
Remitte | 0:48c5b5c0395e | 51 | while(_serial.readable() == 0); |
Remitte | 0:48c5b5c0395e | 52 | _serial.getc(); //'O' |
Remitte | 0:48c5b5c0395e | 53 | while(_serial.readable() == 0); |
Remitte | 0:48c5b5c0395e | 54 | _serial.getc(); //';' |
Remitte | 0:48c5b5c0395e | 55 | } |
Remitte | 0:48c5b5c0395e | 56 | |
Remitte | 0:48c5b5c0395e | 57 | |
Remitte | 0:48c5b5c0395e | 58 | bool Akene_::isReady() { |
Remitte | 0:48c5b5c0395e | 59 | |
Remitte | 0:48c5b5c0395e | 60 | // IMPORTANT WARNING. PLEASE READ BEFORE MODIFYING THE CODE |
Remitte | 0:48c5b5c0395e | 61 | // |
Remitte | 0:48c5b5c0395e | 62 | // The Sigfox network operates on public frequencies. To comply with |
Remitte | 0:48c5b5c0395e | 63 | // radio regulation, it can send radio data a maximum of 1% of the time |
Remitte | 0:48c5b5c0395e | 64 | // to leave room to other devices using the same frequencies. |
Remitte | 0:48c5b5c0395e | 65 | // |
Remitte | 0:48c5b5c0395e | 66 | // Sending a message takes about 6 seconds (it's sent 3 times for |
Remitte | 0:48c5b5c0395e | 67 | // redundancy purposes), meaning the interval between messages should |
Remitte | 0:48c5b5c0395e | 68 | // be 10 minutes. |
Remitte | 0:48c5b5c0395e | 69 | // |
Remitte | 0:48c5b5c0395e | 70 | // Also make sure your send rate complies with the restrictions set |
Remitte | 0:48c5b5c0395e | 71 | // by the particular subscription contract you have with your Sigfox |
Remitte | 0:48c5b5c0395e | 72 | // network operator. |
Remitte | 0:48c5b5c0395e | 73 | // |
Remitte | 0:48c5b5c0395e | 74 | // FAILING TO COMPLY WITH THESE CONSTRAINTS MAY CAUSE YOUR MODEM |
Remitte | 0:48c5b5c0395e | 75 | // TO BE BLOCKED BY YOUR SIFGOX NETWORK OPERATOR. |
Remitte | 0:48c5b5c0395e | 76 | // |
Remitte | 0:48c5b5c0395e | 77 | // You've been warned! |
Remitte | 0:48c5b5c0395e | 78 | |
Remitte | 0:48c5b5c0395e | 79 | unsigned long currentTime = _T.read_ms(); |
Remitte | 0:48c5b5c0395e | 80 | if(currentTime >= _lastSend && (currentTime - _lastSend) <= 600000) { |
Remitte | 0:48c5b5c0395e | 81 | return false; |
Remitte | 0:48c5b5c0395e | 82 | } |
Remitte | 0:48c5b5c0395e | 83 | |
Remitte | 0:48c5b5c0395e | 84 | // Time is ok, ask the modem's status |
Remitte | 0:48c5b5c0395e | 85 | _serial.putc((uint8_t)'\0'); |
Remitte | 0:48c5b5c0395e | 86 | _serial.putc((uint8_t)'S'); |
Remitte | 0:48c5b5c0395e | 87 | _serial.putc((uint8_t)'F'); |
Remitte | 0:48c5b5c0395e | 88 | _serial.putc((uint8_t)'P'); |
Remitte | 0:48c5b5c0395e | 89 | _serial.putc((uint8_t)';'); |
Remitte | 0:48c5b5c0395e | 90 | |
Remitte | 0:48c5b5c0395e | 91 | return _nextReturn() == OK; |
Remitte | 0:48c5b5c0395e | 92 | } |
Remitte | 0:48c5b5c0395e | 93 | |
Remitte | 0:48c5b5c0395e | 94 | bool Akene_::send(const void* data, uint8_t len) { |
Remitte | 0:48c5b5c0395e | 95 | uint8_t* bytes = (uint8_t*)data; |
Remitte | 0:48c5b5c0395e | 96 | |
Remitte | 0:48c5b5c0395e | 97 | if(!isReady()) { |
Remitte | 0:48c5b5c0395e | 98 | return false; |
Remitte | 0:48c5b5c0395e | 99 | } |
Remitte | 0:48c5b5c0395e | 100 | |
Remitte | 0:48c5b5c0395e | 101 | // See comment in isReady() |
Remitte | 0:48c5b5c0395e | 102 | _lastSend = _T.read_ms(); |
Remitte | 0:48c5b5c0395e | 103 | |
Remitte | 0:48c5b5c0395e | 104 | _serial.putc((uint8_t)'\0'); |
Remitte | 0:48c5b5c0395e | 105 | _serial.putc((uint8_t)'S'); |
Remitte | 0:48c5b5c0395e | 106 | _serial.putc((uint8_t)'F'); |
Remitte | 0:48c5b5c0395e | 107 | _serial.putc((uint8_t)'M'); |
Remitte | 0:48c5b5c0395e | 108 | _serial.putc(len); |
Remitte | 0:48c5b5c0395e | 109 | for(uint8_t i = 0; i < len; ++i) { |
Remitte | 0:48c5b5c0395e | 110 | _serial.putc(bytes[i]); |
Remitte | 0:48c5b5c0395e | 111 | } |
Remitte | 0:48c5b5c0395e | 112 | _serial.putc(';'); |
Remitte | 0:48c5b5c0395e | 113 | |
Remitte | 0:48c5b5c0395e | 114 | uint8_t ok = _nextReturn(); |
Remitte | 0:48c5b5c0395e | 115 | if(ok == OK) { |
Remitte | 0:48c5b5c0395e | 116 | _nextReturn(); //SENT |
Remitte | 0:48c5b5c0395e | 117 | return true; |
Remitte | 0:48c5b5c0395e | 118 | } |
Remitte | 0:48c5b5c0395e | 119 | return false; |
Remitte | 0:48c5b5c0395e | 120 | } |
Remitte | 0:48c5b5c0395e | 121 | |
Remitte | 0:48c5b5c0395e | 122 | uint8_t Akene_::getRev() { |
Remitte | 0:48c5b5c0395e | 123 | char c; |
Remitte | 0:48c5b5c0395e | 124 | _serial.putc((uint8_t)'\0'); |
Remitte | 0:48c5b5c0395e | 125 | _serial.putc((uint8_t)'S'); |
Remitte | 0:48c5b5c0395e | 126 | _serial.putc((uint8_t)'F'); |
Remitte | 0:48c5b5c0395e | 127 | _serial.putc((uint8_t)'v'); |
Remitte | 0:48c5b5c0395e | 128 | _serial.putc((uint8_t)';'); |
Remitte | 0:48c5b5c0395e | 129 | |
Remitte | 0:48c5b5c0395e | 130 | while(_serial.readable() == 0); |
Remitte | 0:48c5b5c0395e | 131 | c = _serial.getc(); //'K' or something else |
Remitte | 0:48c5b5c0395e | 132 | if(c == 'K'){ |
Remitte | 0:48c5b5c0395e | 133 | while(_serial.readable() == 0); |
Remitte | 0:48c5b5c0395e | 134 | _serial.getc(); //'O' |
Remitte | 0:48c5b5c0395e | 135 | while(_serial.readable() == 0); |
Remitte | 0:48c5b5c0395e | 136 | _serial.getc(); //';' |
Remitte | 0:48c5b5c0395e | 137 | return 0; |
Remitte | 0:48c5b5c0395e | 138 | } |
Remitte | 0:48c5b5c0395e | 139 | else { |
Remitte | 0:48c5b5c0395e | 140 | uint8_t rev = 10 * c - '0'; |
Remitte | 0:48c5b5c0395e | 141 | while(_serial.readable() == 0); |
Remitte | 0:48c5b5c0395e | 142 | rev += (_serial.getc() - '0'); |
Remitte | 0:48c5b5c0395e | 143 | while(_serial.readable() == 0); |
Remitte | 0:48c5b5c0395e | 144 | _serial.getc(); //'O' |
Remitte | 0:48c5b5c0395e | 145 | while(_serial.readable() == 0); |
Remitte | 0:48c5b5c0395e | 146 | _serial.getc(); //'K' |
Remitte | 0:48c5b5c0395e | 147 | while(_serial.readable() == 0); |
Remitte | 0:48c5b5c0395e | 148 | _serial.getc(); //';' |
Remitte | 0:48c5b5c0395e | 149 | return rev; |
Remitte | 0:48c5b5c0395e | 150 | } |
Remitte | 0:48c5b5c0395e | 151 | } |
Remitte | 0:48c5b5c0395e | 152 | |
Remitte | 0:48c5b5c0395e | 153 | unsigned long Akene_::getID() { |
Remitte | 0:48c5b5c0395e | 154 | _serial.putc((uint8_t)'\0'); |
Remitte | 0:48c5b5c0395e | 155 | _serial.putc((uint8_t)'S'); |
Remitte | 0:48c5b5c0395e | 156 | _serial.putc((uint8_t)'F'); |
Remitte | 0:48c5b5c0395e | 157 | _serial.putc((uint8_t)'I'); |
Remitte | 0:48c5b5c0395e | 158 | _serial.putc((uint8_t)'D'); |
Remitte | 0:48c5b5c0395e | 159 | _serial.putc((uint8_t)';'); |
Remitte | 0:48c5b5c0395e | 160 | |
Remitte | 0:48c5b5c0395e | 161 | //Response is [byte1, byte2, ..., byteN, 'O', 'K'] |
Remitte | 0:48c5b5c0395e | 162 | uint8_t response[8] = {0}; |
Remitte | 0:48c5b5c0395e | 163 | uint8_t i = 0; |
Remitte | 0:48c5b5c0395e | 164 | uint8_t c; |
Remitte | 0:48c5b5c0395e | 165 | while(!_serial.readable()); |
Remitte | 0:48c5b5c0395e | 166 | c = _serial.getc(); |
Remitte | 0:48c5b5c0395e | 167 | while(c != ';') { |
Remitte | 0:48c5b5c0395e | 168 | response[i] = c; |
Remitte | 0:48c5b5c0395e | 169 | while(!_serial.readable()); |
Remitte | 0:48c5b5c0395e | 170 | ++i; |
Remitte | 0:48c5b5c0395e | 171 | c = _serial.getc(); |
Remitte | 0:48c5b5c0395e | 172 | } |
Remitte | 0:48c5b5c0395e | 173 | |
Remitte | 0:48c5b5c0395e | 174 | unsigned long id = 0; |
Remitte | 0:48c5b5c0395e | 175 | |
Remitte | 0:48c5b5c0395e | 176 | for(uint8_t j = 0; j < i-2; ++j) { |
Remitte | 0:48c5b5c0395e | 177 | id += response[j] << ((i-3-j) * 8); |
Remitte | 0:48c5b5c0395e | 178 | } |
Remitte | 0:48c5b5c0395e | 179 | |
Remitte | 0:48c5b5c0395e | 180 | return id; |
Remitte | 0:48c5b5c0395e | 181 | } |
Remitte | 0:48c5b5c0395e | 182 | //Power value: |
Remitte | 0:48c5b5c0395e | 183 | //0 -25 -30 dBm |
Remitte | 0:48c5b5c0395e | 184 | //1 0dBm |
Remitte | 0:48c5b5c0395e | 185 | //2 14dBm |
Remitte | 0:48c5b5c0395e | 186 | //3 16dBm |
Remitte | 0:48c5b5c0395e | 187 | //4 18dBm |
Remitte | 0:48c5b5c0395e | 188 | //5 Max (18-19dBm) |
Remitte | 0:48c5b5c0395e | 189 | bool Akene_::setPower(uint8_t power) { |
Remitte | 0:48c5b5c0395e | 190 | power = power % 6; //It's 0-5 |
Remitte | 0:48c5b5c0395e | 191 | _serial.putc((uint8_t)'\0'); |
Remitte | 0:48c5b5c0395e | 192 | _serial.putc((uint8_t)'S'); |
Remitte | 0:48c5b5c0395e | 193 | _serial.putc((uint8_t)'F'); |
Remitte | 0:48c5b5c0395e | 194 | _serial.putc((uint8_t)'G'); |
Remitte | 0:48c5b5c0395e | 195 | _serial.putc(power); |
Remitte | 0:48c5b5c0395e | 196 | _serial.putc((uint8_t)';'); |
Remitte | 0:48c5b5c0395e | 197 | |
Remitte | 0:48c5b5c0395e | 198 | return _nextReturn() == OK; |
Remitte | 0:48c5b5c0395e | 199 | } |
Remitte | 0:48c5b5c0395e | 200 | |
Remitte | 0:48c5b5c0395e | 201 | uint8_t Akene_::_nextReturn() { |
Remitte | 0:48c5b5c0395e | 202 | while(!_serial.readable()); |
Remitte | 0:48c5b5c0395e | 203 | char fstChar = _serial.getc(); |
Remitte | 0:48c5b5c0395e | 204 | while(_serial.getc() != ';'); |
Remitte | 0:48c5b5c0395e | 205 | return fstChar; |
Remitte | 0:48c5b5c0395e | 206 | } |