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:3dd7a4231f6e, 2020-11-05 (annotated)
- Committer:
- harerimana
- Date:
- Thu Nov 05 16:03:19 2020 +0000
- Revision:
- 8:3dd7a4231f6e
- Parent:
- 7:667534ecd34e
LORA
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
harerimana | 7:667534ecd34e | 1 | #include "mbed.h" // Include the library of Mbed |
harerimana | 7:667534ecd34e | 2 | #include "C12832.h" // Include the library of the specific LCD |
harerimana | 8:3dd7a4231f6e | 3 | #include "mbed_trace.h" |
harerimana | 8:3dd7a4231f6e | 4 | #include "mbed_events.h" |
harerimana | 8:3dd7a4231f6e | 5 | #include "LoRaWANInterface.h" |
harerimana | 8:3dd7a4231f6e | 6 | #include "SX1276_LoRaRadio.h" |
harerimana | 8:3dd7a4231f6e | 7 | #define DEVICEID "FELIX_220014243" |
harerimana | 8:3dd7a4231f6e | 8 | #define YELLOWBLINKINGDURATION 10 |
harerimana | 8:3dd7a4231f6e | 9 | #define REDLIGHTONDURATION 15 |
harerimana | 8:3dd7a4231f6e | 10 | // The port we're sending and receiving on |
harerimana | 8:3dd7a4231f6e | 11 | #define MBED_CONF_LORA_APP_PORT 1 |
harerimana | 7:667534ecd34e | 12 | |
harerimana | 8:3dd7a4231f6e | 13 | DigitalOut yellowled(LED4, 1); // Initializing the yellow light ON (same as yellowled = 1) |
harerimana | 8:3dd7a4231f6e | 14 | DigitalOut redled(LED1, 0); // Initializing the Red light to OFF (same as redled = 0) |
harerimana | 8:3dd7a4231f6e | 15 | InterruptIn pedestrianbutton(p5); |
harerimana | 7:667534ecd34e | 16 | |
harerimana | 7:667534ecd34e | 17 | C12832 lcd(SPI_MOSI, SPI_SCK, SPI_MISO, p8, p11); // create an instance of LCD |
harerimana | 7:667534ecd34e | 18 | |
harerimana | 7:667534ecd34e | 19 | int state = 0; // State variable indicating that YELLOW LIGHT IS ON |
harerimana | 7:667534ecd34e | 20 | |
harerimana | 7:667534ecd34e | 21 | char pedestrianAdvert[] = "2 COFFEES FOR THE PRICE OF 1. ONLY AT CAMELIA"; |
harerimana | 7:667534ecd34e | 22 | char carDriverAdvert[] = "RECEIVE 5%% REDUCTION ON YOUR FULL TANK ...."; |
harerimana | 7:667534ecd34e | 23 | |
harerimana | 8:3dd7a4231f6e | 24 | |
harerimana | 8:3dd7a4231f6e | 25 | // Device credentials, register device as OTAA in The Things Network and copy credentials here |
harerimana | 8:3dd7a4231f6e | 26 | static uint8_t DEV_EUI[] = { 0x26, 0x10, 0x20, 0x20, 0x11, 0x11, 0x20, 0x20 }; |
harerimana | 8:3dd7a4231f6e | 27 | static uint8_t APP_EUI[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x03, 0x74, 0x72 }; |
harerimana | 8:3dd7a4231f6e | 28 | static uint8_t APP_KEY[] = { 0x75, 0x57, 0x56, 0x65, 0xB1, 0x8A, 0x17, 0x52, 0x1B, 0x2D, 0x45, 0xD9, 0xFC, 0x90, 0x9E, 0x05 }; |
harerimana | 8:3dd7a4231f6e | 29 | |
harerimana | 8:3dd7a4231f6e | 30 | |
harerimana | 8:3dd7a4231f6e | 31 | // Peripherals (LoRa radio, temperature sensor and button) |
harerimana | 8:3dd7a4231f6e | 32 | SX1276_LoRaRadio radio(D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9, NC, NC, NC, NC, A4, NC, NC); |
harerimana | 8:3dd7a4231f6e | 33 | InterruptIn btn(BUTTON1); |
harerimana | 8:3dd7a4231f6e | 34 | // EventQueue is required to dispatch events around |
harerimana | 8:3dd7a4231f6e | 35 | static EventQueue ev_queue; |
harerimana | 8:3dd7a4231f6e | 36 | // Constructing Mbed LoRaWANInterface and passing it down the radio object. |
harerimana | 8:3dd7a4231f6e | 37 | static LoRaWANInterface lorawan(radio); |
harerimana | 8:3dd7a4231f6e | 38 | // Application specific callbacks |
harerimana | 8:3dd7a4231f6e | 39 | static lorawan_app_callbacks_t callbacks; |
harerimana | 8:3dd7a4231f6e | 40 | // LoRaWAN stack event handler |
harerimana | 8:3dd7a4231f6e | 41 | static void lora_event_handler(lorawan_event_t event); |
harerimana | 8:3dd7a4231f6e | 42 | |
harerimana | 8:3dd7a4231f6e | 43 | // Send a message over LoRaWAN |
harerimana | 8:3dd7a4231f6e | 44 | static void send_message(long int timestamp, int value) { |
harerimana | 8:3dd7a4231f6e | 45 | uint8_t tx_buffer[50] = { 0 }; |
harerimana | 8:3dd7a4231f6e | 46 | // Sending strings over LoRaWAN is not recommended |
harerimana | 8:3dd7a4231f6e | 47 | sprintf((char*) tx_buffer, "{\"deviceId\":\"%s\", \"timestamp\":%ld,\"value\":%d}", DEVICEID, timestamp, value); |
harerimana | 8:3dd7a4231f6e | 48 | int packet_len = strlen((char*) tx_buffer); |
harerimana | 8:3dd7a4231f6e | 49 | printf("Sending %d bytes: \"%s\"\n", packet_len, tx_buffer); |
harerimana | 8:3dd7a4231f6e | 50 | int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, MSG_UNCONFIRMED_FLAG); |
harerimana | 8:3dd7a4231f6e | 51 | // 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 |
harerimana | 8:3dd7a4231f6e | 52 | if (retcode < 0) { |
harerimana | 8:3dd7a4231f6e | 53 | retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - duty cycle violation\n") |
harerimana | 8:3dd7a4231f6e | 54 | : printf("send() - Error code %d\n", retcode); |
harerimana | 8:3dd7a4231f6e | 55 | return; |
harerimana | 8:3dd7a4231f6e | 56 | } |
harerimana | 8:3dd7a4231f6e | 57 | printf("%d bytes scheduled for transmission\n", retcode); |
harerimana | 8:3dd7a4231f6e | 58 | } |
harerimana | 7:667534ecd34e | 59 | |
harerimana | 8:3dd7a4231f6e | 60 | int initialize_lora() { |
harerimana | 8:3dd7a4231f6e | 61 | 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) { |
harerimana | 8:3dd7a4231f6e | 62 | printf("Set your LoRaWAN credentials first!\n"); |
harerimana | 8:3dd7a4231f6e | 63 | return -1; |
harerimana | 8:3dd7a4231f6e | 64 | } |
harerimana | 8:3dd7a4231f6e | 65 | printf("Press BUTTON1 to send the current value of the temperature sensor!\n"); |
harerimana | 8:3dd7a4231f6e | 66 | // Enable trace output for this demo, so we can see what the LoRaWAN stack does |
harerimana | 8:3dd7a4231f6e | 67 | mbed_trace_init(); |
harerimana | 8:3dd7a4231f6e | 68 | if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { |
harerimana | 8:3dd7a4231f6e | 69 | printf("LoRa initialization failed!\n"); |
harerimana | 8:3dd7a4231f6e | 70 | return -1; |
harerimana | 8:3dd7a4231f6e | 71 | } |
harerimana | 8:3dd7a4231f6e | 72 | // prepare application callbacks |
harerimana | 8:3dd7a4231f6e | 73 | callbacks.events = mbed::callback(lora_event_handler); |
harerimana | 8:3dd7a4231f6e | 74 | lorawan.add_app_callbacks(&callbacks); |
harerimana | 8:3dd7a4231f6e | 75 | // Disable adaptive data rating |
harerimana | 8:3dd7a4231f6e | 76 | if (lorawan.disable_adaptive_datarate() != LORAWAN_STATUS_OK) { |
harerimana | 8:3dd7a4231f6e | 77 | printf("disable_adaptive_datarate failed!\n"); |
harerimana | 8:3dd7a4231f6e | 78 | return -1; |
harerimana | 8:3dd7a4231f6e | 79 | } |
harerimana | 8:3dd7a4231f6e | 80 | lorawan.set_datarate(5); // SF7BW125 |
harerimana | 8:3dd7a4231f6e | 81 | lorawan_connect_t connect_params; |
harerimana | 8:3dd7a4231f6e | 82 | connect_params.connect_type = LORAWAN_CONNECTION_OTAA; |
harerimana | 8:3dd7a4231f6e | 83 | connect_params.connection_u.otaa.dev_eui = DEV_EUI; |
harerimana | 8:3dd7a4231f6e | 84 | connect_params.connection_u.otaa.app_eui = APP_EUI; |
harerimana | 8:3dd7a4231f6e | 85 | connect_params.connection_u.otaa.app_key = APP_KEY; |
harerimana | 8:3dd7a4231f6e | 86 | connect_params.connection_u.otaa.nb_trials = 3; |
harerimana | 8:3dd7a4231f6e | 87 | |
harerimana | 8:3dd7a4231f6e | 88 | lorawan_status_t retcode = lorawan.connect(connect_params); |
harerimana | 8:3dd7a4231f6e | 89 | |
harerimana | 8:3dd7a4231f6e | 90 | if (retcode == LORAWAN_STATUS_OK || |
harerimana | 8:3dd7a4231f6e | 91 | retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { |
harerimana | 8:3dd7a4231f6e | 92 | } else { |
harerimana | 8:3dd7a4231f6e | 93 | printf("Connection error, code = %d\n", retcode); |
harerimana | 8:3dd7a4231f6e | 94 | return -1; |
harerimana | 8:3dd7a4231f6e | 95 | } |
harerimana | 8:3dd7a4231f6e | 96 | |
harerimana | 8:3dd7a4231f6e | 97 | printf("Connection - In Progress ...\r\n"); |
harerimana | 8:3dd7a4231f6e | 98 | // make your event queue dispatching events forever |
harerimana | 8:3dd7a4231f6e | 99 | ev_queue.dispatch_forever(); |
harerimana | 8:3dd7a4231f6e | 100 | return 0; |
harerimana | 8:3dd7a4231f6e | 101 | } |
harerimana | 7:667534ecd34e | 102 | |
harerimana | 7:667534ecd34e | 103 | /** |
harerimana | 7:667534ecd34e | 104 | * Blinking a yellow LED. |
harerimana | 7:667534ecd34e | 105 | * |
harerimana | 7:667534ecd34e | 106 | * @param duration length of time to blink . |
harerimana | 7:667534ecd34e | 107 | */ |
harerimana | 7:667534ecd34e | 108 | static void yellowblinking(int duration){ |
harerimana | 8:3dd7a4231f6e | 109 | // Data to send |
harerimana | 8:3dd7a4231f6e | 110 | // send_message("FELIX", 220014243, 1); |
harerimana | 7:667534ecd34e | 111 | int counter=0; |
harerimana | 7:667534ecd34e | 112 | while(counter < duration) |
harerimana | 0:392dea3408f2 | 113 | { |
harerimana | 7:667534ecd34e | 114 | counter+=1; |
harerimana | 7:667534ecd34e | 115 | yellowled = !yellowled; |
harerimana | 7:667534ecd34e | 116 | wait(1); |
harerimana | 7:667534ecd34e | 117 | } |
harerimana | 7:667534ecd34e | 118 | yellowled = 0; |
harerimana | 7:667534ecd34e | 119 | } |
harerimana | 7:667534ecd34e | 120 | |
harerimana | 7:667534ecd34e | 121 | /** |
harerimana | 7:667534ecd34e | 122 | * Crossing pedestrian. |
harerimana | 7:667534ecd34e | 123 | * |
harerimana | 7:667534ecd34e | 124 | * @param duration length of time for pedestrian to cross. |
harerimana | 7:667534ecd34e | 125 | */ |
harerimana | 7:667534ecd34e | 126 | static void pedestriancrossing(int duration){ |
harerimana | 8:3dd7a4231f6e | 127 | // Data to send |
harerimana | 8:3dd7a4231f6e | 128 | //send_message("FELIX", 220014243, 2); |
harerimana | 7:667534ecd34e | 129 | redled = 1; |
harerimana | 7:667534ecd34e | 130 | int counter=0; |
harerimana | 7:667534ecd34e | 131 | while(counter < duration) |
harerimana | 7:667534ecd34e | 132 | { |
harerimana | 7:667534ecd34e | 133 | counter+=1; |
harerimana | 7:667534ecd34e | 134 | wait(1); |
harerimana | 7:667534ecd34e | 135 | } |
harerimana | 7:667534ecd34e | 136 | redled = 0; |
harerimana | 7:667534ecd34e | 137 | yellowled = 1; |
harerimana | 7:667534ecd34e | 138 | } |
harerimana | 7:667534ecd34e | 139 | |
harerimana | 7:667534ecd34e | 140 | /** |
harerimana | 7:667534ecd34e | 141 | * Display content on LCD. |
harerimana | 7:667534ecd34e | 142 | * |
harerimana | 7:667534ecd34e | 143 | * @param advert The advertising text to be displayed. |
harerimana | 7:667534ecd34e | 144 | */ |
harerimana | 7:667534ecd34e | 145 | static void display(char *advert) { |
harerimana | 7:667534ecd34e | 146 | lcd.cls(); // Clear LCD |
harerimana | 7:667534ecd34e | 147 | lcd.locate(10, 5); // get cursor to position x=3px and y=5px |
harerimana | 7:667534ecd34e | 148 | lcd.printf(advert); // Write text into LCD buffer |
harerimana | 7:667534ecd34e | 149 | lcd.copy_to_lcd(); |
harerimana | 5:e1afaceb1196 | 150 | } |
harerimana | 5:e1afaceb1196 | 151 | |
harerimana | 5:e1afaceb1196 | 152 | |
harerimana | 7:667534ecd34e | 153 | /** |
harerimana | 7:667534ecd34e | 154 | * Callback (Interrupt Service Routine) when a bouton is clicked. |
harerimana | 7:667534ecd34e | 155 | * |
harerimana | 7:667534ecd34e | 156 | */ |
harerimana | 7:667534ecd34e | 157 | void pedestrian_isr() { |
harerimana | 7:667534ecd34e | 158 | wait(1); |
harerimana | 7:667534ecd34e | 159 | if (state == 0) // YELLOW IS ON AND RED IS OFF |
harerimana | 7:667534ecd34e | 160 | { |
harerimana | 7:667534ecd34e | 161 | state = 1; // YELLOW IS BLINKING AND RED IS OFF |
harerimana | 8:3dd7a4231f6e | 162 | send_message(220014243, state); |
harerimana | 7:667534ecd34e | 163 | display(pedestrianAdvert); |
harerimana | 7:667534ecd34e | 164 | yellowblinking(YELLOWBLINKINGDURATION); |
harerimana | 7:667534ecd34e | 165 | wait(1); |
harerimana | 7:667534ecd34e | 166 | state = 2; // YELLOW IS OFF AND RED IS ON |
harerimana | 8:3dd7a4231f6e | 167 | send_message(220014243, state); |
harerimana | 7:667534ecd34e | 168 | display(carDriverAdvert); |
harerimana | 7:667534ecd34e | 169 | pedestriancrossing(REDLIGHTONDURATION); |
harerimana | 7:667534ecd34e | 170 | state = 0; |
harerimana | 8:3dd7a4231f6e | 171 | send_message(220014243, state); |
harerimana | 7:667534ecd34e | 172 | lcd.cls(); |
harerimana | 7:667534ecd34e | 173 | } |
harerimana | 7:667534ecd34e | 174 | if(state == 2) // RED |
harerimana | 7:667534ecd34e | 175 | { |
harerimana | 7:667534ecd34e | 176 | // extend the duration of RED by 5 seconds |
harerimana | 7:667534ecd34e | 177 | } |
harerimana | 7:667534ecd34e | 178 | } |
harerimana | 7:667534ecd34e | 179 | |
harerimana | 7:667534ecd34e | 180 | int main() { |
harerimana | 8:3dd7a4231f6e | 181 | pedestrianbutton.rise(ev_queue.event(&pedestrian_isr)); // Registering an ISR for button click |
harerimana | 8:3dd7a4231f6e | 182 | initialize_lora(); |
harerimana | 8:3dd7a4231f6e | 183 | // Data to send |
harerimana | 8:3dd7a4231f6e | 184 | //send_message("FELIX", 220014243, 0); |
harerimana | 7:667534ecd34e | 185 | } |
harerimana | 7:667534ecd34e | 186 | |
harerimana | 8:3dd7a4231f6e | 187 | // Event handler |
harerimana | 8:3dd7a4231f6e | 188 | static void lora_event_handler(lorawan_event_t event) { |
harerimana | 8:3dd7a4231f6e | 189 | switch (event) { |
harerimana | 8:3dd7a4231f6e | 190 | case CONNECTED: |
harerimana | 8:3dd7a4231f6e | 191 | printf("Connection - Successful\n"); |
harerimana | 8:3dd7a4231f6e | 192 | break; |
harerimana | 8:3dd7a4231f6e | 193 | case DISCONNECTED: |
harerimana | 8:3dd7a4231f6e | 194 | ev_queue.break_dispatch(); |
harerimana | 8:3dd7a4231f6e | 195 | printf("Disconnected Successfully\n"); |
harerimana | 8:3dd7a4231f6e | 196 | break; |
harerimana | 8:3dd7a4231f6e | 197 | case TX_DONE: |
harerimana | 8:3dd7a4231f6e | 198 | printf("Message Sent to Network Server\n"); |
harerimana | 8:3dd7a4231f6e | 199 | break; |
harerimana | 8:3dd7a4231f6e | 200 | case TX_TIMEOUT: |
harerimana | 8:3dd7a4231f6e | 201 | case TX_ERROR: |
harerimana | 8:3dd7a4231f6e | 202 | case TX_CRYPTO_ERROR: |
harerimana | 8:3dd7a4231f6e | 203 | case TX_SCHEDULING_ERROR: |
harerimana | 8:3dd7a4231f6e | 204 | printf("Transmission Error - EventCode = %d\n", event); |
harerimana | 8:3dd7a4231f6e | 205 | break; |
harerimana | 8:3dd7a4231f6e | 206 | case JOIN_FAILURE: |
harerimana | 8:3dd7a4231f6e | 207 | printf("OTAA Failed - Check Keys\n"); |
harerimana | 8:3dd7a4231f6e | 208 | break; |
harerimana | 8:3dd7a4231f6e | 209 | default: |
harerimana | 8:3dd7a4231f6e | 210 | MBED_ASSERT("Unknown Event"); |
harerimana | 8:3dd7a4231f6e | 211 | } |
harerimana | 8:3dd7a4231f6e | 212 | } |