LoRa Access Point 1.5.2018

Dependencies:   mbed ds3231 SX1276Lib_LoRa_Access_Point

Committer:
lukas_formanek
Date:
Sat May 08 16:23:46 2021 +0000
Revision:
11:376bfcdda0d4
Parent:
10:e62222c46ee9
LoRa_Access_Point

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lukas_formanek 0:ea088908ad26 1 #include "mbed.h"
lukas_formanek 0:ea088908ad26 2 #include "RFM95W.h"
lukas_formanek 0:ea088908ad26 3 #include "ESP8266.h"
lukas_formanek 1:7543af31b91f 4 #include "Board.h"
lukas_formanek 10:e62222c46ee9 5 #include "SD.h"
lukas_formanek 11:376bfcdda0d4 6 //#include "RTC.h"
lukas_formanek 0:ea088908ad26 7
lukas_formanek 11:376bfcdda0d4 8 DS3231 rtc(D14, D15);
lukas_formanek 10:e62222c46ee9 9 //time_t epoch_time = 0;
lukas_formanek 10:e62222c46ee9 10
lukas_formanek 10:e62222c46ee9 11 int hour;
lukas_formanek 10:e62222c46ee9 12 int minute;
lukas_formanek 10:e62222c46ee9 13 int second;
lukas_formanek 10:e62222c46ee9 14
lukas_formanek 10:e62222c46ee9 15 int dayOfWeek;
lukas_formanek 10:e62222c46ee9 16 int date;
lukas_formanek 10:e62222c46ee9 17 int month;
lukas_formanek 11:376bfcdda0d4 18 int year;
lukas_formanek 11:376bfcdda0d4 19
lukas_formanek 11:376bfcdda0d4 20
lukas_formanek 11:376bfcdda0d4 21 void RTC_Init()
lukas_formanek 11:376bfcdda0d4 22 {
lukas_formanek 11:376bfcdda0d4 23 rtc.setI2Cfrequency(400000);
lukas_formanek 11:376bfcdda0d4 24 //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
lukas_formanek 11:376bfcdda0d4 25 rtc.convertTemperature();
lukas_formanek 11:376bfcdda0d4 26 int reg=rtc.readRegister(DS3231_Aging_Offset);
lukas_formanek 11:376bfcdda0d4 27 if (reg>127)
lukas_formanek 11:376bfcdda0d4 28 {reg=reg-256;}
lukas_formanek 11:376bfcdda0d4 29
lukas_formanek 11:376bfcdda0d4 30 rtc.OSF();
lukas_formanek 11:376bfcdda0d4 31 // pc.printf("Aging offset : %i\r\n",reg);
lukas_formanek 11:376bfcdda0d4 32 // pc.printf("OSF flag : %i",rtc.OSF());
lukas_formanek 11:376bfcdda0d4 33 // pc.printf("\r\n");
lukas_formanek 11:376bfcdda0d4 34 }
lukas_formanek 11:376bfcdda0d4 35
lukas_formanek 11:376bfcdda0d4 36 void RTC_actualize()
lukas_formanek 11:376bfcdda0d4 37 {
lukas_formanek 11:376bfcdda0d4 38
lukas_formanek 11:376bfcdda0d4 39 rtc.readDateTime(&dayOfWeek,&date,&month,&year,&hour,&minute,&second);
lukas_formanek 11:376bfcdda0d4 40 snprintf(actual_time, 30, "%02i.%02i.%02i %02i:%02i:%02i ",date,month,year,hour,minute,second);
lukas_formanek 11:376bfcdda0d4 41 }
lukas_formanek 10:e62222c46ee9 42
lukas_formanek 11:376bfcdda0d4 43 void RTC_set()
lukas_formanek 11:376bfcdda0d4 44 {
lukas_formanek 11:376bfcdda0d4 45 /*
lukas_formanek 11:376bfcdda0d4 46 DS3231 rtc(D14, D15);
lukas_formanek 11:376bfcdda0d4 47 rtc.setI2Cfrequency(400000);
lukas_formanek 11:376bfcdda0d4 48 //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
lukas_formanek 11:376bfcdda0d4 49 rtc.convertTemperature();
lukas_formanek 11:376bfcdda0d4 50 int reg=rtc.readRegister(DS3231_Aging_Offset);
lukas_formanek 11:376bfcdda0d4 51 if (reg>127)
lukas_formanek 11:376bfcdda0d4 52 {reg=reg-256;}
lukas_formanek 11:376bfcdda0d4 53
lukas_formanek 11:376bfcdda0d4 54 rtc.OSF();
lukas_formanek 11:376bfcdda0d4 55 */
lukas_formanek 11:376bfcdda0d4 56 /*
lukas_formanek 11:376bfcdda0d4 57 ds3231_cntl_stat_t rtc_control_status = {0,0};
lukas_formanek 11:376bfcdda0d4 58
lukas_formanek 11:376bfcdda0d4 59 rtc.set_cntl_stat_reg(rtc_control_status);
lukas_formanek 11:376bfcdda0d4 60 */
lukas_formanek 11:376bfcdda0d4 61 wifi.getServerDateTime(&dayOfWeek,&date,&month,&year,&hour,&minute,&second);
lukas_formanek 11:376bfcdda0d4 62 pc.printf("date time : %02i / %02i-%02i-%02i %02i:%02i:%02i",dayOfWeek,date,month,year,hour,minute,second);
lukas_formanek 11:376bfcdda0d4 63 rtc.setTime(hour,minute,second); // uncomment to set time
lukas_formanek 11:376bfcdda0d4 64 rtc.setDate(dayOfWeek,date,month,year); // uncomment to set date
lukas_formanek 11:376bfcdda0d4 65 }
lukas_formanek 10:e62222c46ee9 66
lukas_formanek 10:e62222c46ee9 67 int main()
lukas_formanek 10:e62222c46ee9 68 {
lukas_formanek 11:376bfcdda0d4 69 // __disable_irq(); // Disable Interrupts
lukas_formanek 11:376bfcdda0d4 70 // RTC_Init();
lukas_formanek 11:376bfcdda0d4 71 RTC_Init();
lukas_formanek 11:376bfcdda0d4 72 wifi.Init();
lukas_formanek 11:376bfcdda0d4 73 // rfm.Init();
lukas_formanek 11:376bfcdda0d4 74 bt.Init();
lukas_formanek 11:376bfcdda0d4 75 // __enable_irq(); // Disable Interrupts
lukas_formanek 11:376bfcdda0d4 76
lukas_formanek 11:376bfcdda0d4 77 RTC_actualize();
lukas_formanek 11:376bfcdda0d4 78 rfm.Init();
lukas_formanek 11:376bfcdda0d4 79 // SDcard.Read();
lukas_formanek 11:376bfcdda0d4 80 // rtc.setDate(7,6,4,2019);
lukas_formanek 11:376bfcdda0d4 81 while(1)
lukas_formanek 11:376bfcdda0d4 82 {
lukas_formanek 11:376bfcdda0d4 83 wifi.CheckSettings();
lukas_formanek 11:376bfcdda0d4 84 if(rfm.ReceivedMessage())
lukas_formanek 11:376bfcdda0d4 85 RTC_actualize();
lukas_formanek 11:376bfcdda0d4 86 // if(wifi.actualizeActualTime())
lukas_formanek 11:376bfcdda0d4 87 // RTC_set();
lukas_formanek 11:376bfcdda0d4 88 }
lukas_formanek 11:376bfcdda0d4 89 }
lukas_formanek 11:376bfcdda0d4 90 /*
lukas_formanek 11:376bfcdda0d4 91 int main()
lukas_formanek 11:376bfcdda0d4 92 {
lukas_formanek 11:376bfcdda0d4 93 rtcClock.Init();
lukas_formanek 11:376bfcdda0d4 94 wifi.Init();
lukas_formanek 11:376bfcdda0d4 95 bt.Init();
lukas_formanek 11:376bfcdda0d4 96 rfm.Init();
lukas_formanek 11:376bfcdda0d4 97 while(1)
lukas_formanek 11:376bfcdda0d4 98 {
lukas_formanek 11:376bfcdda0d4 99 wifi.CheckSettings();
lukas_formanek 11:376bfcdda0d4 100 }
lukas_formanek 11:376bfcdda0d4 101 }
lukas_formanek 11:376bfcdda0d4 102 */
lukas_formanek 11:376bfcdda0d4 103 /*
lukas_formanek 11:376bfcdda0d4 104 int main()
lukas_formanek 11:376bfcdda0d4 105 {
lukas_formanek 10:e62222c46ee9 106 DS3231 rtc(D14,D15);
lukas_formanek 10:e62222c46ee9 107
lukas_formanek 10:e62222c46ee9 108
lukas_formanek 10:e62222c46ee9 109 rtc.setI2Cfrequency(400000);
lukas_formanek 10:e62222c46ee9 110 //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
lukas_formanek 10:e62222c46ee9 111 rtc.convertTemperature();
lukas_formanek 10:e62222c46ee9 112 int reg=rtc.readRegister(DS3231_Aging_Offset);
lukas_formanek 10:e62222c46ee9 113 if (reg>127)
lukas_formanek 10:e62222c46ee9 114 {reg=reg-256;}
lukas_formanek 10:e62222c46ee9 115
lukas_formanek 10:e62222c46ee9 116 pc.printf("Aging offset : %i\r\n",reg);
lukas_formanek 10:e62222c46ee9 117 pc.printf("OSF flag : %i",rtc.OSF());
lukas_formanek 10:e62222c46ee9 118 pc.printf("\r\n");
lukas_formanek 11:376bfcdda0d4 119
lukas_formanek 10:e62222c46ee9 120 rtc.readDate(&date,&month,&year);
lukas_formanek 10:e62222c46ee9 121 pc.printf("date : %02i-%02i-%02i",date,month,year);
lukas_formanek 10:e62222c46ee9 122 pc.printf("\r\n");
lukas_formanek 10:e62222c46ee9 123
lukas_formanek 10:e62222c46ee9 124 //RTC.setTime(19,48,45); // uncomment to set time
lukas_formanek 10:e62222c46ee9 125
lukas_formanek 10:e62222c46ee9 126 rtc.readTime(&hour,&minute,&second);
lukas_formanek 10:e62222c46ee9 127 pc.printf("time : %02i:%02i:%02i",hour,minute,second);
lukas_formanek 10:e62222c46ee9 128 pc.printf("\r\n");
lukas_formanek 10:e62222c46ee9 129
lukas_formanek 10:e62222c46ee9 130 //RTC.setDate(6,22,12,2012); // uncomment to set date
lukas_formanek 10:e62222c46ee9 131
lukas_formanek 10:e62222c46ee9 132 rtc.readDateTime(&dayOfWeek,&date,&month,&year,&hour,&minute,&second);
lukas_formanek 10:e62222c46ee9 133 pc.printf("date time : %i / %02i-%02i-%02i %02i:%02i:%02i",dayOfWeek,date,month,year,hour,minute,second);
lukas_formanek 10:e62222c46ee9 134 pc.printf("\r\n");
lukas_formanek 10:e62222c46ee9 135
lukas_formanek 10:e62222c46ee9 136 pc.printf("temperature :%6.2f",rtc.readTemp());
lukas_formanek 10:e62222c46ee9 137 pc.printf("\r\n");
lukas_formanek 11:376bfcdda0d4 138
lukas_formanek 10:e62222c46ee9 139 wifi.Init();
lukas_formanek 10:e62222c46ee9 140 bt.Init();
lukas_formanek 10:e62222c46ee9 141 rfm.Init();
lukas_formanek 10:e62222c46ee9 142 while(1)
lukas_formanek 10:e62222c46ee9 143 {
lukas_formanek 10:e62222c46ee9 144 wifi.CheckSettings();
lukas_formanek 10:e62222c46ee9 145 rtc.readDateTime(&dayOfWeek,&date,&month,&year,&hour,&minute,&second);
lukas_formanek 10:e62222c46ee9 146 snprintf(actual_time, 30, "%02i.%02i.%02i %02i:%02i:%02i |",date,month,year,hour,minute,second);
lukas_formanek 10:e62222c46ee9 147 // pc.printf("%02i.%02i.%02i %02i:%02i:%02i\r\n",date,month,year,hour,minute,second);
lukas_formanek 10:e62222c46ee9 148 wait(0.5);
lukas_formanek 10:e62222c46ee9 149 }
lukas_formanek 10:e62222c46ee9 150 }
lukas_formanek 11:376bfcdda0d4 151 */
lukas_formanek 10:e62222c46ee9 152
lukas_formanek 10:e62222c46ee9 153 /*
lukas_formanek 5:19b34c4d27a1 154 int main()
lukas_formanek 5:19b34c4d27a1 155 {
lukas_formanek 0:ea088908ad26 156 wifi.Init();
lukas_formanek 0:ea088908ad26 157 bt.Init();
lukas_formanek 0:ea088908ad26 158 rfm.Init();
lukas_formanek 9:28e272b6b018 159 // SDcard.Init();
lukas_formanek 10:e62222c46ee9 160 // Ds3231 rtc(D14, D15);
lukas_formanek 10:e62222c46ee9 161
lukas_formanek 0:ea088908ad26 162 while(1) {
lukas_formanek 0:ea088908ad26 163 wifi.CheckSettings();
lukas_formanek 10:e62222c46ee9 164 epoch_time = rtc.get_epoch();
lukas_formanek 10:e62222c46ee9 165 // pc.printf("\nTime as a basic string = %s \r\n", ctime(&epoch_time));
lukas_formanek 10:e62222c46ee9 166 wait(0.5);
lukas_formanek 8:5d99fbf255d6 167 // rfm.WriteDataToSDcard();
lukas_formanek 0:ea088908ad26 168 }
lukas_formanek 0:ea088908ad26 169 }
lukas_formanek 10:e62222c46ee9 170 */
lukas_formanek 10:e62222c46ee9 171
lukas_formanek 10:e62222c46ee9 172 /*
lukas_formanek 10:e62222c46ee9 173 int main()
lukas_formanek 10:e62222c46ee9 174 {
lukas_formanek 10:e62222c46ee9 175 time_t time;
lukas_formanek 10:e62222c46ee9 176 while(1)
lukas_formanek 10:e62222c46ee9 177 {
lukas_formanek 10:e62222c46ee9 178 epoch_time = rtc.get_epoch();
lukas_formanek 10:e62222c46ee9 179 pc.printf("\nTime as a basic string = %s \r\n", ctime(&epoch_time));
lukas_formanek 10:e62222c46ee9 180 wait(1);
lukas_formanek 10:e62222c46ee9 181 }
lukas_formanek 10:e62222c46ee9 182
lukas_formanek 10:e62222c46ee9 183 }
lukas_formanek 10:e62222c46ee9 184 */
lukas_formanek 10:e62222c46ee9 185
lukas_formanek 10:e62222c46ee9 186
lukas_formanek 10:e62222c46ee9 187 /*
lukas_formanek 10:e62222c46ee9 188 int main(void)
lukas_formanek 10:e62222c46ee9 189 {
lukas_formanek 10:e62222c46ee9 190 wifi.Init();
lukas_formanek 10:e62222c46ee9 191 bt.Init();
lukas_formanek 10:e62222c46ee9 192 rfm.Init();
lukas_formanek 10:e62222c46ee9 193 // SDcard.Init();
lukas_formanek 10:e62222c46ee9 194 // sd.mount();
lukas_formanek 10:e62222c46ee9 195 pc.printf("Start! \r\n");
lukas_formanek 10:e62222c46ee9 196 // mon();
lukas_formanek 10:e62222c46ee9 197 // create_file();
lukas_formanek 10:e62222c46ee9 198 // init_card();
lukas_formanek 10:e62222c46ee9 199
lukas_formanek 10:e62222c46ee9 200 //rtc object
lukas_formanek 10:e62222c46ee9 201 Ds3231 rtc(D14, D15);
lukas_formanek 10:e62222c46ee9 202
lukas_formanek 10:e62222c46ee9 203 time_t epoch_time;
lukas_formanek 10:e62222c46ee9 204
lukas_formanek 10:e62222c46ee9 205 //DS3231 rtc variables
lukas_formanek 10:e62222c46ee9 206
lukas_formanek 10:e62222c46ee9 207 //default, use bit masks in ds3231.h for desired operation
lukas_formanek 10:e62222c46ee9 208 // ds3231_cntl_stat_t rtc_control_status = {0,0};
lukas_formanek 10:e62222c46ee9 209 // ds3231_time_t rtc_time;
lukas_formanek 10:e62222c46ee9 210 // ds3231_calendar_t rtc_calendar;
lukas_formanek 10:e62222c46ee9 211
lukas_formanek 10:e62222c46ee9 212 // rtc.set_cntl_stat_reg(rtc_control_status);
lukas_formanek 10:e62222c46ee9 213 char buffer[32];
lukas_formanek 10:e62222c46ee9 214 while(1)
lukas_formanek 10:e62222c46ee9 215 {
lukas_formanek 10:e62222c46ee9 216 // FILE *myLogFile;
lukas_formanek 10:e62222c46ee9 217 // pc.printf("%c[2J", ESC); //clear screen
lukas_formanek 10:e62222c46ee9 218 // pc.printf("%c[H", ESC); //move cursor to Home
lukas_formanek 10:e62222c46ee9 219
lukas_formanek 10:e62222c46ee9 220 //new epoch time fx
lukas_formanek 10:e62222c46ee9 221 epoch_time = rtc.get_epoch();
lukas_formanek 10:e62222c46ee9 222
lukas_formanek 10:e62222c46ee9 223 pc.printf("\nTime as seconds since January 1, 1970 = %d\r\n", epoch_time);
lukas_formanek 10:e62222c46ee9 224
lukas_formanek 10:e62222c46ee9 225 pc.printf("\nTime as a basic string = %s \r\n", ctime(&epoch_time));
lukas_formanek 10:e62222c46ee9 226
lukas_formanek 10:e62222c46ee9 227 strftime(buffer, 32, "%I:%M %p\n", localtime(&epoch_time));
lukas_formanek 10:e62222c46ee9 228 pc.printf("\nTime as a custom formatted string = %s \r\n", buffer);
lukas_formanek 10:e62222c46ee9 229
lukas_formanek 10:e62222c46ee9 230 if (myLogFile)
lukas_formanek 10:e62222c46ee9 231 fclose(myLogFile);
lukas_formanek 10:e62222c46ee9 232 myLogFile = fopen("/sd/DATA_LOG.txt", "a");
lukas_formanek 10:e62222c46ee9 233 pc.printf("try to open\r\n");
lukas_formanek 10:e62222c46ee9 234 if (myLogFile == NULL)
lukas_formanek 10:e62222c46ee9 235 {
lukas_formanek 10:e62222c46ee9 236 pc.printf("ERROR: failed to open the log file for writing!\r\n");
lukas_formanek 10:e62222c46ee9 237 }
lukas_formanek 10:e62222c46ee9 238 else
lukas_formanek 10:e62222c46ee9 239 {
lukas_formanek 10:e62222c46ee9 240 fprintf(myLogFile," %s" ,ctime(&epoch_time));
lukas_formanek 10:e62222c46ee9 241 // fclose(myLogFile);
lukas_formanek 10:e62222c46ee9 242 // free(myLogFile);
lukas_formanek 10:e62222c46ee9 243 }
lukas_formanek 10:e62222c46ee9 244 fclose(myLogFile);
lukas_formanek 10:e62222c46ee9 245
lukas_formanek 10:e62222c46ee9 246 wait(2);
lukas_formanek 10:e62222c46ee9 247 }
lukas_formanek 10:e62222c46ee9 248 }
lukas_formanek 10:e62222c46ee9 249
lukas_formanek 10:e62222c46ee9 250 */
lukas_formanek 10:e62222c46ee9 251