Application that measures the temperature and humidity every 60s and sends it to a LoRa Gateway.
Dependencies: DHT LMiC SX1276Lib mbed
main.cpp
00001 // License: Revised BSD License, see LICENSE.TXT, (c)2015 Semtech 00002 00003 #include "mbed.h" 00004 #include "lmic.h" 00005 #include "debug.h" 00006 00007 #include "DHT.h" 00008 00009 #define LORAWAN_NET_ID (uint32_t) 0x00000000 00010 // TODO: enter device address below, for TTN just set ??? 00011 #define LORAWAN_DEV_ADDR (uint32_t) 0x02033333 00012 #define LORAWAN_ADR_ON 1 00013 #define LORAWAN_CONFIRMED_MSG_ON 1 00014 #define LORAWAN_APP_PORT 3//15 00015 00016 DHT sensor(A1, AM2302); 00017 00018 static uint8_t NwkSKey[] = { 00019 // TODO: enter network, or use TTN default 00020 // e.g. for 2B7E151628AED2A6ABF7158809CF4F3C => 00021 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 00022 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C 00023 }; 00024 00025 static uint8_t ArtSKey[] = { 00026 // TODO: enter application key, or use TTN default 00027 // e.g. for 2B7E151628AED2A6ABF7158809CF4F3C => 00028 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 00029 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C 00030 }; 00031 00032 osjob_t initjob; 00033 osjob_t sendFrameJob; 00034 u1_t n = 0; 00035 00036 void os_getArtEui (uint8_t *buf) {} // ignore 00037 void os_getDevEui (uint8_t *buf) {} // ignore 00038 void os_getDevKey (uint8_t *buf) {} // ignore 00039 00040 void getTemperatureHumidity(float& temperature, float& humidity) { 00041 00042 int err = 1; 00043 00044 while(err != 0) { 00045 wait(2.0f); 00046 err = sensor.readData(); 00047 } 00048 00049 temperature = sensor.ReadTemperature(CELCIUS); 00050 humidity = sensor.ReadHumidity(); 00051 } 00052 00053 void onSendFrame (osjob_t* j) { 00054 00055 char message[32]; 00056 00057 float temperature; 00058 float humidity; 00059 00060 getTemperatureHumidity(temperature, humidity); 00061 00062 printf("Temperature is %4.2f \r\n", temperature); 00063 printf("Humidity is %4.2f \r\n", humidity); 00064 00065 sprintf(message, "%4.2f;%4.2f", temperature, humidity); 00066 00067 int frameLength = strlen(message); // keep it < 32 00068 for (int i = 0; i < frameLength; i++) { 00069 LMIC.frame[i] = message[i]; 00070 } 00071 int result = LMIC_setTxData2(LORAWAN_APP_PORT, LMIC.frame, 00072 frameLength, LORAWAN_CONFIRMED_MSG_ON); // calls onEvent() 00073 00074 os_setTimedCallback(j, os_getTime() + sec2osticks(60), onSendFrame); 00075 } 00076 00077 void onInit (osjob_t* j) { 00078 LMIC_reset(); 00079 LMIC_setAdrMode(LORAWAN_ADR_ON); 00080 LMIC_setDrTxpow(DR_SF12, 14); 00081 LMIC_setSession(LORAWAN_NET_ID, LORAWAN_DEV_ADDR, NwkSKey, ArtSKey); 00082 onSendFrame(NULL); 00083 } 00084 00085 void onEvent (ev_t ev) { // called by lmic.cpp, see also oslmic.h 00086 debug_event(ev); 00087 if (ev == EV_TXCOMPLETE) { 00088 os_setCallback(&sendFrameJob, onSendFrame); 00089 } 00090 } 00091 00092 int main (void) { 00093 printf("Temperature Humidity IoT\r\n"); 00094 os_init(); 00095 os_setCallback(&initjob, onInit); 00096 os_runloop(); // blocking 00097 }
Generated on Thu Jul 28 2022 15:48:57 by 1.7.2