watchdog_3-leonardo/watchdog_3-leonardo.ino
2020-01-24 22:32:44 +05:00

370 lines
8.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ----------------------------------------------------------------------------------------------------
// Watchdog v3 - Leonardo
// EoF 2017
// ----------------------------------------------------------------------------------------------------
#include <EEPROM.h>
#include "GPRS_Shield_Arduino.h"
// Консоль
#define TIMEOUT 60000
#define CONSOLE_BUFFER_SIZE 64
// Сенсоры
#define T_PIN A1
#define A_PIN A0
// GSM-модем
#define MOD_PWR 9
#define MOD_BR 115200
#define COM_BR 115200
#define SMS_LEN 160
#define PHONE1 "+79123801806"
//#define PHONE2 "79129295702"
//#define PHONE3 "79058201819"
// Размеры буферов;
#define CMD_SIZE 16
#define SMS_SIZE 160
#define PHONE_SIZE 16
#define DATE_SIZE 24
#define BUFFER_SIZE 30
#define MAX_INDEX 9
// Help
#define HELP_MSG "Usage: |status|reset modem|help|sms on|sms off|"
GPRS modem(Serial1, MOD_PWR);
bool modemOK = false;
bool smsOn = false;
char command[CMD_SIZE];
char message[SMS_SIZE];
char phone[PHONE_SIZE];
char datetime[DATE_SIZE];
bool warning = false;
bool alarm = false;
byte counter = 0;
byte temp = 0, hum = 0, loud = 0, externalPower = 0, powerOn = 0, onBattery = 0, caseOpen = 0;
// ----------------------------------------------------------------------------------------------------
// setup
// ----------------------------------------------------------------------------------------------------
void setup()
{
Serial.begin(COM_BR);
Serial1.begin(MOD_BR);
modem.powerOn();
if (modem.init()){
Serial.println("Modem OK");
modemOK = true;
} else {
Serial.println("Modem DEAD");
modemOK = false;
}
}
// ----------------------------------------------------------------------------------------------------
// loop
// ----------------------------------------------------------------------------------------------------
void loop()
{
// counter++;
// if (counter == 20) {
// getStatus();
// counter == 0;
// }
//
// if (!statusOK()) {
//
// }
readSerial(command, CMD_SIZE);
if (command[0] == '\0') readModem(command, CMD_SIZE);
// command = com;
// } else if (mod[0] != '\0') { //&& (!strcmp(sender, PHONE1))) { // || !strcmp(sender, PHONE2) || !strcmp(sender, PHONE3))) {
// command = s,;
// } else command[0] = '\0';
if (command[0] != '\0') {
Serial.print("Received command: ");
Serial.println(command);
}
if (!strcmp(command, "stat")) {
// getStatusMessage(statMsg);
// sendSMS(statMsg);
// Serial.println(statMsg);
} else if (!strcmp(command, "help")) {
// sendSMS(HELP_MSG);
Serial.println(HELP_MSG);
} else if (!strcmp(command, "sms on")) {
smsOn = true;
Serial.println("Outgoing SMS enabled");
// Serial.println("SMS on");
} else if (!strcmp(command, "sms off")) {
smsOn = false;
Serial.println("Outgoing SMS disabled");
// Serial.println("SMS off");
} else if (!strcmp(command, "on")) {
mainPowerOn();
} else if (!strcmp(command, "off")) {
mainPowerOff();
} else if (!strcmp(command, "rst")) {
mainReset();
} else if (!strcmp(command, "a")) {
a_PowerBreak();
} else if (!strcmp(command, "b")) {
b_PowerBreak();
} else if (!strcmp(command, "modem on")) {
modem.powerOn();
} else if (!strcmp(command, "modem off")) {
modem.powerOff();
} else if (!strcmp(command, "modem reset")) {
modem.reset();
} else if (!strcmp(command, "modem console")) {
modemConsole();
} else if (!strcmp(command, "pp")) {
printAddressBookFromEEPROM();
} else if (!strcmp(command, "edit ab")) {
editAddressBook();
} else if (!strcmp(command, "show ab")) {
printAddressBookFromEEPROM();
} else if (!strcmp(command, "clear ab")) {
clearAddressBookFromEEPROM();
}
}
// Консоль модема
void modemConsole() {
unsigned long t = millis();
unsigned char buffer[CONSOLE_BUFFER_SIZE];
int count = 0;
Serial.println("Modem console mode on");
Serial.print("Timeout is ");
Serial.print(TIMEOUT);
Serial.println(" ms after last command");
while (millis() - t < TIMEOUT) {
if (Serial1.available()) {
while(Serial1.available()) {
buffer[count++] = Serial1.read();
if(count == CONSOLE_BUFFER_SIZE) break;
}
Serial.write(buffer, count);
count = 0;
}
if (Serial.available()) {
Serial1.write(Serial.read());
t = millis();
}
}
Serial.println("Modem console mode off");
}
// Читаем команды с GSM-модема
void readModem(char* msg, byte size) {
msg[0] = '\0';
message[0] = '\0';
if (!modemOK) return;
if (modem.getSMS(message, phone, datetime)) {
// Serial.print("Msg: ");
// Serial.println(message);
// Serial.print("Phn: ");
// Serial.println(phone);
// Serial.print("Dtm: ");
// Serial.println(datetime);
for (byte i = 0; i < size; i++)
msg[i] = message[i];
}
}
// Читаем команды с COM-порта
void readSerial(char* com, byte size) {
com[0] = '\0';
com[size] = '\0';
byte i = 0;
while (Serial.available() > 0 && i < size - 1) {
com[i] = Serial.read();
if (com[i] == '\r') {
com[i] = '\0';
} else if (com[i] == '\n') {
com[i] = '\0';
break;
}
i++;
}
}
// Управление подключенным оборудованием
void mainPowerOn() {
if (powerOn != 1) {
modem.controlGPIO(1, 1);
delay(500);
modem.controlGPIO(1, 0);
}
// !!!
powerOn = 1;
}
void mainPowerOff() {
modem.controlGPIO(1, 1);
delay(5000);
modem.controlGPIO(1, 0);
// !!!
powerOn = 0;
}
void mainReset() {
modem.controlGPIO(2, 1);
delay(500);
modem.controlGPIO(2, 0);
}
void a_PowerBreak() {
modem.controlGPIO(3, 1);
delay(5000);
modem.controlGPIO(3, 0);
}
void b_PowerBreak() {
modem.controlGPIO(4, 1);
delay(5000);
modem.controlGPIO(4, 0);
}
// Адресная книга в EEPROM
void savePhoneNumber (int index, char * phone) {
int addr = index * PHONE_SIZE;
for (byte i = 0; i < PHONE_SIZE; i++) {
EEPROM[addr + i] = phone[i];
}
}
void delPhoneNumber (int index) {
char none[PHONE_SIZE];
clearBuffer(none, PHONE_SIZE);
strcpy(none, "<NONE>");
savePhoneNumber(index, none);
}
bool loadPhoneNumber (int index, char * phone) {
int addr = index * PHONE_SIZE;
for (byte i = 0; i < PHONE_SIZE - 1; i++) {
phone[i] = EEPROM[addr + i];
}
phone[PHONE_SIZE] = '\0';
return true;
}
void printPhoneFromEEPROM(byte index) {
char phone[PHONE_SIZE];
int addr = index * PHONE_SIZE;
if (index > MAX_INDEX) {
Serial.print("Max index is: ");
Serial.println(MAX_INDEX);
return;
}
loadPhoneNumber(index, phone);
Serial.print(index);
Serial.print(": ");
Serial.println(phone);
}
void printAddressBookFromEEPROM() {
Serial.println("i Phone");
for (byte i = 0; i <= MAX_INDEX; i++) {
printPhoneFromEEPROM(i);
}
}
void clearAddressBookFromEEPROM() {
for (byte i = 0; i <= MAX_INDEX; i++) {
delPhoneNumber(i);
}
}
void editAddressBook() {
unsigned long t = millis();
char buffer[BUFFER_SIZE];
buffer[0] = '\0';
char *p;
char indexString[3], phone[PHONE_SIZE];
int index;
byte i = 0;
Serial.println("Edit address book mode");
Serial.println("format: \"i:+xxxxxxxxxxx\"");
Serial.println("Type \"exit\" to return");
//Serial.println("Type \"show\" to view, \"clear\" to erase, \"exit\" to return");
while (millis() - t < TIMEOUT) {
readSerial(buffer, BUFFER_SIZE);
if (buffer[0] != '\0') {
t = millis();
if (!strcmp(buffer, "exit")) {
break;
} else {
// } else if (!strcmp(buffer, "show")) {
// printAddressBookFromEEPROM();
// } else if (!strcmp(buffer, "clear")) {
// clearAddressBookFromEEPROM();
// } else {
p = strstr(buffer, ":+");
i = 0;
while(buffer[i] != ':' && i < BUFFER_SIZE) i++;
if (p == NULL || i == 0 || i > 2) {
Serial.println("ERR: Unknown format!");
}
else {
// Get index
clearBuffer(indexString, 3);
strncpy(indexString, buffer, i);
index = atoi(indexString);
// Get phone
i = 0;
p = strstr(buffer, "+");
strcpy(phone, p);
// Save
savePhoneNumber(index, phone);
Serial.print(index);
Serial.print(": ");
Serial.print(phone);
Serial.println(" added");
}
}
}
}
Serial.println("Bye!");
}
void clearBuffer(char * buffer, byte size) {
for(byte i = 0; i < size; i++) buffer[i] = '\0';
}