Last active
January 23, 2020 02:39
-
-
Save choonewza/aa21260f85fa59d0e8571965046f8dc6 to your computer and use it in GitHub Desktop.
Content CAT LORA LAB 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Downlink from Lora IoT by CAT is 2020012412105920200124143000 (yyyymmddhhmmssyyyymmddhhmmss) | |
Start 20200124121059 => 2020-01-24 12:10:59 | |
Stop 20200124143000 => 2020-01-24 14:30:00 | |
*/ | |
#include <TimeLib.h> | |
#include <Wire.h> | |
#include <DS3231.h> | |
#include "CatLoRaS76S.h" | |
#include "LedModule.h" | |
#include "DateTimeLib.h" | |
#define RED_LED_PIN 13 | |
#define GREEN_LED_PIN 12 | |
#define LORA_ABP_CLASS "C" | |
#define LORA_DEV_EUI "????????????????" | |
#define LORA_DEV_ADDR "????????" | |
#define LORA_NWKS_KEY "????????????????????????????????" | |
#define LORA_APPS_KEY "????????????????????????????????" | |
String startLedTime = "1970-01-01 00:00:01"; | |
String stopLedTime = "1970-01-01 00:00:01"; | |
LedModule redLed(RED_LED_PIN); | |
LedModule greenLed(GREEN_LED_PIN); | |
uint32_t startLedUnixtime; | |
uint32_t stopLedUnixtime; | |
DS3231 clock; | |
RTCDateTime dt; | |
uint8_t seconds = 0; | |
CatLoRaS76S lora; | |
uint8_t intervalLoraTX = 30; | |
uint8_t intervalLoraRX = 1; | |
uint8_t port = 1; | |
String payload = "00"; | |
void setup() { | |
Serial.begin(115200); | |
delay(2000); | |
// Initialize LED | |
redLed.begin(); | |
greenLed.begin(); | |
redLed.on(); | |
// Initialize DS3231 | |
Serial.println("Initialize DS3231"); | |
clock.begin(); | |
if (clock.isReady() && Serial) { | |
clock.setDateTime(__DATE__, __TIME__); | |
} | |
// Initialize LoRa | |
Serial.println("-> Lora Setting..."); | |
lora.begin(115200); | |
Serial.println("-> Lora ABP Join..."); | |
lora.joinABP(String(LORA_ABP_CLASS), | |
String(LORA_DEV_EUI), | |
String(LORA_DEV_ADDR), | |
String(LORA_NWKS_KEY), | |
String(LORA_APPS_KEY)); | |
// Convert datetime setup led to unixtime | |
startLedUnixtime = DateTimeLib::unixtime(startLedTime); | |
stopLedUnixtime = DateTimeLib::unixtime(stopLedTime); | |
// start-up success -> red led turn-on; | |
redLed.off(); | |
Serial.println("-> Ready Go."); | |
} | |
void loop() { | |
dt = clock.getDateTime(); | |
if (dt.second != seconds) { | |
seconds = dt.second; | |
// ----- Show current datetime on terminal | |
String datetime = String(clock.dateFormat("Y-m-d H:i:s", dt)); | |
Serial.println(datetime); | |
// ----- Convert current datetime to unixtime ----- | |
uint32_t utNow = DateTimeLib::unixtime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second); | |
// ----- Check datetime for action ----- | |
if (utNow >= startLedUnixtime && utNow <= stopLedUnixtime) { | |
greenLed.on(); | |
} else { | |
greenLed.off(); | |
} | |
// ----- LoRa TX ----- | |
if (seconds % intervalLoraTX == 0) { | |
loraTransmit(); | |
} | |
// ----- LoRa RX ----- | |
if (seconds % intervalLoraRX == 0) { | |
loraReceive(); | |
} | |
} | |
} | |
void loraTransmit() { | |
lora.transmit(port, payload); | |
} | |
void loraReceive() { | |
String receive = lora.receive(); | |
if (receive != "") { | |
unsigned int rxPort = lora.getPortReceive(receive); | |
String rxPayload = lora.getPayloadReceive(receive); | |
if (rxPort != 0 && rxPayload != "") { | |
Serial.println("-----RX From CAT LORA-----"); | |
Serial.println(String("Port = ") + rxPort); | |
Serial.println(String("Payload = ") + rxPayload); | |
/*Downlink from Lora IoT by CAT is 2020012412105920200124143000 (yyyymmddhhmmssyyyymmddhhmmss) | |
Start 20200124121059 => 2020-01-24 12:10:59 | |
Stop 20200124143000 => 2020-01-24 14:30:00 | |
rxPayload = "2020012412105920200124143000"; | |
*/ | |
//new start datetime | |
uint16_t sYear = (uint16_t) rxPayload.substring(0, 4).toInt(); | |
uint8_t sMonth = (uint8_t) rxPayload.substring(4, 6).toInt(); | |
uint8_t sDay = (uint8_t) rxPayload.substring(6, 8).toInt(); | |
uint8_t sHour = (uint8_t) rxPayload.substring(8, 10).toInt(); | |
uint8_t sMinute = (uint8_t) rxPayload.substring(10, 12).toInt(); | |
uint8_t sSecond = (uint8_t) rxPayload.substring(12, 14).toInt(); | |
//new stop datetime | |
uint16_t eYear = (uint16_t) rxPayload.substring(14, 18).toInt(); | |
uint8_t eMonth = (uint8_t) rxPayload.substring(18, 20).toInt(); | |
uint8_t eDay = (uint8_t) rxPayload.substring(20, 22).toInt(); | |
uint8_t eHour = (uint8_t) rxPayload.substring(22, 24).toInt(); | |
uint8_t eMinute = (uint8_t) rxPayload.substring(24, 26).toInt(); | |
uint8_t eSecond = (uint8_t) rxPayload.substring(26, 28).toInt(); | |
//set startLedUnixtime and stopLedUnixtime | |
startLedUnixtime = DateTimeLib::unixtime(sYear, sMonth, sDay, sHour, sMinute, sSecond); | |
stopLedUnixtime = DateTimeLib::unixtime(eYear, eMonth, eDay, eHour, eMinute, eSecond); | |
//set datetime format yyyy-mm-dd HH:mm:ss | |
char startLedDateTime[32]; | |
char stopLedDateTime[32]; | |
sprintf(startLedDateTime, "%04d-%02d-%02d %02d:%02d:%02d", sYear, sMonth, sDay, sHour, sMinute, sSecond); | |
sprintf(stopLedDateTime, "%04d-%02d-%02d %02d:%02d:%02d", eYear, eMonth, eDay, eHour, eMinute, eSecond); | |
//Show new datetime | |
Serial.println(String("New Start LED : ") + startLedDateTime); | |
Serial.println(String("New Stop LED : ") + stopLedDateTime); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "DateTimeLib.h" | |
const uint8_t daysArray[] PROGMEM = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; | |
uint32_t DateTimeLib::unixtime(const char *datetime) | |
{ | |
return DateTimeLib::unixtime(String(datetime)); | |
} | |
uint32_t DateTimeLib::unixtime(String datetime) | |
{ | |
uint16_t year = (uint16_t) datetime.substring(0, 4).toInt(); | |
uint8_t month = (uint8_t) datetime.substring(5, 7).toInt(); | |
uint8_t day = (uint8_t) datetime.substring(8, 10).toInt(); | |
uint8_t hour = (uint8_t) datetime.substring(11, 13).toInt(); | |
uint8_t minute = (uint8_t) datetime.substring(14, 16).toInt(); | |
uint8_t second = (uint8_t) datetime.substring(17, 19).toInt(); | |
return DateTimeLib::unixtime(year, month, day, hour, minute, second); | |
} | |
uint32_t DateTimeLib::unixtime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) | |
{ | |
uint32_t u; | |
u = time2long(date2days(year, month, day), hour, minute, second); | |
u += 946681200; | |
return u; | |
} | |
uint16_t DateTimeLib::date2days(uint16_t year, uint8_t month, uint8_t day) | |
{ | |
year = year - 2000; | |
uint16_t days16 = day; | |
for (uint8_t i = 1; i < month; ++i) | |
{ | |
days16 += pgm_read_byte(daysArray + i - 1); | |
} | |
if ((month == 2) && isLeapYear(year)) | |
{ | |
++days16; | |
} | |
return days16 + 365 * year + (year + 3) / 4 - 1; | |
} | |
long DateTimeLib::time2long(uint16_t days, uint8_t hours, uint8_t minutes, uint8_t seconds) | |
{ | |
return ((days * 24L + hours) * 60 + minutes) * 60 + seconds; | |
} | |
uint16_t DateTimeLib::dayInYear(uint16_t year, uint8_t month, uint8_t day) | |
{ | |
uint16_t fromDate; | |
uint16_t toDate; | |
fromDate = date2days(year, 1, 1); | |
toDate = date2days(year, month, day); | |
return (toDate - fromDate); | |
} | |
bool DateTimeLib::isLeapYear(uint16_t year) | |
{ | |
return (year % 4 == 0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DateTimeLib_h | |
#define DateTimeLib_h | |
#include "Arduino.h" | |
class DateTimeLib | |
{ | |
public: | |
static uint32_t unixtime(const char* datetime); //format yyyy-mm-dd HH:mm:ss | |
static uint32_t unixtime(String datetime); //format yyyy-mm-dd HH:mm:ss | |
static uint32_t unixtime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second); | |
static uint16_t date2days(uint16_t year, uint8_t month, uint8_t day); | |
static long time2long(uint16_t days, uint8_t hours, uint8_t minutes, uint8_t seconds); | |
static uint16_t dayInYear(uint16_t year, uint8_t month, uint8_t day); | |
static bool isLeapYear(uint16_t year); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment