Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@8:0503a220c5f2, 2021-07-11 (annotated)
- Committer:
- marumbo
- Date:
- Sun Jul 11 19:27:40 2021 +0000
- Revision:
- 8:0503a220c5f2
- Parent:
- 7:ec8f01387c36
Lora timer start fix;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marumbo | 5:235bf7c41262 | 1 | #include "mbed.h" |
marumbo | 0:0b9e73d44412 | 2 | #include "C12832.h" |
marumbo | 0:0b9e73d44412 | 3 | #include "Sht31.h" |
marumbo | 0:0b9e73d44412 | 4 | #include <string> |
marumbo | 0:0b9e73d44412 | 5 | #include "mbed_trace.h" |
marumbo | 0:0b9e73d44412 | 6 | #include "mbed_events.h" |
marumbo | 0:0b9e73d44412 | 7 | #include "LoRaWANInterface.h" |
marumbo | 0:0b9e73d44412 | 8 | #include "SX1276_LoRaRadio.h" |
marumbo | 3:ab4d9bcf8dd1 | 9 | |
marumbo | 0:0b9e73d44412 | 10 | // Device credentials, register device as OTAA in The Things Network and copy credentials here |
marumbo | 0:0b9e73d44412 | 11 | static uint8_t DEV_EUI[] = { 0x26, 0x10, 0x20, 0x20, 0x11, 0x11, 0x20, 0x20 }; |
marumbo | 0:0b9e73d44412 | 12 | static uint8_t APP_EUI[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x03, 0x74, 0x72 }; |
marumbo | 0:0b9e73d44412 | 13 | static uint8_t APP_KEY[] = { 0x75, 0x57, 0x56, 0x65, 0xB1, 0x8A, 0x17, 0x52, 0x1B, 0x2D, 0x45, 0xD9, 0xFC, 0x90, 0x9E, 0x05 }; |
marumbo | 3:ab4d9bcf8dd1 | 14 | |
marumbo | 0:0b9e73d44412 | 15 | // The port we're sending and receiving on |
marumbo | 0:0b9e73d44412 | 16 | #define MBED_CONF_LORA_APP_PORT 15 |
marumbo | 3:ab4d9bcf8dd1 | 17 | |
marumbo | 0:0b9e73d44412 | 18 | // Peripherals (LoRa radio, temperature sensor and button) |
marumbo | 0:0b9e73d44412 | 19 | SX1276_LoRaRadio radio(D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9, NC, NC, NC, NC, A4, NC, NC); |
marumbo | 0:0b9e73d44412 | 20 | static EventQueue ev_queue; |
marumbo | 3:ab4d9bcf8dd1 | 21 | |
marumbo | 0:0b9e73d44412 | 22 | // Constructing Mbed LoRaWANInterface and passing it down the radio object. |
marumbo | 0:0b9e73d44412 | 23 | static LoRaWANInterface lorawan(radio); |
marumbo | 3:ab4d9bcf8dd1 | 24 | |
marumbo | 0:0b9e73d44412 | 25 | // Application specific callbacks |
marumbo | 0:0b9e73d44412 | 26 | static lorawan_app_callbacks_t callbacks; |
marumbo | 3:ab4d9bcf8dd1 | 27 | |
marumbo | 0:0b9e73d44412 | 28 | // LoRaWAN stack event handler |
marumbo | 0:0b9e73d44412 | 29 | static void lora_event_handler(lorawan_event_t event); |
marumbo | 3:ab4d9bcf8dd1 | 30 | |
marumbo | 0:0b9e73d44412 | 31 | static void send_message(int topicId, long int duration, string group) { |
marumbo | 0:0b9e73d44412 | 32 | uint8_t tx_buffer[50] = { 0 }; |
marumbo | 2:a128db827b61 | 33 | |
marumbo | 3:ab4d9bcf8dd1 | 34 | string deviceId = "My Device"; |
marumbo | 2:a128db827b61 | 35 | long int timestamp = 1234567892; |
marumbo | 2:a128db827b61 | 36 | int value = 2; |
marumbo | 3:ab4d9bcf8dd1 | 37 | sprintf((char*) tx_buffer, "{\"deviceId\":\"%s\",\"TopicId\":%d,\"Group\":%s}}", deviceId.c_str(), topicId, group.c_str()); |
marumbo | 0:0b9e73d44412 | 38 | // Sending strings over LoRaWAN is not recommended |
marumbo | 2:a128db827b61 | 39 | // sprintf((char*) tx_buffer, "{\"topicId\":%d, \"duration\":%ld,\"group\":\"%s\"}", topicId, duration, group.c_str()); |
marumbo | 0:0b9e73d44412 | 40 | int packet_len = strlen((char*) tx_buffer); |
marumbo | 0:0b9e73d44412 | 41 | printf("Sending %d bytes: \"%s\"\n", packet_len, tx_buffer); |
marumbo | 0:0b9e73d44412 | 42 | int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, MSG_UNCONFIRMED_FLAG); |
marumbo | 0:0b9e73d44412 | 43 | // for some reason send() returns -1... I cannot find out why, the stack returns the right number. I feel that this is some weird Emscripten quirk |
marumbo | 0:0b9e73d44412 | 44 | if (retcode < 0) { |
marumbo | 0:0b9e73d44412 | 45 | retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - duty cycle violation\n") |
marumbo | 0:0b9e73d44412 | 46 | : printf("send() - Error code %d\n", retcode); |
marumbo | 2:a128db827b61 | 47 | return; |
marumbo | 0:0b9e73d44412 | 48 | } |
marumbo | 2:a128db827b61 | 49 | |
marumbo | 0:0b9e73d44412 | 50 | printf("%d bytes scheduled for transmission\n", retcode); |
marumbo | 2:a128db827b61 | 51 | callbacks.events = mbed::callback(lora_event_handler); |
marumbo | 2:a128db827b61 | 52 | lorawan.add_app_callbacks(&callbacks); |
marumbo | 0:0b9e73d44412 | 53 | } |
marumbo | 3:ab4d9bcf8dd1 | 54 | |
marumbo | 0:0b9e73d44412 | 55 | int initialize_lora() { |
marumbo | 0:0b9e73d44412 | 56 | if (DEV_EUI[0] == 0x0 && DEV_EUI[1] == 0x0 && DEV_EUI[2] == 0x0 && DEV_EUI[3] == 0x0 && DEV_EUI[4] == 0x0 && DEV_EUI[5] == 0x0 && DEV_EUI[6] == 0x0 && DEV_EUI[7] == 0x0) { |
marumbo | 0:0b9e73d44412 | 57 | printf("Set your LoRaWAN credentials first!\n"); |
marumbo | 2:a128db827b61 | 58 | return -1; |
marumbo | 0:0b9e73d44412 | 59 | } |
marumbo | 2:a128db827b61 | 60 | printf("Initializing LORA!\n"); |
marumbo | 0:0b9e73d44412 | 61 | // Enable trace output for this demo, so we can see what the LoRaWAN stack does |
marumbo | 0:0b9e73d44412 | 62 | mbed_trace_init(); |
marumbo | 0:0b9e73d44412 | 63 | if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { |
marumbo | 0:0b9e73d44412 | 64 | printf("LoRa initialization failed!\n"); |
marumbo | 2:a128db827b61 | 65 | return -1; |
marumbo | 0:0b9e73d44412 | 66 | } |
marumbo | 0:0b9e73d44412 | 67 | // prepare application callbacks |
marumbo | 0:0b9e73d44412 | 68 | callbacks.events = mbed::callback(lora_event_handler); |
marumbo | 0:0b9e73d44412 | 69 | lorawan.add_app_callbacks(&callbacks); |
marumbo | 0:0b9e73d44412 | 70 | // Disable adaptive data rating |
marumbo | 0:0b9e73d44412 | 71 | if (lorawan.disable_adaptive_datarate() != LORAWAN_STATUS_OK) { |
marumbo | 0:0b9e73d44412 | 72 | printf("disable_adaptive_datarate failed!\n"); |
marumbo | 2:a128db827b61 | 73 | return -1; |
marumbo | 0:0b9e73d44412 | 74 | } |
marumbo | 0:0b9e73d44412 | 75 | lorawan.set_datarate(5); // SF7BW125 |
marumbo | 0:0b9e73d44412 | 76 | lorawan_connect_t connect_params; |
marumbo | 0:0b9e73d44412 | 77 | connect_params.connect_type = LORAWAN_CONNECTION_OTAA; |
marumbo | 0:0b9e73d44412 | 78 | connect_params.connection_u.otaa.dev_eui = DEV_EUI; |
marumbo | 0:0b9e73d44412 | 79 | connect_params.connection_u.otaa.app_eui = APP_EUI; |
marumbo | 0:0b9e73d44412 | 80 | connect_params.connection_u.otaa.app_key = APP_KEY; |
marumbo | 0:0b9e73d44412 | 81 | connect_params.connection_u.otaa.nb_trials = 3; |
marumbo | 3:ab4d9bcf8dd1 | 82 | |
marumbo | 0:0b9e73d44412 | 83 | lorawan_status_t retcode = lorawan.connect(connect_params); |
marumbo | 3:ab4d9bcf8dd1 | 84 | |
marumbo | 0:0b9e73d44412 | 85 | if (retcode == LORAWAN_STATUS_OK || |
marumbo | 0:0b9e73d44412 | 86 | retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { |
marumbo | 0:0b9e73d44412 | 87 | } else { |
marumbo | 0:0b9e73d44412 | 88 | printf("Connection error, code = %d\n", retcode); |
marumbo | 2:a128db827b61 | 89 | return -1; |
marumbo | 0:0b9e73d44412 | 90 | } |
marumbo | 3:ab4d9bcf8dd1 | 91 | |
marumbo | 0:0b9e73d44412 | 92 | printf("Connection - In Progress ...\r\n"); |
marumbo | 0:0b9e73d44412 | 93 | // make your event queue dispatching events forever |
marumbo | 0:0b9e73d44412 | 94 | ev_queue.dispatch_forever(); |
marumbo | 0:0b9e73d44412 | 95 | return 0; |
marumbo | 0:0b9e73d44412 | 96 | } |
marumbo | 3:ab4d9bcf8dd1 | 97 | |
marumbo | 3:ab4d9bcf8dd1 | 98 | |
marumbo | 3:ab4d9bcf8dd1 | 99 | |
marumbo | 0:0b9e73d44412 | 100 | //declare lcd |
marumbo | 0:0b9e73d44412 | 101 | C12832 lcd(SPI_MOSI, SPI_SCK, SPI_MISO, p8, p11); |
marumbo | 3:ab4d9bcf8dd1 | 102 | |
marumbo | 0:0b9e73d44412 | 103 | //humidity and temperature sensor |
marumbo | 0:0b9e73d44412 | 104 | Sht31 sht31(I2C_SDA, I2C_SCL); |
marumbo | 3:ab4d9bcf8dd1 | 105 | |
marumbo | 0:0b9e73d44412 | 106 | //declare components |
marumbo | 2:a128db827b61 | 107 | //InterruptIn pushButton(BUTTON1); //p11 |
marumbo | 0:0b9e73d44412 | 108 | InterruptIn pushButton(p11); |
marumbo | 0:0b9e73d44412 | 109 | DigitalOut whiteLed(p8); |
marumbo | 0:0b9e73d44412 | 110 | DigitalOut redLed(p9); |
marumbo | 0:0b9e73d44412 | 111 | DigitalOut blueLed(p10); |
marumbo | 3:ab4d9bcf8dd1 | 112 | DigitalOut led2(LED2); |
marumbo | 3:ab4d9bcf8dd1 | 113 | |
marumbo | 0:0b9e73d44412 | 114 | void State1(); |
marumbo | 0:0b9e73d44412 | 115 | void State2(); |
marumbo | 0:0b9e73d44412 | 116 | void Mode1(); |
marumbo | 0:0b9e73d44412 | 117 | void Mode2(); |
marumbo | 0:0b9e73d44412 | 118 | void CheckDirection(); |
marumbo | 0:0b9e73d44412 | 119 | void PrintContent(); |
marumbo | 0:0b9e73d44412 | 120 | void StartButtonTimer(); |
marumbo | 0:0b9e73d44412 | 121 | void StopButtonTimer(); |
marumbo | 0:0b9e73d44412 | 122 | void DurationCheck(); |
marumbo | 0:0b9e73d44412 | 123 | void NextTopic(); |
marumbo | 3:ab4d9bcf8dd1 | 124 | |
marumbo | 0:0b9e73d44412 | 125 | //topics and content |
marumbo | 0:0b9e73d44412 | 126 | string content[5][2]= {{"Topic 1","Content 1"},{"Topic 2","Content 2"},{"Topic 3","Content 3"},{"Topic 4","Content 4"},{"Topic 5","Content 5"}}; |
marumbo | 0:0b9e73d44412 | 127 | string companyName = "Godfrey&SonsTech"; |
marumbo | 3:ab4d9bcf8dd1 | 128 | |
marumbo | 0:0b9e73d44412 | 129 | |
marumbo | 0:0b9e73d44412 | 130 | int direction = 0; // Forward is 0 False and Reverse 1true |
marumbo | 2:a128db827b61 | 131 | int current_topic = 0;; |
marumbo | 0:0b9e73d44412 | 132 | time_t loraTimerStart; |
marumbo | 0:0b9e73d44412 | 133 | time_t loraTimerStop; |
marumbo | 0:0b9e73d44412 | 134 | time_t buttonTimerStart; |
marumbo | 0:0b9e73d44412 | 135 | time_t buttonTimerStop; |
marumbo | 0:0b9e73d44412 | 136 | int loraDuration = 0; |
marumbo | 0:0b9e73d44412 | 137 | int buttonDuration = 0; |
marumbo | 3:ab4d9bcf8dd1 | 138 | Ticker flipper; |
marumbo | 3:ab4d9bcf8dd1 | 139 | |
marumbo | 3:ab4d9bcf8dd1 | 140 | |
marumbo | 0:0b9e73d44412 | 141 | void PrintTemperatureAndTime() { |
marumbo | 0:0b9e73d44412 | 142 | |
marumbo | 0:0b9e73d44412 | 143 | time_t seconds = time(NULL); |
marumbo | 0:0b9e73d44412 | 144 | |
marumbo | 0:0b9e73d44412 | 145 | float temp = sht31.readTemperature(); |
marumbo | 0:0b9e73d44412 | 146 | float humidity = sht31.readHumidity(); |
marumbo | 0:0b9e73d44412 | 147 | lcd.locate(3, 3); |
marumbo | 0:0b9e73d44412 | 148 | lcd.printf("Temperature: %.2f C", temp); |
marumbo | 0:0b9e73d44412 | 149 | |
marumbo | 0:0b9e73d44412 | 150 | lcd.locate(3, 13); |
marumbo | 0:0b9e73d44412 | 151 | lcd.printf("Date/Time: %s", ctime(&seconds)); |
marumbo | 0:0b9e73d44412 | 152 | // turn on LED if the temperature is above 25 degrees |
marumbo | 0:0b9e73d44412 | 153 | } |
marumbo | 0:0b9e73d44412 | 154 | |
marumbo | 0:0b9e73d44412 | 155 | void WelcomeMessage(string companyName){ |
marumbo | 0:0b9e73d44412 | 156 | lcd.cls(); |
marumbo | 0:0b9e73d44412 | 157 | lcd.locate(3, 3); |
marumbo | 0:0b9e73d44412 | 158 | lcd.printf("Welcome"); |
marumbo | 0:0b9e73d44412 | 159 | lcd.locate(3, 13); |
marumbo | 0:0b9e73d44412 | 160 | lcd.printf(companyName.c_str()); |
marumbo | 0:0b9e73d44412 | 161 | wait(5); |
marumbo | 0:0b9e73d44412 | 162 | lcd.cls(); |
marumbo | 0:0b9e73d44412 | 163 | |
marumbo | 0:0b9e73d44412 | 164 | //call Mode1 |
marumbo | 0:0b9e73d44412 | 165 | Mode1(); |
marumbo | 0:0b9e73d44412 | 166 | |
marumbo | 0:0b9e73d44412 | 167 | } |
marumbo | 0:0b9e73d44412 | 168 | // create an instance of LCD |
marumbo | 0:0b9e73d44412 | 169 | void Display(string lcdPrint) { |
marumbo | 0:0b9e73d44412 | 170 | lcd.cls(); // Clear LCD |
marumbo | 0:0b9e73d44412 | 171 | lcd.locate(3,5); // get cursor to position x=3px and y=5px |
marumbo | 0:0b9e73d44412 | 172 | lcd.printf(lcdPrint.c_str()); // Write text into LCD buffer |
marumbo | 0:0b9e73d44412 | 173 | // Clear LCD |
marumbo | 0:0b9e73d44412 | 174 | } |
marumbo | 8:0503a220c5f2 | 175 | |
marumbo | 3:ab4d9bcf8dd1 | 176 | |
marumbo | 0:0b9e73d44412 | 177 | void StartButtonTimer(){ |
marumbo | 0:0b9e73d44412 | 178 | printf("Button Timer start \n"); |
marumbo | 0:0b9e73d44412 | 179 | buttonTimerStart = time(NULL); |
marumbo | 4:63bf2f3d54a5 | 180 | printf("Button timer start: %ld \n",buttonTimerStart); |
marumbo | 0:0b9e73d44412 | 181 | wait(0.5); |
marumbo | 0:0b9e73d44412 | 182 | |
marumbo | 0:0b9e73d44412 | 183 | |
marumbo | 0:0b9e73d44412 | 184 | } |
marumbo | 3:ab4d9bcf8dd1 | 185 | |
marumbo | 0:0b9e73d44412 | 186 | void StopButtonTimer(){ |
marumbo | 0:0b9e73d44412 | 187 | printf("Button timer stop \n"); |
marumbo | 0:0b9e73d44412 | 188 | loraTimerStop =time(NULL); |
marumbo | 0:0b9e73d44412 | 189 | buttonTimerStop = time(NULL); |
marumbo | 3:ab4d9bcf8dd1 | 190 | printf("Button timer stop: %ld \n",buttonTimerStop); |
marumbo | 0:0b9e73d44412 | 191 | wait(0.5); |
marumbo | 0:0b9e73d44412 | 192 | |
marumbo | 0:0b9e73d44412 | 193 | loraDuration = loraTimerStop - loraTimerStart; |
marumbo | 0:0b9e73d44412 | 194 | |
marumbo | 0:0b9e73d44412 | 195 | buttonDuration = buttonTimerStop - buttonTimerStart; |
marumbo | 4:63bf2f3d54a5 | 196 | printf("Duration Lora: %d \n", loraDuration); |
marumbo | 4:63bf2f3d54a5 | 197 | printf("Duration Button: %d \n", buttonDuration); |
marumbo | 0:0b9e73d44412 | 198 | |
marumbo | 0:0b9e73d44412 | 199 | DurationCheck(); |
marumbo | 0:0b9e73d44412 | 200 | |
marumbo | 0:0b9e73d44412 | 201 | } |
marumbo | 3:ab4d9bcf8dd1 | 202 | |
marumbo | 0:0b9e73d44412 | 203 | void NextTopic(){ |
marumbo | 0:0b9e73d44412 | 204 | if(direction == 0){ |
marumbo | 2:a128db827b61 | 205 | if(current_topic == 4){ |
marumbo | 0:0b9e73d44412 | 206 | current_topic = -1; |
marumbo | 0:0b9e73d44412 | 207 | } |
marumbo | 0:0b9e73d44412 | 208 | current_topic++; |
marumbo | 0:0b9e73d44412 | 209 | } |
marumbo | 0:0b9e73d44412 | 210 | |
marumbo | 0:0b9e73d44412 | 211 | if(direction ==1){ |
marumbo | 2:a128db827b61 | 212 | if(current_topic == 0){ |
marumbo | 0:0b9e73d44412 | 213 | current_topic = 5; |
marumbo | 0:0b9e73d44412 | 214 | } |
marumbo | 0:0b9e73d44412 | 215 | current_topic --; |
marumbo | 0:0b9e73d44412 | 216 | } |
marumbo | 0:0b9e73d44412 | 217 | |
marumbo | 6:288c9d9d59e4 | 218 | CheckDirection(); |
marumbo | 0:0b9e73d44412 | 219 | } |
marumbo | 3:ab4d9bcf8dd1 | 220 | |
marumbo | 0:0b9e73d44412 | 221 | void DurationCheck(){ |
marumbo | 0:0b9e73d44412 | 222 | printf("duration check details: \n"); |
marumbo | 0:0b9e73d44412 | 223 | printf("Duration Lora: %d \n", loraDuration); |
marumbo | 0:0b9e73d44412 | 224 | printf("Duration Button: %d \n", buttonDuration); |
marumbo | 0:0b9e73d44412 | 225 | |
marumbo | 2:a128db827b61 | 226 | if(loraDuration> 5) { |
marumbo | 0:0b9e73d44412 | 227 | //add lora sending function |
marumbo | 0:0b9e73d44412 | 228 | printf("Id: %d, duration: %d \n", current_topic, loraDuration ); |
marumbo | 2:a128db827b61 | 229 | send_message(current_topic, loraDuration, "Grp2"); |
marumbo | 0:0b9e73d44412 | 230 | } |
marumbo | 0:0b9e73d44412 | 231 | |
marumbo | 2:a128db827b61 | 232 | if(buttonDuration < 3) |
marumbo | 0:0b9e73d44412 | 233 | { |
marumbo | 0:0b9e73d44412 | 234 | //call next topic |
marumbo | 0:0b9e73d44412 | 235 | NextTopic(); |
marumbo | 0:0b9e73d44412 | 236 | } |
marumbo | 0:0b9e73d44412 | 237 | |
marumbo | 2:a128db827b61 | 238 | if(buttonDuration >= 3 && buttonDuration <= 5){ |
marumbo | 0:0b9e73d44412 | 239 | direction = !direction; |
marumbo | 0:0b9e73d44412 | 240 | NextTopic(); |
marumbo | 0:0b9e73d44412 | 241 | } |
marumbo | 0:0b9e73d44412 | 242 | |
marumbo | 2:a128db827b61 | 243 | if(buttonDuration > 5){ |
marumbo | 0:0b9e73d44412 | 244 | Display("Reset to factory ...."); |
marumbo | 0:0b9e73d44412 | 245 | wait(10); |
marumbo | 0:0b9e73d44412 | 246 | WelcomeMessage(companyName); |
marumbo | 0:0b9e73d44412 | 247 | |
marumbo | 0:0b9e73d44412 | 248 | } |
marumbo | 0:0b9e73d44412 | 249 | |
marumbo | 0:0b9e73d44412 | 250 | } |
marumbo | 3:ab4d9bcf8dd1 | 251 | |
marumbo | 3:ab4d9bcf8dd1 | 252 | |
marumbo | 0:0b9e73d44412 | 253 | void State1(){ |
marumbo | 0:0b9e73d44412 | 254 | current_topic = 0; |
marumbo | 0:0b9e73d44412 | 255 | PrintTemperatureAndTime(); |
marumbo | 0:0b9e73d44412 | 256 | wait(3); |
marumbo | 0:0b9e73d44412 | 257 | State2(); |
marumbo | 0:0b9e73d44412 | 258 | } |
marumbo | 3:ab4d9bcf8dd1 | 259 | |
marumbo | 0:0b9e73d44412 | 260 | void State2(){ |
marumbo | 0:0b9e73d44412 | 261 | printf("state 2 .. \n"); |
marumbo | 0:0b9e73d44412 | 262 | |
marumbo | 0:0b9e73d44412 | 263 | while(current_topic< 5){ |
marumbo | 0:0b9e73d44412 | 264 | printf("current topic %d \n", current_topic); |
marumbo | 0:0b9e73d44412 | 265 | Display(content[current_topic][0]); |
marumbo | 0:0b9e73d44412 | 266 | wait(2); |
marumbo | 0:0b9e73d44412 | 267 | current_topic ++; |
marumbo | 0:0b9e73d44412 | 268 | } |
marumbo | 0:0b9e73d44412 | 269 | |
marumbo | 0:0b9e73d44412 | 270 | State1(); |
marumbo | 0:0b9e73d44412 | 271 | } |
marumbo | 3:ab4d9bcf8dd1 | 272 | |
marumbo | 0:0b9e73d44412 | 273 | void PrintContent(){ |
marumbo | 8:0503a220c5f2 | 274 | |
marumbo | 3:ab4d9bcf8dd1 | 275 | Display(content[current_topic][1]); |
marumbo | 3:ab4d9bcf8dd1 | 276 | |
marumbo | 0:0b9e73d44412 | 277 | } |
marumbo | 3:ab4d9bcf8dd1 | 278 | |
marumbo | 0:0b9e73d44412 | 279 | void CheckDirection(){ |
marumbo | 8:0503a220c5f2 | 280 | |
marumbo | 8:0503a220c5f2 | 281 | |
marumbo | 4:63bf2f3d54a5 | 282 | pushButton.rise(callback(&StartButtonTimer)); |
marumbo | 4:63bf2f3d54a5 | 283 | pushButton.fall(callback(&StopButtonTimer)); |
marumbo | 8:0503a220c5f2 | 284 | |
marumbo | 8:0503a220c5f2 | 285 | loraTimerStart = time(NULL); |
marumbo | 7:ec8f01387c36 | 286 | while(1) { |
marumbo | 8:0503a220c5f2 | 287 | |
marumbo | 8:0503a220c5f2 | 288 | |
marumbo | 0:0b9e73d44412 | 289 | if(direction == 0) //O is a forward direction |
marumbo | 0:0b9e73d44412 | 290 | { |
marumbo | 0:0b9e73d44412 | 291 | redLed = 0; |
marumbo | 0:0b9e73d44412 | 292 | blueLed = 1; |
marumbo | 0:0b9e73d44412 | 293 | PrintContent(); |
marumbo | 0:0b9e73d44412 | 294 | } |
marumbo | 0:0b9e73d44412 | 295 | |
marumbo | 0:0b9e73d44412 | 296 | if(direction == 1) //O is a forward direction |
marumbo | 0:0b9e73d44412 | 297 | { |
marumbo | 0:0b9e73d44412 | 298 | redLed = 1; |
marumbo | 0:0b9e73d44412 | 299 | blueLed = 0; |
marumbo | 0:0b9e73d44412 | 300 | PrintContent(); |
marumbo | 0:0b9e73d44412 | 301 | } |
marumbo | 4:63bf2f3d54a5 | 302 | |
marumbo | 4:63bf2f3d54a5 | 303 | wait_ms(200); |
marumbo | 4:63bf2f3d54a5 | 304 | } |
marumbo | 0:0b9e73d44412 | 305 | } |
marumbo | 3:ab4d9bcf8dd1 | 306 | |
marumbo | 3:ab4d9bcf8dd1 | 307 | void BlinkWhiteLed() { |
marumbo | 3:ab4d9bcf8dd1 | 308 | |
marumbo | 3:ab4d9bcf8dd1 | 309 | whiteLed = !whiteLed; |
marumbo | 3:ab4d9bcf8dd1 | 310 | led2 = !led2; |
marumbo | 3:ab4d9bcf8dd1 | 311 | |
marumbo | 3:ab4d9bcf8dd1 | 312 | |
marumbo | 0:0b9e73d44412 | 313 | } |
marumbo | 3:ab4d9bcf8dd1 | 314 | |
marumbo | 0:0b9e73d44412 | 315 | void Mode1(){ |
marumbo | 3:ab4d9bcf8dd1 | 316 | |
marumbo | 3:ab4d9bcf8dd1 | 317 | printf("Mode 1... \n"); |
marumbo | 2:a128db827b61 | 318 | printf("current topic %s \n", content[current_topic][0].c_str()); |
marumbo | 8:0503a220c5f2 | 319 | blueLed = 0; |
marumbo | 8:0503a220c5f2 | 320 | redLed = 0; |
marumbo | 8:0503a220c5f2 | 321 | |
marumbo | 3:ab4d9bcf8dd1 | 322 | flipper.attach(&BlinkWhiteLed, 1.0); |
marumbo | 3:ab4d9bcf8dd1 | 323 | pushButton.fall(callback(&Mode2)); |
marumbo | 3:ab4d9bcf8dd1 | 324 | |
marumbo | 3:ab4d9bcf8dd1 | 325 | State1(); |
marumbo | 0:0b9e73d44412 | 326 | } |
marumbo | 3:ab4d9bcf8dd1 | 327 | |
marumbo | 0:0b9e73d44412 | 328 | void Mode2(){ |
marumbo | 3:ab4d9bcf8dd1 | 329 | |
marumbo | 0:0b9e73d44412 | 330 | printf("Mode 2... \n"); |
marumbo | 0:0b9e73d44412 | 331 | printf("current topic %s \n", content[current_topic][0].c_str()); |
marumbo | 4:63bf2f3d54a5 | 332 | flipper.detach(); |
marumbo | 5:235bf7c41262 | 333 | whiteLed = 0; |
marumbo | 4:63bf2f3d54a5 | 334 | |
marumbo | 0:0b9e73d44412 | 335 | lcd.cls(); |
marumbo | 4:63bf2f3d54a5 | 336 | wait_ms(500); |
marumbo | 0:0b9e73d44412 | 337 | CheckDirection(); |
marumbo | 4:63bf2f3d54a5 | 338 | |
marumbo | 0:0b9e73d44412 | 339 | |
marumbo | 0:0b9e73d44412 | 340 | } |
marumbo | 3:ab4d9bcf8dd1 | 341 | |
marumbo | 3:ab4d9bcf8dd1 | 342 | int main() { |
marumbo | 0:0b9e73d44412 | 343 | |
marumbo | 0:0b9e73d44412 | 344 | Display("Initializing .............."); |
marumbo | 0:0b9e73d44412 | 345 | initialize_lora(); |
marumbo | 0:0b9e73d44412 | 346 | WelcomeMessage(companyName); |
marumbo | 3:ab4d9bcf8dd1 | 347 | |
marumbo | 0:0b9e73d44412 | 348 | } |
marumbo | 3:ab4d9bcf8dd1 | 349 | |
marumbo | 2:a128db827b61 | 350 | // Event handler |
marumbo | 0:0b9e73d44412 | 351 | static void lora_event_handler(lorawan_event_t event) { |
marumbo | 0:0b9e73d44412 | 352 | switch (event) { |
marumbo | 0:0b9e73d44412 | 353 | case CONNECTED: |
marumbo | 0:0b9e73d44412 | 354 | printf("Connection - Successful\n"); |
marumbo | 2:a128db827b61 | 355 | ev_queue.break_dispatch(); |
marumbo | 0:0b9e73d44412 | 356 | break; |
marumbo | 0:0b9e73d44412 | 357 | case DISCONNECTED: |
marumbo | 0:0b9e73d44412 | 358 | ev_queue.break_dispatch(); |
marumbo | 0:0b9e73d44412 | 359 | printf("Disconnected Successfully\n"); |
marumbo | 0:0b9e73d44412 | 360 | break; |
marumbo | 0:0b9e73d44412 | 361 | case TX_DONE: |
marumbo | 0:0b9e73d44412 | 362 | printf("Message Sent to Network Server\n"); |
marumbo | 2:a128db827b61 | 363 | ev_queue.break_dispatch(); |
marumbo | 0:0b9e73d44412 | 364 | break; |
marumbo | 0:0b9e73d44412 | 365 | case TX_TIMEOUT: |
marumbo | 0:0b9e73d44412 | 366 | case TX_ERROR: |
marumbo | 0:0b9e73d44412 | 367 | case TX_CRYPTO_ERROR: |
marumbo | 0:0b9e73d44412 | 368 | case TX_SCHEDULING_ERROR: |
marumbo | 2:a128db827b61 | 369 | ev_queue.break_dispatch(); |
marumbo | 0:0b9e73d44412 | 370 | printf("Transmission Error - EventCode = %d\n", event); |
marumbo | 0:0b9e73d44412 | 371 | break; |
marumbo | 0:0b9e73d44412 | 372 | case JOIN_FAILURE: |
marumbo | 0:0b9e73d44412 | 373 | printf("OTAA Failed - Check Keys\n"); |
marumbo | 0:0b9e73d44412 | 374 | break; |
marumbo | 0:0b9e73d44412 | 375 | default: |
marumbo | 0:0b9e73d44412 | 376 | MBED_ASSERT("Unknown Event"); |
marumbo | 0:0b9e73d44412 | 377 | } |
marumbo | 2:a128db827b61 | 378 | } |