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.
Dependents: Turtle_RadioShuttle
examples/RadioTest.cpp@0:0c31756924a2, 2019-02-06 (annotated)
- Committer:
- Helmut64
- Date:
- Wed Feb 06 15:26:48 2019 +0000
- Revision:
- 0:0c31756924a2
- Child:
- 2:50e8a3c35d1f
new lib
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Helmut64 | 0:0c31756924a2 | 1 | /* |
| Helmut64 | 0:0c31756924a2 | 2 | * The file is Licensed under the Apache License, Version 2.0 |
| Helmut64 | 0:0c31756924a2 | 3 | * (c) 2017 Helmut Tschemernjak |
| Helmut64 | 0:0c31756924a2 | 4 | * 30826 Garbsen (Hannover) Germany |
| Helmut64 | 0:0c31756924a2 | 5 | */ |
| Helmut64 | 0:0c31756924a2 | 6 | |
| Helmut64 | 0:0c31756924a2 | 7 | #include "mbed.h" |
| Helmut64 | 0:0c31756924a2 | 8 | #include "PinMap.h" |
| Helmut64 | 0:0c31756924a2 | 9 | #include "sx1276-mbed-hal.h" |
| Helmut64 | 0:0c31756924a2 | 10 | #include "RadioShuttle.h" |
| Helmut64 | 0:0c31756924a2 | 11 | #include "RadioStatus.h" |
| Helmut64 | 0:0c31756924a2 | 12 | #include "RadioSecurity.h" |
| Helmut64 | 0:0c31756924a2 | 13 | #include "main.h" |
| Helmut64 | 0:0c31756924a2 | 14 | #ifdef FEATURE_NVPROPERTY |
| Helmut64 | 0:0c31756924a2 | 15 | #include <NVPropertyProviderInterface.h> |
| Helmut64 | 0:0c31756924a2 | 16 | #include "NVProperty.h" |
| Helmut64 | 0:0c31756924a2 | 17 | #endif |
| Helmut64 | 0:0c31756924a2 | 18 | |
| Helmut64 | 0:0c31756924a2 | 19 | |
| Helmut64 | 0:0c31756924a2 | 20 | #ifdef FEATURE_LORA |
| Helmut64 | 0:0c31756924a2 | 21 | |
| Helmut64 | 0:0c31756924a2 | 22 | #define CHECK_ERROR_RET(func, err) { \ |
| Helmut64 | 0:0c31756924a2 | 23 | if (err) { \ |
| Helmut64 | 0:0c31756924a2 | 24 | dprintf("Error in %s: %s", func, rs->StrError(err)); \ |
| Helmut64 | 0:0c31756924a2 | 25 | return err; \ |
| Helmut64 | 0:0c31756924a2 | 26 | } \ |
| Helmut64 | 0:0c31756924a2 | 27 | } |
| Helmut64 | 0:0c31756924a2 | 28 | |
| Helmut64 | 0:0c31756924a2 | 29 | #ifdef TARGET_STM32L4 |
| Helmut64 | 0:0c31756924a2 | 30 | #define RADIO_SERVER 1 |
| Helmut64 | 0:0c31756924a2 | 31 | #endif |
| Helmut64 | 0:0c31756924a2 | 32 | |
| Helmut64 | 0:0c31756924a2 | 33 | #ifdef RADIO_SERVER |
| Helmut64 | 0:0c31756924a2 | 34 | bool server = true; |
| Helmut64 | 0:0c31756924a2 | 35 | #else |
| Helmut64 | 0:0c31756924a2 | 36 | bool server = false; |
| Helmut64 | 0:0c31756924a2 | 37 | #endif |
| Helmut64 | 0:0c31756924a2 | 38 | |
| Helmut64 | 0:0c31756924a2 | 39 | bool usePassword = false; // password the can used indepenend of AES |
| Helmut64 | 0:0c31756924a2 | 40 | bool useAES = false; // AES needs the usePassword option on |
| Helmut64 | 0:0c31756924a2 | 41 | bool useNodeOffline = true; // when idle turns the radio off and enters deelsleep |
| Helmut64 | 0:0c31756924a2 | 42 | |
| Helmut64 | 0:0c31756924a2 | 43 | |
| Helmut64 | 0:0c31756924a2 | 44 | |
| Helmut64 | 0:0c31756924a2 | 45 | static const int myTempSensorApp = 0x0001; // Must be unique world wide. |
| Helmut64 | 0:0c31756924a2 | 46 | #ifndef RADIO_SERVER |
| Helmut64 | 0:0c31756924a2 | 47 | int myDeviceID = 1; |
| Helmut64 | 0:0c31756924a2 | 48 | int remoteDeviceID = 14; |
| Helmut64 | 0:0c31756924a2 | 49 | uint32_t myCode = 0; |
| Helmut64 | 0:0c31756924a2 | 50 | #else |
| Helmut64 | 0:0c31756924a2 | 51 | int myDeviceID = 14; |
| Helmut64 | 0:0c31756924a2 | 52 | int remoteDeviceID = 1; |
| Helmut64 | 0:0c31756924a2 | 53 | uint32_t myCode = 0; |
| Helmut64 | 0:0c31756924a2 | 54 | #endif |
| Helmut64 | 0:0c31756924a2 | 55 | |
| Helmut64 | 0:0c31756924a2 | 56 | unsigned char samplePassword[] = { "RadioShuttleFly" }; |
| Helmut64 | 0:0c31756924a2 | 57 | |
| Helmut64 | 0:0c31756924a2 | 58 | /* |
| Helmut64 | 0:0c31756924a2 | 59 | * For details review: SX1276GenericLib/sx1276/sx1276.h |
| Helmut64 | 0:0c31756924a2 | 60 | * Supported spreading factors SF 7,8, 9, 10, 11, (12 does not work well) |
| Helmut64 | 0:0c31756924a2 | 61 | * Working frequencies using the 125000 bandwidth which leaves |
| Helmut64 | 0:0c31756924a2 | 62 | * sufficient distance to the neighbour channel |
| Helmut64 | 0:0c31756924a2 | 63 | * EU: 868.1, 868.3, 868.5 (Default LoRaWAN EU channels) |
| Helmut64 | 0:0c31756924a2 | 64 | * EU: 865.1, 865.3, 865.5, 865.7, 865.9 (additional channels) |
| Helmut64 | 0:0c31756924a2 | 65 | * EU: 866.1, 866.3, 866.5, 866.7, 866.9 (additional channels) |
| Helmut64 | 0:0c31756924a2 | 66 | * EU: 867.1, 867.3, 867.5, 867.7, 867.9 (additional channels) |
| Helmut64 | 0:0c31756924a2 | 67 | * Utilisation of these channels should not exceed 1% per hour per node |
| Helmut64 | 0:0c31756924a2 | 68 | * Bandwidth changes other than 125k requires different channels distances |
| Helmut64 | 0:0c31756924a2 | 69 | */ |
| Helmut64 | 0:0c31756924a2 | 70 | const RadioShuttle::RadioProfile myProfile[] = { |
| Helmut64 | 0:0c31756924a2 | 71 | /* |
| Helmut64 | 0:0c31756924a2 | 72 | * Our default profile |
| Helmut64 | 0:0c31756924a2 | 73 | * frequency, bandwidth, TX power, spreading factor, frequency-offset |
| Helmut64 | 0:0c31756924a2 | 74 | */ |
| Helmut64 | 0:0c31756924a2 | 75 | { 868100000, 125000, 14, 7, 0 }, |
| Helmut64 | 0:0c31756924a2 | 76 | { 0, 0, 0, 0, 0 }, |
| Helmut64 | 0:0c31756924a2 | 77 | }; |
| Helmut64 | 0:0c31756924a2 | 78 | |
| Helmut64 | 0:0c31756924a2 | 79 | |
| Helmut64 | 0:0c31756924a2 | 80 | struct sensor { |
| Helmut64 | 0:0c31756924a2 | 81 | uint8_t version; |
| Helmut64 | 0:0c31756924a2 | 82 | uint8_t padding; |
| Helmut64 | 0:0c31756924a2 | 83 | uint16_t pm25; |
| Helmut64 | 0:0c31756924a2 | 84 | uint16_t pm10; |
| Helmut64 | 0:0c31756924a2 | 85 | uint16_t id; |
| Helmut64 | 0:0c31756924a2 | 86 | } PMAppData; |
| Helmut64 | 0:0c31756924a2 | 87 | |
| Helmut64 | 0:0c31756924a2 | 88 | |
| Helmut64 | 0:0c31756924a2 | 89 | void TempSensorRecvHandler(int AppID, RadioShuttle::devid_t stationID, int msgID, int status, void *buffer, int length) |
| Helmut64 | 0:0c31756924a2 | 90 | { |
| Helmut64 | 0:0c31756924a2 | 91 | switch(status) { |
| Helmut64 | 0:0c31756924a2 | 92 | case RadioShuttle::MS_SentCompleted: // A SendMsg has been sent. |
| Helmut64 | 0:0c31756924a2 | 93 | dprintf("MSG_SentCompleted: id=%d %d bytes", msgID, length); |
| Helmut64 | 0:0c31756924a2 | 94 | break; |
| Helmut64 | 0:0c31756924a2 | 95 | case RadioShuttle::MS_SentCompletedConfirmed:// A SendMsg has been sent and confirmed |
| Helmut64 | 0:0c31756924a2 | 96 | dprintf("MSG_SentCompletedConfirmed: id=%d %d bytes", msgID, length); |
| Helmut64 | 0:0c31756924a2 | 97 | break; |
| Helmut64 | 0:0c31756924a2 | 98 | case RadioShuttle::MS_SentTimeout: // A timeout occurred, number of retries exceeded |
| Helmut64 | 0:0c31756924a2 | 99 | dprintf("MSG_SentTimeout ID: %d", msgID); |
| Helmut64 | 0:0c31756924a2 | 100 | break; |
| Helmut64 | 0:0c31756924a2 | 101 | |
| Helmut64 | 0:0c31756924a2 | 102 | case RadioShuttle::MS_RecvData: // a simple input message |
| Helmut64 | 0:0c31756924a2 | 103 | dprintf("MSG_RecvData ID: %d, len=%d", msgID, length); |
| Helmut64 | 0:0c31756924a2 | 104 | { |
| Helmut64 | 0:0c31756924a2 | 105 | struct sensor *p = (sensor *)buffer; |
| Helmut64 | 0:0c31756924a2 | 106 | if (length == sizeof(struct sensor) && p->version == 1) { |
| Helmut64 | 0:0c31756924a2 | 107 | dprintf("ParticalData: PM10: %.1f (μg/m3) PM2.5: %.1f (μg/m3) ID: %d", (float)p->pm10 / 10.0, (float)p->pm25 / 10.0, p->id); |
| Helmut64 | 0:0c31756924a2 | 108 | } |
| Helmut64 | 0:0c31756924a2 | 109 | // dump("MSG_RecvData", buffer, length); |
| Helmut64 | 0:0c31756924a2 | 110 | } |
| Helmut64 | 0:0c31756924a2 | 111 | break; |
| Helmut64 | 0:0c31756924a2 | 112 | case RadioShuttle::MS_RecvDataConfirmed: // received a confirmed message |
| Helmut64 | 0:0c31756924a2 | 113 | dprintf("MSG_RecvDataConfirmed ID: %d, len=%d", msgID, length); |
| Helmut64 | 0:0c31756924a2 | 114 | // dump("MSG_RecvDataConfirmed", buffer, length); |
| Helmut64 | 0:0c31756924a2 | 115 | break; |
| Helmut64 | 0:0c31756924a2 | 116 | case RadioShuttle::MS_NoStationFound: |
| Helmut64 | 0:0c31756924a2 | 117 | dprintf("MSG_NoStationFound"); |
| Helmut64 | 0:0c31756924a2 | 118 | break; |
| Helmut64 | 0:0c31756924a2 | 119 | case RadioShuttle::MS_NoStationSupportsApp: |
| Helmut64 | 0:0c31756924a2 | 120 | dprintf("MSG_NoStationSupportsApp"); |
| Helmut64 | 0:0c31756924a2 | 121 | break; |
| Helmut64 | 0:0c31756924a2 | 122 | case RadioShuttle::MS_AuthenicationRequired: // the password does not match. |
| Helmut64 | 0:0c31756924a2 | 123 | dprintf("MSG_AuthenicationRequired"); |
| Helmut64 | 0:0c31756924a2 | 124 | break; |
| Helmut64 | 0:0c31756924a2 | 125 | |
| Helmut64 | 0:0c31756924a2 | 126 | case RadioShuttle::MS_StationConnected: // a confirmation that the connection was accepted |
| Helmut64 | 0:0c31756924a2 | 127 | dprintf("MSG_StationConnected"); |
| Helmut64 | 0:0c31756924a2 | 128 | break; |
| Helmut64 | 0:0c31756924a2 | 129 | case RadioShuttle::MS_StationDisconnected: // a confirmation that the disconnect was accepted |
| Helmut64 | 0:0c31756924a2 | 130 | dprintf("MSG_StationDisconnected"); |
| Helmut64 | 0:0c31756924a2 | 131 | break; |
| Helmut64 | 0:0c31756924a2 | 132 | default: |
| Helmut64 | 0:0c31756924a2 | 133 | break; |
| Helmut64 | 0:0c31756924a2 | 134 | } |
| Helmut64 | 0:0c31756924a2 | 135 | } |
| Helmut64 | 0:0c31756924a2 | 136 | |
| Helmut64 | 0:0c31756924a2 | 137 | |
| Helmut64 | 0:0c31756924a2 | 138 | Radio *radio; |
| Helmut64 | 0:0c31756924a2 | 139 | RadioShuttle *rs; |
| Helmut64 | 0:0c31756924a2 | 140 | RadioStatusInterface *statusIntf; |
| Helmut64 | 0:0c31756924a2 | 141 | RadioSecurityInterface *securityIntf; |
| Helmut64 | 0:0c31756924a2 | 142 | |
| Helmut64 | 0:0c31756924a2 | 143 | int InitRadio() |
| Helmut64 | 0:0c31756924a2 | 144 | { |
| Helmut64 | 0:0c31756924a2 | 145 | Radio *radio; |
| Helmut64 | 0:0c31756924a2 | 146 | RSCode err; |
| Helmut64 | 0:0c31756924a2 | 147 | |
| Helmut64 | 0:0c31756924a2 | 148 | #ifdef FEATURE_NVPROPERTY |
| Helmut64 | 0:0c31756924a2 | 149 | { |
| Helmut64 | 0:0c31756924a2 | 150 | NVProperty prop; |
| Helmut64 | 0:0c31756924a2 | 151 | |
| Helmut64 | 0:0c31756924a2 | 152 | myDeviceID = prop.GetProperty(prop.LORA_DEVICE_ID, 0); |
| Helmut64 | 0:0c31756924a2 | 153 | myCode = prop.GetProperty(prop.LORA_CODE_ID, 0); |
| Helmut64 | 0:0c31756924a2 | 154 | } |
| Helmut64 | 0:0c31756924a2 | 155 | #endif |
| Helmut64 | 0:0c31756924a2 | 156 | |
| Helmut64 | 0:0c31756924a2 | 157 | #ifdef TARGET_DISCO_L072CZ_LRWAN1 |
| Helmut64 | 0:0c31756924a2 | 158 | radio = new SX1276Generic(NULL, MURATA_SX1276, |
| Helmut64 | 0:0c31756924a2 | 159 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
| Helmut64 | 0:0c31756924a2 | 160 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, |
| Helmut64 | 0:0c31756924a2 | 161 | LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); |
| Helmut64 | 0:0c31756924a2 | 162 | #elif defined(HELTECL432_REV1) |
| Helmut64 | 0:0c31756924a2 | 163 | radio = new SX1276Generic(NULL, HELTEC_L4_1276, |
| Helmut64 | 0:0c31756924a2 | 164 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
| Helmut64 | 0:0c31756924a2 | 165 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, |
| Helmut64 | 0:0c31756924a2 | 166 | LORA_ANT_PWR); |
| Helmut64 | 0:0c31756924a2 | 167 | #else // RFM95 |
| Helmut64 | 0:0c31756924a2 | 168 | radio = new SX1276Generic(NULL, RFM95_SX1276, |
| Helmut64 | 0:0c31756924a2 | 169 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
| Helmut64 | 0:0c31756924a2 | 170 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5); |
| Helmut64 | 0:0c31756924a2 | 171 | #endif |
| Helmut64 | 0:0c31756924a2 | 172 | |
| Helmut64 | 0:0c31756924a2 | 173 | |
| Helmut64 | 0:0c31756924a2 | 174 | statusIntf = new MyRadioStatus(); |
| Helmut64 | 0:0c31756924a2 | 175 | securityIntf = new RadioSecurity(); |
| Helmut64 | 0:0c31756924a2 | 176 | |
| Helmut64 | 0:0c31756924a2 | 177 | rs = new RadioShuttle("MyRadioShuttle"); |
| Helmut64 | 0:0c31756924a2 | 178 | |
| Helmut64 | 0:0c31756924a2 | 179 | rs->EnablePacketTrace(RadioShuttle::DEV_ID_ANY, true, true); |
| Helmut64 | 0:0c31756924a2 | 180 | |
| Helmut64 | 0:0c31756924a2 | 181 | err = rs->AddLicense(myDeviceID, myCode); |
| Helmut64 | 0:0c31756924a2 | 182 | CHECK_ERROR_RET("AddLicense", err); |
| Helmut64 | 0:0c31756924a2 | 183 | |
| Helmut64 | 0:0c31756924a2 | 184 | err = rs->AddRadio(radio, MODEM_LORA, myProfile); |
| Helmut64 | 0:0c31756924a2 | 185 | CHECK_ERROR_RET("AddRadio", err); |
| Helmut64 | 0:0c31756924a2 | 186 | dprintf("Radio: %.1f MHz, SF%d, %.f kHz", (float)myProfile[0].Frequency/1000000.0, myProfile[0].SpreadingFaktor, (float)myProfile[0].Bandwidth/1000.0); |
| Helmut64 | 0:0c31756924a2 | 187 | |
| Helmut64 | 0:0c31756924a2 | 188 | rs->AddRadioStatus(statusIntf); |
| Helmut64 | 0:0c31756924a2 | 189 | CHECK_ERROR_RET("AddRadioStatus", err); |
| Helmut64 | 0:0c31756924a2 | 190 | |
| Helmut64 | 0:0c31756924a2 | 191 | rs->AddRadioSecurity(securityIntf); |
| Helmut64 | 0:0c31756924a2 | 192 | CHECK_ERROR_RET("AddRadioSecurity", err); |
| Helmut64 | 0:0c31756924a2 | 193 | |
| Helmut64 | 0:0c31756924a2 | 194 | /* |
| Helmut64 | 0:0c31756924a2 | 195 | * The password parameter can be skipped if no password is required |
| Helmut64 | 0:0c31756924a2 | 196 | */ |
| Helmut64 | 0:0c31756924a2 | 197 | if (usePassword) { |
| Helmut64 | 0:0c31756924a2 | 198 | err = rs->RegisterApplication(myTempSensorApp, &TempSensorRecvHandler, samplePassword, sizeof(samplePassword)-1); |
| Helmut64 | 0:0c31756924a2 | 199 | } else { |
| Helmut64 | 0:0c31756924a2 | 200 | err = rs->RegisterApplication(myTempSensorApp, &TempSensorRecvHandler); |
| Helmut64 | 0:0c31756924a2 | 201 | err = rs->RegisterApplication(10, &TempSensorRecvHandler); |
| Helmut64 | 0:0c31756924a2 | 202 | } |
| Helmut64 | 0:0c31756924a2 | 203 | if (err) |
| Helmut64 | 0:0c31756924a2 | 204 | return err; |
| Helmut64 | 0:0c31756924a2 | 205 | |
| Helmut64 | 0:0c31756924a2 | 206 | if (server) { |
| Helmut64 | 0:0c31756924a2 | 207 | err = rs->Startup(RadioShuttle::RS_Station_Basic); |
| Helmut64 | 0:0c31756924a2 | 208 | dprintf("Startup as a Server: %s ID=%d", rs->GetRadioName(rs->GetRadioType()), myDeviceID); |
| Helmut64 | 0:0c31756924a2 | 209 | } else { |
| Helmut64 | 0:0c31756924a2 | 210 | if (useNodeOffline) |
| Helmut64 | 0:0c31756924a2 | 211 | err = rs->Startup(RadioShuttle::RS_Node_Offline); |
| Helmut64 | 0:0c31756924a2 | 212 | else |
| Helmut64 | 0:0c31756924a2 | 213 | err = rs->Startup(RadioShuttle::RS_Node_Online); |
| Helmut64 | 0:0c31756924a2 | 214 | dprintf("Startup as a Node: %s ID=%d", rs->GetRadioName(rs->GetRadioType()), myDeviceID); |
| Helmut64 | 0:0c31756924a2 | 215 | if (!err && rs->AppRequiresAuthentication(myTempSensorApp) == RS_PasswordSet) { |
| Helmut64 | 0:0c31756924a2 | 216 | err = rs->Connect(myTempSensorApp, remoteDeviceID); |
| Helmut64 | 0:0c31756924a2 | 217 | } |
| Helmut64 | 0:0c31756924a2 | 218 | } |
| Helmut64 | 0:0c31756924a2 | 219 | CHECK_ERROR_RET("Startup", err); |
| Helmut64 | 0:0c31756924a2 | 220 | return 0; |
| Helmut64 | 0:0c31756924a2 | 221 | } |
| Helmut64 | 0:0c31756924a2 | 222 | |
| Helmut64 | 0:0c31756924a2 | 223 | void DeInitRadio() |
| Helmut64 | 0:0c31756924a2 | 224 | { |
| Helmut64 | 0:0c31756924a2 | 225 | if (securityIntf) { |
| Helmut64 | 0:0c31756924a2 | 226 | delete securityIntf; |
| Helmut64 | 0:0c31756924a2 | 227 | securityIntf = NULL; |
| Helmut64 | 0:0c31756924a2 | 228 | } |
| Helmut64 | 0:0c31756924a2 | 229 | if (statusIntf) { |
| Helmut64 | 0:0c31756924a2 | 230 | delete statusIntf; |
| Helmut64 | 0:0c31756924a2 | 231 | statusIntf = NULL; |
| Helmut64 | 0:0c31756924a2 | 232 | } |
| Helmut64 | 0:0c31756924a2 | 233 | if (rs) { |
| Helmut64 | 0:0c31756924a2 | 234 | delete rs; |
| Helmut64 | 0:0c31756924a2 | 235 | rs = NULL; |
| Helmut64 | 0:0c31756924a2 | 236 | } |
| Helmut64 | 0:0c31756924a2 | 237 | if (radio) { |
| Helmut64 | 0:0c31756924a2 | 238 | delete radio; |
| Helmut64 | 0:0c31756924a2 | 239 | radio = NULL; |
| Helmut64 | 0:0c31756924a2 | 240 | } |
| Helmut64 | 0:0c31756924a2 | 241 | } |
| Helmut64 | 0:0c31756924a2 | 242 | |
| Helmut64 | 0:0c31756924a2 | 243 | int RadioTest() |
| Helmut64 | 0:0c31756924a2 | 244 | { |
| Helmut64 | 0:0c31756924a2 | 245 | extern volatile int pressedCount; |
| Helmut64 | 0:0c31756924a2 | 246 | |
| Helmut64 | 0:0c31756924a2 | 247 | if (InitRadio() != 0) |
| Helmut64 | 0:0c31756924a2 | 248 | return -1; |
| Helmut64 | 0:0c31756924a2 | 249 | |
| Helmut64 | 0:0c31756924a2 | 250 | |
| Helmut64 | 0:0c31756924a2 | 251 | for(;;) { |
| Helmut64 | 0:0c31756924a2 | 252 | static int cnt = 0; |
| Helmut64 | 0:0c31756924a2 | 253 | if (cnt != pressedCount) { |
| Helmut64 | 0:0c31756924a2 | 254 | if (cnt > 0) { |
| Helmut64 | 0:0c31756924a2 | 255 | int flags = 0; |
| Helmut64 | 0:0c31756924a2 | 256 | flags |= RadioShuttle::MF_NeedsConfirm; // optional |
| Helmut64 | 0:0c31756924a2 | 257 | if (usePassword && useAES) |
| Helmut64 | 0:0c31756924a2 | 258 | flags |= RadioShuttle::MF_Encrypted; |
| Helmut64 | 0:0c31756924a2 | 259 | if (server) { |
| Helmut64 | 0:0c31756924a2 | 260 | static char msg[] = "The server feels very good today"; |
| Helmut64 | 0:0c31756924a2 | 261 | rs->SendMsg(myTempSensorApp, msg, sizeof(msg), flags, remoteDeviceID); |
| Helmut64 | 0:0c31756924a2 | 262 | } else { |
| Helmut64 | 0:0c31756924a2 | 263 | static char msg[] = "Hello, the temperature is 26 celsius"; |
| Helmut64 | 0:0c31756924a2 | 264 | rs->SendMsg(myTempSensorApp, msg, sizeof(msg), flags, remoteDeviceID); |
| Helmut64 | 0:0c31756924a2 | 265 | } |
| Helmut64 | 0:0c31756924a2 | 266 | } |
| Helmut64 | 0:0c31756924a2 | 267 | cnt = pressedCount; |
| Helmut64 | 0:0c31756924a2 | 268 | } |
| Helmut64 | 0:0c31756924a2 | 269 | |
| Helmut64 | 0:0c31756924a2 | 270 | if (rs->Idle() && rs->GetRadioType() == RadioShuttle::RS_Node_Offline) { |
| Helmut64 | 0:0c31756924a2 | 271 | sleep(); // uses deepsleep() when idle lowest power mode; |
| Helmut64 | 0:0c31756924a2 | 272 | } else { |
| Helmut64 | 0:0c31756924a2 | 273 | sleep(); // timer and radio interrupts will wakeup us |
| Helmut64 | 0:0c31756924a2 | 274 | } |
| Helmut64 | 0:0c31756924a2 | 275 | rs->RunShuttle(); // process all pending events |
| Helmut64 | 0:0c31756924a2 | 276 | } |
| Helmut64 | 0:0c31756924a2 | 277 | return 0; |
| Helmut64 | 0:0c31756924a2 | 278 | } |
| Helmut64 | 0:0c31756924a2 | 279 | |
| Helmut64 | 0:0c31756924a2 | 280 | |
| Helmut64 | 0:0c31756924a2 | 281 | int RadioUpdate(bool keyPressed) |
| Helmut64 | 0:0c31756924a2 | 282 | { |
| Helmut64 | 0:0c31756924a2 | 283 | if (!rs) |
| Helmut64 | 0:0c31756924a2 | 284 | return 0; |
| Helmut64 | 0:0c31756924a2 | 285 | |
| Helmut64 | 0:0c31756924a2 | 286 | if (keyPressed) { |
| Helmut64 | 0:0c31756924a2 | 287 | int flags = 0; |
| Helmut64 | 0:0c31756924a2 | 288 | flags |= RadioShuttle::MF_NeedsConfirm; // optional |
| Helmut64 | 0:0c31756924a2 | 289 | if (usePassword && useAES) |
| Helmut64 | 0:0c31756924a2 | 290 | flags |= RadioShuttle::MF_Encrypted; |
| Helmut64 | 0:0c31756924a2 | 291 | if (server) { |
| Helmut64 | 0:0c31756924a2 | 292 | static char msg[] = "The server feels very good today"; |
| Helmut64 | 0:0c31756924a2 | 293 | rs->SendMsg(myTempSensorApp, msg, sizeof(msg), flags, remoteDeviceID); |
| Helmut64 | 0:0c31756924a2 | 294 | } else { |
| Helmut64 | 0:0c31756924a2 | 295 | static char msg[] = "Hello, the temperature is 26 celsius"; |
| Helmut64 | 0:0c31756924a2 | 296 | rs->SendMsg(myTempSensorApp, msg, sizeof(msg), flags, remoteDeviceID); |
| Helmut64 | 0:0c31756924a2 | 297 | } |
| Helmut64 | 0:0c31756924a2 | 298 | } |
| Helmut64 | 0:0c31756924a2 | 299 | rs->RunShuttle(); |
| Helmut64 | 0:0c31756924a2 | 300 | return 0; |
| Helmut64 | 0:0c31756924a2 | 301 | } |
| Helmut64 | 0:0c31756924a2 | 302 | |
| Helmut64 | 0:0c31756924a2 | 303 | bool RadioISIdle() |
| Helmut64 | 0:0c31756924a2 | 304 | { |
| Helmut64 | 0:0c31756924a2 | 305 | if (!rs) |
| Helmut64 | 0:0c31756924a2 | 306 | return true; |
| Helmut64 | 0:0c31756924a2 | 307 | return rs->Idle(); |
| Helmut64 | 0:0c31756924a2 | 308 | } |
| Helmut64 | 0:0c31756924a2 | 309 | |
| Helmut64 | 0:0c31756924a2 | 310 | void InitLoRaChipWithShutdown() |
| Helmut64 | 0:0c31756924a2 | 311 | { |
| Helmut64 | 0:0c31756924a2 | 312 | #ifdef LORA_CS |
| Helmut64 | 0:0c31756924a2 | 313 | if (LORA_CS == NC) |
| Helmut64 | 0:0c31756924a2 | 314 | return; |
| Helmut64 | 0:0c31756924a2 | 315 | #ifdef HELTECL432_REV1 |
| Helmut64 | 0:0c31756924a2 | 316 | Radio *radio = new SX1276Generic(NULL, HELTEC_L4_1276, |
| Helmut64 | 0:0c31756924a2 | 317 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
| Helmut64 | 0:0c31756924a2 | 318 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, LORA_ANT_PWR); |
| Helmut64 | 0:0c31756924a2 | 319 | #else |
| Helmut64 | 0:0c31756924a2 | 320 | Radio *radio = new SX1276Generic(NULL, RFM95_SX1276, |
| Helmut64 | 0:0c31756924a2 | 321 | LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, |
| Helmut64 | 0:0c31756924a2 | 322 | LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5); |
| Helmut64 | 0:0c31756924a2 | 323 | #endif |
| Helmut64 | 0:0c31756924a2 | 324 | |
| Helmut64 | 0:0c31756924a2 | 325 | RadioEvents_t radioEvents; |
| Helmut64 | 0:0c31756924a2 | 326 | memset(&radioEvents, 0, sizeof(radioEvents)); |
| Helmut64 | 0:0c31756924a2 | 327 | if (radio->Init(&radioEvents)) { |
| Helmut64 | 0:0c31756924a2 | 328 | radio->Sleep(); |
| Helmut64 | 0:0c31756924a2 | 329 | delete radio; |
| Helmut64 | 0:0c31756924a2 | 330 | } |
| Helmut64 | 0:0c31756924a2 | 331 | #endif |
| Helmut64 | 0:0c31756924a2 | 332 | } |
| Helmut64 | 0:0c31756924a2 | 333 | #endif // FEATURE_LORA |