You’re right Nick
I have commented some stuff out which seem not to be necessary for normal operation; this code was a test that I have found.
I have found bits and pieces and was able to get something together so in the meantime, the sending works for both open and closed GPIO.
This will be used to get the status of dumb smoke detectors and the idea was to primarily, receive a SMS.
I also thought of linking this to HA but I have the impression now that I might have been a little to hasty when I ordered this board.
Next is to send to multiple numbers, but this should not be a problem by defining SMS_TARGET1 and SMS_TARGET2 and use those, I guess.
Of course the normal state should not be sent but only after being restored, so that has to change somehow.
#define TINY_GSM_MODEM_SIM7000
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#define SerialAT Serial1
#define BUTTON_PIN 32
// Your GPRS credentials, if any
const char apn[] = ""; //SET TO YOUR APN
const char gprsUser[] = "";
const char gprsPass[] = "";
// Set phone number, if you want to test SMS
// Set a recipient phone number to test sending SMS (it must be in international format including the "+" sign)
#define SMS_TARGET ""
#include <TinyGsmClient.h>
#include <SPI.h>
#include <SD.h>
#include <Ticker.h>
#ifdef DUMP_AT_COMMANDS // if enabled it requires the streamDebugger lib
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, Serial);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 60 // Time ESP32 will go to sleep (in seconds)
#define UART_BAUD 115200
#define PIN_DTR 25
#define PIN_TX 27
#define PIN_RX 26
#define PWR_PIN 4
#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 13
#define LED_PIN 12
int counter, lastIndex, numberOfPieces = 24;
String pieces[24], input;
void setup(){
// Set console baud rate
Serial.begin(115200);
delay(10);
pinMode(BUTTON_PIN, INPUT); //configures pin as an input
// Set LED OFF
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, HIGH);
delay(300);
digitalWrite(PWR_PIN, LOW);
SPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
if (!SD.begin(SD_CS)) {
Serial.println("SDCard MOUNT FAIL");
} else {
uint32_t cardSize = SD.cardSize() / (1024 * 1024);
String str = "SDCard Size: " + String(cardSize) + "MB";
Serial.println(str);
}
Serial.println("\nWait...");
delay(1000);
SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
if (!modem.restart()) {
Serial.println("Failed to restart modem, attempting to continue without restarting");
}
}
void loop(){
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
if (!modem.init()) {
Serial.println("Failed to restart modem, attempting to continue without restarting");
}
// String name = modem.getModemName();
// delay(500);
// Serial.println("Modem Name: " + name);
// String modemInfo = modem.getModemInfo();
// delay(500);
// Serial.println("Modem Info: " + modemInfo);
// // 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);
String res;
// CHANGE NETWORK MODE, IF NEEDED
res = modem.setNetworkMode(2);
if (res != "1") {
DBG("setNetworkMode false ");
return ;
}
delay(200);
// CHANGE PREFERRED MODE, IF NEEDED
res = modem.setPreferredMode(1);
if (res != "1") {
DBG("setPreferredMode false ");
return ;
}
delay(200);
// 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");
// }
if (analogRead(BUTTON_PIN) == 0) {
Serial.println("Contact Closed!");
res = modem.sendSMS(SMS_TARGET, String("Test: Contact Closed"));
DBG("SMS:", res ? "OK" : "fail");
}
else {
Serial.println("Contact Open!");
res = modem.sendSMS(SMS_TARGET, String("Test: Contact Open"));
DBG("SMS:", res ? "OK" : "fail");
}
delay(10000);
// // Do nothing forevermore
// while (true) {
// modem.maintain();
// }
}