LoRa Access Point 1.5.2018

Dependencies:   mbed ds3231 SX1276Lib_LoRa_Access_Point

Files at this revision

API Documentation at this revision

Comitter:
lukas_formanek
Date:
Sat May 08 16:23:46 2021 +0000
Parent:
10:e62222c46ee9
Commit message:
LoRa_Access_Point

Changed in this revision

Board.h Show annotated file Show diff for this revision Revisions of this file
ESP8266.cpp Show annotated file Show diff for this revision Revisions of this file
ESP8266.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
RTC.cpp Show annotated file Show diff for this revision Revisions of this file
RTC.h Show annotated file Show diff for this revision Revisions of this file
SD.cpp Show annotated file Show diff for this revision Revisions of this file
SD.h Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
ds3231.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Board.h	Thu Mar 28 09:55:48 2019 +0000
+++ b/Board.h	Sat May 08 16:23:46 2021 +0000
@@ -5,7 +5,7 @@
 #include "DS3231.h"
 
 #define BAUDRATE        115200
-#define LED_BLIK_PERIOD 0.10
+#define LED_BLIK_PERIOD 0.05
 
 #define LED_PIN     PB_0            // PB_0  - SMD lED on PCB
 #define NOISE_PIN   PB_1
@@ -37,6 +37,9 @@
 #define SD_SCK      PC_10
 #define SD_CS       PC_9
 
+#define RTC_SDA     PB_9
+#define RTC_SCL     PB_8
+
 extern Serial pc;
 //extern DS3231 rtc;
 // extern time_t epoch_time; 
--- a/ESP8266.cpp	Thu Mar 28 09:55:48 2019 +0000
+++ b/ESP8266.cpp	Sat May 08 16:23:46 2021 +0000
@@ -22,6 +22,15 @@
     receiveResponse = false;
     setServerIp = false;
     setWifiSettings = false;
+    
+    actualizeTime = false;
+    hour = 0;
+    minute = 0;
+    second = 0;
+    dayOfWeek = 0;
+    date = 0;
+    month = 0;
+    year = 0;  
 };
 
 
@@ -34,6 +43,15 @@
     receiveResponse = false;
     setServerIp = false;
     setWifiSettings = false;
+    
+    actualizeTime = false;
+    hour = 0;
+    minute = 0;
+    second = 0;
+    dayOfWeek = 0;
+    date = 0;
+    month = 0;
+    year = 0;  
 };
 
 
@@ -67,6 +85,11 @@
 
             if (strstr(buffer, "0|") != NULL)
                 rfm.SendMessage(buffer);
