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@0:0b9e73d44412, 2021-07-08 (annotated)
- Committer:
- marumbo
- Date:
- Thu Jul 08 09:34:51 2021 +0000
- Revision:
- 0:0b9e73d44412
- Child:
- 1:a06fd6ffdb3e
Initial commit with Lora, small pushbutton issues for rise and fall to fix,; Need to clean code, either arrange in different folders or arrange systematically;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marumbo | 0:0b9e73d44412 | 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 | 0:0b9e73d44412 | 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 | 0:0b9e73d44412 | 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 | 0:0b9e73d44412 | 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 | 0:0b9e73d44412 | 21 | |
marumbo | 0:0b9e73d44412 | 22 | // Constructing Mbed LoRaWANInterface and passing it down the radio object. |
marumbo | 0:0b9e73d44412 | 23 | static LoRaWANInterface lorawan(radio); |
marumbo | 0:0b9e73d44412 | 24 | |
marumbo | 0:0b9e73d44412 | 25 | // Application specific callbacks |
marumbo | 0:0b9e73d44412 | 26 | static lorawan_app_callbacks_t callbacks; |
marumbo | 0:0b9e73d44412 | 27 | |
marumbo | 0:0b9e73d44412 | 28 | // LoRaWAN stack event handler |
marumbo | 0:0b9e73d44412 | 29 | static void lora_event_handler(lorawan_event_t event); |
marumbo | 0:0b9e73d44412 | 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 | 0:0b9e73d44412 | 33 | // Sending strings over LoRaWAN is not recommended |
marumbo | 0:0b9e73d44412 | 34 | sprintf((char*) tx_buffer, "{\"topicId\":%d, \"duration\":%ld,\"group\":\"%s\"}", topicId, duration, group.c_str()); |
marumbo | 0:0b9e73d44412 | 35 | int packet_len = strlen((char*) tx_buffer); |
marumbo | 0:0b9e73d44412 | 36 | printf("Sending %d bytes: \"%s\"\n", packet_len, tx_buffer); |
marumbo | 0:0b9e73d44412 | 37 | int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, MSG_UNCONFIRMED_FLAG); |
marumbo | 0:0b9e73d44412 | 38 | // 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 | 39 | if (retcode < 0) { |
marumbo | 0:0b9e73d44412 | 40 | retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - duty cycle violation\n") |
marumbo | 0:0b9e73d44412 | 41 | : printf("send() - Error code %d\n", retcode); |
marumbo | 0:0b9e73d44412 | 42 | return; |
marumbo | 0:0b9e73d44412 | 43 | } |
marumbo | 0:0b9e73d44412 | 44 | printf("%d bytes scheduled for transmission\n", retcode); |
marumbo | 0:0b9e73d44412 | 45 | } |
marumbo | 0:0b9e73d44412 | 46 | |
marumbo | 0:0b9e73d44412 | 47 | int initialize_lora() { |
marumbo | 0:0b9e73d44412 | 48 | 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 | 49 | printf("Set your LoRaWAN credentials first!\n"); |
marumbo | 0:0b9e73d44412 | 50 | // return -1; |
marumbo | 0:0b9e73d44412 | 51 | } |
marumbo | 0:0b9e73d44412 | 52 | printf("Initializing Lora device!\n"); |
marumbo | 0:0b9e73d44412 | 53 | // Enable trace output for this demo, so we can see what the LoRaWAN stack does |
marumbo | 0:0b9e73d44412 | 54 | mbed_trace_init(); |
marumbo | 0:0b9e73d44412 | 55 | if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { |
marumbo | 0:0b9e73d44412 | 56 | printf("LoRa initialization failed!\n"); |
marumbo | 0:0b9e73d44412 | 57 | // return -1; |
marumbo | 0:0b9e73d44412 | 58 | } |
marumbo | 0:0b9e73d44412 | 59 | // prepare application callbacks |
marumbo | 0:0b9e73d44412 | 60 | callbacks.events = mbed::callback(lora_event_handler); |
marumbo | 0:0b9e73d44412 | 61 | lorawan.add_app_callbacks(&callbacks); |
marumbo | 0:0b9e73d44412 | 62 | // Disable adaptive data rating |
marumbo | 0:0b9e73d44412 | 63 | if (lorawan.disable_adaptive_datarate() != LORAWAN_STATUS_OK) { |
marumbo | 0:0b9e73d44412 | 64 | printf("disable_adaptive_datarate failed!\n"); |
marumbo | 0:0b9e73d44412 | 65 | // return -1; |
marumbo | 0:0b9e73d44412 | 66 | } |
marumbo | 0:0b9e73d44412 | 67 | lorawan.set_datarate(5); // SF7BW125 |
marumbo | 0:0b9e73d44412 | 68 | lorawan_connect_t connect_params; |
marumbo | 0:0b9e73d44412 | 69 | connect_params.connect_type = LORAWAN_CONNECTION_OTAA; |
marumbo | 0:0b9e73d44412 | 70 | connect_params.connection_u.otaa.dev_eui = DEV_EUI; |
marumbo | 0:0b9e73d44412 | 71 | connect_params.connection_u.otaa.app_eui = APP_EUI; |
marumbo | 0:0b9e73d44412 | 72 | connect_params.connection_u.otaa.app_key = APP_KEY; |
marumbo | 0:0b9e73d44412 | 73 | connect_params.connection_u.otaa.nb_trials = 3; |
marumbo | 0:0b9e73d44412 | 74 | |
marumbo | 0:0b9e73d44412 | 75 | lorawan_status_t retcode = lorawan.connect(connect_params); |
marumbo | 0:0b9e73d44412 | 76 | |
marumbo | 0:0b9e73d44412 | 77 | if (retcode == LORAWAN_STATUS_OK || |
marumbo | 0:0b9e73d44412 | 78 | retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { |
marumbo | 0:0b9e73d44412 | 79 | } else { |
marumbo | 0:0b9e73d44412 | 80 | printf("Connection error, code = %d\n", retcode); |
marumbo | 0:0b9e73d44412 | 81 | // return -1; |
marumbo | 0:0b9e73d44412 | 82 | } |
marumbo | 0:0b9e73d44412 | 83 | |
marumbo | 0:0b9e73d44412 | 84 | printf("Connection - In Progress ...\r\n"); |
marumbo | 0:0b9e73d44412 | 85 | // make your event queue dispatching events forever |
marumbo | 0:0b9e73d44412 | 86 | ev_queue.dispatch_forever(); |
marumbo | 0:0b9e73d44412 | 87 | return 0; |
marumbo | 0:0b9e73d44412 | 88 | |
marumbo | 0:0b9e73d44412 | 89 | } |
marumbo | 0:0b9e73d44412 | 90 | |
marumbo | 0:0b9e73d44412 | 91 | |
marumbo | 0:0b9e73d44412 | 92 | //declare lcd |
marumbo | 0:0b9e73d44412 | 93 | C12832 lcd(SPI_MOSI, SPI_SCK, SPI_MISO, p8, p11); |
marumbo | 0:0b9e73d44412 | 94 | |
marumbo | 0:0b9e73d44412 | 95 | //humidity and temperature sensor |
marumbo | 0:0b9e73d44412 | 96 | Sht31 sht31(I2C_SDA, I2C_SCL); |
marumbo | 0:0b9e73d44412 | 97 | |
marumbo | 0:0b9e73d44412 | 98 | //declare components |
marumbo | 0:0b9e73d44412 | 99 | InterruptIn pushButton(p11); |
marumbo | 0:0b9e73d44412 | 100 | DigitalOut whiteLed(p8); |
marumbo | 0:0b9e73d44412 | 101 | DigitalOut redLed(p9); |
marumbo | 0:0b9e73d44412 | 102 | DigitalOut blueLed(p10); |
marumbo | 0:0b9e73d44412 | 103 | |
marumbo | 0:0b9e73d44412 | 104 | void State1(); |
marumbo | 0:0b9e73d44412 | 105 | void State2(); |
marumbo | 0:0b9e73d44412 | 106 | void Mode1(); |
marumbo | 0:0b9e73d44412 | 107 | void Mode2(); |
marumbo | 0:0b9e73d44412 | 108 | void CheckDirection(); |
marumbo | 0:0b9e73d44412 | 109 | void PrintContent(); |
marumbo | 0:0b9e73d44412 | 110 | void StartButtonTimer(); |
marumbo | 0:0b9e73d44412 | 111 | void StopButtonTimer(); |
marumbo | 0:0b9e73d44412 | 112 | void DurationCheck(); |
marumbo | 0:0b9e73d44412 | 113 | void NextTopic(); |
marumbo | 0:0b9e73d44412 | 114 | |
marumbo | 0:0b9e73d44412 | 115 | //topics and content |
marumbo | 0:0b9e73d44412 | 116 | 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 | 117 | string companyName = "Godfrey&SonsTech"; |
marumbo | 0:0b9e73d44412 | 118 | |
marumbo | 0:0b9e73d44412 | 119 | |
marumbo | 0:0b9e73d44412 | 120 | int direction = 0; // Forward is 0 False and Reverse 1true |
marumbo | 0:0b9e73d44412 | 121 | int current_topic; |
marumbo | 0:0b9e73d44412 | 122 | time_t loraTimerStart; |
marumbo | 0:0b9e73d44412 | 123 | time_t loraTimerStop; |
marumbo | 0:0b9e73d44412 | 124 | time_t buttonTimerStart; |
marumbo | 0:0b9e73d44412 | 125 | time_t buttonTimerStop; |
marumbo | 0:0b9e73d44412 | 126 | int loraDuration = 0; |
marumbo | 0:0b9e73d44412 | 127 | int buttonDuration = 0; |
marumbo | 0:0b9e73d44412 | 128 | |
marumbo | 0:0b9e73d44412 | 129 | |
marumbo | 0:0b9e73d44412 | 130 | |
marumbo | 0:0b9e73d44412 | 131 | volatile bool running = true; |
marumbo | 0:0b9e73d44412 | 132 | |
marumbo | 0:0b9e73d44412 | 133 | |
marumbo | 0:0b9e73d44412 | 134 | |
marumbo | 0:0b9e73d44412 | 135 | void PrintTemperatureAndTime() { |
marumbo | 0:0b9e73d44412 | 136 | |
marumbo | 0:0b9e73d44412 | 137 | time_t seconds = time(NULL); |
marumbo | 0:0b9e73d44412 | 138 | |
marumbo | 0:0b9e73d44412 | 139 | float temp = sht31.readTemperature(); |
marumbo | 0:0b9e73d44412 | 140 | float humidity = sht31.readHumidity(); |
marumbo | 0:0b9e73d44412 | 141 | lcd.locate(3, 3); |
marumbo | 0:0b9e73d44412 | 142 | lcd.printf("Temperature: %.2f C", temp); |
marumbo | 0:0b9e73d44412 | 143 | |
marumbo | 0:0b9e73d44412 | 144 | lcd.locate(3, 13); |
marumbo | 0:0b9e73d44412 | 145 | lcd.printf("Date/Time: %s", ctime(&seconds)); |
marumbo | 0:0b9e73d44412 | 146 | // turn on LED if the temperature is above 25 degrees |
marumbo | 0:0b9e73d44412 | 147 | wait(0.5f); |
marumbo | 0:0b9e73d44412 | 148 | } |
marumbo | 0:0b9e73d44412 | 149 | |
marumbo | 0:0b9e73d44412 | 150 | void WelcomeMessage(string companyName){ |
marumbo | 0:0b9e73d44412 | 151 | lcd.cls(); |
marumbo | 0:0b9e73d44412 | 152 | lcd.locate(3, 3); |
marumbo | 0:0b9e73d44412 | 153 | lcd.printf("Welcome"); |
marumbo | 0:0b9e73d44412 | 154 | lcd.locate(3, 13); |
marumbo | 0:0b9e73d44412 | 155 | lcd.printf(companyName.c_str()); |
marumbo | 0:0b9e73d44412 | 156 | wait(5); |
marumbo | 0:0b9e73d44412 | 157 | lcd.cls(); |
marumbo | 0:0b9e73d44412 | 158 | |
marumbo | 0:0b9e73d44412 | 159 | //call Mode1 |
marumbo | 0:0b9e73d44412 | 160 | Mode1(); |
marumbo | 0:0b9e73d44412 | 161 | |
marumbo | 0:0b9e73d44412 | 162 | } |
marumbo | 0:0b9e73d44412 | 163 | // create an instance of LCD |
marumbo | 0:0b9e73d44412 | 164 | void Display(string lcdPrint) { |
marumbo | 0:0b9e73d44412 | 165 | lcd.cls(); // Clear LCD |
marumbo | 0:0b9e73d44412 | 166 | lcd.locate(3,5); // get cursor to position x=3px and y=5px |
marumbo | 0:0b9e73d44412 | 167 | lcd.printf(lcdPrint.c_str()); // Write text into LCD buffer |
marumbo | 0:0b9e73d44412 | 168 | // Clear LCD |
marumbo | 0:0b9e73d44412 | 169 | } |
marumbo | 0:0b9e73d44412 | 170 | |
marumbo | 0:0b9e73d44412 | 171 | void StartButtonTimer(){ |
marumbo | 0:0b9e73d44412 | 172 | printf("Button Timer start \n"); |
marumbo | 0:0b9e73d44412 | 173 | buttonTimerStart = time(NULL); |
marumbo | 0:0b9e73d44412 | 174 | printf("Button timer start: %d \n",buttonTimerStart); |
marumbo | 0:0b9e73d44412 | 175 | wait(0.5); |
marumbo | 0:0b9e73d44412 | 176 | |
marumbo | 0:0b9e73d44412 | 177 | |
marumbo | 0:0b9e73d44412 | 178 | } |
marumbo | 0:0b9e73d44412 | 179 | |
marumbo | 0:0b9e73d44412 | 180 | void StopButtonTimer(){ |
marumbo | 0:0b9e73d44412 | 181 | printf("Button timer stop \n"); |
marumbo | 0:0b9e73d44412 | 182 | loraTimerStop =time(NULL); |
marumbo | 0:0b9e73d44412 | 183 | buttonTimerStop = time(NULL); |
marumbo | 0:0b9e73d44412 | 184 | printf("Button timer stop: %d \n",buttonTimerStop); |
marumbo | 0:0b9e73d44412 | 185 | wait(0.5); |
marumbo | 0:0b9e73d44412 | 186 | |
marumbo | 0:0b9e73d44412 | 187 | loraDuration = loraTimerStop - loraTimerStart; |
marumbo | 0:0b9e73d44412 | 188 | |
marumbo | 0:0b9e73d44412 | 189 | buttonDuration = buttonTimerStop - buttonTimerStart; |
marumbo | 0:0b9e73d44412 | 190 | printf("Duration Lora: %d \n", loraDuration); |
marumbo | 0:0b9e73d44412 | 191 | printf("Duration Button: %d \n", buttonDuration); |
marumbo | 0:0b9e73d44412 | 192 | |
marumbo | 0:0b9e73d44412 | 193 | DurationCheck(); |
marumbo | 0:0b9e73d44412 | 194 | |
marumbo | 0:0b9e73d44412 | 195 | } |
marumbo | 0:0b9e73d44412 | 196 | |
marumbo | 0:0b9e73d44412 | 197 | void NextTopic(){ |
marumbo | 0:0b9e73d44412 | 198 | if(direction == 0){ |
marumbo | 0:0b9e73d44412 | 199 | if(current_topic >= 4){ |
marumbo | 0:0b9e73d44412 | 200 | current_topic = -1; |
marumbo | 0:0b9e73d44412 | 201 | } |
marumbo | 0:0b9e73d44412 | 202 | current_topic++; |
marumbo | 0:0b9e73d44412 | 203 | } |
marumbo | 0:0b9e73d44412 | 204 | |
marumbo | 0:0b9e73d44412 | 205 | if(direction ==1){ |
marumbo | 0:0b9e73d44412 | 206 | if(current_topic < 0){ |
marumbo | 0:0b9e73d44412 | 207 | current_topic = 5; |
marumbo | 0:0b9e73d44412 | 208 | } |
marumbo | 0:0b9e73d44412 | 209 | current_topic --; |
marumbo | 0:0b9e73d44412 | 210 | } |
marumbo | 0:0b9e73d44412 | 211 | |
marumbo | 0:0b9e73d44412 | 212 | Mode2(); |
marumbo | 0:0b9e73d44412 | 213 | } |
marumbo | 0:0b9e73d44412 | 214 | |
marumbo | 0:0b9e73d44412 | 215 | void DurationCheck(){ |
marumbo | 0:0b9e73d44412 | 216 | printf("duration check details: \n"); |
marumbo | 0:0b9e73d44412 | 217 | printf("Duration Lora: %d \n", loraDuration); |
marumbo | 0:0b9e73d44412 | 218 | printf("Duration Button: %d \n", buttonDuration); |
marumbo | 0:0b9e73d44412 | 219 | |
marumbo | 0:0b9e73d44412 | 220 | if(loraDuration> 7) { |
marumbo | 0:0b9e73d44412 | 221 | //add lora sending function |
marumbo | 0:0b9e73d44412 | 222 | printf("Id: %d, duration: %d \n", current_topic, loraDuration ); |
marumbo | 0:0b9e73d44412 | 223 | send_message(current_topic, loraDuration, "Group2"); |
marumbo | 0:0b9e73d44412 | 224 | } |
marumbo | 0:0b9e73d44412 | 225 | |
marumbo | 0:0b9e73d44412 | 226 | if(buttonDuration < 5) |
marumbo | 0:0b9e73d44412 | 227 | { |
marumbo | 0:0b9e73d44412 | 228 | //call next topic |
marumbo | 0:0b9e73d44412 | 229 | NextTopic(); |
marumbo | 0:0b9e73d44412 | 230 | } |
marumbo | 0:0b9e73d44412 | 231 | |
marumbo | 0:0b9e73d44412 | 232 | if(buttonDuration >= 5 && buttonDuration <= 9){ |
marumbo | 0:0b9e73d44412 | 233 | direction = !direction; |
marumbo | 0:0b9e73d44412 | 234 | NextTopic(); |
marumbo | 0:0b9e73d44412 | 235 | } |
marumbo | 0:0b9e73d44412 | 236 | |
marumbo | 0:0b9e73d44412 | 237 | if(buttonDuration > 9){ |
marumbo | 0:0b9e73d44412 | 238 | Display("Reset to factory ...."); |
marumbo | 0:0b9e73d44412 | 239 | wait(10); |
marumbo | 0:0b9e73d44412 | 240 | WelcomeMessage(companyName); |
marumbo | 0:0b9e73d44412 | 241 | |
marumbo | 0:0b9e73d44412 | 242 | } |
marumbo | 0:0b9e73d44412 | 243 | |
marumbo | 0:0b9e73d44412 | 244 | } |
marumbo | 0:0b9e73d44412 | 245 | |
marumbo | 0:0b9e73d44412 | 246 | |
marumbo | 0:0b9e73d44412 | 247 | void State1(){ |
marumbo | 0:0b9e73d44412 | 248 | current_topic = 0; |
marumbo | 0:0b9e73d44412 | 249 | PrintTemperatureAndTime(); |
marumbo | 0:0b9e73d44412 | 250 | wait(3); |
marumbo | 0:0b9e73d44412 | 251 | State2(); |
marumbo | 0:0b9e73d44412 | 252 | } |
marumbo | 0:0b9e73d44412 | 253 | |
marumbo | 0:0b9e73d44412 | 254 | void State2(){ |
marumbo | 0:0b9e73d44412 | 255 | printf("state 2 .. \n"); |
marumbo | 0:0b9e73d44412 | 256 | |
marumbo | 0:0b9e73d44412 | 257 | while(current_topic< 5){ |
marumbo | 0:0b9e73d44412 | 258 | printf("current topic %d \n", current_topic); |
marumbo | 0:0b9e73d44412 | 259 | Display(content[current_topic][0]); |
marumbo | 0:0b9e73d44412 | 260 | wait(2); |
marumbo | 0:0b9e73d44412 | 261 | current_topic ++; |
marumbo | 0:0b9e73d44412 | 262 | } |
marumbo | 0:0b9e73d44412 | 263 | |
marumbo | 0:0b9e73d44412 | 264 | State1(); |
marumbo | 0:0b9e73d44412 | 265 | } |
marumbo | 0:0b9e73d44412 | 266 | |
marumbo | 0:0b9e73d44412 | 267 | void PrintContent(){ |
marumbo | 0:0b9e73d44412 | 268 | loraTimerStart = time(NULL); |
marumbo | 0:0b9e73d44412 | 269 | |
marumbo | 0:0b9e73d44412 | 270 | while(1){ |
marumbo | 0:0b9e73d44412 | 271 | Display(content[current_topic][1]); |
marumbo | 0:0b9e73d44412 | 272 | wait(0.5); |
marumbo | 0:0b9e73d44412 | 273 | pushButton.mode(PullUp); |
marumbo | 0:0b9e73d44412 | 274 | pushButton.fall(callback(&StartButtonTimer)); |
marumbo | 0:0b9e73d44412 | 275 | pushButton.rise(callback(&StopButtonTimer)); |
marumbo | 0:0b9e73d44412 | 276 | } |
marumbo | 0:0b9e73d44412 | 277 | } |
marumbo | 0:0b9e73d44412 | 278 | |
marumbo | 0:0b9e73d44412 | 279 | void CheckDirection(){ |
marumbo | 0:0b9e73d44412 | 280 | |
marumbo | 0:0b9e73d44412 | 281 | if(direction == 0) //O is a forward direction |
marumbo | 0:0b9e73d44412 | 282 | { |
marumbo | 0:0b9e73d44412 | 283 | redLed = 0; |
marumbo | 0:0b9e73d44412 | 284 | blueLed = 1; |
marumbo | 0:0b9e73d44412 | 285 | wait(0.5); |
marumbo | 0:0b9e73d44412 | 286 | PrintContent(); |
marumbo | 0:0b9e73d44412 | 287 | } |
marumbo | 0:0b9e73d44412 | 288 | |
marumbo | 0:0b9e73d44412 | 289 | if(direction == 1) //O is a forward direction |
marumbo | 0:0b9e73d44412 | 290 | { |
marumbo | 0:0b9e73d44412 | 291 | redLed = 1; |
marumbo | 0:0b9e73d44412 | 292 | blueLed = 0; |
marumbo | 0:0b9e73d44412 | 293 | PrintContent(); |
marumbo | 0:0b9e73d44412 | 294 | } |
marumbo | 0:0b9e73d44412 | 295 | } |
marumbo | 0:0b9e73d44412 | 296 | |
marumbo | 0:0b9e73d44412 | 297 | void blink(DigitalOut *led) { |
marumbo | 0:0b9e73d44412 | 298 | while (running) { |
marumbo | 0:0b9e73d44412 | 299 | *led = !*led; |
marumbo | 0:0b9e73d44412 | 300 | wait(1); |
marumbo | 0:0b9e73d44412 | 301 | pushButton.fall(callback(&Mode2)); |
marumbo | 0:0b9e73d44412 | 302 | State1(); |
marumbo | 0:0b9e73d44412 | 303 | } |
marumbo | 0:0b9e73d44412 | 304 | } |
marumbo | 0:0b9e73d44412 | 305 | |
marumbo | 0:0b9e73d44412 | 306 | void Mode1(){ |
marumbo | 0:0b9e73d44412 | 307 | while (1){ |
marumbo | 0:0b9e73d44412 | 308 | redLed = 0; |
marumbo | 0:0b9e73d44412 | 309 | blueLed = 0; |
marumbo | 0:0b9e73d44412 | 310 | direction = 0; |
marumbo | 0:0b9e73d44412 | 311 | current_topic = 0; |
marumbo | 0:0b9e73d44412 | 312 | |
marumbo | 0:0b9e73d44412 | 313 | blink(&whiteLed); |
marumbo | 0:0b9e73d44412 | 314 | wait(0.5); |
marumbo | 0:0b9e73d44412 | 315 | } |
marumbo | 0:0b9e73d44412 | 316 | } |
marumbo | 0:0b9e73d44412 | 317 | |
marumbo | 0:0b9e73d44412 | 318 | void Mode2(){ |
marumbo | 0:0b9e73d44412 | 319 | while (1){ |
marumbo | 0:0b9e73d44412 | 320 | printf("Mode 2... \n"); |
marumbo | 0:0b9e73d44412 | 321 | printf("current topic %s \n", content[current_topic][0].c_str()); |
marumbo | 0:0b9e73d44412 | 322 | lcd.cls(); |
marumbo | 0:0b9e73d44412 | 323 | CheckDirection(); |
marumbo | 0:0b9e73d44412 | 324 | wait(0.5); |
marumbo | 0:0b9e73d44412 | 325 | |
marumbo | 0:0b9e73d44412 | 326 | Mode1(); |
marumbo | 0:0b9e73d44412 | 327 | |
marumbo | 0:0b9e73d44412 | 328 | } |
marumbo | 0:0b9e73d44412 | 329 | } |
marumbo | 0:0b9e73d44412 | 330 | |
marumbo | 0:0b9e73d44412 | 331 | int main() { |
marumbo | 0:0b9e73d44412 | 332 | while (1) { |
marumbo | 0:0b9e73d44412 | 333 | |
marumbo | 0:0b9e73d44412 | 334 | Display("Initializing .............."); |
marumbo | 0:0b9e73d44412 | 335 | initialize_lora(); |
marumbo | 0:0b9e73d44412 | 336 | |
marumbo | 0:0b9e73d44412 | 337 | WelcomeMessage(companyName); |
marumbo | 0:0b9e73d44412 | 338 | |
marumbo | 0:0b9e73d44412 | 339 | wait_ms(500); |
marumbo | 0:0b9e73d44412 | 340 | } |
marumbo | 0:0b9e73d44412 | 341 | } |
marumbo | 0:0b9e73d44412 | 342 | |
marumbo | 0:0b9e73d44412 | 343 | |
marumbo | 0:0b9e73d44412 | 344 | static void lora_event_handler(lorawan_event_t event) { |
marumbo | 0:0b9e73d44412 | 345 | switch (event) { |
marumbo | 0:0b9e73d44412 | 346 | case CONNECTED: |
marumbo | 0:0b9e73d44412 | 347 | printf("Connection - Successful\n"); |
marumbo | 0:0b9e73d44412 | 348 | ev_queue.break_dispatch(); |
marumbo | 0:0b9e73d44412 | 349 | |
marumbo | 0:0b9e73d44412 | 350 | break; |
marumbo | 0:0b9e73d44412 | 351 | case DISCONNECTED: |
marumbo | 0:0b9e73d44412 | 352 | ev_queue.break_dispatch(); |
marumbo | 0:0b9e73d44412 | 353 | printf("Disconnected Successfully\n"); |
marumbo | 0:0b9e73d44412 | 354 | break; |
marumbo | 0:0b9e73d44412 | 355 | case TX_DONE: |
marumbo | 0:0b9e73d44412 | 356 | printf("Message Sent to Network Server\n"); |
marumbo | 0:0b9e73d44412 | 357 | break; |
marumbo | 0:0b9e73d44412 | 358 | case TX_TIMEOUT: |
marumbo | 0:0b9e73d44412 | 359 | case TX_ERROR: |
marumbo | 0:0b9e73d44412 | 360 | case TX_CRYPTO_ERROR: |
marumbo | 0:0b9e73d44412 | 361 | case TX_SCHEDULING_ERROR: |
marumbo | 0:0b9e73d44412 | 362 | printf("Transmission Error - EventCode = %d\n", event); |
marumbo | 0:0b9e73d44412 | 363 | break; |
marumbo | 0:0b9e73d44412 | 364 | case JOIN_FAILURE: |
marumbo | 0:0b9e73d44412 | 365 | printf("OTAA Failed - Check Keys\n"); |
marumbo | 0:0b9e73d44412 | 366 | break; |
marumbo | 0:0b9e73d44412 | 367 | default: |
marumbo | 0:0b9e73d44412 | 368 | MBED_ASSERT("Unknown Event"); |
marumbo | 0:0b9e73d44412 | 369 | } |
marumbo | 0:0b9e73d44412 | 370 | } |