Send SMS with SIM7000G when signal on pins?

Can anyone please point me in some direction (guide/video/…) on how to create a sketch for receiving an SMS when there is a signal on some pins?
The signal has no current so, it’s just like a switch.

I just started with this board and even though I have managed to configure a relay with ESPHome (which was ready bought), this is my first experience of programming an ESP device.
I took a test program which works, I’m able to send an SMS upon uploading the code. (it was using a loop which I’ve managed to comment out :star_struck:)

I have seen the SIM800 component and all the topics here but that didn’t bring much.
So any help is highly appreciated.
TIA

I don’t see an esphome component for the sim7000g. Are you aware of one that is not in the official esphome docs?

Hi Nick, of course I did a search for this model first.
Since it got it working, now I only need to find out how to program it so that a SMS is sent upon a signal on a GPIO

I also wonder if it would be possible to output that signal again over another GPIO towards HA.

Are you using the SIM800L component or something else? Here is what I use if it is the same component. This sends an SMS to two different numbers from one button press.

#Smoke alarm pin
  - platform: gpio
    device_class: smoke
    pin:
      number: 33
      inverted: True
      mode:
        input: true
        pullup: true
    name: SmokeAlarmTrigger
    id: alarm_smoke
    icon: "mdi:smoke-detector"
    on_press:
      then:
        - sim800l.send_sms:
           recipient: '123456789'
           message:  !lambda 'return "SMOKE ALARM ACTIVATED at " + id(hatime).now().strftime("%H:%M");'
        - delay: 5s
        - sim800l.send_sms:
           recipient: '987654321'
           message:  !lambda 'return "SMOKE ALARM ACTIVATED at " + id(hatime).now().strftime("%H:%M");'

Hi EBME2, thanks for jumping in.
No, I’m not using the SIM800L component.

How about showing us your yaml then? we are in the dark!

You’re right Nick :blush:
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();
  // }
}

So you are not using esphome? Kinda confusing that you labeled the thread esphome. I’ll fix that.

Hi
you did it in esphome or with arduino code?