+/*                
+            if (strstr(buffer, "~TIME~") != NULL)
+                convert_time();
+*/
+
 
             /*
                        if( strncmp( ( const char* )_responseBuf, "+IPD,", 5 ) == 0 )
@@ -133,9 +156,10 @@
 void ESP8266::SendMessage(char message[])
 {
 //    pc.printf("%.19s  ", ctime(&epoch_time));
-    pc.printf("%s",actual_time);
-    pc.printf("%s",message);
-    wifiUart.printf("%s",message);
+//    pc.printf("%s",actual_time);
+//    pc.printf("%s",rtcClock.GetTime());
+    pc.printf("%s\r\n",message);
+    wifiUart.printf("%s\r\n",message);
 };
 
 void ESP8266::CheckSettings()
@@ -191,3 +215,90 @@
     printf("ATTTTTTTTTTT");
     wait(0.2);
 };
+
+void ESP8266::convert_time()
+{
+    pc.printf("%s\r\n", buffer);
+    char * pch;
+    uint8_t state=0;
+    pch = strtok (buffer,DELIMITER);
+    while (pch != NULL) {
+        switch(state) {
+            case 0 :
+                wifi.dayOfWeek=atoi(pch);
+                wifi.dayOfWeek++;
+                state=1;
+//                pc.printf("converted day of week : %d  \r\n",dayOfWeek);
+                break;
+                
+            case 1 :
+                wifi.date=atoi(pch);
+                state=2;
+//                pc.printf("converted date : %d  \r\n",date);
+                break;
+
+            case 2 :
+                wifi.month=atoi(pch);
+                state=3;
+                break;
+
+            case 3 :
+                wifi.year=atoi(pch);
+                state=4;
+                break;
+
+            case 4 :
+                wifi.hour=atoi(pch);
+                state=5;
+                break;
+
+            case 5 :
+                wifi.minute=atoi(pch);
+                state=6;
+                break;
+
+            case 6 :
+                wifi.second=atoi(pch);
+                state=7;
+                break;
+
+            default:
+                state=7;
+                break;
+        }
+        pch = strtok (NULL, DELIMITER);
+    }
+    actualizeTime = true ;
+    
+};
+
+void ESP8266::getServerDateTime(int *w, int *d, int *mo, int *y, int *h, int *m, int *s)
+{
+/*
+    *w = wifi.dayOfWeek;
+    *d = wifi.date;
+    *mo = month;
+    *y = year;
+    *h = hour;
+    *m = minute;
+    *s = second;
+*/
+    *w = wifi.dayOfWeek;
+    *d = wifi.date;
+    *mo = wifi.month;
+    *y = wifi.year;
+    *h = wifi.hour;
+    *m = wifi.minute;
+    *s = wifi.second;
+
+};
+
+bool ESP8266::actualizeActualTime()
+{
+    if(actualizeTime)
+    {
+        actualizeTime = false;
+        return true;
+    }
+    return false;
+};
--- a/ESP8266.h	Thu Mar 28 09:55:48 2019 +0000
+++ b/ESP8266.h	Sat May 08 16:23:46 2021 +0000
@@ -5,12 +5,12 @@
 #include "RFM95W.h"
 #include "Board.h"
 #include "HC05.h"
+//#include "RTC.h"
 //#include "SD.h"
 
 #define BUFFER_SIZE 1024
 #define SETTING_SIZE 64
 
-
 class ESP8266
 {
 private:
@@ -27,9 +27,19 @@
     volatile bool receiveResponse;
     volatile bool setServerIp;
     volatile bool setWifiSettings;
-
+    
+    volatile bool actualizeTime;
+    int hour;
+    int minute;
+    int second;
+    int dayOfWeek;
+    int date;
+    int month;
+    int year;  
+    
     void ClearBuffer();
     void RxWifiInterrupt();
+    void convert_time();
 public:
     ESP8266(PinName tx, PinName rx, PinName reset);
     ESP8266();
@@ -41,6 +51,9 @@
     void Test();
     void CheckSettings();
     void ConfirmReceivedAck(uint8_t from);
+    
+    void getServerDateTime(int *w, int *d, int *mo, int *y, int *h, int *m, int *s);
+    bool actualizeActualTime();
 };
 
 extern ESP8266 wifi;
