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