tfsd
Dependencies: Servo GroveGPS Cayenne-LPP driver_mbed_TH02
main.cpp@64:4cd8fed3f437, 2022-03-15 (annotated)
- Committer:
- younousleconquerant
- Date:
- Tue Mar 15 08:54:01 2022 +0000
- Revision:
- 64:4cd8fed3f437
- Parent:
- 63:cd8ab5860303
- Child:
- 65:724a61ad654c
totql;
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 | 64:4cd8fed3f437 | 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 | 64:4cd8fed3f437 | 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 | 64:4cd8fed3f437 | 235 | |
younousleconquerant | 64:4cd8fed3f437 | 236 | //RFID |
younousleconquerant | 64:4cd8fed3f437 | 237 | if ( ! RfChip.PICC_IsNewCardPresent()) |
younousleconquerant | 64:4cd8fed3f437 | 238 | { |
younousleconquerant | 64:4cd8fed3f437 | 239 | wait_ms(500); |
younousleconquerant | 64:4cd8fed3f437 | 240 | } |
mbed_official | 0:7037ed05f54f | 241 | |
younousleconquerant | 64:4cd8fed3f437 | 242 | // Select one of the cards |
younousleconquerant | 64:4cd8fed3f437 | 243 | if (!RfChip.PICC_ReadCardSerial()) |
younousleconquerant | 64:4cd8fed3f437 | 244 | { |
younousleconquerant | 64:4cd8fed3f437 | 245 | wait_ms(500); |
younousleconquerant | 64:4cd8fed3f437 | 246 | printf("\r\nReading a card"); |
younousleconquerant | 64:4cd8fed3f437 | 247 | } |
younousleconquerant | 64:4cd8fed3f437 | 248 | |
younousleconquerant | 64:4cd8fed3f437 | 249 | // Print Card UID |
younousleconquerant | 64:4cd8fed3f437 | 250 | char nfcData[100]; |
younousleconquerant | 64:4cd8fed3f437 | 251 | printf("\r\nCard UID: "); |
younousleconquerant | 64:4cd8fed3f437 | 252 | for (uint8_t i = 0; i < RfChip.uid.size; i++) |
younousleconquerant | 64:4cd8fed3f437 | 253 | { |
younousleconquerant | 64:4cd8fed3f437 | 254 | printf(" %X02", RfChip.uid.uidByte[i]); |
younousleconquerant | 64:4cd8fed3f437 | 255 | nfcData[i] = RfChip.uid.uidByte[i]; |
superphil06 | 59:7b58c5775a56 | 256 | } |
younousleconquerant | 64:4cd8fed3f437 | 257 | printf("\r\n"); |
younousleconquerant | 64:4cd8fed3f437 | 258 | |
younousleconquerant | 64:4cd8fed3f437 | 259 | // Print Card type |
younousleconquerant | 64:4cd8fed3f437 | 260 | uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); |
younousleconquerant | 64:4cd8fed3f437 | 261 | printf("PICC Type: %s \r\n", RfChip.PICC_GetTypeName(piccType)); |
younousleconquerant | 64:4cd8fed3f437 | 262 | strcat(nfcData, RfChip.PICC_GetTypeName(piccType)); |
younousleconquerant | 64:4cd8fed3f437 | 263 | printf("DATA send: %s \r\n", nfcData); |
younousleconquerant | 64:4cd8fed3f437 | 264 | wait_ms(1000); |
younousleconquerant | 64:4cd8fed3f437 | 265 | |
superphil06 | 57:5ec0379d69d6 | 266 | Payload.reset(); |
younousleconquerant | 64:4cd8fed3f437 | 267 | size = Payload.addTemperature(1, (float) fTemp); |
younousleconquerant | 64:4cd8fed3f437 | 268 | size = size + Payload.addRelativeHumidity(2, fHumid); |
younousleconquerant | 64:4cd8fed3f437 | 269 | size = size + Payload.addGPS(3, atof(gpsLat), atof(gpsLon), atof(gpsalt)); |
younousleconquerant | 64:4cd8fed3f437 | 270 | size = size + Payload.addNFC(4, (char *)nfcData); |
superphil06 | 58:81c66fac6476 | 271 | |
superphil06 | 61:3cb7cf8226cf | 272 | // send complete message with cayenne format |
younousleconquerant | 64:4cd8fed3f437 | 273 | retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, Payload.getBuffer(), Payload.getSize(), |
younousleconquerant | 64:4cd8fed3f437 | 274 | MSG_UNCONFIRMED_FLAG); |
mbed_official | 0:7037ed05f54f | 275 | |
mbed_official | 0:7037ed05f54f | 276 | if (retcode < 0) { |
mbed_official | 0:7037ed05f54f | 277 | retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") |
mbed_official | 46:a124538e2891 | 278 | : printf("\r\n send() - Error code %d \r\n", retcode); |
mbed_official | 26:f07f5febf97f | 279 | |
mbed_official | 26:f07f5febf97f | 280 | if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { |
mbed_official | 26:f07f5febf97f | 281 | //retry in 3 seconds |
mbed_official | 26:f07f5febf97f | 282 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 26:f07f5febf97f | 283 | ev_queue.call_in(3000, send_message); |
mbed_official | 26:f07f5febf97f | 284 | } |
mbed_official | 26:f07f5febf97f | 285 | } |
mbed_official | 0:7037ed05f54f | 286 | return; |
mbed_official | 0:7037ed05f54f | 287 | } |
mbed_official | 0:7037ed05f54f | 288 | |
mbed_official | 0:7037ed05f54f | 289 | printf("\r\n %d bytes scheduled for transmission \r\n", retcode); |
mbed_official | 12:5015dfead3f2 | 290 | memset(tx_buffer, 0, sizeof(tx_buffer)); |
mbed_official | 0:7037ed05f54f | 291 | } |
mbed_official | 0:7037ed05f54f | 292 | |
mbed_official | 0:7037ed05f54f | 293 | /** |
mbed_official | 0:7037ed05f54f | 294 | * Receive a message from the Network Server |
mbed_official | 0:7037ed05f54f | 295 | */ |
mbed_official | 0:7037ed05f54f | 296 | static void receive_message() |
younousleconquerant | 64:4cd8fed3f437 | 297 | { |
younousleconquerant | 64:4cd8fed3f437 | 298 | int num_port, iPosition=0,iIndex,iEtatAlarme; |
mbed_official | 47:b6d132f1079f | 299 | uint8_t port; |
mbed_official | 47:b6d132f1079f | 300 | int flags; |
mbed_official | 47:b6d132f1079f | 301 | int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags); |
mbed_official | 0:7037ed05f54f | 302 | |
mbed_official | 0:7037ed05f54f | 303 | if (retcode < 0) { |
mbed_official | 0:7037ed05f54f | 304 | printf("\r\n receive() - Error code %d \r\n", retcode); |
mbed_official | 0:7037ed05f54f | 305 | return; |
mbed_official | 0:7037ed05f54f | 306 | } |
mbed_official | 0:7037ed05f54f | 307 | |
mbed_official | 47:b6d132f1079f | 308 | printf(" RX Data on port %u (%d bytes): ", port, retcode); |
mbed_official | 0:7037ed05f54f | 309 | for (uint8_t i = 0; i < retcode; i++) { |
superphil06 | 59:7b58c5775a56 | 310 | printf("%02x", rx_buffer[i]); |
mbed_official | 0:7037ed05f54f | 311 | } |
younousleconquerant | 64:4cd8fed3f437 | 312 | //printf("\n test value=%d", port); |
younousleconquerant | 64:4cd8fed3f437 | 313 | // *****************************code todo here ******************************************** |
younousleconquerant | 64:4cd8fed3f437 | 314 | switch (port) { |
superphil06 | 61:3cb7cf8226cf | 315 | case 3: // control led |
younousleconquerant | 64:4cd8fed3f437 | 316 | printf("\n led=%d", (int)rx_buffer[0]); |
younousleconquerant | 64:4cd8fed3f437 | 317 | if ((rx_buffer[0]-0x30)==0) |
younousleconquerant | 64:4cd8fed3f437 | 318 | |
younousleconquerant | 64:4cd8fed3f437 | 319 | iEtatAlarme=0; |
younousleconquerant | 64:4cd8fed3f437 | 320 | else iEtatAlarme=1; |
superphil06 | 62:10ba6387b44c | 321 | Alarme.write(iEtatAlarme); |
younousleconquerant | 64:4cd8fed3f437 | 322 | |
younousleconquerant | 64:4cd8fed3f437 | 323 | printf("\n alarme=%d",iEtatAlarme); |
younousleconquerant | 64:4cd8fed3f437 | 324 | break; |
superphil06 | 61:3cb7cf8226cf | 325 | case 4:// control servomotor |
younousleconquerant | 64:4cd8fed3f437 | 326 | for (iIndex=0; iIndex<retcode; iIndex++) { |
younousleconquerant | 64:4cd8fed3f437 | 327 | iPosition=iPosition*10+(rx_buffer[iIndex]-0x30);// convert receive string to angular position |
younousleconquerant | 64:4cd8fed3f437 | 328 | } |
younousleconquerant | 64:4cd8fed3f437 | 329 | |
younousleconquerant | 64:4cd8fed3f437 | 330 | |
younousleconquerant | 64:4cd8fed3f437 | 331 | printf("\n servo position =%d",iPosition); |
younousleconquerant | 64:4cd8fed3f437 | 332 | Myservo.position ( iPosition-45 ); // set servo motor position from 0 to 180 |
younousleconquerant | 64:4cd8fed3f437 | 333 | break; |
younousleconquerant | 64:4cd8fed3f437 | 334 | default: |
younousleconquerant | 64:4cd8fed3f437 | 335 | printf("\n port inconnu =%d",(int)port); |
younousleconquerant | 64:4cd8fed3f437 | 336 | break; |
younousleconquerant | 64:4cd8fed3f437 | 337 | } |
younousleconquerant | 64:4cd8fed3f437 | 338 | |
younousleconquerant | 64:4cd8fed3f437 | 339 | |
younousleconquerant | 64:4cd8fed3f437 | 340 | // ***************************** end code todo here ***************************************** |
younousleconquerant | 64:4cd8fed3f437 | 341 | |
younousleconquerant | 64:4cd8fed3f437 | 342 | |
mbed_official | 12:5015dfead3f2 | 343 | memset(rx_buffer, 0, sizeof(rx_buffer)); |
mbed_official | 0:7037ed05f54f | 344 | } |
mbed_official | 0:7037ed05f54f | 345 | |
mbed_official | 0:7037ed05f54f | 346 | /** |
mbed_official | 0:7037ed05f54f | 347 | * Event handler |
mbed_official | 0:7037ed05f54f | 348 | */ |
mbed_official | 0:7037ed05f54f | 349 | static void lora_event_handler(lorawan_event_t event) |
mbed_official | 0:7037ed05f54f | 350 | { |
mbed_official | 0:7037ed05f54f | 351 | switch (event) { |
mbed_official | 0:7037ed05f54f | 352 | case CONNECTED: |
mbed_official | 0:7037ed05f54f | 353 | printf("\r\n Connection - Successful \r\n"); |
mbed_official | 0:7037ed05f54f | 354 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 355 | send_message(); |
mbed_official | 0:7037ed05f54f | 356 | } else { |
mbed_official | 0:7037ed05f54f | 357 | ev_queue.call_every(TX_TIMER, send_message); |
mbed_official | 0:7037ed05f54f | 358 | } |
mbed_official | 0:7037ed05f54f | 359 | |
mbed_official | 0:7037ed05f54f | 360 | break; |
mbed_official | 0:7037ed05f54f | 361 | case DISCONNECTED: |
mbed_official | 0:7037ed05f54f | 362 | ev_queue.break_dispatch(); |
mbed_official | 0:7037ed05f54f | 363 | printf("\r\n Disconnected Successfully \r\n"); |
mbed_official | 0:7037ed05f54f | 364 | break; |
mbed_official | 0:7037ed05f54f | 365 | case TX_DONE: |
mbed_official | 0:7037ed05f54f | 366 | printf("\r\n Message Sent to Network Server \r\n"); |
mbed_official | 0:7037ed05f54f | 367 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 368 | send_message(); |
mbed_official | 0:7037ed05f54f | 369 | } |
mbed_official | 0:7037ed05f54f | 370 | break; |
mbed_official | 0:7037ed05f54f | 371 | case TX_TIMEOUT: |
mbed_official | 0:7037ed05f54f | 372 | case TX_ERROR: |
mbed_official | 0:7037ed05f54f | 373 | case TX_CRYPTO_ERROR: |
mbed_official | 0:7037ed05f54f | 374 | case TX_SCHEDULING_ERROR: |
mbed_official | 0:7037ed05f54f | 375 | printf("\r\n Transmission Error - EventCode = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 376 | // try again |
mbed_official | 0:7037ed05f54f | 377 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 378 | send_message(); |
mbed_official | 0:7037ed05f54f | 379 | } |
mbed_official | 0:7037ed05f54f | 380 | break; |
mbed_official | 0:7037ed05f54f | 381 | case RX_DONE: |
mbed_official | 0:7037ed05f54f | 382 | printf("\r\n Received message from Network Server \r\n"); |
mbed_official | 0:7037ed05f54f | 383 | receive_message(); |
mbed_official | 0:7037ed05f54f | 384 | break; |
mbed_official | 0:7037ed05f54f | 385 | case RX_TIMEOUT: |
younousleconquerant | 64:4cd8fed3f437 | 386 | printf("\r\n timeout in reception - Code = %d \r\n", event); |
superphil06 | 59:7b58c5775a56 | 387 | break; |
mbed_official | 0:7037ed05f54f | 388 | case RX_ERROR: |
mbed_official | 0:7037ed05f54f | 389 | printf("\r\n Error in reception - Code = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 390 | break; |
mbed_official | 0:7037ed05f54f | 391 | case JOIN_FAILURE: |
mbed_official | 0:7037ed05f54f | 392 | printf("\r\n OTAA Failed - Check Keys \r\n"); |
mbed_official | 0:7037ed05f54f | 393 | break; |
mbed_official | 26:f07f5febf97f | 394 | case UPLINK_REQUIRED: |
mbed_official | 26:f07f5febf97f | 395 | printf("\r\n Uplink required by NS \r\n"); |
mbed_official | 26:f07f5febf97f | 396 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 26:f07f5febf97f | 397 | send_message(); |
mbed_official | 26:f07f5febf97f | 398 | } |
mbed_official | 26:f07f5febf97f | 399 | break; |
mbed_official | 0:7037ed05f54f | 400 | default: |
mbed_official | 0:7037ed05f54f | 401 | MBED_ASSERT("Unknown Event"); |
mbed_official | 0:7037ed05f54f | 402 | } |
mbed_official | 0:7037ed05f54f | 403 | } |
mbed_official | 0:7037ed05f54f | 404 | |
mbed_official | 0:7037ed05f54f | 405 | // EOF |