--- a/RFM95W.cpp	Thu Mar 28 09:55:48 2019 +0000
+++ b/RFM95W.cpp	Sat May 08 16:23:46 2021 +0000
@@ -53,7 +53,7 @@
 
 void RFM95W::OnLedTick()
 {
-    if(ledState<6) {
+    if(ledState<4) {
         indicationLed = !indicationLed;
     } else {
         ledState = 0;
@@ -66,7 +66,7 @@
 void RFM95W::SendMessage()
 {
     snprintf((char *)ack, 3, "%c%c%c",GATEWAY_ID, sendBuffer[0], messageNumber);
-    radio.Send( sendBuffer, 15 );
+    radio.Send( sendBuffer, 30 );
 }
 
 void RFM95W::SendAck(uint8_t addr, uint8_t messageNumber)
@@ -133,7 +133,7 @@
         uint8_t msgFrom = payload[1];
         indicationLed=0;
         ledTicker.detach();
-        ledTicker.attach(callback(this,&RFM95W::OnLedTick), LED_BLIK_PERIOD);
+        ledTicker.attach(callback(this,&RFM95W::OnLedTick), 0.05);
         if((size == 3) && (strncmp( ( const char* )payload, ( const char* )ack, 3 ) == 0) ) {
             receivedAck = true;
             wifi.ConfirmReceivedAck(msgFrom);
@@ -154,7 +154,7 @@
         messageNumbers[msgFrom] = payload[2];
         payload[2] = 48;
         memmove(payload, payload+4, size - 4 + 1);      // orezem prve styri bajty
-        snprintf((char *)receivedMessage, BUFF_SIZE, "%d|%d|%d|%d|%s|\r\n", rssi, snr, GATEWAY_ID, msgFrom, payload);
+        snprintf((char *)receivedMessage, BUFF_SIZE, "%s|%d|%d|%d|%d|%s|",actual_time, rssi, snr, GATEWAY_ID, msgFrom, payload);
         writeDataToSD = true;
         wifi.SendMessage((char *)receivedMessage);  
         SDcard.Write((char *)receivedMessage);
@@ -278,7 +278,7 @@
     radio.Write( REG_PACONFIG, paConfig );
 
     pc.printf("________Start aplikacie________\r\n" );
-    timeOnAirSec = (radio.TimeOnAir( MODEM_LORA, 10)/1000.0);              // time on air v ms
+    timeOnAirSec = (radio.TimeOnAir( MODEM_LORA, 25)/1000.0);              // time on air v ms
     pc.printf( "\n\n\r------- Time on air : %f sec. -------\n\r", timeOnAirSec);
     indicationLed = 1;
     InitRandom();
@@ -300,3 +300,9 @@
     }
     srand(seed);
 };
+
+bool RFM95W::ReceivedMessage()
+{
+    if(writeDataToSD) return true;
+    return false;
+};
--- a/RFM95W.h	Thu Mar 28 09:55:48 2019 +0000
+++ b/RFM95W.h	Sat May 08 16:23:46 2021 +0000
@@ -101,6 +101,7 @@
     void SendMessage(char* message);
     
     void WriteDataToSDcard();
+    bool ReceivedMessage();
 };
 extern RFM95W rfm;
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RTC.cpp	Sat May 08 16:23:46 2021 +0000
@@ -0,0 +1,60 @@
+#include "RTC.h"
+/*
+DS3231 rtc(D14,D15);
+Rtc rtcClock;
+
+
+Rtc::Rtc()
+//: rtc(PB_9, PB_8)
+{
+    Init();
+};
+
+//Rtc::Rtc(PinName sda, PinName scl)
+//: rtc(sda, scl)
+//{   
+//}
+
+
+void Rtc::Init()
+{
+    rtc.setI2Cfrequency(400000);
+    //RTC.writeRegister(DS3231_Aging_Offset,0); // uncomment to set Aging Offset 1LSB = approx. 0.1 ppm according from datasheet = 0.05 ppm @ 21 °C from my measurments
+    rtc.convertTemperature(); 
+    int reg=rtc.readRegister(DS3231_Aging_Offset);
+    if (reg>127)
+        {reg=reg-256;}
+
+    pc.printf("Aging offset : %i\r\n",reg);       
+    pc.printf("OSF flag : %i",rtc.OSF());
+    pc.printf("\r\n");
+/   
+     rtc.readDate(&date,&month,&year);
+     pc.printf("date : %02i-%02i-%02i",date,month,year);
+     pc.printf("\r\n");
+     
+     //RTC.setTime(19,48,45); // uncomment to set time
+     
+     rtc.readTime(&hour,&minute,&second);
+     pc.printf("time : %02i:%02i:%02i",hour,minute,second);
+     pc.printf("\r\n");
+     
+     //RTC.setDate(6,22,12,2012); // uncomment to set date
+
+     rtc.readDateTime(&dayOfWeek,&date,&month,&year,&hour,&minute,&second);
+     pc.printf("date time : %i / %02i-%02i-%02i %02i:%02i:%02i",dayOfWeek,date,month,year,hour,minute,second);
+     pc.printf("\r\n");
+     
+     pc.printf("temperature :%6.2f",rtc.readTemp());
+     pc.printf("\r\n");
+
+};
+
+char* Rtc::GetTime()
+{
+    rtc.readDateTime(&dayOfWeek,&date,&month,&year,&hour,&minute,&second);
+    snprintf((char*)actual_time, 30, "%02i.%02i.%02i %02i:%02i:%02i |",date,month,year,hour,minute,second);
+    return ((char*)actual_time);
+};
+
+*/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RTC.h	Sat May 08 16:23:46 2021 +0000
@@ -0,0 +1,35 @@
+#ifndef RTC_H
+#define RTC_H
+
+/*
+#include "mbed.h"
+#include "SDFileSystem.h"
+//#include "RFM95W.h"
+#include "DS3231.h"
+#include "Board.h"
+//#include "HC05.h"
+
+ 
+class Rtc
+{
+private:
+    char actual_time[30];
+    int hour;
+    int minute;
+    int second;
+
+    int dayOfWeek;
+    int date;
+    int month;
+    int year;
+      
+public:
+    Rtc();
+//    Rtc(PinName sda, PinName scl);
+    void Init();
+    char* GetTime();
+};
+
+extern Rtc rtcClock;
+*/
+#endif
\ No newline at end of file
--- a/SD.cpp	Thu Mar 28 09:55:48 2019 +0000
+++ b/SD.cpp	Sat May 08 16:23:46 2021 +0000
@@ -93,8 +93,9 @@
 //        pc.printf("3. \r\n");
 //         fprintf(myLogFile,"HAMTALA");
 //        fprintf(myLogFile,"%.19s|", ctime(&epoch_time));
-        fprintf(myLogFile,"%s", actual_time);
-        fprintf(myLogFile,"%s", data);
+//        fprintf(myLogFile,"%s", actual_time);
+//        fprintf(myLogFile,"%s", rtcClock.GetTime());
+        fprintf(myLogFile,"%s\r\n", data);
 //        pc.printf("4. \r\n");
  //       fprintf(myLogFile,"HAMTALA");
 //        if(myLogFile != NULL)
@@ -107,3 +108,26 @@
 //    __enable_irq();     // Enable Interrupts
 }
 
