SHARE
TWEET

Untitled

a guest Oct 20th, 2017 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <SoftwareSerial.h>
  3. #include "cuGSM_basic_lbr.h"
  4. #include "cuGSM_SMS_lbr.h"
  5. #include "DHT.h"
  6. #define dhtTYPE DHT11
  7. #define UNO_MODE                /** USED BY MODEM LIBRARY **/
  8. #define next_it {delay(5000); continue;}
  9.  
  10. /*********************************  CONSTANTS VARIABLES   *********************************/
  11. const int pumpPIN = 9;                        /** DEFINE PUMP WATER **/
  12. const int dhtPIN = 8;                           /** DEFINE DHT11 AIR SENSOR **/
  13. const int powerPIN = 7;                         /** PIN USED TO ON/OFF GSM MODEM **/
  14. const int resetPIN = 6;                         /** PIN USED TO RESET GSM MODEM **/
  15. const int statusPIN = 5;                        /** PIN USED TO ON/OFF GSM MODEM **/
  16. const int relayPIN = 4;                         /** PIN USED TO ON/OFF RELAY **/
  17. const int soilPIN = A2;                     /** DEFINE SOIL SENSOR **/
  18. const int lightPIN = A1;                      /** DEFINE LIGHT SENSOR **/
  19. const int swimmerPIN = A0;                    /** DEFINE SWOMMER SENSOR **/
  20.  
  21. const int BUFFDSIZE = 160;                      /** USED BY MODEM LIBRARY **/
  22. const int ORDERS_COUNT = 5;                     /** AMOUNT OF COMMANDS **/
  23. const int SENSORS_COUNT = 5;          /** AMOUNT OF SENSORS **/
  24.  
  25. const int smsInterval = 30000;                  /** INTERVAL TIME TO CHECK MESSAGES **/
  26. const int intervalCheck = 30000;                /** ... **/
  27. int i_sensorValue = 0;
  28. float fl_sensorValue = 0;
  29. char ch_sensorValue[] = {0};
  30.  
  31. /*********************************   LIBRARY VARIABLES   *********************************/
  32. char buffd[BUFFDSIZE];
  33. char readBuffer[160];
  34. char tmpChar[20];
  35. char secvar[40];
  36. char ch;
  37. int res = 0;
  38. int i = 0;
  39. int powerState = 0;
  40. int state = 0;
  41.  
  42. /*********************************   GLOBAL VARIABLES   *********************************/
  43. bool relayPOWER = false;                        /** DEFINE RELAY STATE **/
  44. bool pumpPOWER = false;                       /** DEFINE PUMP WATER STATE **/
  45. bool registerSim = false;               /** IF TRUE SIM CARD REGISTER SUCCESS **/
  46. int timeToCheckSMS = 0;                         /** PRIMARY VALUE TO CHECK SMS **/
  47. int timeToMeasure = 0;                          /** PRIMARY VALUE TO EXECUTE MEASURES **/
  48.  
  49. char* testNumber = {0};             /** PHONE NUMBER TO AUTHORIZATION **/
  50. static char batteryLevel[5];                    /** BATTERY LEVEL - VALUE IN mV **/
  51. static char phoneNumber[11] = "48537037054";    /** ADMIN PHONE NUMBER **/
  52.  
  53. static char *SMS_ORDERS[] = {"BATTERY", "MEASURE", "PUMP", "USSD", "RESTART"};                              /** SMS ORDER **/
  54. static char *SMS_ORDERS_VALUES[] = {"TEMP", "HUMI", "LIGHT", "SOIL", "SWIMMER"};                    /** SMS ORDER VALUE **/
  55. static char *MEASURE_NAME[]= {"TEMPERATURE (C): ", "HUMIDITY (%): ", "LIGHT (%): ", "SOIL (%): ", "SWIMMER (%): "};   /** RESPONSE SMS MEASURE **/
  56.  
  57. SoftwareSerial agsmSerial(2, 3);        /** GSM RX => 2 ,GSM TX => 3 **/
  58. DHT dht(dhtPIN, dhtTYPE);
  59.  
  60. /*********************************   GLOBAL FUNCTIONS   *********************************/
  61. void setupDevice(){
  62.   pinMode(resetPIN, OUTPUT);
  63.   digitalWrite(resetPIN, HIGH);
  64.   pinMode(powerPIN, OUTPUT);
  65.   digitalWrite(powerPIN, HIGH);
  66.   pinMode(statusPIN, INPUT);
  67.   pinMode(relayPIN, OUTPUT);
  68.   digitalWrite(relayPIN, relayPOWER);
  69.   pinMode(pumpPIN, OUTPUT);
  70.   digitalWrite(pumpPIN, pumpPOWER);
  71.   delay(100);  
  72. }
  73.  
  74. char* getBatteryLevel(){
  75.   fATcmd(F("+CBC"));
  76.   parseResponce("OK", "+CBC:", batteryLevel, ",", 2);
  77.   return batteryLevel;
  78. }
  79.  
  80. int getSmsOrder(char order[]) {
  81.   for (int i = 0; i < ORDERS_COUNT; i++) {
  82.     if (strcmp(SMS_ORDERS[i], order) == 0)
  83.       return i;
  84.   }
  85.   return -1;
  86. }
  87.  
  88. int getSmsOrderValue(char order[]) {
  89.   for (int i = 0; i < ORDERS_COUNT; i++) {
  90.     if (strcmp(SMS_ORDERS_VALUES[i], order) == 0)
  91.       return i;
  92.   }
  93.   return -1;
  94. }
  95.  
  96. void relayChangeState() {
  97.   relayPOWER=!(relayPOWER);
  98.   digitalWrite(relayPIN, relayPOWER);
  99. }
  100.  
  101. void pumpChangeState(){
  102.   pumpPOWER=!(pumpPOWER);
  103.   digitalWrite(pumpPIN, pumpPOWER);
  104. }
  105.  
  106. bool authNumber(int l){
  107.   char delimiter[] = ",";
  108.   char tmpIndex[] = {0};
  109.   char* cutBuffd = {0};
  110.   *tmpChar = {0};
  111.  
  112.   strcpy(tmpChar, "+CMGR=");
  113.   sprintf(tmpIndex,"%d", l);
  114.   strcat(tmpChar, tmpIndex);    
  115.   sendATcommand(tmpChar, "OK", "+CMS ERROR:", 1);
  116.   cutBuffd = strtok(buffd, delimiter);
  117.   testNumber = strtok(NULL, delimiter);
  118.   for(int k = 2; k < strlen(testNumber) - 1; k++){
  119.     Serial.println("leci for");
  120.     if(strcmp(testNumber[k], phoneNumber[k-2]) != 0){
  121.        Serial.println("leci if");
  122.       clearBUFFD();
  123.       clearagsmSerial();
  124.       return false;
  125.     }
  126.    
  127.   }
  128.   clearBUFFD();
  129.   clearagsmSerial();
  130.   Serial.flush();
  131.   agsmSerial.flush();
  132.   return true;
  133. }
  134.  
  135. void setup(){
  136.   /**********************   PREPARE DEVICE TO WORK   **********************/
  137.   Serial.begin(57600);            /** START SERIAL CONNECTION **/
  138.   agsmSerial.begin(9600);           /** NEXT, MODEM SETUP **/
  139.   clearagsmSerial();
  140.   clearSerial();
  141.   Serial.flush();
  142.   agsmSerial.flush();
  143.  
  144.   /**********************  SET UP GSM MODEM  ******************************/
  145.   setupDevice();                /** CONFIGURE UNO I/O TO BE USED WITH MODEM **/
  146.   delay(500);
  147.   powerOnModem();                /** POWER ON MODEM **/
  148.   clearBUFFD();
  149.   while (!registerSim) {
  150.     Serial.println(F("CHECKING SIM CARD STATUS..."));       /** CHECK REGISTER IN NETWORK **/
  151.     res = fATcmd(F("+CREG?"));
  152.     res = parseResponce("OK", "+CREG:", tmpChar, ",", 1);
  153.     res = atoi(tmpChar);
  154.     if (res == 0) {
  155.       fATcmd(F("+CPIN=5024"), 20, "OK", "+CMS ERROR:");
  156.       delay(3000);
  157.     }
  158.     registerSim = true;
  159.   }
  160.   /** SEND MESSAGE TO ADMIN **/
  161.   //sendSMS(phoneNumber, "SIM CARD REGISTER SUCCESS. AHOJ DEVICE IS READY.");
  162.   Serial.println("[STS] - SIM CARD IS REGISTERED.");
  163. }
  164.  
  165. int doSMSOrders(int counterSMS){
  166.   int accIndex = 1;            /** ACTUAL INDEX OF SMS **/
  167.   char delimiters[] = ":";
  168.   char* order_type = {0};
  169.   char* order_value = {0};
  170.   char sms[20];
  171.   char smsResponce[] = {0};
  172.  
  173.   Serial.print("[STS]: SMS NUMBER IN QUEUE: ");             /** WHILE MESSAGES ARE IN QUEUE **/
  174.   Serial.println(counterSMS);
  175.   Serial.println("---- START READING ORDERS ----");
  176.   Serial.flush();
  177.   clearagsmSerial();
  178.   while(accIndex < counterSMS+1){
  179.     order_type = strtok(buffd, delimiters);
  180.     order_value = strtok(NULL, delimiters);
  181.     Serial.print("[STS]: READING SMS: ");
  182.     Serial.println(accIndex);
  183.     Serial.flush();
  184.     readSMS(accIndex);
  185.     if(authNumber(accIndex) != false){
  186.       Serial.println("[RES]: SENDER: ADMINISTRATOR");
  187.       accIndex++;
  188.     }
  189.     else{
  190.       Serial.println("[RES]: SENDER: UNKOWN");
  191.       accIndex++;
  192.     }
  193.   }
  194.   fATcmd(F("+CMGD=1,4"), 20, "OK", "+CMS ERROR:");
  195.   Serial.println("--- END CHECK LOOP --- ");
  196. }
  197.  
  198. void loop(){
  199.   Serial.println("\n--- CHECK MESSAGE LOOP ---");
  200.   if((timeToCheckSMS <= 0) && (registerSim == true)){
  201.     Serial.print("[STS] - CHECKING MESSAGES...");
  202.     noSMS = 0;
  203.     listSMS();
  204.     if(noSMS > 0){
  205.       Serial.print("\n[STS] - WE HAVE SOME SMS...: ");
  206.       Serial.println(noSMS);
  207.       doSMSOrders(noSMS);
  208.     }
  209.     if(noSMS <= 0)
  210.       Serial.print("\n[STS] - MESSAGE BOX IS EMPTY.\n");
  211.     Serial.flush();
  212.     timeToCheckSMS = smsInterval;
  213.     Serial.println("--- WAIT FOR NEXT CHECK ---");
  214.     delay(15000);                                                                 /** INTERVAL CHECK **/
  215.     timeToCheckSMS = timeToCheckSMS - intervalCheck;
  216.     timeToMeasure = timeToMeasure - intervalCheck;
  217.   }
  218. }
RAW Paste Data
Top