Temperature reading using NUCLEO-L152RE microcontroller and Grove – Temperature&Humidity Sensor Pro.

Dependencies:   DHT LMiC SX1276Lib mbed

Fork of LoRaWAN_send_text by Thomas Amberg

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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) 0x02031003
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 float getTemperature() {
00041 
00042     int err = 1;
00043   
00044     while(err != 0) {
00045         wait(2.0f);
00046         err = sensor.readData();
00047     }
00048     
00049     return sensor.ReadTemperature(CELCIUS);
00050 }
00051 
00052 void onSendFrame (osjob_t* j) {
00053  
00054     char message[32];
00055     
00056     float temperature = getTemperature();
00057     printf("Temperature is %4.2f \r\n", temperature);
00058     
00059     sprintf(message, "%4.2f", temperature);
00060     
00061     int frameLength = strlen(message); // keep it < 32
00062     for (int i = 0; i < frameLength; i++) {
00063         LMIC.frame[i] = message[i];
00064     }
00065     int result = LMIC_setTxData2(LORAWAN_APP_PORT, LMIC.frame, 
00066         frameLength, LORAWAN_CONFIRMED_MSG_ON); // calls onEvent()
00067         
00068     os_setTimedCallback(j, os_getTime() + sec2osticks(60), onSendFrame);
00069 }
00070 
00071 void onInit (osjob_t* j) {
00072     LMIC_reset();
00073     LMIC_setAdrMode(LORAWAN_ADR_ON);
00074     LMIC_setDrTxpow(DR_SF12, 14);
00075     LMIC_setSession(LORAWAN_NET_ID, LORAWAN_DEV_ADDR, NwkSKey, ArtSKey);
00076     onSendFrame(NULL);
00077 }
00078 
00079 void onEvent (ev_t ev) { // called by lmic.cpp, see also oslmic.h
00080     debug_event(ev);
00081     if (ev == EV_TXCOMPLETE) {
00082         os_setCallback(&sendFrameJob, onSendFrame);
00083     }
00084 }
00085 
00086 int main (void) {
00087     debug_str("main\r\n");
00088     os_init();
00089     os_setCallback(&initjob, onInit);
00090     os_runloop(); // blocking
00091 }