+void SD::Read()
+{
+    char myline [50];
+    FILE *myLogFile = fopen("/sd/DATA_LOG.txt","r");
+    if (myLogFile == NULL)
+    {
+        pc.printf("ERROR: failed to open the log file for writing!\r\n");
+    }
+    else
+    {
+        pc.printf("READING FROM SD  ::!\r\n");
+        while (!feof(myLogFile))
+        {
+            fscanf(myLogFile,"%[^\n]\n",myline);
+//            pc.printf("%s\r\n", myline);
+            wifi.SendMessage((char *)myline); 
+            wait(0.01f);
+        }
+        
+        fclose(myLogFile);
+
+    }
+};
\ No newline at end of file
--- a/SD.h	Thu Mar 28 09:55:48 2019 +0000
+++ b/SD.h	Sat May 08 16:23:46 2021 +0000
@@ -6,7 +6,7 @@
 //#include "RFM95W.h"
 #include "DS3231.h"
 #include "Board.h"
-//#include "HC05.h"
+#include "ESP8266.h"
 
  
 class SD
@@ -19,11 +19,12 @@
     SD(PinName mosi, PinName miso, PinName sck, PinName cs);
     void Init();
     void Write(char* data);
+    void Read();
 
 };
 
 extern SD SDcard;
-//extern Ds3231 rtc; 
+
 
 
 #endif
\ No newline at end of file
--- a/SDFileSystem.lib	Thu Mar 28 09:55:48 2019 +0000
+++ b/SDFileSystem.lib	Sat May 08 16:23:46 2021 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/kenjiArai/code/SDFileSystem/#81bd75334b0d
+https://os.mbed.com/users/lukas_formanek/code/LoRa_Access_Point_/#281171c0e92b
--- a/ds3231.lib	Thu Mar 28 09:55:48 2019 +0000
+++ b/ds3231.lib	Sat May 08 16:23:46 2021 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/Maxim-Integrated/code/ds3231/#bf11392ccaea
+https://os.mbed.com/users/lukas_formanek/code/ds3231/#610f7091e0ac
--- a/main.cpp	Thu Mar 28 09:55:48 2019 +0000
+++ b/main.cpp	Sat May 08 16:23:46 2021 +0000
@@ -3,8 +3,9 @@
 #include "ESP8266.h"
 #include "Board.h"
 #include "SD.h"
