#include <AM2302-Sensor.h>
char text[25];
uint8_t number;
uint16_t number2;
uint8_t day;
uint8_t month;
uint16_t year;
uint8_t WireSensorTimer[5];
const uint8_t MaxRFsensors PROGMEM = 4;
const char WireSensorName[][18] PROGMEM = { "Dvere", "Zvonek", "Vypadek napajeni", "Obnoveni napajeni", "Teplota" };
const uint8_t RFsensorNameLenght PROGMEM = 18;
struct RFSENSOR {
uint32_t code;
char name[RFsensorNameLenght];
uint8_t timer;
};
RFSENSOR RFsensor[MaxRFsensors];
uint8_t RFsensors = 4;
int8_t i = 0;
char smsBuffer[161];
char TempArray[9];
uint8_t Iteration;
bool SendingSMS = true;
const char PROGMEM Quote = '"';
bool Armed = false;
bool OnAccumulator = true;
void PrintText(int number, char input[] = {""});
constexpr unsigned int SENSOR_PIN{ A12 };
AM2302::AM2302_Sensor am2302{ SENSOR_PIN };
void setup() {
Serial.begin(115200);
WireSensorTimer[0] = 219;
WireSensorTimer[1] = 4;
WireSensorTimer[2] = 60;
WireSensorTimer[3] = 250;
WireSensorTimer[4] = 196;
strcpy_P(RFsensor[0].name, PSTR("Senzor jedna"));
RFsensor[0].timer = 200;
strcpy_P(RFsensor[1].name, PSTR("Senzor dva"));
RFsensor[1].timer = 30;
strcpy_P(RFsensor[2].name, PSTR("Senzor tri"));
RFsensor[2].timer = 230;
strcpy_P(RFsensor[3].name, PSTR("Senzor ctyri"));
RFsensor[3].timer = 1;
// if (!am2302.begin()) {
// Serial.println(F("Error sensor check!"));
// } else {
// Serial.println(F("Sensor initialized."));
// }
// delay(2000);
/*
for (Iteration = 0 + (3 * SendingSMS); Iteration < 3; Iteration++) {
if (Iteration == 2) {
Serial.println("Iteration inside is 2.");
}
delay(800);
}
Serial.print("Iteration outside of cycle is ");
Serial.println(Iteration);
*/
// int* p = new int(10);
// p[0] = 147;
// Serial.print("pointer p");
// Serial.println(&p);
// strcpy_P(smsBuffer, PSTR("Timer state: "));
// PrintText(52);
// PrintText(52, text);
// strcat_P(smsBuffer, utoa(WireSensorTimer[i], TempArray, 10));
// utoa(WireSensorTimer[i], TempArray, 10);
// strcat(smsBuffer, TempArray);
// Serial.println(smsBuffer);
// Serial.print("Temporary array: ");
// Serial.println(TempArray);
// stav();
// *text = ReadAM2302pass();
// Serial.println(text);
// ReadAM2302(text);
// PrintText(352);
/*
strcpy_P(smsBuffer, PSTR("Dlouha textova zprava s beznymi znaky textu v \"uvozovkach:\", napriklad \"kokrhac\", \"Mourinka\", slepicka \"Stazinka\", carka a prechodem\nna novy radek pred koncem."));
// strcpy_P(smsBuffer, PSTR("testovaci zprava\nradek \"pred:\", koncem, atd."));
Iteration = strlen(smsBuffer);
Serial.print("Length of variable smsBuffer: ");
Serial.println(Iteration);
Serial.println("Transcript of the variable: ");
for (uint16_t x = 0; x < Iteration; x++) {
Serial.print(smsBuffer[x]);
if (smsBuffer[x] == '\"') {
Serial.print("\"");
}
delay(80);
}
Serial.println("\nEnd of transcript.");
*/
}
void loop() {
SourceStrings(smsBuffer);
i = sscanf_P(smsBuffer, PSTR("+%[^:]: %hhu"), text, &number);
Serial.print("Source string: ");
Serial.println(smsBuffer);
// Serial.print(text);
if (i >= 1) {
Serial.print("Identificator: ");
Serial.print(text);
}
if (i == 2) {
Serial.print(", number: ");
Serial.print(number);
}
Serial.println();
// Serial.print(", params: ");
// Serial.println(i);
// if (strcmp_P(text, PSTR("CREG")) == 0 && (number == 0 || number == 2 || number == 4)) { // Not registered in network.
// Serial.println("CREG parameter 0, 2, 4");
// }
// text[0] = '-';
// text[1] = '\0';
// number = -1;
i = sscanf(smsBuffer, "+CLIP: \"%[^\"]", text);
if (i == 1) {
Serial.print("Found caller ID: ");
Serial.println(text);
}
number = 0;
if (strcmp_P(text, PSTR("CMTI")) == 0) {
sscanf_P(smsBuffer, PSTR("+CMTI: \"%*[^\"]\",%hhu"), &number);
// sscanf_P(smsBuffer, PSTR("+CMTI: \"%*s\",%hhu"), &number);
Serial.print("Received SMS in slot: ");
Serial.println(number);
}
if (strcmp_P(text, PSTR("CCLK")) == 0 || strcmp_P(text, PSTR("CMTI")) == 0) {
char Answer[80] = {};
uint8_t year = 0;
uint8_t month = 0;
uint8_t day = 0;
uint8_t hour = 0;
uint8_t minute = 0;
uint8_t second = 0;
int8_t tz = 0;
switch (ReadRTC(&year, &month, &day, &hour, &minute, &second, &tz)) {
case 1:
Serial.println("Time stamp decoded:");
sprintf_P(Answer, PSTR("%02u-%02u-%02u, %02u:%02u:%02u, TZ: %+d"), year, month, day, hour, minute, second, tz);
Serial.println(Answer);
break;
case 0:
Serial.println(F("Module's RTC can't be read right now."));
break;
case 2:
Serial.println(F("An SMS alert was found in reply."));
}
}
if (strcmp_P(text, PSTR("CPMS")) == 0) {
uint8_t NumSMS;
// +CPMS: "SM_P",0,23,"ME",0,23,"ME",0,23
if (sscanf_P(smsBuffer, PSTR("+CPMS: \"%*[^\"]\",%hhu,"), &NumSMS) == 1){
Serial.print(F("Number of stored SMS: "));
Serial.println(NumSMS);
}
}
text[0] = 0;
// SourceStrings();
// Serial.print("Source string: ");
// Serial.println(smsBuffer);
// // i = sscanf(smsBuffer, "+CLIP: \"%[+1234567890]", text);
// i = sscanf(smsBuffer, "+CLIP: \"%[^\"]", text);
// if (i == 1) {
// Serial.print("Found caller ID: ");
// Serial.println(text);
// // Serial.print(", and number of read characters: ");
// // Serial.println(number);
// }
/*
Serial.print("Source string: ");
Serial.println(smsBuffer);
i = sscanf(smsBuffer, "Na %*u je zustatek kreditu %u,%*u %*s %*s %*s %*u.%hhu.%u", &number, &month, &year);
if (i == 3) {
Serial.print("Found number ");
Serial.print(amount);
Serial.print(", year ");
Serial.print(year);
Serial.print(" and month ");
Serial.println(month);
} */
/*
Serial.print("Source string: ");
Serial.println(smsBuffer);
i = strncmp_P(smsBuffer, PSTR("+CMTI:"), 6);
if (1) {
Serial.print("Match number: ");
Serial.println(i);
} */
// delay(1000);
delay(2000);
}
uint8_t ReadRTC(uint8_t *year, uint8_t *month, uint8_t *day, uint8_t *hr, uint8_t *min, uint8_t *sec, int8_t *tz) {
uint8_t ReturnValue = 1;
uint8_t yearTwoNum;
uint8_t IterIn = 0;
do {
if (IterIn == 4) return 0;
IterIn++;
if (strlen(smsBuffer) != 29) {
if (strncmp_P(smsBuffer, PSTR("+CMTI"), 5) == 0) {
ReturnValue = 2;
return ReturnValue; // For testing only.
}
delay(100);
continue;
}
} while (sscanf_P(smsBuffer, PSTR("+CCLK: \"%2hhu/%2hhu/%2hhu,%2hhu:%2hhu:%2hhu%3hhu\""),
&yearTwoNum, month, day, hr, min, sec, tz) != 7);
*year = (uint16_t) yearTwoNum + (uint16_t) 2000; // Return value as year 2000+
return ReturnValue;
}
void SourceStrings(char * msg) {
static int Counter = 0;
if (Counter == 18) {
Counter = 0;
}
switch (Counter) {
case 0:
strcpy_P(msg, PSTR("+CREG: 1,1"));
break;
case 1:
strcpy_P(msg, PSTR("+CCLK: \"25/07/24,21:31:14+08\""));
break;
case 2:
strcpy_P(msg, PSTR("+CCLK: \"8/15,19:29:17+08\""));
break;
case 3:
strcpy_P(msg, PSTR("+CMGS: 22"));
break;
case 4:
strcpy_P(msg, PSTR("+CMGS: 1"));
break;
case 5:
strcpy_P(msg, PSTR("+CMS ERROR: 2"));
break;
case 6:
strcpy_P(msg, PSTR("+CMS ERROR: 45,25,\"sdf\""));
break;
case 7:
strcpy_P(msg, PSTR("+CMTI: \"ME\",21"));
break;
case 8:
strcpy_P(msg, PSTR("*PSUTTZ: 2025,2,15,2,2,47,\"+4\",0"));
break;
case 9:
strcpy_P(msg, PSTR("+CPMS: \"SM_P\",12,23,\"ME\",0,23,\"ME\",0,23"));
break;
case 10:
strcpy_P(msg, PSTR("+CIEV: 10,\"23001\",\"T-Mobile CZ\",\"T-Mobile CZ\", 0, 0"));
break;
case 11:
strcpy_P(msg, PSTR("DST: 0"));
break;
case 12:
strcpy_P(msg, PSTR("+CBC: 0,61,3889"));
break;
case 13:
strcpy_P(msg, PSTR("+CALV: 1"));
break;
case 14:
strcpy_P(msg, PSTR("+CREG: (0-2)"));
break;
case 15:
strcpy_P(msg, PSTR("+CLIP: \"+882451751775914\",145,\"\",0,\"\",0"));
break;
case 16:
strcpy_P(msg, PSTR("Na 739912205 je zustatek kreditu 244,27 Kc platny do 09.01.2026. T-Mobile #mezisvymi"));
break;
case 17:
strcpy_P(msg, PSTR("+CMTI: \"xdre\",21"));
break;
}
Counter ++;
}
void stav() {
char TempArray[16] = {};
char smsBuffer[161] = {};
Serial.println(F("State command."));
// Create State SMS
if (Armed) {
strcpy_P(smsBuffer, PSTR("Aktivni\n"));
} else {
strcpy_P(smsBuffer, PSTR("Neaktivni\n"));
}
ReadAM2302(TempArray);
strcat(smsBuffer, TempArray);
if (OnAccumulator) {
strcat_P(smsBuffer, PSTR("\nAkumulator: "));
strcat_P(smsBuffer, PSTR("5,24"));
// TempArray[0] = '\0';
strcat_P(smsBuffer, PSTR("V"));
}
for (uint8_t i = 0; i < 5; i++) {
if (WireSensorTimer[i] > 0) {
strcat_P(smsBuffer, PSTR("\n"));
strcat_P(smsBuffer, WireSensorName[i]);
if (WireSensorTimer[i] <= 60) {
strcat_P(smsBuffer, PSTR(" 2 "));
utoa(WireSensorTimer[i], TempArray, 10);
strcat(smsBuffer, TempArray);
} else if (WireSensorTimer[i] > 195) {
strcat_P(smsBuffer, PSTR(" 1 "));
utoa(WireSensorTimer[i] - 195, TempArray, 10);
strcat(smsBuffer, TempArray);
} else {
strcat_P(smsBuffer, PSTR(" odchod"));
}
}
}
for (uint8_t i = 0; i < RFsensors; i++) {
if (RFsensor[i].timer > 0) {
if (strlen(smsBuffer) + strlen(RFsensor[i].name) + 6 > 160) break;
strcat_P(smsBuffer, PSTR("\n"));
strcat(smsBuffer, RFsensor[i].name);
if (RFsensor[i].timer <= 60) {
strcat_P(smsBuffer, PSTR(" 2 "));
utoa(RFsensor[i].timer, TempArray, 10);
strcat(smsBuffer, TempArray);
} else if (RFsensor[i].timer > 195) {
strcat_P(smsBuffer, PSTR(" 1 "));
utoa(RFsensor[i].timer - 195, TempArray, 10);
strcat(smsBuffer, TempArray);
}
}
}
Serial.println(smsBuffer);
Serial.print("SMS Buffer size: ");
Serial.println(strlen(smsBuffer));
}
void ReadAM2302(char *buffer) {
char arr[19] = {};
char temp[6] = {};
char humid[5] = {};
switch (am2302.read()) {
case AM2302::AM2302_ERROR_READ_FREQ:
while (am2302.read() == AM2302::AM2302_ERROR_READ_FREQ) {
;
}
case AM2302::AM2302_READ_OK:
dtostrf(am2302.get_Temperature(), 4, 1, temp);
temp[strchr(temp, '.') - temp] = ',';
dtostrf(am2302.get_Humidity(), 4, 1, humid);
humid[strchr(humid, '.') - humid] = ',';
sprintf_P(buffer, PSTR("%s°C; %s%%"), temp, humid);
break;
case AM2302::AM2302_ERROR_TIMEOUT:
strcpy_P(buffer, PSTR("Timeout error."));
break;
case AM2302::AM2302_ERROR_CHECKSUM:
strcpy_P(buffer, PSTR("Checksum error."));
}
}
char* ReadAM2302pass() {
char buffer[18];
char arr[19] = {};
char temp[6] = {};
char humid[5] = {};
switch (am2302.read()) {
case AM2302::AM2302_ERROR_READ_FREQ:
while (am2302.read() == AM2302::AM2302_ERROR_READ_FREQ) {
;
}
case AM2302::AM2302_READ_OK:
dtostrf(am2302.get_Temperature(), 4, 1, temp);
temp[strchr(temp, '.') - temp] = ',';
dtostrf(am2302.get_Humidity(), 4, 1, humid);
humid[strchr(humid, '.') - humid] = ',';
sprintf_P(buffer, PSTR("%s°C; %s%%"), temp, humid);
break;
case AM2302::AM2302_ERROR_TIMEOUT:
strcpy_P(buffer, PSTR("Timeout error."));
break;
case AM2302::AM2302_ERROR_CHECKSUM:
strcpy_P(buffer, PSTR("Checksum error."));
}
return buffer;
}
void PrintText(int number, char input[] = {""}) {
// char input[25] = {};
char output[45] = {};
if (input[0] == '\0') {
Serial.println("Passed array is empty.");
} else {
strcpy(output, "Second sentence: ");
strcat(output, input);
Serial.println(output);
}
Serial.print("Passed number: ");
Serial.println(number);
}