![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
tfsd
Dependencies: Servo GroveGPS Cayenne-LPP driver_mbed_TH02
main.cpp@65:724a61ad654c, 2022-03-29 (annotated)
- Committer:
- younousleconquerant
- Date:
- Tue Mar 29 09:43:58 2022 +0000
- Revision:
- 65:724a61ad654c
- Parent:
- 64:4cd8fed3f437
12;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:7037ed05f54f | 1 | /** |
mbed_official | 0:7037ed05f54f | 2 | * Copyright (c) 2017, Arm Limited and affiliates. |
mbed_official | 0:7037ed05f54f | 3 | * SPDX-License-Identifier: Apache-2.0 |
mbed_official | 0:7037ed05f54f | 4 | * |
mbed_official | 0:7037ed05f54f | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 0:7037ed05f54f | 6 | * you may not use this file except in compliance with the License. |
mbed_official | 0:7037ed05f54f | 7 | * You may obtain a copy of the License at |
mbed_official | 0:7037ed05f54f | 8 | * |
mbed_official | 0:7037ed05f54f | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 0:7037ed05f54f | 10 | * |
mbed_official | 0:7037ed05f54f | 11 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 0:7037ed05f54f | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 0:7037ed05f54f | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 0:7037ed05f54f | 14 | * See the License for the specific language governing permissions and |
mbed_official | 0:7037ed05f54f | 15 | * limitations under the License. |
mbed_official | 0:7037ed05f54f | 16 | */ |
mbed_official | 0:7037ed05f54f | 17 | #include <stdio.h> |
younousleconquerant | 64:4cd8fed3f437 | 18 | #include <stdlib.h> // <cstdlib> en C++ |
younousleconquerant | 64:4cd8fed3f437 | 19 | #include "driver_mbed_TH02.h" |
younousleconquerant | 64:4cd8fed3f437 | 20 | #include "GroveGPS.h" |
superphil06 | 57:5ec0379d69d6 | 21 | #include "CayenneLPP.h" |
younousleconquerant | 64:4cd8fed3f437 | 22 | |
younousleconquerant | 64:4cd8fed3f437 | 23 | #include "MFRC522.h" |
younousleconquerant | 64:4cd8fed3f437 | 24 | |
mbed_official | 0:7037ed05f54f | 25 | #include "lorawan/LoRaWANInterface.h" |
mbed_official | 0:7037ed05f54f | 26 | #include "lorawan/system/lorawan_data_structures.h" |
mbed_official | 0:7037ed05f54f | 27 | #include "events/EventQueue.h" |
mbed_official | 0:7037ed05f54f | 28 | |
mbed_official | 0:7037ed05f54f | 29 | // Application helpers |
mbed_official | 0:7037ed05f54f | 30 | #include "DummySensor.h" |
mbed_official | 0:7037ed05f54f | 31 | #include "trace_helper.h" |
mbed_official | 0:7037ed05f54f | 32 | #include "lora_radio_helper.h" |
superphil06 | 58:81c66fac6476 | 33 | #include "Servo.h" |
mbed_official | 0:7037ed05f54f | 34 | |
mbed_official | 0:7037ed05f54f | 35 | using namespace events; |
mbed_official | 0:7037ed05f54f | 36 | |
mbed_official | 12:5015dfead3f2 | 37 | // Max payload size can be LORAMAC_PHY_MAXPAYLOAD. |
mbed_official | 12:5015dfead3f2 | 38 | // This example only communicates with much shorter messages (<30 bytes). |
mbed_official | 12:5015dfead3f2 | 39 | // If longer messages are used, these buffers must be changed accordingly. |
mbed_official | 12:5015dfead3f2 | 40 | uint8_t tx_buffer[30]; |
mbed_official | 12:5015dfead3f2 | 41 | uint8_t rx_buffer[30]; |
mbed_official | 0:7037ed05f54f | 42 | |
superphil06 | 57:5ec0379d69d6 | 43 | #define MAX_SIZE 200 // depends on spreading factor and frequency used |
superphil06 | 57:5ec0379d69d6 | 44 | |
superphil06 | 57:5ec0379d69d6 | 45 | CayenneLPP Payload(MAX_SIZE); |
superphil06 | 57:5ec0379d69d6 | 46 | |
superphil06 | 57:5ec0379d69d6 | 47 | float celsius = -4.1; |
superphil06 | 57:5ec0379d69d6 | 48 | float accel[] = {1.234, -1.234, 0}; |
superphil06 | 57:5ec0379d69d6 | 49 | float rh = 30; |
superphil06 | 57:5ec0379d69d6 | 50 | float hpa = 1014.1; |
superphil06 | 57:5ec0379d69d6 | 51 | float latitude = 42.3519; |
superphil06 | 57:5ec0379d69d6 | 52 | float longitude = -87.9094; |
superphil06 | 57:5ec0379d69d6 | 53 | float altitude=10; |
superphil06 | 57:5ec0379d69d6 | 54 | |
superphil06 | 57:5ec0379d69d6 | 55 | int size = 0; |
superphil06 | 57:5ec0379d69d6 | 56 | |
superphil06 | 57:5ec0379d69d6 | 57 | |
superphil06 | 58:81c66fac6476 | 58 | DigitalOut Alarme (PC_13);// alarme LED output |
superphil06 | 58:81c66fac6476 | 59 | Servo Myservo(PA_7); //servomotor output |
younousleconquerant | 64:4cd8fed3f437 | 60 | GroveGPS MyGPS(PA_9, PA_10); |
younousleconquerant | 64:4cd8fed3f437 | 61 | |
younousleconquerant | 64:4cd8fed3f437 | 62 | #define MF_RESET D9 //Reset D7 |
younousleconquerant | 64:4cd8fed3f437 | 63 | #define SPI_MOSI D11 //MOSI |
younousleconquerant | 64:4cd8fed3f437 | 64 | #define SPI_MISO D12 //MISO |
younousleconquerant | 64:4cd8fed3f437 | 65 | #define SPI_SCK D13 //Clock |
younousleconquerant | 64:4cd8fed3f437 | 66 | #define SPI_CS D7 //Chip Select D9 |
younousleconquerant | 64:4cd8fed3f437 | 67 | MFRC522 RfChip(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET); |
younousleconquerant | 64:4cd8fed3f437 | 68 | //driver_mbed_TH02 MyTH02 (PB_9,PB_8);// connect hsensor on RX2 TX2 |
mbed_official | 0:7037ed05f54f | 69 | /* |
mbed_official | 0:7037ed05f54f | 70 | * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing |
mbed_official | 0:7037ed05f54f | 71 | */ |
superphil06 | 59:7b58c5775a56 | 72 | #define TX_TIMER 20000 |
mbed_official | 0:7037ed05f54f | 73 | |
mbed_official | 0:7037ed05f54f | 74 | /** |
mbed_official | 0:7037ed05f54f | 75 | * Maximum number of events for the event queue. |
mbed_official | 12:5015dfead3f2 | 76 | * 10 is the safe number for the stack events, however, if application |
mbed_official | 0:7037ed05f54f | 77 | * also uses the queue for whatever purposes, this number should be increased. |
mbed_official | 0:7037ed05f54f | 78 | */ |
superphil06 | 59:7b58c5775a56 | 79 | #define MAX_NUMBER_OF_EVENTS 30 |
mbed_official | 0:7037ed05f54f | 80 | |
mbed_official | 0:7037ed05f54f | 81 | /** |
mbed_official | 0:7037ed05f54f | 82 | * Maximum number of retries for CONFIRMED messages before giving up |
mbed_official | 0:7037ed05f54f | 83 | */ |
mbed_official | 0:7037ed05f54f | 84 | #define CONFIRMED_MSG_RETRY_COUNTER 3 |
mbed_official | 0:7037ed05f54f | 85 | |
mbed_official | 0:7037ed05f54f | 86 | /** |
mbed_official | 0:7037ed05f54f | 87 | * Dummy pin for dummy sensor |
mbed_official | 0:7037ed05f54f | 88 | */ |
mbed_official | 0:7037ed05f54f | 89 | #define PC_9 0 |
mbed_official | 0:7037ed05f54f | 90 | |
mbed_official | 0:7037ed05f54f | 91 | /** |
mbed_official | 0:7037ed05f54f | 92 | * Dummy sensor class object |
mbed_official | 0:7037ed05f54f | 93 | */ |
mbed_official | 0:7037ed05f54f | 94 | DS1820 ds1820(PC_9); |
mbed_official | 0:7037ed05f54f | 95 | |
mbed_official | 0:7037ed05f54f | 96 | /** |
mbed_official | 0:7037ed05f54f | 97 | * This event queue is the global event queue for both the |
mbed_official | 0:7037ed05f54f | 98 | * application and stack. To conserve memory, the stack is designed to run |
mbed_official | 0:7037ed05f54f | 99 | * in the same thread as the application and the application is responsible for |
mbed_official | 0:7037ed05f54f | 100 | * providing an event queue to the stack that will be used for ISR deferment as |
mbed_official | 0:7037ed05f54f | 101 | * well as application information event queuing. |
mbed_official | 0:7037ed05f54f | 102 | */ |
mbed_official | 46:a124538e2891 | 103 | static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE); |
mbed_official | 0:7037ed05f54f | 104 | |
mbed_official | 0:7037ed05f54f | 105 | /** |
mbed_official | 0:7037ed05f54f | 106 | * Event handler. |
mbed_official | 0:7037ed05f54f | 107 | * |
mbed_official | 0:7037ed05f54f | 108 | * This will be passed to the LoRaWAN stack to queue events for the |
mbed_official | 0:7037ed05f54f | 109 | * application which in turn drive the application. |
mbed_official | 0:7037ed05f54f | 110 | */ |
mbed_official | 0:7037ed05f54f | 111 | static void lora_event_handler(lorawan_event_t event); |
mbed_official | 0:7037ed05f54f | 112 | |
mbed_official | 0:7037ed05f54f | 113 | /** |
mbed_official | 46:a124538e2891 | 114 | * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper. |
mbed_official | 0:7037ed05f54f | 115 | */ |
mbed_official | 2:dc95ac6d6d4e | 116 | static LoRaWANInterface lorawan(radio); |
mbed_official | 0:7037ed05f54f | 117 | |
mbed_official | 0:7037ed05f54f | 118 | /** |
mbed_official | 0:7037ed05f54f | 119 | * Application specific callbacks |
mbed_official | 0:7037ed05f54f | 120 | */ |
mbed_official | 0:7037ed05f54f | 121 | static lorawan_app_callbacks_t callbacks; |
mbed_official | 0:7037ed05f54f | 122 | |
superphil06 | 58:81c66fac6476 | 123 | void servo(uint8_t uAngle) |
superphil06 | 58:81c66fac6476 | 124 | { |
younousleconquerant | 64:4cd8fed3f437 | 125 | |
younousleconquerant | 64:4cd8fed3f437 | 126 | } |
superphil06 | 58:81c66fac6476 | 127 | |
superphil06 | 58:81c66fac6476 | 128 | |
younousleconquerant | 64:4cd8fed3f437 | 129 | double tempo = 0.3; // LED blinking delay |
younousleconquerant | 64:4cd8fed3f437 | 130 | |
younousleconquerant | 64:4cd8fed3f437 | 131 | // Change LEDs blinking frequency |
younousleconquerant | 64:4cd8fed3f437 | 132 | void change_blinking_frequency() |
younousleconquerant | 64:4cd8fed3f437 | 133 | { |
younousleconquerant | 64:4cd8fed3f437 | 134 | if (tempo == 0.3) // If leds have low frequency |
younousleconquerant | 64:4cd8fed3f437 | 135 | tempo = 0.1; // Set the fast frequency |
younousleconquerant | 64:4cd8fed3f437 | 136 | else // If leds have fast frequency |
younousleconquerant | 64:4cd8fed3f437 | 137 | tempo = 0.3; // Set the low frequency |
younousleconquerant | 64:4cd8fed3f437 | 138 | } |
superphil06 | 58:81c66fac6476 | 139 | |
mbed_official | 0:7037ed05f54f | 140 | /** |
mbed_official | 0:7037ed05f54f | 141 | * Entry point for application |
mbed_official | 0:7037ed05f54f | 142 | */ |
mbed_official | 46:a124538e2891 | 143 | int main(void) |
mbed_official | 0:7037ed05f54f | 144 | { |
mbed_official | 0:7037ed05f54f | 145 | // setup tracing |
mbed_official | 0:7037ed05f54f | 146 | setup_trace(); |
younousleconquerant | 64:4cd8fed3f437 | 147 | // th02 temerature sensor section |
superphil06 | 58:81c66fac6476 | 148 | int iTemp,iTime,iTempbrute,iRH,iRHbrute; |
younousleconquerant | 64:4cd8fed3f437 | 149 | // Myservo.calibrate(0.0005, 45); |
superphil06 | 58:81c66fac6476 | 150 | printf ("\n\r start reading TH02 for first time"); |
younousleconquerant | 64:4cd8fed3f437 | 151 | //myTH02.startTempConv(true,true); |
younousleconquerant | 64:4cd8fed3f437 | 152 | printf("\r\n*********************Initialization**********************************"); |
younousleconquerant | 64:4cd8fed3f437 | 153 | RfChip.PCD_Init(); /* Init. RC522 Chip*/ |
younousleconquerant | 65:724a61ad654c | 154 | printf("\r\n**********************Starting Reading Tags**************************"); |
mbed_official | 0:7037ed05f54f | 155 | |
mbed_official | 0:7037ed05f54f | 156 | // stores the status of a call to LoRaWAN protocol |
mbed_official | 0:7037ed05f54f | 157 | lorawan_status_t retcode; |
mbed_official | 0:7037ed05f54f | 158 | |
mbed_official | 0:7037ed05f54f | 159 | // Initialize LoRaWAN stack |
mbed_official | 2:dc95ac6d6d4e | 160 | if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 161 | printf("\r\n LoRa initialization failed! \r\n"); |
mbed_official | 0:7037ed05f54f | 162 | return -1; |
mbed_official | 0:7037ed05f54f | 163 | } |
mbed_official | 0:7037ed05f54f | 164 | |
mbed_official | 0:7037ed05f54f | 165 | printf("\r\n Mbed LoRaWANStack initialized \r\n"); |
mbed_official | 0:7037ed05f54f | 166 | |
mbed_official | 0:7037ed05f54f | 167 | // prepare application callbacks |
mbed_official | 0:7037ed05f54f | 168 | callbacks.events = mbed::callback(lora_event_handler); |
mbed_official | 2:dc95ac6d6d4e | 169 | lorawan.add_app_callbacks(&callbacks); |
mbed_official | 0:7037ed05f54f | 170 | |
mbed_official | 0:7037ed05f54f | 171 | // Set number of retries in case of CONFIRMED messages |
mbed_official | 2:dc95ac6d6d4e | 172 | if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) |
mbed_official | 46:a124538e2891 | 173 | != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 174 | printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); |
mbed_official | 0:7037ed05f54f | 175 | return -1; |
mbed_official | 0:7037ed05f54f | 176 | } |
mbed_official | 0:7037ed05f54f | 177 | |
mbed_official | 0:7037ed05f54f | 178 | printf("\r\n CONFIRMED message retries : %d \r\n", |
mbed_official | 0:7037ed05f54f | 179 | CONFIRMED_MSG_RETRY_COUNTER); |
mbed_official | 0:7037ed05f54f | 180 | |
mbed_official | 0:7037ed05f54f | 181 | // Enable adaptive data rate |
mbed_official | 2:dc95ac6d6d4e | 182 | if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 183 | printf("\r\n enable_adaptive_datarate failed! \r\n"); |
mbed_official | 0:7037ed05f54f | 184 | return -1; |
mbed_official | 0:7037ed05f54f | 185 | } |
mbed_official | 0:7037ed05f54f | 186 | |
mbed_official | 0:7037ed05f54f | 187 | printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); |
mbed_official | 0:7037ed05f54f | 188 | |
mbed_official | 2:dc95ac6d6d4e | 189 | retcode = lorawan.connect(); |
mbed_official | 0:7037ed05f54f | 190 | |
mbed_official | 0:7037ed05f54f | 191 | if (retcode == LORAWAN_STATUS_OK || |
mbed_official | 46:a124538e2891 | 192 | retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { |
mbed_official | 0:7037ed05f54f | 193 | } else { |
mbed_official | 0:7037ed05f54f | 194 | printf("\r\n Connection error, code = %d \r\n", retcode); |
mbed_official | 0:7037ed05f54f | 195 | return -1; |
mbed_official | 0:7037ed05f54f | 196 | } |
mbed_official | 0:7037ed05f54f | 197 | |
mbed_official | 0:7037ed05f54f | 198 | printf("\r\n Connection - In Progress ...\r\n"); |
mbed_official | 0:7037ed05f54f | 199 | |
mbed_official | 0:7037ed05f54f | 200 | // make your event queue dispatching events forever |
mbed_official | 0:7037ed05f54f | 201 | ev_queue.dispatch_forever(); |
mbed_official | 3:8c7198d1a2a1 | 202 | |
mbed_official | 3:8c7198d1a2a1 | 203 | return 0; |
mbed_official | 0:7037ed05f54f | 204 | } |
mbed_official | 0:7037ed05f54f | 205 | |
superphil06 | 61:3cb7cf8226cf | 206 | /************************************************************************************************************* |
mbed_official | 0:7037ed05f54f | 207 | * Sends a message to the Network Server |
superphil06 | 61:3cb7cf8226cf | 208 | *************************************************************************************************************/ |
younousleconquerant | 64:4cd8fed3f437 | 209 | |
younousleconquerant | 65:724a61ad654c | 210 | |
mbed_official | 0:7037ed05f54f | 211 | static void send_message() |
younousleconquerant | 64:4cd8fed3f437 | 212 | { |
younousleconquerant | 64:4cd8fed3f437 | 213 | int iTime,iTempbrute,iRHbrute; |
younousleconquerant | 64:4cd8fed3f437 | 214 | float fTemp,fRH; |
younousleconquerant | 64:4cd8fed3f437 | 215 | uint16_t packet_len; |
superphil06 | 58:81c66fac6476 | 216 | int16_t retcode; |
superphil06 | 58:81c66fac6476 | 217 | int32_t sensor_value, rh_value; |
younousleconquerant | 64:4cd8fed3f437 | 218 | float fHumid; |
younousleconquerant | 64:4cd8fed3f437 | 219 | fTemp=myTH02.ReadTemperature(); |
younousleconquerant | 64:4cd8fed3f437 | 220 | printf("\r\n temp=%f",fTemp); |
younousleconquerant | 64:4cd8fed3f437 | 221 | fHumid=myTH02.ReadHumidity(); |
younousleconquerant | 64:4cd8fed3f437 | 222 | printf("\r\n humid=%f",fHumid); |
superphil06 | 57:5ec0379d69d6 | 223 | |
younousleconquerant | 64:4cd8fed3f437 | 224 | //GPS |
younousleconquerant | 64:4cd8fed3f437 | 225 | char gpsLat[16]; |
younousleconquerant | 64:4cd8fed3f437 | 226 | char gpsLon[16]; |
younousleconquerant | 64:4cd8fed3f437 | 227 | char gpsalt[16]; |
younousleconquerant | 64:4cd8fed3f437 | 228 | MyGPS.getLatitude(gpsLat); |
younousleconquerant | 64:4cd8fed3f437 | 229 | MyGPS.getLongitude(gpsLon); |
younousleconquerant | 64:4cd8fed3f437 | 230 | MyGPS.getAltitude(gpsalt); |
younousleconquerant | 64:4cd8fed3f437 | 231 | |
younousleconquerant | 64:4cd8fed3f437 | 232 | printf("\r\n gpsLatitude=%s", gpsLat); |
younousleconquerant | 64:4cd8fed3f437 | 233 | printf("\r\n gpsLongitude=%s", gpsLon); |
younousleconquerant | 64:4cd8fed3f437 | 234 | printf("\r\n gpsAltitude=%s", gpsalt); |
younousleconquerant | 65:724a61ad654c | 235 | |
mbed_official | 0:7037ed05f54f | 236 | |
younousleconquerant | 64:4cd8fed3f437 | 237 | // Select one of the cards |
younousleconquerant | 65:724a61ad654c | 238 | wait_ms(500); |
younousleconquerant | 65:724a61ad654c | 239 | if (!RfChip.PICC_ReadCardSerial()) { |
younousleconquerant | 65:724a61ad654c | 240 | wait_ms(500); |
younousleconquerant | 65:724a61ad654c | 241 | printf("\r\nReading a card"); |
younousleconquerant | 65:724a61ad654c | 242 | if (RfChip.PICC_IsNewCardPresent()) { |
younousleconquerant | 65:724a61ad654c | 243 | // Print Card UID |
younousleconquerant | 65:724a61ad654c | 244 | printf("\r\nNew card UID: "); |
younousleconquerant | 65:724a61ad654c | 245 | for (uint8_t i = 0; i < RfChip.uid.size; i++) { |
younousleconquerant | 65:724a61ad654c | 246 | printf(" %X02", RfChip.uid.uidByte[i]); |
younousleconquerant | 65:724a61ad654c | 247 | } |
younousleconquerant | 65:724a61ad654c | 248 | printf("\r\n"); |
younousleconquerant | 65:724a61ad654c | 249 | } |
younousleconquerant | 64:4cd8fed3f437 | 250 | } |
younousleconquerant | 65:724a61ad654c | 251 | |
younousleconquerant | 65:724a61ad654c | 252 | // Print Card type |
younousleconquerant | 65:724a61ad654c | 253 | //uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); |
younousleconquerant | 65:724a61ad654c | 254 | // printf("PICC Type: %s \r\n", RfChip.PICC_GetTypeName(piccType)); |
younousleconquerant | 65:724a61ad654c | 255 | //strcat(nfcData, RfChip.PICC_GetTypeName(piccType)); |
younousleconquerant | 65:724a61ad654c | 256 | //printf("DATA send: %s \r\n", nfcData); |
younousleconquerant | 65:724a61ad654c | 257 | //wait_ms(1000); |
younousleconquerant | 64:4cd8fed3f437 | 258 | |
superphil06 | 57:5ec0379d69d6 | 259 | Payload.reset(); |
younousleconquerant | 64:4cd8fed3f437 | 260 | size = Payload.addTemperature(1, (float) fTemp); |
younousleconquerant | 64:4cd8fed3f437 | 261 | size = size + Payload.addRelativeHumidity(2, fHumid); |
younousleconquerant | 64:4cd8fed3f437 | 262 | size = size + Payload.addGPS(3, atof(gpsLat), atof(gpsLon), atof(gpsalt)); |
younousleconquerant | 65:724a61ad654c | 263 | size = size + Payload.addNFC(4, (char *)RfChip.uid.uidByte); |
superphil06 | 58:81c66fac6476 | 264 | |
superphil06 | 61:3cb7cf8226cf | 265 | // send complete message with cayenne format |
younousleconquerant | 64:4cd8fed3f437 | 266 | retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, Payload.getBuffer(), Payload.getSize(), |
younousleconquerant | 64:4cd8fed3f437 | 267 | MSG_UNCONFIRMED_FLAG); |
mbed_official | 0:7037ed05f54f | 268 | |
mbed_official | 0:7037ed05f54f | 269 | if (retcode < 0) { |
mbed_official | 0:7037ed05f54f | 270 | retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") |
mbed_official | 46:a124538e2891 | 271 | : printf("\r\n send() - Error code %d \r\n", retcode); |
mbed_official | 26:f07f5febf97f | 272 | |
mbed_official | 26:f07f5febf97f | 273 | if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { |
mbed_official | 26:f07f5febf97f | 274 | //retry in 3 seconds |
mbed_official | 26:f07f5febf97f | 275 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 26:f07f5febf97f | 276 | ev_queue.call_in(3000, send_message); |
mbed_official | 26:f07f5febf97f | 277 | } |
mbed_official | 26:f07f5febf97f | 278 | } |
mbed_official | 0:7037ed05f54f | 279 | return; |
mbed_official | 0:7037ed05f54f | 280 | } |
mbed_official | 0:7037ed05f54f | 281 | |
mbed_official | 0:7037ed05f54f | 282 | printf("\r\n %d bytes scheduled for transmission \r\n", retcode); |
mbed_official | 12:5015dfead3f2 | 283 | memset(tx_buffer, 0, sizeof(tx_buffer)); |
mbed_official | 0:7037ed05f54f | 284 | } |
mbed_official | 0:7037ed05f54f | 285 | |
mbed_official | 0:7037ed05f54f | 286 | /** |
mbed_official | 0:7037ed05f54f | 287 | * Receive a message from the Network Server |
mbed_official | 0:7037ed05f54f | 288 | */ |
mbed_official | 0:7037ed05f54f | 289 | static void receive_message() |
younousleconquerant | 64:4cd8fed3f437 | 290 | { |
younousleconquerant | 64:4cd8fed3f437 | 291 | int num_port, iPosition=0,iIndex,iEtatAlarme; |
mbed_official | 47:b6d132f1079f | 292 | uint8_t port; |
mbed_official | 47:b6d132f1079f | 293 | int flags; |
mbed_official | 47:b6d132f1079f | 294 | int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags); |
mbed_official | 0:7037ed05f54f | 295 | |
mbed_official | 0:7037ed05f54f | 296 | if (retcode < 0) { |
mbed_official | 0:7037ed05f54f | 297 | printf("\r\n receive() - Error code %d \r\n", retcode); |
mbed_official | 0:7037ed05f54f | 298 | return; |
mbed_official | 0:7037ed05f54f | 299 | } |
mbed_official | 0:7037ed05f54f | 300 | |
mbed_official | 47:b6d132f1079f | 301 | printf(" RX Data on port %u (%d bytes): ", port, retcode); |
mbed_official | 0:7037ed05f54f | 302 | for (uint8_t i = 0; i < retcode; i++) { |
superphil06 | 59:7b58c5775a56 | 303 | printf("%02x", rx_buffer[i]); |
mbed_official | 0:7037ed05f54f | 304 | } |
younousleconquerant | 64:4cd8fed3f437 | 305 | //printf("\n test value=%d", port); |
younousleconquerant | 64:4cd8fed3f437 | 306 | // *****************************code todo here ******************************************** |
younousleconquerant | 64:4cd8fed3f437 | 307 | switch (port) { |
superphil06 | 61:3cb7cf8226cf | 308 | case 3: // control led |
younousleconquerant | 64:4cd8fed3f437 | 309 | printf("\n led=%d", (int)rx_buffer[0]); |
younousleconquerant | 64:4cd8fed3f437 | 310 | if ((rx_buffer[0]-0x30)==0) |
younousleconquerant | 64:4cd8fed3f437 | 311 | |
younousleconquerant | 64:4cd8fed3f437 | 312 | iEtatAlarme=0; |
younousleconquerant | 64:4cd8fed3f437 | 313 | else iEtatAlarme=1; |
superphil06 | 62:10ba6387b44c | 314 | Alarme.write(iEtatAlarme); |
younousleconquerant | 64:4cd8fed3f437 | 315 | |
younousleconquerant | 64:4cd8fed3f437 | 316 | printf("\n alarme=%d",iEtatAlarme); |
younousleconquerant | 64:4cd8fed3f437 | 317 | break; |
superphil06 | 61:3cb7cf8226cf | 318 | case 4:// control servomotor |
younousleconquerant | 64:4cd8fed3f437 | 319 | for (iIndex=0; iIndex<retcode; iIndex++) { |
younousleconquerant | 64:4cd8fed3f437 | 320 | iPosition=iPosition*10+(rx_buffer[iIndex]-0x30);// convert receive string to angular position |
younousleconquerant | 64:4cd8fed3f437 | 321 | } |
younousleconquerant | 64:4cd8fed3f437 | 322 | |
younousleconquerant | 64:4cd8fed3f437 | 323 | |
younousleconquerant | 64:4cd8fed3f437 | 324 | printf("\n servo position =%d",iPosition); |
younousleconquerant | 64:4cd8fed3f437 | 325 | Myservo.position ( iPosition-45 ); // set servo motor position from 0 to 180 |
younousleconquerant | 64:4cd8fed3f437 | 326 | break; |
younousleconquerant | 64:4cd8fed3f437 | 327 | default: |
younousleconquerant | 64:4cd8fed3f437 | 328 | printf("\n port inconnu =%d",(int)port); |
younousleconquerant | 64:4cd8fed3f437 | 329 | break; |
younousleconquerant | 64:4cd8fed3f437 | 330 | } |
younousleconquerant | 64:4cd8fed3f437 | 331 | |
younousleconquerant | 64:4cd8fed3f437 | 332 | |
younousleconquerant | 64:4cd8fed3f437 | 333 | // ***************************** end code todo here ***************************************** |
younousleconquerant | 64:4cd8fed3f437 | 334 | |
younousleconquerant | 64:4cd8fed3f437 | 335 | |
mbed_official | 12:5015dfead3f2 | 336 | memset(rx_buffer, 0, sizeof(rx_buffer)); |
mbed_official | 0:7037ed05f54f | 337 | } |
mbed_official | 0:7037ed05f54f | 338 | |
mbed_official | 0:7037ed05f54f | 339 | /** |
mbed_official | 0:7037ed05f54f | 340 | * Event handler |
mbed_official | 0:7037ed05f54f | 341 | */ |
mbed_official | 0:7037ed05f54f | 342 | static void lora_event_handler(lorawan_event_t event) |
mbed_official | 0:7037ed05f54f | 343 | { |
mbed_official | 0:7037ed05f54f | 344 | switch (event) { |
mbed_official | 0:7037ed05f54f | 345 | case CONNECTED: |
mbed_official | 0:7037ed05f54f | 346 | printf("\r\n Connection - Successful \r\n"); |
mbed_official | 0:7037ed05f54f | 347 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 348 | send_message(); |
mbed_official | 0:7037ed05f54f | 349 | } else { |
mbed_official | 0:7037ed05f54f | 350 | ev_queue.call_every(TX_TIMER, send_message); |
mbed_official | 0:7037ed05f54f | 351 | } |
mbed_official | 0:7037ed05f54f | 352 | |
mbed_official | 0:7037ed05f54f | 353 | break; |
mbed_official | 0:7037ed05f54f | 354 | case DISCONNECTED: |
mbed_official | 0:7037ed05f54f | 355 | ev_queue.break_dispatch(); |
mbed_official | 0:7037ed05f54f | 356 | printf("\r\n Disconnected Successfully \r\n"); |
mbed_official | 0:7037ed05f54f | 357 | break; |
mbed_official | 0:7037ed05f54f | 358 | case TX_DONE: |
mbed_official | 0:7037ed05f54f | 359 | printf("\r\n Message Sent to Network Server \r\n"); |
mbed_official | 0:7037ed05f54f | 360 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 361 | send_message(); |
mbed_official | 0:7037ed05f54f | 362 | } |
mbed_official | 0:7037ed05f54f | 363 | break; |
mbed_official | 0:7037ed05f54f | 364 | case TX_TIMEOUT: |
mbed_official | 0:7037ed05f54f | 365 | case TX_ERROR: |
mbed_official | 0:7037ed05f54f | 366 | case TX_CRYPTO_ERROR: |
mbed_official | 0:7037ed05f54f | 367 | case TX_SCHEDULING_ERROR: |
mbed_official | 0:7037ed05f54f | 368 | printf("\r\n Transmission Error - EventCode = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 369 | // try again |
mbed_official | 0:7037ed05f54f | 370 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 371 | send_message(); |
mbed_official | 0:7037ed05f54f | 372 | } |
mbed_official | 0:7037ed05f54f | 373 | break; |
mbed_official | 0:7037ed05f54f | 374 | case RX_DONE: |
mbed_official | 0:7037ed05f54f | 375 | printf("\r\n Received message from Network Server \r\n"); |
mbed_official | 0:7037ed05f54f | 376 | receive_message(); |
mbed_official | 0:7037ed05f54f | 377 | break; |
mbed_official | 0:7037ed05f54f | 378 | case RX_TIMEOUT: |
younousleconquerant | 64:4cd8fed3f437 | 379 | printf("\r\n timeout in reception - Code = %d \r\n", event); |
superphil06 | 59:7b58c5775a56 | 380 | break; |
mbed_official | 0:7037ed05f54f | 381 | case RX_ERROR: |
mbed_official | 0:7037ed05f54f | 382 | printf("\r\n Error in reception - Code = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 383 | break; |
mbed_official | 0:7037ed05f54f | 384 | case JOIN_FAILURE: |
mbed_official | 0:7037ed05f54f | 385 | printf("\r\n OTAA Failed - Check Keys \r\n"); |
mbed_official | 0:7037ed05f54f | 386 | break; |
mbed_official | 26:f07f5febf97f | 387 | case UPLINK_REQUIRED: |
mbed_official | 26:f07f5febf97f | 388 | printf("\r\n Uplink required by NS \r\n"); |
mbed_official | 26:f07f5febf97f | 389 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 26:f07f5febf97f | 390 | send_message(); |
mbed_official | 26:f07f5febf97f | 391 | } |
mbed_official | 26:f07f5febf97f | 392 | break; |
mbed_official | 0:7037ed05f54f | 393 | default: |
mbed_official | 0:7037ed05f54f | 394 | MBED_ASSERT("Unknown Event"); |
mbed_official | 0:7037ed05f54f | 395 | } |
mbed_official | 0:7037ed05f54f | 396 | } |
mbed_official | 0:7037ed05f54f | 397 | |
mbed_official | 0:7037ed05f54f | 398 | // EOF |