LoRa_Node_STM32F103C8T6

Dependencies:   mbed mbed-STM32F103C8T6 OneWireCRC_LoRa_Node SX1276Lib_LoRa_Node

Files at this revision

API Documentation at this revision

Comitter:
lukas_formanek
Date:
Sat May 08 16:04:09 2021 +0000
Parent:
8:978eb43296ae
Commit message:
Lora_Node

Changed in this revision

Board.cpp Show annotated file Show diff for this revision Revisions of this file
Board.h Show annotated file Show diff for this revision Revisions of this file
RFM95W.cpp Show annotated file Show diff for this revision Revisions of this file
RFM95W.h Show annotated file Show diff for this revision Revisions of this file
SX1276Lib.lib Show annotated file Show diff for this revision Revisions of this file
Thermometer.cpp Show annotated file Show diff for this revision Revisions of this file
Thermometer.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/Board.cpp	Tue May 08 13:24:52 2018 +0000
+++ b/Board.cpp	Sat May 08 16:04:09 2021 +0000
@@ -1,3 +1,3 @@
 #include "Board.h"
 
-Serial PC(PC_TX, PC_RX, BAUDRATE);
\ No newline at end of file
+// Serial PC(PC_TX, PC_RX, BAUDRATE);
\ No newline at end of file
--- a/Board.h	Tue May 08 13:24:52 2018 +0000
+++ b/Board.h	Sat May 08 16:04:09 2021 +0000
@@ -3,15 +3,25 @@
 
 #include "mbed.h"
 
-#define MEASURE_INTERVAL    30       // 30 sekund - interval merania
+#define GATEWAY_ID 1                 // Adresa gatewayu (id)
+#define NODE_ID 3                    // Moja adresa (id) 
+#define MEASURE_INTERVAL 60          // 60 sekund - interval merania
+
+#define V_BATT_REFF 8.3              // max napatie baterii
 
-#define BAUDRATE            115200
+#define BATTERY_PIN PB_0
+#define FOTO_PIN    PB_1
+
+#define LED_SIGNALIZE 0
+
+
+#define BAUDRATE            9600
 #define LED_BLIK_PERIOD     0.10
 
 #define LED_PIN             PC_13
 
 #define THERM_PIN           PB_10
-#define NOISE_PIN           PB_0     // inicializacia random - read analog value
+#define NOISE_PIN           PA_1     // inicializacia random - read analog value
 
 #define PC_TX       PA_2
 #define PC_RX       PA_3
@@ -30,6 +40,6 @@
 
 
 
-extern Serial PC;
+// extern Serial PC;
 
 #endif