+//#include "RTC.h"
 
-//DS3231 rtc(D14, D15);
+DS3231 rtc(D14, D15);
 //time_t epoch_time = 0;
 
 int hour;
@@ -14,12 +15,94 @@
 int dayOfWeek;
 int date;
 int month;
-int year;  
-   
+int year; 
+ 
+
+void RTC_Init()
+{
+    rtc.setI2Cfrequency(400000);
+    //RTC.writeRegister(DS3231_Aging_Offset,0); // uncomment to set Aging Offset 1LSB = approx. 0.1 ppm according from datasheet = 0.05 ppm @ 21 °C from my measurments
+    rtc.convertTemperature(); 
+    int reg=rtc.readRegister(DS3231_Aging_Offset);
+    if (reg>127)
+        {reg=reg-256;}
+  
+    rtc.OSF();
+//    pc.printf("Aging offset : %i\r\n",reg);       
+//    pc.printf("OSF flag : %i",rtc.OSF());
+//    pc.printf("\r\n");
+}
+
+void RTC_actualize()
+{
+
+    rtc.readDateTime(&dayOfWeek,&date,&month,&year,&hour,&minute,&second);
+    snprintf(actual_time, 30, "%02i.%02i.%02i %02i:%02i:%02i ",date,month,year,hour,minute,second);
+}
 
+void RTC_set()
+{
+/*
+    DS3231 rtc(D14, D15);
+    rtc.setI2Cfrequency(400000);
+    //RTC.writeRegister(DS3231_Aging_Offset,0); // uncomment to set Aging Offset 1LSB = approx. 0.1 ppm according from datasheet = 0.05 ppm @ 21 °C from my measurments
+    rtc.convertTemperature(); 
+    int reg=rtc.readRegister(DS3231_Aging_Offset);
+    if (reg>127)
+        {reg=reg-256;}
+ 
+    rtc.OSF();
+ */ 
+/*
+    ds3231_cntl_stat_t rtc_control_status = {0,0}; 
+    
+    rtc.set_cntl_stat_reg(rtc_control_status);
+*/    
+    wifi.getServerDateTime(&dayOfWeek,&date,&month,&year,&hour,&minute,&second);
+    pc.printf("date time : %02i / %02i-%02i-%02i %02i:%02i:%02i",dayOfWeek,date,month,year,hour,minute,second);
+    rtc.setTime(hour,minute,second);        // uncomment to set time
+    rtc.setDate(dayOfWeek,date,month,year); // uncomment to set date
+}
 
 int main()
 {
+//    __disable_irq();    // Disable Interrupts
+//    RTC_Init();
+    RTC_Init();
+    wifi.Init();
+//    rfm.Init();
+    bt.Init();
+//    __enable_irq();    // Disable Interrupts
+
+    RTC_actualize();
+    rfm.Init();
+//    SDcard.Read();
+//    rtc.setDate(7,6,4,2019);
+    while(1)
+    {
+        wifi.CheckSettings();
+        if(rfm.ReceivedMessage())
+            RTC_actualize();
+//        if(wifi.actualizeActualTime())
+//            RTC_set();
+    }
+}
+/*
+int main()
+{
+    rtcClock.Init();
+    wifi.Init();
+    bt.Init();
+    rfm.Init();
+    while(1)
+    {
+        wifi.CheckSettings();
+    }
+}
+*/
+/*
+int main()
+{
     DS3231 rtc(D14,D15);
 
     
@@ -33,7 +116,7 @@
     pc.printf("Aging offset : %i\r\n",reg);       
     pc.printf("OSF flag : %i",rtc.OSF());
     pc.printf("\r\n");
-/*    
+    
      rtc.readDate(&date,&month,&year);
      pc.printf("date : %02i-%02i-%02i",date,month,year);
      pc.printf("\r\n");
@@ -52,7 +135,7 @@
      
      pc.printf("temperature :%6.2f",rtc.readTemp());
      pc.printf("\r\n");
-*/
+
     wifi.Init();
     bt.Init();
     rfm.Init();
@@ -65,6 +148,7 @@
         wait(0.5);
     }
 }
+*/
 
 /*
 int main()