/* FILE: main.cpp AUTHOR: David Kuhn PURPOSE: Rain detector with call/alarm function */ #define TINY_GSM_MODEM_SIM7600 //The AT instruction of A7670 is compatible with SIM7600 #define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb #define SerialAT Serial1 // See all AT commands #define DUMP_AT_COMMANDS // Set GPRS const char apn[] = "internet"; //SET TO YOUR APN const char gprsUser[] = ""; const char gprsPass[] = ""; // set GSM PIN #define GSM_PIN "xxxx" // Set phone number #define CALL_TARGET "+41xxxxxxxxx" #define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds #define TIME_TO_SLEEP 600 // Time ESP32 will go to sleep (in seconds) #define UART_BAUD 115200 #define PIN_DTR 25 #define PIN_TX 26 #define PIN_RX 27 #define PWR_PIN 4 #define BAT_ADC 35 #define BAT_EN 12 #define PIN_RI 33 #define RESET 5 #define SD_MISO 2 #define SD_MOSI 15 #define SD_SCLK 14 #define SD_CS 13 #include #include "Arduino.h" #ifdef DUMP_AT_COMMANDS // if enabled it requires the streamDebugger lib #include StreamDebugger debugger(SerialAT, Serial); TinyGsm modem(debugger); #else TinyGsm modem(SerialAT); #endif int rainSensorPin = 36; int value; int counter, lastIndex, numberOfPieces = 24; String pieces[24], input; bool reply = false; void modem_on() { pinMode(BAT_EN, OUTPUT); digitalWrite(BAT_EN, HIGH); //A7670 Reset pinMode(RESET, OUTPUT); digitalWrite(RESET, LOW); delay(100); digitalWrite(RESET, HIGH); delay(3000); digitalWrite(RESET, LOW); pinMode(PWR_PIN, OUTPUT); digitalWrite(PWR_PIN, LOW); delay(100); digitalWrite(PWR_PIN, HIGH); delay(1000); digitalWrite(PWR_PIN, LOW); int i = 10; Serial.println("\nTesting Modem Response...\n"); Serial.println("****"); while (i) { SerialAT.println("AT"); delay(500); if (SerialAT.available()) { String r = SerialAT.readString(); Serial.println(r); if ( r.indexOf("OK") >= 0 ) { reply = true; break;; } } delay(500); i--; } Serial.println("****\n"); } void setup() { Serial.begin(115200); // Set console baud rate SerialAT.begin(115200, SERIAL_8N1, PIN_RX, PIN_TX); delay(100); modem_on(); if (reply) { Serial.println(F("***********************************************************")); Serial.println(F(" You can now send AT commands")); Serial.println(F(" Enter \"AT\" (without quotes), and you should see \"OK\"")); Serial.println(F(" If it doesn't work, select \"Both NL & CR\" in Serial Monitor")); Serial.println(F(" DISCLAIMER: Entering AT commands without knowing what they do")); Serial.println(F(" can have undesired consiquinces...")); Serial.println(F("***********************************************************\n")); } else { Serial.println(F("***********************************************************")); Serial.println(F(" Failed to connect to the modem! Check the baud and try again.")); Serial.println(F("***********************************************************\n")); } // Unlock your SIM card with a PIN if needed if ( GSM_PIN && modem.getSimStatus() != 3 ) { modem.simUnlock(GSM_PIN); } modem.sendAT("+CFUN=0 "); if (modem.waitResponse(10000L) != 1) { DBG(" +CFUN=0 false "); } delay(200); /* 2 Automatic 13 GSM only 38 LTE only 51 GSM and LTE only * * * */ String res; // CHANGE NETWORK MODE, IF NEEDED res = modem.setNetworkMode(2); if (res != "1") { DBG("setNetworkMode false "); return ; } delay(200); modem.sendAT("+CFUN=1 "); if (modem.waitResponse(10000L) != 1) { DBG(" +CFUN=1 false "); } delay(200); SerialAT.println("AT+CGDCONT?"); delay(500); if (SerialAT.available()) { input = SerialAT.readString(); for (int i = 0; i < input.length(); i++) { if (input.substring(i, i + 1) == "\n") { pieces[counter] = input.substring(lastIndex, i); lastIndex = i + 1; counter++; } if (i == input.length() - 1) { pieces[counter] = input.substring(lastIndex, i); } } // Reset for reuse input = ""; counter = 0; lastIndex = 0; for ( int y = 0; y < numberOfPieces; y++) { for ( int x = 0; x < pieces[y].length(); x++) { char c = pieces[y][x]; //gets one byte from buffer if (c == ',') { if (input.indexOf(": ") >= 0) { String data = input.substring((input.indexOf(": ") + 1)); if ( data.toInt() > 0 && data.toInt() < 25) { modem.sendAT("+CGDCONT=" + String(data.toInt()) + ",\"IP\",\"" + String(apn) + "\",\"0.0.0.0\",0,0,0,0"); } input = ""; break; } // Reset for reuse input = ""; } else { input += c; } } } } else { Serial.println("Failed to get PDP!"); } Serial.println("\n\n\nWaiting for network..."); if (!modem.waitForNetwork()) { delay(10000); return; } if (modem.isNetworkConnected()) { Serial.println("Network connected"); } } void loop() { value = analogRead(36); Serial.println(value); if (value < 3000) { Serial.println("Making a call..."); modem.callNumber(CALL_TARGET); delay(60000); modem.callHangup(); } delay(5000); }