\ No newline at end of file
--- a/RFM95W.cpp	Tue May 08 13:24:52 2018 +0000
+++ b/RFM95W.cpp	Sat May 08 16:04:09 2021 +0000
@@ -41,7 +41,7 @@
 {
     memset(receivedMessage, '\0', sizeof(receivedMessage));
     ledState = 0;
-    timeOnAirSec = 0.3;
+    timeOnAirSec = 0.4;
     indicationLed = 1;
     messageNumber = NODE_ID;
     sendCounter = 0;
@@ -63,7 +63,7 @@
 void RFM95W::SendMessage()
 {
     snprintf((char *)ack, 3, "%c%c%c",NODE_ID, sendBuffer[0], messageNumber);
-    radio.Send( sendBuffer, 10 );
+    radio.Send( sendBuffer, 30 );
 }
 
 void RFM95W::SendAck(uint8_t addr, uint8_t messageNumber)
@@ -80,17 +80,19 @@
     while(number == 0) {
         number = rand() % 1500;
     }
-    return (timeOnAirSec * (number / 150.0));
+    return (timeOnAirSec * (number / 500.0));
 };
 
 void RFM95W::OnCheckAck()
 {
-    if((receivedAck == false) && (sendCounter < MAX_RESENDS))
+    if((receivedAck == false) && (sendCounter < MAX_RESENDS)) {
+        sendTicker.detach();
         sendTicker.attach(this,&RFM95W::OnSendAgain, CalculateRandomTime());
-    else if((receivedAck == false) && (sendCounter >= MAX_RESENDS)) {
+    } else if((receivedAck == false) && (sendCounter >= MAX_RESENDS)) {
         messageNumber++;
         if(messageNumber>255)
             messageNumber = 0;
+        radio.Sleep();
     }
     ackTicker.detach();
 };
@@ -104,29 +106,34 @@
 
 void RFM95W::OnTxDone( void )
 {
-    radio.Sleep();
     if(sendingAck)
         sendingAck = false;
     else {
         receivedAck = false;
-        ackTicker.attach(this,&RFM95W::OnCheckAck,timeOnAirSec*3);
+        ackTicker.detach();
+        ackTicker.attach(this,&RFM95W::OnCheckAck,timeOnAirSec*2);
     }
     radio.Rx(0);
-//    PC.printf("on Tx done...\r\n");
 };
 
 void RFM95W::OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
 {
     radio.Sleep();
     if(payload[0] == NODE_ID) {
-        ledTicker.attach(this,&RFM95W::OnLedTick, LED_BLIK_PERIOD);
+        indicationLed = 1;
+//        #if LED_SIGNALIZE == 1
+//            ledTicker.detach();
+//            ledTicker.attach(this,&RFM95W::OnLedTick, LED_BLIK_PERIOD);
+//        #endif
         if((size == 3) && (strncmp( ( const char* )payload, ( const char* )ack, 3 ) == 0) ) {
             receivedAck = true;
 //            PC.printf("Received Ack : %s \r\n",ack);
             messageNumber++;
             if(messageNumber>255)
                 messageNumber = 0;
-            radio.Rx(0);
+//            radio.Rx(0);
+//            radio.Sleep();
+            radio.Rx(1500);         // 1,5 s este cakam
             return;
         }
         sendingAck = true;
@@ -134,7 +141,7 @@
         SendAck(MsgFrom,payload[2]);
 //        PC.printf("MSG from : %c , message number received= %d \r\n",MsgFrom,payload[2]);
         if(payload[2] == messageNumbers[MsgFrom]) {
-            PC.printf("--------------------- Ta ista sprava -------------------- \r\n");
+//            PC.printf("--------------------- Ta ista sprava -------------------- \r\n");
             return;
         }
         messageNumbers[MsgFrom] = payload[2];
@@ -150,9 +157,9 @@
 
 void RFM95W::ProcessReceivedMessage()
 {
-        int time = atoi((const char*)receivedMessage);
-        thermometer.StopPeriodicMeassure();
-        thermometer.StartPeriodicMeassure(time);
+    int time = atoi((const char*)receivedMessage);
+    thermometer.StopPeriodicMeassure();
+    thermometer.StartPeriodicMeassure(time);
 //        PC.printf("Skonvertovane cislo : %d \r\n",time);
 };
 
@@ -165,21 +172,21 @@
 void RFM95W::OnRxTimeout( void )
 {
     radio.Sleep();
-    radio.Rx(0);
+//    radio.Rx(0);
 };
 
 void RFM95W::OnRxError( void )
 {
     radio.Sleep();
 //    PC.printf("Chyba prijatia ! \r\n");
-    radio.Rx(0);
+//    radio.Rx(0);
 };
 
 void RFM95W::OnCadDone( bool channelActivityDetected ) {};
 
-void RFM95W::SendValue(uint8_t addr, float value)
+void RFM95W::SendValue(uint8_t addr, float tempValue, uint16_t fotoValue, float batteryValue)
 {
-    snprintf((char *)sendBuffer, BUFF_SIZE, "%c%c%c|%.2f|",addr, NODE_ID, messageNumber, value);
+    snprintf((char *)sendBuffer, BUFF_SIZE, "%c%c%c|%.2f|%d|%.3f|",addr, NODE_ID, messageNumber, tempValue, fotoValue, batteryValue);
     sendCounter = 0;
     receivedAck = false;
     SendMessage();
@@ -187,7 +194,7 @@
 
 void RFM95W::Init( void )
 {
-    PC.printf( "\n\n\r------- RFM95W GATEWAY -------\n\r" );
+//    PC.printf( "\n\n\r------- RFM95W GATEWAY -------\n\r" );
     // Initialize Radio driver
     radioEvents.TxDone = TxDone;
     radioEvents.RxDone = RxDone;
@@ -198,12 +205,12 @@
     radio.Init( &radioEvents );
     // verify the connection with the board
     while( radio.Read( REG_VERSION ) == 0x00  ) {
-        PC.printf( "Radiovy modul nie je pripojeny!\n\r", NULL );
+//        PC.printf( "Radiovy modul nie je pripojeny!\n\r", NULL );
         wait(1);
     }
     radio.SetChannel( RF_FREQUENCY );
 #if USE_MODEM_LORA == 1
-    PC.printf("\n\r        > LORA Mod <        \n\n\r");
+//    PC.printf("\n\r        > LORA Mod <        \n\n\r");
     radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
                        LORA_SPREADING_FACTOR, LORA_CODINGRATE,
                        LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
@@ -218,7 +225,7 @@
 
 #elif USE_MODEM_FSK == 1
 
-    PC.printf("\n\r        > FSK Mod <        \n\n\r");
+//    PC.printf("\n\r        > FSK Mod <        \n\n\r");
     radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
                        FSK_DATARATE, 0,
                        FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
@@ -240,15 +247,16 @@
     paConfig |= RF_PACONFIG_PASELECT_PABOOST;
     radio.Write( REG_PACONFIG, paConfig );
 
-    PC.printf("________Start aplikacie________\r\n" );
-    timeOnAirSec = (radio.TimeOnAir( MODEM_LORA, 10)/1000.0);              // time on air v ms
-    PC.printf( "\n\n\r------- Time on air : %f sec. -------\n\r", timeOnAirSec);
+//    PC.printf("________Start aplikacie________\r\n" );
+    timeOnAirSec = (radio.TimeOnAir( MODEM_LORA, 30)/1000.0);              // time on air v ms
+//    PC.printf( "\n\n\r------- Time on air : %f sec. -------\n\r", timeOnAirSec);
     indicationLed = 1;
     InitRandom();
     messageNumber = rand();
     for(uint16_t i = 0; i < MAX_DEVICES; i++)
         messageNumbers[i]=rand() % 256;
-    radio.Rx(0);
+//    radio.Rx(0);
+    radio.Sleep();
 };
 
 void RFM95W::InitRandom()
--- a/RFM95W.h	Tue May 08 13:24:52 2018 +0000
+++ b/RFM95W.h	Sat May 08 16:04:09 2021 +0000
@@ -6,8 +6,8 @@
 #include "Thermometer.h"
 #include "Board.h"
 
-#define GATEWAY_ID 1    // Adresa gatewayu (id)
-#define NODE_ID 4       // Moja adresa (id) 
+// #define GATEWAY_ID 1    // Adresa gatewayu (id)
+// #define NODE_ID 2      // Moja adresa (id) 
 
 ////////////////////////////////////////////////////////////////////////////////
 
@@ -97,7 +97,7 @@
     void OnRxTimeout( void );
     void OnRxError( void );
     void OnCadDone( bool channelActivityDetected );  
-    void SendValue(uint8_t addr, float value);
+    void SendValue(uint8_t addr, float tempValue, uint16_t fotoValue, float batteryValue);
 };
 extern RFM95W rfm;
 
--- a/SX1276Lib.lib	Tue May 08 13:24:52 2018 +0000
+++ b/SX1276Lib.lib	Sat May 08 16:04:09 2021 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/lukas_formanek/code/SX1276Lib_LoRa_Node/#72f100539076
+https://os.mbed.com/users/lukas_formanek/code/SX1276Lib_LoRa_Node/#97ea7ccf4d1d
--- a/Thermometer.cpp	Tue May 08 13:24:52 2018 +0000
+++ b/Thermometer.cpp	Sat May 08 16:04:09 2021 +0000
@@ -1,11 +1,17 @@
 #include "Thermometer.h"
 
-Thermometer thermometer(true, true, false, THERM_PIN);
+const double voltage_divider_ref = V_BATT_REFF * (1.0/(1.0+1.6));   // max uroven napatia na delici
+
+Thermometer thermometer(true, true, false, THERM_PIN, BATTERY_PIN, FOTO_PIN);
 
-Thermometer::Thermometer(bool crcOn, bool useAddr, bool parasitic, PinName pin)
-    : thermometer(crcOn, useAddr, parasitic, pin)
+Thermometer::Thermometer(bool crcOn, bool useAddr, bool parasitic, PinName pinThermometer, PinName bat, PinName fot)
+    : thermometer(crcOn, useAddr, parasitic, pinThermometer),
+      battery(bat),
+      foto(fot)
 {
     meassuredValue = 0.0;
+    batteryValue = 0.0;
+    fotoValue = 0.0;
     intervalSeconds = MEASURE_INTERVAL;
     counter = 0;
 };
@@ -21,7 +27,8 @@
     counter = 0;
     Meassure();
     intervalSeconds = seconds;
-    thermometerTicker.attach_us(this,&Thermometer::OnSampleTick,1000000);       // kazdu sekundu
+//    thermometerTicker.attach_us(this,&Thermometer::OnSampleTick,1000000);       // kazdu sekundu
+    thermometerTicker.attach(this,&Thermometer::OnSampleTick,1.0);              // kazdu sekundu
 };
 
 void Thermometer::StopPeriodicMeassure()
@@ -33,17 +40,21 @@
 void Thermometer::Meassure()
 {
     meassuredValue = thermometer.readTemperature();
+    batteryValue = ((battery.read()*3.3)/voltage_divider_ref)*V_BATT_REFF;
+    fotoValue = foto.read_u16();
     while((rint(meassuredValue)) == 85) {
         Init();
         meassuredValue = thermometer.readTemperature();
+        batteryValue = ((battery.read()*3.3)/voltage_divider_ref)*V_BATT_REFF;
+        fotoValue = foto.read_u16();
     }
-    rfm.SendValue(GATEWAY_ID, meassuredValue);
+    rfm.SendValue(GATEWAY_ID, meassuredValue, fotoValue, batteryValue);
 };
 
 void Thermometer::OnSampleTick()
 {
     counter++;
-    if(counter >= intervalSeconds) {
+    if(counter >= 60) {
         counter = 0;
         Meassure();
     }
--- a/Thermometer.h	Tue May 08 13:24:52 2018 +0000
+++ b/Thermometer.h	Sat May 08 16:04:09 2021 +0000
@@ -18,14 +18,19 @@
 {
 private:
     THERMOMETER thermometer;
+    AnalogIn battery;
+    AnalogIn foto;
     Ticker thermometerTicker;
     volatile float meassuredValue;
+    volatile float batteryValue;
+//    volatile float fotoValue;
+    volatile uint16_t fotoValue;
     volatile int intervalSeconds;
     volatile int counter;
 
     void OnSampleTick();
 public:
-    Thermometer(bool crcOn, bool useAddr, bool parasitic, PinName pin);
+    Thermometer(bool crcOn, bool useAddr, bool parasitic, PinName pinThermometer, PinName battery, PinName foto);
     void Init();
     void Meassure();
     void StartPeriodicMeassure(int seconds);
--- a/mbed.bld	Tue May 08 13:24:52 2018 +0000
+++ b/mbed.bld	Sat May 08 16:04:09 2021 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/5aab5a7997ee
\ No newline at end of file
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file