Echo example of class C devices receiving downlink and sending as next uplink
Dependencies: libmDot-mbed5 ISL29011
examples/src/class_c_example_copy.cpp@34:42343999ecc1, 2019-02-28 (annotated)
- Committer:
- tesla_xudong
- Date:
- Thu Feb 28 00:24:08 2019 +0000
- Revision:
- 34:42343999ecc1
- Parent:
- 33:ff681aa203f3
fdd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tesla_xudong | 33:ff681aa203f3 | 1 | #include "dot_util.h" |
tesla_xudong | 33:ff681aa203f3 | 2 | #include "RadioEvent.h" |
tesla_xudong | 34:42343999ecc1 | 3 | #include <string> |
tesla_xudong | 34:42343999ecc1 | 4 | #include <MTSText.h> |
tesla_xudong | 33:ff681aa203f3 | 5 | #if ACTIVE_EXAMPLE == CLASS_C_EXAMPLE |
tesla_xudong | 34:42343999ecc1 | 6 | #include <stdlib.h> |
tesla_xudong | 33:ff681aa203f3 | 7 | ///////////////////////////////////////////////////////////////////////////// |
tesla_xudong | 33:ff681aa203f3 | 8 | // -------------------- DOT LIBRARY REQUIRED ------------------------------// |
tesla_xudong | 33:ff681aa203f3 | 9 | // * Because these example programs can be used for both mDot and xDot // |
tesla_xudong | 33:ff681aa203f3 | 10 | // devices, the LoRa stack is not included. The libmDot library should // |
tesla_xudong | 33:ff681aa203f3 | 11 | // be imported if building for mDot devices. The libxDot library // |
tesla_xudong | 33:ff681aa203f3 | 12 | // should be imported if building for xDot devices. // |
tesla_xudong | 33:ff681aa203f3 | 13 | // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ // |
tesla_xudong | 33:ff681aa203f3 | 14 | // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ // |
tesla_xudong | 33:ff681aa203f3 | 15 | // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ // |
tesla_xudong | 33:ff681aa203f3 | 16 | // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ // |
tesla_xudong | 33:ff681aa203f3 | 17 | ///////////////////////////////////////////////////////////////////////////// |
tesla_xudong | 33:ff681aa203f3 | 18 | |
tesla_xudong | 33:ff681aa203f3 | 19 | ///////////////////////////////////////////////////////////// |
tesla_xudong | 33:ff681aa203f3 | 20 | // * these options must match the settings on your gateway // |
tesla_xudong | 33:ff681aa203f3 | 21 | // * edit their values to match your configuration // |
tesla_xudong | 33:ff681aa203f3 | 22 | // * frequency sub band is only relevant for the 915 bands // |
tesla_xudong | 33:ff681aa203f3 | 23 | // * either the network name and passphrase can be used or // |
tesla_xudong | 33:ff681aa203f3 | 24 | // the network ID (8 bytes) and KEY (16 bytes) // |
tesla_xudong | 33:ff681aa203f3 | 25 | ///////////////////////////////////////////////////////////// |
tesla_xudong | 33:ff681aa203f3 | 26 | static std::string network_name = "iotlab01"; |
tesla_xudong | 33:ff681aa203f3 | 27 | static std::string network_passphrase = "11223344"; |
tesla_xudong | 33:ff681aa203f3 | 28 | //static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 }; |
tesla_xudong | 33:ff681aa203f3 | 29 | //static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B }; |
tesla_xudong | 34:42343999ecc1 | 30 | |
tesla_xudong | 34:42343999ecc1 | 31 | |
tesla_xudong | 34:42343999ecc1 | 32 | static uint8_t network_address[] = {0x0a,0x00,0x00,0x1e}; |
tesla_xudong | 34:42343999ecc1 | 33 | //static uint8_t network_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 }; |
tesla_xudong | 34:42343999ecc1 | 34 | //static uint8_t data_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 }; |
tesla_xudong | 34:42343999ecc1 | 35 | |
tesla_xudong | 34:42343999ecc1 | 36 | static uint8_t network_session_key[] = {0x11, 0x22, 0x33, 0x44,0x55,0x66,0x77,0x80 }; |
tesla_xudong | 34:42343999ecc1 | 37 | static uint8_t data_session_key[] = { 0x11, 0x11, 0x22, 0x22, 0x33, 0x33, 0x44, 0x44, 0x11, 0x11, 0x22, 0x22, 0x33, 0x33, 0x44, 0x40 }; |
tesla_xudong | 34:42343999ecc1 | 38 | |
tesla_xudong | 34:42343999ecc1 | 39 | |
tesla_xudong | 34:42343999ecc1 | 40 | |
tesla_xudong | 34:42343999ecc1 | 41 | |
tesla_xudong | 33:ff681aa203f3 | 42 | static uint8_t frequency_sub_band = 4; |
tesla_xudong | 33:ff681aa203f3 | 43 | static lora::NetworkType network_type = lora::PUBLIC_LORAWAN; |
tesla_xudong | 33:ff681aa203f3 | 44 | static uint8_t join_delay = 6; |
tesla_xudong | 34:42343999ecc1 | 45 | static uint8_t ack = 1; |
tesla_xudong | 33:ff681aa203f3 | 46 | static bool adr = true; |
tesla_xudong | 33:ff681aa203f3 | 47 | |
tesla_xudong | 33:ff681aa203f3 | 48 | mDot* dot = NULL; |
tesla_xudong | 33:ff681aa203f3 | 49 | lora::ChannelPlan* plan = NULL; |
tesla_xudong | 33:ff681aa203f3 | 50 | |
tesla_xudong | 33:ff681aa203f3 | 51 | Serial pc(USBTX, USBRX); |
tesla_xudong | 34:42343999ecc1 | 52 | Serial pi(PA_2,PA_3); |
tesla_xudong | 33:ff681aa203f3 | 53 | |
tesla_xudong | 34:42343999ecc1 | 54 | PwmOut pwm_L(PA_7); |
tesla_xudong | 34:42343999ecc1 | 55 | PwmOut pwm_R(PB_0); |
tesla_xudong | 34:42343999ecc1 | 56 | PwmOut pwm_G(PA_5); |
tesla_xudong | 34:42343999ecc1 | 57 | PwmOut pwm_B(PA_1); |
tesla_xudong | 34:42343999ecc1 | 58 | |
tesla_xudong | 34:42343999ecc1 | 59 | |
tesla_xudong | 34:42343999ecc1 | 60 | |
tesla_xudong | 34:42343999ecc1 | 61 | |
tesla_xudong | 34:42343999ecc1 | 62 | |
tesla_xudong | 33:ff681aa203f3 | 63 | #if defined(TARGET_XDOT_L151CC) |
tesla_xudong | 33:ff681aa203f3 | 64 | I2C i2c(I2C_SDA, I2C_SCL); |
tesla_xudong | 33:ff681aa203f3 | 65 | ISL29011 lux(i2c); |
tesla_xudong | 33:ff681aa203f3 | 66 | #else |
tesla_xudong | 33:ff681aa203f3 | 67 | AnalogIn lux(XBEE_AD0); |
tesla_xudong | 33:ff681aa203f3 | 68 | #endif |
tesla_xudong | 33:ff681aa203f3 | 69 | |
tesla_xudong | 33:ff681aa203f3 | 70 | std::string Last_RX; |
tesla_xudong | 33:ff681aa203f3 | 71 | |
tesla_xudong | 33:ff681aa203f3 | 72 | class MyEvent : public mDotEvent |
tesla_xudong | 33:ff681aa203f3 | 73 | { |
tesla_xudong | 33:ff681aa203f3 | 74 | |
tesla_xudong | 33:ff681aa203f3 | 75 | public: |
tesla_xudong | 33:ff681aa203f3 | 76 | MyEvent() {} |
tesla_xudong | 33:ff681aa203f3 | 77 | |
tesla_xudong | 33:ff681aa203f3 | 78 | virtual ~MyEvent() {} |
tesla_xudong | 33:ff681aa203f3 | 79 | |
tesla_xudong | 33:ff681aa203f3 | 80 | |
tesla_xudong | 33:ff681aa203f3 | 81 | virtual void MacEvent(LoRaMacEventFlags* flags, LoRaMacEventInfo* info) { |
tesla_xudong | 33:ff681aa203f3 | 82 | |
tesla_xudong | 33:ff681aa203f3 | 83 | if (mts::MTSLog::getLogLevel() == mts::MTSLog::TRACE_LEVEL) { |
tesla_xudong | 33:ff681aa203f3 | 84 | std::string msg = "OK"; |
tesla_xudong | 33:ff681aa203f3 | 85 | switch (info->Status) { |
tesla_xudong | 33:ff681aa203f3 | 86 | case LORAMAC_EVENT_INFO_STATUS_ERROR: |
tesla_xudong | 33:ff681aa203f3 | 87 | msg = "ERROR"; |
tesla_xudong | 33:ff681aa203f3 | 88 | break; |
tesla_xudong | 33:ff681aa203f3 | 89 | case LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT: |
tesla_xudong | 33:ff681aa203f3 | 90 | msg = "TX_TIMEOUT"; |
tesla_xudong | 33:ff681aa203f3 | 91 | break; |
tesla_xudong | 33:ff681aa203f3 | 92 | case LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT: |
tesla_xudong | 33:ff681aa203f3 | 93 | msg = "RX_TIMEOUT"; |
tesla_xudong | 33:ff681aa203f3 | 94 | break; |
tesla_xudong | 33:ff681aa203f3 | 95 | case LORAMAC_EVENT_INFO_STATUS_RX_ERROR: |
tesla_xudong | 33:ff681aa203f3 | 96 | msg = "RX_ERROR"; |
tesla_xudong | 33:ff681aa203f3 | 97 | break; |
tesla_xudong | 33:ff681aa203f3 | 98 | case LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL: |
tesla_xudong | 33:ff681aa203f3 | 99 | msg = "JOIN_FAIL"; |
tesla_xudong | 33:ff681aa203f3 | 100 | break; |
tesla_xudong | 33:ff681aa203f3 | 101 | case LORAMAC_EVENT_INFO_STATUS_DOWNLINK_FAIL: |
tesla_xudong | 33:ff681aa203f3 | 102 | msg = "DOWNLINK_FAIL"; |
tesla_xudong | 33:ff681aa203f3 | 103 | break; |
tesla_xudong | 33:ff681aa203f3 | 104 | case LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL: |
tesla_xudong | 33:ff681aa203f3 | 105 | msg = "ADDRESS_FAIL"; |
tesla_xudong | 33:ff681aa203f3 | 106 | break; |
tesla_xudong | 33:ff681aa203f3 | 107 | case LORAMAC_EVENT_INFO_STATUS_MIC_FAIL: |
tesla_xudong | 33:ff681aa203f3 | 108 | msg = "MIC_FAIL"; |
tesla_xudong | 33:ff681aa203f3 | 109 | break; |
tesla_xudong | 33:ff681aa203f3 | 110 | default: |
tesla_xudong | 33:ff681aa203f3 | 111 | break; |
tesla_xudong | 33:ff681aa203f3 | 112 | } |
tesla_xudong | 33:ff681aa203f3 | 113 | logTrace("Event: %s", msg.c_str()); |
tesla_xudong | 33:ff681aa203f3 | 114 | |
tesla_xudong | 33:ff681aa203f3 | 115 | logTrace("Flags Tx: %d Rx: %d RxData: %d RxSlot: %d LinkCheck: %d JoinAccept: %d", |
tesla_xudong | 33:ff681aa203f3 | 116 | flags->Bits.Tx, flags->Bits.Rx, flags->Bits.RxData, flags->Bits.RxSlot, flags->Bits.LinkCheck, flags->Bits.JoinAccept); |
tesla_xudong | 33:ff681aa203f3 | 117 | logTrace("Info: Status: %d ACK: %d Retries: %d TxDR: %d RxPort: %d RxSize: %d RSSI: %d SNR: %d Energy: %d Margin: %d Gateways: %d", |
tesla_xudong | 33:ff681aa203f3 | 118 | info->Status, info->TxAckReceived, info->TxNbRetries, info->TxDatarate, info->RxPort, info->RxBufferSize, |
tesla_xudong | 33:ff681aa203f3 | 119 | info->RxRssi, info->RxSnr, info->Energy, info->DemodMargin, info->NbGateways); |
tesla_xudong | 33:ff681aa203f3 | 120 | } |
tesla_xudong | 33:ff681aa203f3 | 121 | |
tesla_xudong | 33:ff681aa203f3 | 122 | if (flags->Bits.Rx) { |
tesla_xudong | 33:ff681aa203f3 | 123 | |
tesla_xudong | 33:ff681aa203f3 | 124 | logDebug("Rx %d bytes", info->RxBufferSize); |
tesla_xudong | 33:ff681aa203f3 | 125 | if (info->RxBufferSize > 0) { |
tesla_xudong | 33:ff681aa203f3 | 126 | // print RX data as hexadecimal |
tesla_xudong | 33:ff681aa203f3 | 127 | //printf("Rx data: %s\r\n", mts::Text::bin2hexString(info->RxBuffer, info->RxBufferSize).c_str()); |
tesla_xudong | 33:ff681aa203f3 | 128 | |
tesla_xudong | 33:ff681aa203f3 | 129 | // print RX data as string |
tesla_xudong | 33:ff681aa203f3 | 130 | std::string rx((const char*)info->RxBuffer, info->RxBufferSize); |
tesla_xudong | 33:ff681aa203f3 | 131 | //printf("Rx data: %s\r\n", rx.c_str()); |
tesla_xudong | 33:ff681aa203f3 | 132 | Last_RX = rx; |
tesla_xudong | 33:ff681aa203f3 | 133 | } |
tesla_xudong | 33:ff681aa203f3 | 134 | } |
tesla_xudong | 33:ff681aa203f3 | 135 | } |
tesla_xudong | 33:ff681aa203f3 | 136 | }; |
tesla_xudong | 33:ff681aa203f3 | 137 | |
tesla_xudong | 33:ff681aa203f3 | 138 | |
tesla_xudong | 33:ff681aa203f3 | 139 | char* vector2string(std::vector<uint8_t> var) |
tesla_xudong | 33:ff681aa203f3 | 140 | { |
tesla_xudong | 33:ff681aa203f3 | 141 | char* test = new char[var.size()]; |
tesla_xudong | 33:ff681aa203f3 | 142 | |
tesla_xudong | 33:ff681aa203f3 | 143 | std::copy(var.begin(),var.end(),test); |
tesla_xudong | 33:ff681aa203f3 | 144 | |
tesla_xudong | 33:ff681aa203f3 | 145 | return test; |
tesla_xudong | 33:ff681aa203f3 | 146 | } |
tesla_xudong | 33:ff681aa203f3 | 147 | |
tesla_xudong | 33:ff681aa203f3 | 148 | |
tesla_xudong | 33:ff681aa203f3 | 149 | |
tesla_xudong | 33:ff681aa203f3 | 150 | void send_string(std::string data,mDot *dot) |
tesla_xudong | 33:ff681aa203f3 | 151 | { |
tesla_xudong | 33:ff681aa203f3 | 152 | int32_t ret; |
tesla_xudong | 33:ff681aa203f3 | 153 | std::vector<uint8_t> sendData; |
tesla_xudong | 33:ff681aa203f3 | 154 | int i; |
tesla_xudong | 33:ff681aa203f3 | 155 | |
tesla_xudong | 33:ff681aa203f3 | 156 | for(i=0; i<data.size(); i++ ) |
tesla_xudong | 33:ff681aa203f3 | 157 | sendData.push_back( data[i] ); |
tesla_xudong | 33:ff681aa203f3 | 158 | |
tesla_xudong | 33:ff681aa203f3 | 159 | |
tesla_xudong | 33:ff681aa203f3 | 160 | ret = dot->send(sendData); |
tesla_xudong | 33:ff681aa203f3 | 161 | if (ret != mDot::MDOT_OK) { |
tesla_xudong | 33:ff681aa203f3 | 162 | logError("failed to send data to "); |
tesla_xudong | 33:ff681aa203f3 | 163 | } else { |
tesla_xudong | 33:ff681aa203f3 | 164 | logInfo("successfully sent data to "); |
tesla_xudong | 33:ff681aa203f3 | 165 | } |
tesla_xudong | 33:ff681aa203f3 | 166 | |
tesla_xudong | 33:ff681aa203f3 | 167 | } |
tesla_xudong | 34:42343999ecc1 | 168 | |
tesla_xudong | 33:ff681aa203f3 | 169 | |
tesla_xudong | 33:ff681aa203f3 | 170 | std::string get_buffer; |
tesla_xudong | 33:ff681aa203f3 | 171 | |
tesla_xudong | 34:42343999ecc1 | 172 | //void Uart_interrupt(){ |
tesla_xudong | 34:42343999ecc1 | 173 | // |
tesla_xudong | 34:42343999ecc1 | 174 | // char get_char; |
tesla_xudong | 34:42343999ecc1 | 175 | // while(!pc.readable()); |
tesla_xudong | 34:42343999ecc1 | 176 | // |
tesla_xudong | 34:42343999ecc1 | 177 | // get_char=pc.getc(); |
tesla_xudong | 34:42343999ecc1 | 178 | // if (get_char!='-') |
tesla_xudong | 34:42343999ecc1 | 179 | // { |
tesla_xudong | 34:42343999ecc1 | 180 | // get_buffer=get_buffer+get_char; |
tesla_xudong | 34:42343999ecc1 | 181 | // } |
tesla_xudong | 34:42343999ecc1 | 182 | // else |
tesla_xudong | 34:42343999ecc1 | 183 | // { |
tesla_xudong | 34:42343999ecc1 | 184 | // //send_string(get_buffer,dot); |
tesla_xudong | 34:42343999ecc1 | 185 | // pi.printf("%s",get_buffer.c_str()); |
tesla_xudong | 34:42343999ecc1 | 186 | // pc.printf("Mdot received from PC: %s",get_buffer.c_str()); |
tesla_xudong | 34:42343999ecc1 | 187 | // send_string(get_buffer.c_str(),dot); |
tesla_xudong | 34:42343999ecc1 | 188 | // get_buffer.clear(); |
tesla_xudong | 34:42343999ecc1 | 189 | // } |
tesla_xudong | 34:42343999ecc1 | 190 | // |
tesla_xudong | 34:42343999ecc1 | 191 | // } |
tesla_xudong | 34:42343999ecc1 | 192 | |
tesla_xudong | 34:42343999ecc1 | 193 | |
tesla_xudong | 34:42343999ecc1 | 194 | void Uart_interrupt_pi(){ |
tesla_xudong | 33:ff681aa203f3 | 195 | |
tesla_xudong | 33:ff681aa203f3 | 196 | char get_char; |
tesla_xudong | 34:42343999ecc1 | 197 | while(!pi.readable()); |
tesla_xudong | 33:ff681aa203f3 | 198 | |
tesla_xudong | 34:42343999ecc1 | 199 | get_char=pi.getc(); |
tesla_xudong | 34:42343999ecc1 | 200 | pc.putc(get_char); |
tesla_xudong | 34:42343999ecc1 | 201 | // send_char(get_char,dot); |
tesla_xudong | 33:ff681aa203f3 | 202 | } |
tesla_xudong | 34:42343999ecc1 | 203 | |
tesla_xudong | 34:42343999ecc1 | 204 | |
tesla_xudong | 33:ff681aa203f3 | 205 | |
tesla_xudong | 33:ff681aa203f3 | 206 | int main() { |
tesla_xudong | 33:ff681aa203f3 | 207 | |
tesla_xudong | 34:42343999ecc1 | 208 | |
tesla_xudong | 34:42343999ecc1 | 209 | pwm_L.period_ms(1); |
tesla_xudong | 34:42343999ecc1 | 210 | pwm_R.period_ms(1); |
tesla_xudong | 34:42343999ecc1 | 211 | pwm_G.period_ms(1); |
tesla_xudong | 34:42343999ecc1 | 212 | pwm_B.period_ms(1); |
tesla_xudong | 34:42343999ecc1 | 213 | |
tesla_xudong | 34:42343999ecc1 | 214 | pc.baud(9600); |
tesla_xudong | 34:42343999ecc1 | 215 | //pc.attach(&Uart_interrupt, SerialBase::RxIrq); |
tesla_xudong | 34:42343999ecc1 | 216 | |
tesla_xudong | 34:42343999ecc1 | 217 | pi.baud(9600); |
tesla_xudong | 34:42343999ecc1 | 218 | pi.attach(&Uart_interrupt_pi, SerialBase::RxIrq); |
tesla_xudong | 33:ff681aa203f3 | 219 | // pc.attach(&Uart_send_interrupt, SerialBase::TxIrq); |
tesla_xudong | 33:ff681aa203f3 | 220 | #if defined(TARGET_XDOT_L151CC) |
tesla_xudong | 33:ff681aa203f3 | 221 | i2c.frequency(400000); |
tesla_xudong | 33:ff681aa203f3 | 222 | #endif |
tesla_xudong | 33:ff681aa203f3 | 223 | |
tesla_xudong | 33:ff681aa203f3 | 224 | // Custom event handler for automatically displaying RX data |
tesla_xudong | 33:ff681aa203f3 | 225 | //RadioEvent events; |
tesla_xudong | 33:ff681aa203f3 | 226 | MyEvent events; |
tesla_xudong | 33:ff681aa203f3 | 227 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
tesla_xudong | 33:ff681aa203f3 | 228 | |
tesla_xudong | 33:ff681aa203f3 | 229 | #if CHANNEL_PLAN == CP_US915 |
tesla_xudong | 33:ff681aa203f3 | 230 | plan = new lora::ChannelPlan_US915(); |
tesla_xudong | 33:ff681aa203f3 | 231 | #elif CHANNEL_PLAN == CP_AU915 |
tesla_xudong | 33:ff681aa203f3 | 232 | plan = new lora::ChannelPlan_AU915(); |
tesla_xudong | 33:ff681aa203f3 | 233 | #elif CHANNEL_PLAN == CP_EU868 |
tesla_xudong | 33:ff681aa203f3 | 234 | plan = new lora::ChannelPlan_EU868(); |
tesla_xudong | 33:ff681aa203f3 | 235 | #elif CHANNEL_PLAN == CP_KR920 |
tesla_xudong | 33:ff681aa203f3 | 236 | plan = new lora::ChannelPlan_KR920(); |
tesla_xudong | 33:ff681aa203f3 | 237 | #elif CHANNEL_PLAN == CP_AS923 |
tesla_xudong | 33:ff681aa203f3 | 238 | plan = new lora::ChannelPlan_AS923(); |
tesla_xudong | 33:ff681aa203f3 | 239 | #elif CHANNEL_PLAN == CP_AS923_JAPAN |
tesla_xudong | 33:ff681aa203f3 | 240 | plan = new lora::ChannelPlan_AS923_Japan(); |
tesla_xudong | 33:ff681aa203f3 | 241 | #elif CHANNEL_PLAN == CP_IN865 |
tesla_xudong | 33:ff681aa203f3 | 242 | plan = new lora::ChannelPlan_IN865(); |
tesla_xudong | 33:ff681aa203f3 | 243 | #endif |
tesla_xudong | 33:ff681aa203f3 | 244 | assert(plan); |
tesla_xudong | 33:ff681aa203f3 | 245 | |
tesla_xudong | 33:ff681aa203f3 | 246 | dot = mDot::getInstance(plan); |
tesla_xudong | 33:ff681aa203f3 | 247 | assert(dot); |
tesla_xudong | 33:ff681aa203f3 | 248 | |
tesla_xudong | 33:ff681aa203f3 | 249 | logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); |
tesla_xudong | 33:ff681aa203f3 | 250 | |
tesla_xudong | 33:ff681aa203f3 | 251 | // start from a well-known state |
tesla_xudong | 33:ff681aa203f3 | 252 | logInfo("defaulting Dot configuration"); |
tesla_xudong | 33:ff681aa203f3 | 253 | dot->resetConfig(); |
tesla_xudong | 33:ff681aa203f3 | 254 | dot->resetNetworkSession(); |
tesla_xudong | 33:ff681aa203f3 | 255 | |
tesla_xudong | 33:ff681aa203f3 | 256 | // make sure library logging is turned on |
tesla_xudong | 33:ff681aa203f3 | 257 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
tesla_xudong | 33:ff681aa203f3 | 258 | |
tesla_xudong | 33:ff681aa203f3 | 259 | // attach the custom events handler |
tesla_xudong | 33:ff681aa203f3 | 260 | dot->setEvents(&events); |
tesla_xudong | 33:ff681aa203f3 | 261 | |
tesla_xudong | 33:ff681aa203f3 | 262 | // update configuration if necessary |
tesla_xudong | 33:ff681aa203f3 | 263 | if (dot->getJoinMode() != mDot::OTA) { |
tesla_xudong | 33:ff681aa203f3 | 264 | logInfo("changing network join mode to OTA"); |
tesla_xudong | 33:ff681aa203f3 | 265 | if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) { |
tesla_xudong | 33:ff681aa203f3 | 266 | logError("failed to set network join mode to OTA"); |
tesla_xudong | 33:ff681aa203f3 | 267 | } |
tesla_xudong | 33:ff681aa203f3 | 268 | } |
tesla_xudong | 33:ff681aa203f3 | 269 | // in OTA and AUTO_OTA join modes, the credentials can be passed to the library as a name and passphrase or an ID and KEY |
tesla_xudong | 33:ff681aa203f3 | 270 | // only one method or the other should be used! |
tesla_xudong | 33:ff681aa203f3 | 271 | // network ID = crc64(network name) |
tesla_xudong | 33:ff681aa203f3 | 272 | // network KEY = cmac(network passphrase) |
tesla_xudong | 33:ff681aa203f3 | 273 | update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, network_type, ack); |
tesla_xudong | 33:ff681aa203f3 | 274 | //update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack); |
tesla_xudong | 34:42343999ecc1 | 275 | |
tesla_xudong | 34:42343999ecc1 | 276 | } |
tesla_xudong | 34:42343999ecc1 | 277 | // in MANUAL join mode there is no join request/response transaction |
tesla_xudong | 34:42343999ecc1 | 278 | // as long as the Dot is configured correctly and provisioned correctly on the gateway, it should be able to communicate |
tesla_xudong | 34:42343999ecc1 | 279 | // network address - 4 bytes (00000001 - FFFFFFFE) |
tesla_xudong | 34:42343999ecc1 | 280 | // network session key - 16 bytes |
tesla_xudong | 34:42343999ecc1 | 281 | // data session key - 16 bytes |
tesla_xudong | 34:42343999ecc1 | 282 | // to provision your Dot with a Conduit gateway, follow the following steps |
tesla_xudong | 34:42343999ecc1 | 283 | // * ssh into the Conduit |
tesla_xudong | 34:42343999ecc1 | 284 | // * provision the Dot using the lora-query application: http://www.multitech.net/developer/software/lora/lora-network-server/ |
tesla_xudong | 34:42343999ecc1 | 285 | // lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304 |
tesla_xudong | 34:42343999ecc1 | 286 | // * if you change the network address, network session key, or data session key, make sure you update them on the gateway |
tesla_xudong | 34:42343999ecc1 | 287 | // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation |
tesla_xudong | 34:42343999ecc1 | 288 | |
tesla_xudong | 34:42343999ecc1 | 289 | //update_manual_config(network_address, network_session_key, data_session_key, frequency_sub_band, network_type, ack); |
tesla_xudong | 34:42343999ecc1 | 290 | |
tesla_xudong | 34:42343999ecc1 | 291 | |
tesla_xudong | 33:ff681aa203f3 | 292 | // configure the Dot for class C operation |
tesla_xudong | 33:ff681aa203f3 | 293 | // the Dot must also be configured on the gateway for class C |
tesla_xudong | 33:ff681aa203f3 | 294 | // use the lora-query application to do this on a Conduit: http://www.multitech.net/developer/software/lora/lora-network-server/ |
tesla_xudong | 33:ff681aa203f3 | 295 | // to provision your Dot for class C operation with a 3rd party gateway, see the gateway or network provider documentation |
tesla_xudong | 33:ff681aa203f3 | 296 | logInfo("changing network mode to class C"); |
tesla_xudong | 33:ff681aa203f3 | 297 | if (dot->setClass("C") != mDot::MDOT_OK) { |
tesla_xudong | 33:ff681aa203f3 | 298 | logError("failed to set network mode to class C"); |
tesla_xudong | 33:ff681aa203f3 | 299 | } |
tesla_xudong | 33:ff681aa203f3 | 300 | |
tesla_xudong | 33:ff681aa203f3 | 301 | // enable or disable Adaptive Data Rate |
tesla_xudong | 33:ff681aa203f3 | 302 | dot->setAdr(adr); |
tesla_xudong | 33:ff681aa203f3 | 303 | |
tesla_xudong | 33:ff681aa203f3 | 304 | // Configure the join delay |
tesla_xudong | 33:ff681aa203f3 | 305 | dot->setJoinDelay(join_delay); |
tesla_xudong | 33:ff681aa203f3 | 306 | |
tesla_xudong | 33:ff681aa203f3 | 307 | // save changes to configuration |
tesla_xudong | 33:ff681aa203f3 | 308 | logInfo("saving configuration"); |
tesla_xudong | 33:ff681aa203f3 | 309 | if (!dot->saveConfig()) { |
tesla_xudong | 33:ff681aa203f3 | 310 | logError("failed to save configuration"); |
tesla_xudong | 33:ff681aa203f3 | 311 | } |
tesla_xudong | 33:ff681aa203f3 | 312 | |
tesla_xudong | 33:ff681aa203f3 | 313 | // display configuration |
tesla_xudong | 33:ff681aa203f3 | 314 | display_config(); |
tesla_xudong | 33:ff681aa203f3 | 315 | // uint32_t ret; |
tesla_xudong | 33:ff681aa203f3 | 316 | // uint16_t light; |
tesla_xudong | 33:ff681aa203f3 | 317 | std::string tx_str; |
tesla_xudong | 33:ff681aa203f3 | 318 | // uint8_t recv_from_uart; |
tesla_xudong | 33:ff681aa203f3 | 319 | |
tesla_xudong | 33:ff681aa203f3 | 320 | std::vector<uint8_t> tx_data; |
tesla_xudong | 33:ff681aa203f3 | 321 | if (!dot->getNetworkJoinStatus()) { |
tesla_xudong | 33:ff681aa203f3 | 322 | join_network(); |
tesla_xudong | 33:ff681aa203f3 | 323 | } |
tesla_xudong | 33:ff681aa203f3 | 324 | send_string("start!",dot); |
tesla_xudong | 34:42343999ecc1 | 325 | |
tesla_xudong | 33:ff681aa203f3 | 326 | while (true) { |
tesla_xudong | 33:ff681aa203f3 | 327 | |
tesla_xudong | 33:ff681aa203f3 | 328 | // join network if not joined |
tesla_xudong | 33:ff681aa203f3 | 329 | if (!dot->getNetworkJoinStatus()) { |
tesla_xudong | 33:ff681aa203f3 | 330 | join_network(); |
tesla_xudong | 33:ff681aa203f3 | 331 | } |
tesla_xudong | 33:ff681aa203f3 | 332 | |
tesla_xudong | 33:ff681aa203f3 | 333 | if (!Last_RX.empty()) { |
tesla_xudong | 33:ff681aa203f3 | 334 | |
tesla_xudong | 33:ff681aa203f3 | 335 | tx_str=Last_RX; |
tesla_xudong | 33:ff681aa203f3 | 336 | Last_RX.clear(); |
tesla_xudong | 33:ff681aa203f3 | 337 | |
tesla_xudong | 33:ff681aa203f3 | 338 | pc.printf("%s",tx_str.c_str()); |
tesla_xudong | 34:42343999ecc1 | 339 | //pi.printf("%s",tx_str.c_str()); |
tesla_xudong | 34:42343999ecc1 | 340 | //const char *t_json=tx_str.c_str(); |
tesla_xudong | 34:42343999ecc1 | 341 | //std::vector<std::string> t_json=split(tx_str,","); |
tesla_xudong | 34:42343999ecc1 | 342 | |
tesla_xudong | 33:ff681aa203f3 | 343 | send_string(tx_str.c_str(),dot); |
tesla_xudong | 34:42343999ecc1 | 344 | string tem; |
tesla_xudong | 34:42343999ecc1 | 345 | tem.assign(tx_str,1,2); |
tesla_xudong | 34:42343999ecc1 | 346 | double l=atoi(tem.c_str()); |
tesla_xudong | 34:42343999ecc1 | 347 | tem.assign(tx_str,3,2); |
tesla_xudong | 34:42343999ecc1 | 348 | double r=atoi(tem.c_str()); |
tesla_xudong | 34:42343999ecc1 | 349 | tem.assign(tx_str,5,2); |
tesla_xudong | 34:42343999ecc1 | 350 | double g=atoi(tem.c_str()); |
tesla_xudong | 34:42343999ecc1 | 351 | tem.assign(tx_str,7,2); |
tesla_xudong | 34:42343999ecc1 | 352 | double b=atoi(tem.c_str()); |
tesla_xudong | 34:42343999ecc1 | 353 | |
tesla_xudong | 34:42343999ecc1 | 354 | pwm_L=1-(l/100.0); |
tesla_xudong | 34:42343999ecc1 | 355 | pwm_R=r/100.0; |
tesla_xudong | 34:42343999ecc1 | 356 | pwm_G=g/100.0; |
tesla_xudong | 34:42343999ecc1 | 357 | pwm_B=b/100.0; |
tesla_xudong | 34:42343999ecc1 | 358 | pc.printf("%f,%f,%f,%f",l,r,g,b); |
tesla_xudong | 34:42343999ecc1 | 359 | |
tesla_xudong | 33:ff681aa203f3 | 360 | } |
tesla_xudong | 33:ff681aa203f3 | 361 | |
tesla_xudong | 33:ff681aa203f3 | 362 | |
tesla_xudong | 33:ff681aa203f3 | 363 | } |
tesla_xudong | 34:42343999ecc1 | 364 | |
tesla_xudong | 33:ff681aa203f3 | 365 | } |
tesla_xudong | 33:ff681aa203f3 | 366 | |
tesla_xudong | 33:ff681aa203f3 | 367 | #endif |
tesla_xudong | 33:ff681aa203f3 | 368 |