/* Modul GPRS part of Arduino Mega Server project */ #ifdef GPRS_FEATURE #include <SoftwareSerial.h> #define ALWAYS 1 #define GPRS_ON_PIN 2 #define GPRS_STATE_PIN 3 #define GPRS_RX_PIN 62 #define GPRS_TX_PIN 63 #define MASTER "+7yyyxxxxxxx" #define MESSAGE "Hello" #define CHECK_ERROR 0 #define CHECK_MARKER 1 #define CHECK_OK 2 #define NO_CHECK 3 #define MARKER_OK "OK" #define MARKER_NO_CHECK "-" // GPRS commands #define DATA_TEMP1 "temp1" #define DATA_CONT1 "cont1" #define DATA_LEAK1 "leak1" #define DATA_SMOKE1 "smoke1" #define DATA_ALL "all" #define DATA_RELAY1 "relay1" #define DATA_SERVO1 "servo1" #define DATA_PERIOD "period" #define CMD_RELAY1 "relay1=" #define CMD_SERVO1 "servo1=" #define CMD_PERIOD "period=" byte gprsPeriod = 24; SoftwareSerial GPRS(GPRS_RX_PIN, GPRS_TX_PIN); #define MAX_SOFT_SERIAL 128 char bufferGprs[MAX_SOFT_SERIAL]; int curBuf = 0; #define MESSAGE_LENGTH 20 char message[MESSAGE_LENGTH]; char phone[16]; char datetime[24]; void gprsInit() { initStart("GPRS", true); GPRS.begin(9600); gprsOnOff(); gprsStart(); sendGprs("AT+CFUN=1", "OK"); sendGprs("AT+CNMI=2,1", "OK"); sendGprs("AT+CMGF=1", "OK"); sendGprs("AT+CLIP=1", "OK"); modulGprs = MODUL_ENABLE; initDone(true); } void clearBufferGprs() { for (int i = 0; i < curBuf; i++) { bufferGprs[i] = 0; } } void gprsStart() { while (ALWAYS) { delay(1000); curBuf = 0; GPRS.println("AT"); if (GPRS.available() > 0) { while (GPRS.available() > 0) { bufferGprs[curBuf++] = GPRS.read(); } bufferGprs[curBuf] = '\0'; if (strcmp(bufferGprs, "AT\r\n\r\nOK\r\n") == 0) { timeStamp(); Serial.println(" GPRS ON"); break; } else { Serial.print("."); } } clearBufferGprs(); } } // gprsStart() void gprsOnOff() { pinMode(GPRS_ON_PIN, OUTPUT); if (digitalRead(GPRS_STATE_PIN) != HIGH) { digitalWrite(GPRS_ON_PIN, HIGH); delay(3000); } digitalWrite(GPRS_ON_PIN, LOW); } bool clearSoftwareSerial() { if (GPRS.available() > 0) { while (GPRS.available() > 0) { char c = GPRS.read(); } } } #define DELAY_GPRS_COMMAND 2000 byte sendGprs(String command, char marker[]) { unsigned long timer = millis(); bool success = false; clearSoftwareSerial(); clearBufferGprs(); Serial.print(F("Send command: ")); //Serial.println(command); while (ALWAYS) { if (millis() - timer > DELAY_GPRS_COMMAND) { Serial.print(F("Send error: ")); Serial.println(command); //clearBufferGprs(); return CHECK_ERROR; } curBuf = 0; GPRS.println(command); delay(280); if (GPRS.available() > 0) { while (GPRS.available() > 0) { char c = GPRS.read(); if (curBuf > MAX_SOFT_SERIAL - 2) {break;} bufferGprs[curBuf++] = c; Serial.print(c); } Serial.println(); bufferGprs[curBuf] = '\0'; if (marker == MARKER_NO_CHECK) { Serial.print(F("Send no check, ")); Serial.println(millis() - timer); return NO_CHECK; } else if (StrContains(bufferGprs, marker)) { Serial.print(F("Send success, ")); Serial.println(millis() - timer); return CHECK_MARKER; } else if (StrContains(bufferGprs, MARKER_OK)) { Serial.print(F("Send success, ")); Serial.println(millis() - timer); return CHECK_OK; } else { Serial.println("."); } } // if (GPRS.available() > 0) delay(500); } // while (ALWAYS) } // sendGprs( ) // +CMGR: "REC READ", "XXXXXXXXXXX", "", "16/10/01,10:00:00+12" // SMS text void parseSms(char *message, char *phone, char *datetime) { int i = 0; int j = 0; while (bufferGprs[i] != '\"') {i++;} i++; while (bufferGprs[i] != '\"') {i++;} i++; while (bufferGprs[i] != '\"') {i++;} i++; while (bufferGprs[i] != '\"') {phone[j++] = bufferGprs[i++];} phone[j] = '\0'; i++; while (bufferGprs[i] != '\"') {i++;} i++; while (bufferGprs[i] != '\"') {i++;} i++; while (bufferGprs[i] != '\"') {i++;} i++; j = 0; while (bufferGprs[i] != '\"') {datetime[j++] = bufferGprs[i++];} datetime[j] = '\0'; i++; while (bufferGprs[i] != '\n') {i++;} i++; j = 0; //Serial.print(F("strlen(bufferGprs): ")); Serial.println(strlen(bufferGprs)); while (i < strlen(bufferGprs) - 1) { if (j > MESSAGE_LENGTH - 1) {break;} if ((byte)bufferGprs[i] == 13) {break;} message[j++] = bufferGprs[i++]; } Serial.print(F("strlen(message1): ")); Serial.println(strlen(message)); message[j] = '\0'; Serial.print(F("strlen(message2): ")); Serial.println(strlen(message)); for (int z = 0; z < strlen(message); z++) { Serial.print((byte)message[z]); Serial.print(F(" ")); } Serial.println(); } void deleteSms() { if (sendGprs("AT+CMGDA=\"DEL ALL\"", "OK")) { Serial.println(F("All SMS deleted")); } else { Serial.println(F("Error delete all SMS")); } } String stringSens(byte v) { String s = ""; switch (v) { case 0: s = (F("OFF")); break; case 1: s = (F("ON")); break; default: s = (F("?")); } return s; } String stringLeak(byte v) { String s = ""; switch (v) { case 0: s = (F("LEAK!")); break; case 1: s = (F("OK")); break; default: s = (F("?")); } return s; } String stringSmoke(byte v) { String s = ""; switch (v) { case 0: s = (F("OK")); break; case 1: s = (F("SMOKE!")); break; default: s = (F("?")); } return s; } String mkTemp1() {String s = DATA_TEMP1; s += '='; s += String(lpTempTemp); s += '\n'; return s;} String mkCont1() {String s = DATA_CONT1; s += '='; s += stringSens(lpContCont1); s += '\n'; return s;} String mkLeak1() {String s = DATA_LEAK1; s += '='; s += stringLeak(lpLeakLeak1); s += '\n'; return s;} String mkSmoke1() {String s = DATA_SMOKE1; s += '='; s += stringSmoke(smokeSmoke); s += '\n'; return s;} String mkRelay1() {String s = DATA_RELAY1; s += '='; s += stringSens(relayRelay); s += '\n'; return s;} String mkServo1() {String s = DATA_SERVO1; s += '='; s += stringSens(servoState); s += '\n'; return s;} String mkPeriod() {String s = DATA_PERIOD; s += '='; s += String(gprsPeriod); s += '\n'; return s;} String mkAll() { String s = ""; s += mkTemp1(); s += mkCont1(); s += mkLeak1(); s += mkSmoke1(); s += mkRelay1(); s += mkServo1(); s += mkPeriod(); return s; } void gprsSetRelay(byte v) { switch (v) { case 0: if (relayRelay) { relayRelay = STATE_OFF; } break; case 1: if (!relayRelay) { relayRelay = STATE_ON; } break; } } void gprsSetServo(byte v) { switch (v) { case 0: servoState = STATE_OFF; break; case 1: servoState = STATE_ON; break; } } void gprsAnswer() { String s = ""; String mess = String(message); String data = ""; if (mess == DATA_TEMP1) {s += mkTemp1();} else if (mess == DATA_CONT1) {s += mkCont1();} else if (mess == DATA_LEAK1) {s += mkLeak1();} else if (mess == DATA_SMOKE1) {s += mkSmoke1();} else if (mess == DATA_RELAY1) {s += mkRelay1();} else if (mess == DATA_SERVO1) {s += mkServo1();} else if (mess == DATA_PERIOD) {s += mkPeriod();} else if (mess == DATA_ALL) {s += mkAll();} else if (mess.indexOf(F("=")) >= 0) { byte p = mess.indexOf(F("=")); if (mess.indexOf(CMD_RELAY1) >= 0) {data = mess.substring(p + 1); gprsSetRelay(data.toInt()); s += DATA_RELAY1; s += '='; s += stringSens(relayRelay);} else if (mess.indexOf(CMD_SERVO1) >= 0) {data = mess.substring(p + 1); gprsSetServo(data.toInt()); s += DATA_SERVO1; s += '='; s += stringSens(servoState);} else if (mess.indexOf(CMD_PERIOD) >= 0) {data = mess.substring(p + 1); gprsPeriod = data.toInt(); s += DATA_PERIOD; s += '='; s += String(gprsPeriod);} } else { Serial.println(F("Not command!")); } //Serial.print(F("mess: ")); Serial.println(mess); //Serial.print(F("answ: ")); Serial.println(s); if (s == "") {s = "Error";} Serial.println(F("Send answer... ")); if (sendSms(MASTER, s)) { Serial.println(F("success")); } else { Serial.println(F("error")); } } void readSms() { byte result = sendGprs("AT+CMGR=1,1", "+CMGR:"); if (result == CHECK_MARKER) { parseSms(message, phone, datetime); Serial.print(F("Number: ")); Serial.println(phone); Serial.print(F("Datetime: ")); Serial.println(datetime); Serial.print(F("Message: ")); Serial.println(message); deleteSms(); if (String(phone) == String(MASTER)) { Serial.println(F("Message from MASTER!")); gprsAnswer(); } } else if (result == CHECK_OK) { Serial.println(F("No SMS")); } else { Serial.println(F("Error read SMS")); } } bool sendSms(char *number, String data) { String numstr = "AT+CMGS=\"" + String(number) + "\""; String messtr = data + String((char)26); if (sendGprs(numstr, ">")) { if (sendGprs(messtr, MARKER_NO_CHECK)) { return true; } } return false; } void sendPeriod() { Serial.println(F("Send period SMS... ")); if (sendSms(MASTER, mkAll())) { Serial.println(F("success")); } else { Serial.println(F("error")); } } void gprsWorks() { if (cycle20s) { readSms(); } switch (gprsPeriod) { case 1: if (cycle1h) {sendPeriod();} break; case 6: if (cycle6h) {sendPeriod();} break; case 12: if (cycle12h) {sendPeriod();} break; default: if (cycle24h) {sendPeriod();} break; } } #endif // GPRS_FEATURE
#define GPRS_FEATURE
byte modulGprs = MODUL_NOT_COMPILLED;
float lpTempTemp; byte lpContCont1; byte lpLeakLeak1; byte smokeSmoke; byte relayRelay; byte servoState;
temp1=20.50
.temp1 - temperature request
cont1 - request for contact status
leak1 - request for leakage sensor status
smoke1 - request for smoke sensor status
relay1 - request for relay status (key)
servo1 - request servo servo status
period - request for the period of automatic telemetry parcels
all - request of all system parameters
relay1 = - relay control command (key)
servo1 = - servo drive control command
period = - command to change the telemetry auto-posting period
relay1=1
, response relay1=ON
. Command relay1=0
, response relay1=OFF
. void gprsInit() { initStart("GPRS", true); GPRS.begin(9600); gprsOnOff(); gprsStart(); sendGprs("AT+CFUN=1", "OK"); sendGprs("AT+CNMI=2,1", "OK"); sendGprs("AT+CMGF=1", "OK"); sendGprs("AT+CLIP=1", "OK"); modulGprs = MODUL_ENABLE; initDone(true); }
void gprsWorks() { if (cycle20s) { readSms(); } switch (gprsPeriod) { case 1: if (cycle1h) {sendPeriod();} break; case 6: if (cycle6h) {sendPeriod();} break; case 12: if (cycle12h) {sendPeriod();} break; default: if (cycle24h) {sendPeriod();} break; } }
bool sendSms(char *number, String data) { String numstr = "AT+CMGS=\"" + String(number) + "\""; String messtr = data + String((char)26); if (sendGprs(numstr, ">")) { if (sendGprs(messtr, MARKER_NO_CHECK)) { return true; } } return false; }
void readSms() { byte result = sendGprs("AT+CMGR=1,1", "+CMGR:"); if (result == CHECK_MARKER) { parseSms(message, phone, datetime); Serial.print(F("Number: ")); Serial.println(phone); Serial.print(F("Datetime: ")); Serial.println(datetime); Serial.print(F("Message: ")); Serial.println(message); deleteSms(); if (String(phone) == String(MASTER)) { Serial.println(F("Message from MASTER!")); gprsAnswer(); } } else if (result == CHECK_OK) { Serial.println(F("No SMS")); } else { Serial.println(F("Error read SMS")); } }
void gprsAnswer() { String s = ""; String mess = String(message); String data = ""; if (mess == DATA_TEMP1) {s += mkTemp1();} else if (mess == DATA_CONT1) {s += mkCont1();} else if (mess == DATA_LEAK1) {s += mkLeak1();} else if (mess == DATA_SMOKE1) {s += mkSmoke1();} else if (mess == DATA_RELAY1) {s += mkRelay1();} else if (mess == DATA_SERVO1) {s += mkServo1();} else if (mess == DATA_PERIOD) {s += mkPeriod();} else if (mess == DATA_ALL) {s += mkAll();} else if (mess.indexOf(F("=")) >= 0) { byte p = mess.indexOf(F("=")); if (mess.indexOf(CMD_RELAY1) >= 0) {data = mess.substring(p + 1); gprsSetRelay(data.toInt()); s += DATA_RELAY1; s += '='; s += stringSens(relayRelay);} else if (mess.indexOf(CMD_SERVO1) >= 0) {data = mess.substring(p + 1); gprsSetServo(data.toInt()); s += DATA_SERVO1; s += '='; s += stringSens(servoState);} else if (mess.indexOf(CMD_PERIOD) >= 0) {data = mess.substring(p + 1); gprsPeriod = data.toInt(); s += DATA_PERIOD; s += '='; s += String(gprsPeriod);} } else { Serial.println(F("Not command!")); } //Serial.print(F("mess: ")); Serial.println(mess); //Serial.print(F("answ: ")); Serial.println(s); if (s == "") {s = "Error";} Serial.println(F("Send answer... ")); if (sendSms(MASTER, s)) { Serial.println(F("success")); } else { Serial.println(F("error")); } }
String stringSens(byte v) String stringLeak(byte v) String stringSmoke(byte v) String mkTemp1() String mkCont1() String mkLeak1() String mkSmoke1() String mkRelay1() String mkServo1() String mkPeriod() String mkAll()
void gprsSetRelay(byte v) void gprsSetServo(byte v)
SoftwareSerial.h
. Row #define _SS_MAX_RX_BUFF 64 // RX buffer size
#define _SS_MAX_RX_BUFF 128 // RX buffer size
Source: https://habr.com/ru/post/401721/
All Articles