SafeDetect
/
tx_serial_from_pi
this works for reading in data from pi via usb
main.cpp@103:d8e01f94dd43, 2019-12-10 (annotated)
- Committer:
- candre97
- Date:
- Tue Dec 10 06:05:28 2019 +0000
- Revision:
- 103:d8e01f94dd43
- Parent:
- 102:630e8252eabf
- Child:
- 104:2b36fa37e411
all done, working XDOT code!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
candre97 | 102:630e8252eabf | 1 | #include "mbed.h" |
candre97 | 102:630e8252eabf | 2 | #include "dot_util.h" |
candre97 | 102:630e8252eabf | 3 | #include "RadioEvent.h" |
candre97 | 102:630e8252eabf | 4 | #include <stdlib.h> |
candre97 | 103:d8e01f94dd43 | 5 | #include <inttypes.h> |
candre97 | 103:d8e01f94dd43 | 6 | |
candre97 | 102:630e8252eabf | 7 | |
candre97 | 102:630e8252eabf | 8 | // setting must match settings on the gateway |
candre97 | 102:630e8252eabf | 9 | static std::string network_name = "LAleakers"; |
candre97 | 102:630e8252eabf | 10 | static std::string network_passphrase = "stephisbroke"; |
candre97 | 102:630e8252eabf | 11 | static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 }; |
candre97 | 102:630e8252eabf | 12 | static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B }; |
candre97 | 102:630e8252eabf | 13 | static uint8_t frequency_sub_band = 7; |
candre97 | 102:630e8252eabf | 14 | static lora::NetworkType network_type = lora::PUBLIC_LORAWAN; |
candre97 | 102:630e8252eabf | 15 | static uint8_t join_delay = 5; |
candre97 | 102:630e8252eabf | 16 | static uint8_t ack = 0; |
candre97 | 102:630e8252eabf | 17 | static bool adr = false; |
candre97 | 102:630e8252eabf | 18 | |
candre97 | 102:630e8252eabf | 19 | static bool deep_sleep = false; |
candre97 | 102:630e8252eabf | 20 | |
candre97 | 102:630e8252eabf | 21 | mDot* dot = NULL; |
candre97 | 102:630e8252eabf | 22 | lora::ChannelPlan* plan = new lora::ChannelPlan_US915(); |
candre97 | 102:630e8252eabf | 23 | |
candre97 | 102:630e8252eabf | 24 | Serial pc(USBTX, USBRX); |
candre97 | 102:630e8252eabf | 25 | |
candre97 | 102:630e8252eabf | 26 | I2C i2c(I2C_SDA, I2C_SCL); |
candre97 | 102:630e8252eabf | 27 | |
candre97 | 102:630e8252eabf | 28 | DigitalOut led(LED1); |
mbed_official | 82:abf1b1785bd7 | 29 | |
candre97 | 102:630e8252eabf | 30 | int main() { |
candre97 | 102:630e8252eabf | 31 | //pc.printf("Echoes back to the screen anything you type\n"); |
candre97 | 102:630e8252eabf | 32 | |
candre97 | 102:630e8252eabf | 33 | RadioEvent events; |
candre97 | 102:630e8252eabf | 34 | printf("HELLO!!!\n"); |
candre97 | 102:630e8252eabf | 35 | pc.printf("Hi from pc\n"); |
candre97 | 102:630e8252eabf | 36 | //pc.baud(115200); |
candre97 | 102:630e8252eabf | 37 | |
candre97 | 102:630e8252eabf | 38 | i2c.frequency(400000); |
candre97 | 102:630e8252eabf | 39 | |
candre97 | 103:d8e01f94dd43 | 40 | //mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
candre97 | 102:630e8252eabf | 41 | |
candre97 | 102:630e8252eabf | 42 | assert(plan); |
Jonathan Austin |
0:2757d7abb7d9 | 43 | |
candre97 | 102:630e8252eabf | 44 | dot = mDot::getInstance(plan); |
candre97 | 102:630e8252eabf | 45 | assert(dot); |
Jonathan Austin |
0:2757d7abb7d9 | 46 | |
candre97 | 102:630e8252eabf | 47 | // attach the custom events handler |
candre97 | 102:630e8252eabf | 48 | dot->setEvents(&events); |
candre97 | 102:630e8252eabf | 49 | |
candre97 | 102:630e8252eabf | 50 | printf("S"); |
candre97 | 102:630e8252eabf | 51 | |
candre97 | 102:630e8252eabf | 52 | if (!dot->getStandbyFlag()) { |
candre97 | 102:630e8252eabf | 53 | logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
candre97 | 102:630e8252eabf | 54 | |
candre97 | 102:630e8252eabf | 55 | // start from a well-known state |
candre97 | 102:630e8252eabf | 56 | logInfo("defaulting Dot configuration"); |
candre97 | 102:630e8252eabf | 57 | dot->resetConfig(); |
candre97 | 102:630e8252eabf | 58 | dot->resetNetworkSession(); |
candre97 | 102:630e8252eabf | 59 | |
candre97 | 102:630e8252eabf | 60 | // make sure library logging is turned on |
candre97 | 102:630e8252eabf | 61 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
mbed_official | 88:bea4f2daa48c | 62 | |
candre97 | 102:630e8252eabf | 63 | // update configuration if necessary |
candre97 | 102:630e8252eabf | 64 | if (dot->getJoinMode() != mDot::OTA) { |
candre97 | 102:630e8252eabf | 65 | logInfo("changing network join mode to OTA"); |
candre97 | 102:630e8252eabf | 66 | if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) { |
candre97 | 102:630e8252eabf | 67 | logError("failed to set network join mode to OTA"); |
candre97 | 102:630e8252eabf | 68 | } |
candre97 | 102:630e8252eabf | 69 | } |
candre97 | 102:630e8252eabf | 70 | // 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 |
candre97 | 102:630e8252eabf | 71 | // only one method or the other should be used! |
candre97 | 102:630e8252eabf | 72 | // network ID = crc64(network name) |
candre97 | 102:630e8252eabf | 73 | // network KEY = cmac(network passphrase) |
candre97 | 102:630e8252eabf | 74 | update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, network_type, ack); |
candre97 | 102:630e8252eabf | 75 | //update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack); |
candre97 | 102:630e8252eabf | 76 | |
candre97 | 102:630e8252eabf | 77 | // configure network link checks |
candre97 | 102:630e8252eabf | 78 | // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots |
candre97 | 102:630e8252eabf | 79 | // check the link every count packets |
candre97 | 102:630e8252eabf | 80 | // declare the Dot disconnected after threshold failed link checks |
candre97 | 102:630e8252eabf | 81 | // for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row |
candre97 | 102:630e8252eabf | 82 | update_network_link_check_config(3, 5); |
candre97 | 102:630e8252eabf | 83 | |
candre97 | 102:630e8252eabf | 84 | // enable or disable Adaptive Data Rate |
candre97 | 102:630e8252eabf | 85 | dot->setAdr(adr); |
candre97 | 102:630e8252eabf | 86 | |
candre97 | 102:630e8252eabf | 87 | // Configure the join delay |
candre97 | 102:630e8252eabf | 88 | dot->setJoinDelay(join_delay); |
mbed_official | 82:abf1b1785bd7 | 89 | |
candre97 | 102:630e8252eabf | 90 | // save changes to configuration |
candre97 | 102:630e8252eabf | 91 | logInfo("saving configuration"); |
candre97 | 102:630e8252eabf | 92 | if (!dot->saveConfig()) { |
candre97 | 102:630e8252eabf | 93 | logError("failed to save configuration"); |
candre97 | 102:630e8252eabf | 94 | } |
candre97 | 102:630e8252eabf | 95 | |
candre97 | 102:630e8252eabf | 96 | // display configuration |
candre97 | 102:630e8252eabf | 97 | display_config(); |
candre97 | 102:630e8252eabf | 98 | } else { |
candre97 | 102:630e8252eabf | 99 | // restore the saved session if the dot woke from deepsleep mode |
candre97 | 102:630e8252eabf | 100 | // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep |
candre97 | 102:630e8252eabf | 101 | logInfo("restoring network session from NVM"); |
candre97 | 102:630e8252eabf | 102 | dot->restoreNetworkSession(); |
candre97 | 102:630e8252eabf | 103 | } |
candre97 | 102:630e8252eabf | 104 | |
candre97 | 102:630e8252eabf | 105 | // join network if not joined |
candre97 | 102:630e8252eabf | 106 | if (!dot->getNetworkJoinStatus()) { |
candre97 | 102:630e8252eabf | 107 | join_network(); |
candre97 | 102:630e8252eabf | 108 | pc.printf("joining network\r"); |
candre97 | 102:630e8252eabf | 109 | } |
candre97 | 102:630e8252eabf | 110 | |
candre97 | 103:d8e01f94dd43 | 111 | printf("TX DR: %i\r", dot->getTxDataRate()); |
candre97 | 103:d8e01f94dd43 | 112 | |
candre97 | 103:d8e01f94dd43 | 113 | uint8_t max_dr = dot->getMaxDatarate(); |
candre97 | 103:d8e01f94dd43 | 114 | |
candre97 | 103:d8e01f94dd43 | 115 | printf("MAX DR: %i\r", max_dr); |
candre97 | 103:d8e01f94dd43 | 116 | |
candre97 | 103:d8e01f94dd43 | 117 | if (dot->setTxDataRate(max_dr) != mDot::MDOT_OK) { |
candre97 | 102:630e8252eabf | 118 | logError("failed to set TX datarate to DR1"); |
candre97 | 102:630e8252eabf | 119 | } |
candre97 | 102:630e8252eabf | 120 | |
candre97 | 103:d8e01f94dd43 | 121 | pc.printf("TX DR: %i\r", dot->getTxDataRate()); |
candre97 | 103:d8e01f94dd43 | 122 | int ARR_SIZE = dot->getNextTxMaxSize(); |
candre97 | 103:d8e01f94dd43 | 123 | int t_size = 17; |
candre97 | 103:d8e01f94dd43 | 124 | ARR_SIZE -= t_size; |
candre97 | 103:d8e01f94dd43 | 125 | ARR_SIZE -= 5; // for the commas & colons in there |
candre97 | 103:d8e01f94dd43 | 126 | char arr[ARR_SIZE]; |
candre97 | 103:d8e01f94dd43 | 127 | char t_arr[t_size]; |
candre97 | 102:630e8252eabf | 128 | |
Jonathan Austin |
0:2757d7abb7d9 | 129 | while (true) { |
candre97 | 103:d8e01f94dd43 | 130 | //pc.printf("Max Payload: %i\n\r", dot->getNextTxMaxSize()); |
candre97 | 103:d8e01f94dd43 | 131 | memset(arr, 0, ARR_SIZE); |
candre97 | 103:d8e01f94dd43 | 132 | memset(t_arr, 0, t_size); |
candre97 | 103:d8e01f94dd43 | 133 | |
candre97 | 102:630e8252eabf | 134 | |
candre97 | 102:630e8252eabf | 135 | std::vector<uint8_t> tx_data; |
candre97 | 103:d8e01f94dd43 | 136 | tx_data.clear(); |
candre97 | 102:630e8252eabf | 137 | |
candre97 | 103:d8e01f94dd43 | 138 | uint64_t gps_t = dot->getGPSTime(); |
candre97 | 103:d8e01f94dd43 | 139 | printf("%llu \n", gps_t); |
candre97 | 103:d8e01f94dd43 | 140 | snprintf(t_arr, t_size,"T:%llu,", gps_t); |
candre97 | 103:d8e01f94dd43 | 141 | |
candre97 | 103:d8e01f94dd43 | 142 | //pc.printf("Enter a message to TX: "); |
candre97 | 102:630e8252eabf | 143 | char c = 'f'; |
candre97 | 103:d8e01f94dd43 | 144 | pc.printf("ready\n"); |
candre97 | 103:d8e01f94dd43 | 145 | //pc.printf("Your message: "); |
candre97 | 103:d8e01f94dd43 | 146 | for(int i = 0; i < ARR_SIZE && c != '!'; i++){ |
candre97 | 102:630e8252eabf | 147 | c = pc.getc(); |
candre97 | 103:d8e01f94dd43 | 148 | pc.printf("%c", c); |
candre97 | 102:630e8252eabf | 149 | arr[i] = c; |
candre97 | 102:630e8252eabf | 150 | } |
candre97 | 102:630e8252eabf | 151 | //pc.gets(arr, 9); |
candre97 | 103:d8e01f94dd43 | 152 | //pc.printf("sending: %s\r", arr); |
candre97 | 102:630e8252eabf | 153 | |
candre97 | 103:d8e01f94dd43 | 154 | for(int i = 0; i < t_size && t_arr[i] != ','; i++) { |
candre97 | 103:d8e01f94dd43 | 155 | tx_data.push_back(t_arr[i]); |
candre97 | 103:d8e01f94dd43 | 156 | } |
candre97 | 103:d8e01f94dd43 | 157 | tx_data.push_back(','); |
candre97 | 103:d8e01f94dd43 | 158 | |
candre97 | 103:d8e01f94dd43 | 159 | for(int i = 0; arr[i] != '!' && i < ARR_SIZE; i++) { |
candre97 | 103:d8e01f94dd43 | 160 | if(arr[i] == '!'){ |
candre97 | 103:d8e01f94dd43 | 161 | break; |
candre97 | 103:d8e01f94dd43 | 162 | } |
candre97 | 102:630e8252eabf | 163 | tx_data.push_back(arr[i]); |
candre97 | 102:630e8252eabf | 164 | } |
candre97 | 102:630e8252eabf | 165 | send_data(tx_data); |
candre97 | 103:d8e01f94dd43 | 166 | //pc.printf("message sent... \r"); |
candre97 | 102:630e8252eabf | 167 | } |
candre97 | 102:630e8252eabf | 168 | return 0; |
candre97 | 102:630e8252eabf | 169 | } |
mbed_official | 82:abf1b1785bd7 | 170 |