Need To send data using sim800 and esp32

I need to send a message to the given phone number when the user ID is typed.
Can anyone please help me with this

#include <WiFi.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

const byte ROWS = 4;  //four rows
const byte COLS = 4;  //three columns
char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 13, 12, 14, 27 };  //connect to the row pinouts of the keypad
byte colPins[COLS] = { 26, 25, 33, 32 };  //connect to the column pinouts of the keypad

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

String Event_Name = "Attendance";

String Key = "kPABqDk57rSZkTz1bUMG1sZ0jIulbdgG_9o5kYYFbMB";  //nlGjtBEifiySWwQmTgZAipFdSmCgQLXOa4cMcAnin05

// Replace with your unique IFTTT URL resource
String resource = "/trigger/" + Event_Name + "/with/key/" + Key;

// Maker Webhooks IFTTT
const char* server = "maker.ifttt.com";

// Replace with your SSID and Password
const char* ssid = "Dineth";
const char* password = "Dineth2008";

int input;

struct {
  const char* name;
  const int id;
}

Users[] = {
  { "Parrot", 11 },
  { "bla", 12 },
  { "Dineth", 13 },
  { "Aakesh", 14 }
};

size_t UserCount = sizeof Users / sizeof Users[0];

void setup() {
  Serial.begin(9600);

  lcd.begin();
  lcd.setCursor(3, 0);
  lcd.print("Attendance");  
  lcd.setCursor(4, 1);
  lcd.print("System"); 
  delay(1600);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Enter Your ID:");

  Serial.print("Connecting to: ");
  Serial.print(ssid);
  WiFi.begin(ssid, password);

  int timeout = 10 * 4;  // 10 seconds
  while (WiFi.status() != WL_CONNECTED && (timeout-- > 0)) {
    delay(250);
    Serial.print(".");
  }
  Serial.println("");

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Failed to connect, going back to sleep");
  }

  Serial.print("WiFi connected in: ");
  Serial.print(millis());
  Serial.print(", IP address: ");
  Serial.println(WiFi.localIP());
}
void loop() {
  char key = keypad.getKey();
  if (key) {
    if (key >= '0' && key <= '9') {
      input *= 10;
      input += key - '0';
    }
      lcd.setCursor(0, 1);
      lcd.print(input);
      Serial.print(input);
    if (key == '#')  // 'enter'
    {
      for (size_t i = 0; i < UserCount; i++) {
        if (input == Users[i].id) {
          SendData(Users[i].name, Users[i].id);
          return;
        }
      }
      // User ID not found!
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("No ID Found Like: ");
      lcd.setCursor(0, 1);
      lcd.println(input);
      delay(2000);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter Your ID:");
      input = 0;
      
    }

    if (key == '*') {
      input = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter Your ID:");
    }
  }
}


void makeIFTTTRequest(const char *name, int id) {
  Serial.print("Connecting to ");
  Serial.print(server);

  WiFiClient client;
  int retries = 5;
  while (!!!client.connect(server, 80) && (retries-- > 0)) {
    Serial.print(".");
  }
  Serial.println();
  if (!!!client.connected()) {
    Serial.println("Failed to connect...");
  }

  Serial.print("Request resource: ");
  Serial.println(resource);

  // Temperature in Celsius
  String jsonObject = String("{\"value1\":\"") + name + "\",\"value2\":\"" + String(id) + "\"}";

  client.println(String("POST ") + resource + " HTTP/1.1");
  client.println(String("Host: ") + server);
  client.println("Connection: close\r\nContent-Type: application/json");
  client.print("Content-Length: ");
  client.println(jsonObject.length());
  client.println();
  client.println(jsonObject);

  int timeout = 5 * 10;  // 5 seconds
  while (!!!client.available() && (timeout-- > 0)) {
    delay(100);
  }
  if (!!!client.available()) {
    Serial.println("No response...");
  }
  while (client.available()) {
    Serial.write(client.read());
  }

  Serial.println("\nclosing connection");
  client.stop();
}

void SendData(const char *name, int id) {
  lcd.clear();
  lcd.setCursor(3, 0);
  lcd.print("Attended ;)");
  lcd.setCursor(0, 1);
  lcd.println(name);
  makeIFTTTRequest(name, id);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Enter Your ID:");
  
  input = 0;
}

I don't see any interaction with SIM800 in the code. Did you try to add it? - please show your efforts

1 Like

Search for projects done using that technology. Google "ESP + SIM800".

1 Like

Yes, I tried using the Softwareserial library it works with my Arduino Uno.
but it doesn't work with esp32. So I removed that part of the code

have a look at receiving-sms-but-sim800l-not-responding-on-at-commands

1 Like

I need to use it with Esp32 it works finely with arduinos

And what do you expect from the forum? Writing the code for someone is not in a traditions of the community.
You should try to do it yourself and ask to help with your code.

1 Like

the ESP32 has hardware serial ports no need to use SoftwareSerial, e.g. try this

// // SIM800L EVB ESP32 test using Serial2 (pins 16 Rx  and 17 Tx)

// connect so
// SIM800 5V to ESP32 5V
// SIM800 GND to ESP32 GND
// SIM800 VDD to ESP32 3.3v
// SIM80 TXD to ESP32 pin 16 Rx
// SIM800 TXD to ESP32 pin 17 tx

// AT+CGMI  returns the manufacturer's name
// AT+CGMM returns the MODEM model number
// AT+CGMR returns details of the software and model revision level
// AT+CGSN returns the MODEM's serial number

#include <Arduino.h>

// ESP32 example using HardwareSerial library to access Serial port 2 (pins 16 and 17)
// from https://programming.vip/docs/esp32-use-of-hardwareserial-library.html

#define SERIAL_BAUD 115200

HardwareSerial serial2(2);//Serial port 2

int distance = 0;

void setup() {
  //Initialize serial port 0
  Serial.begin(SERIAL_BAUD);
  //Initialize serial port 2
  serial2.begin(SERIAL_BAUD, SERIAL_8N1);
  Serial.println("\nSIM800 EVB to ESP32 Serial port 2 test");
}

void loop() {
  while (serial2.available() > 0) {
    uint8_t byteFromSerial = serial2.read();
    Serial.write(byteFromSerial);
  }
  while (Serial.available() > 0) {
    uint8_t byteFromSerial1 = Serial.read();
    serial2.write(byteFromSerial1);
  }
}

using a NodeMCU ESP_WROOM_32 to a SIM800L EVB board
when AT commands are entered the serial monitor displays

SIM800 EVB to ESP32 Serial port 2 test
AT
OK
AT+CGMI
SIMCOM_Ltd

OK
AT+CGMM
SIMCOM_SIM800L

OK
1 Like

I made the code. I have one problem.

HardwareSerial sim800(1);
const String PHONE = "+9477555535";
#define rxPin 4//GSM Module RX pin to ESP32 Pin 2
#define txPin 2//GSM Module TX pin to ESP32 Pin 4
#define BAUD_RATE 9600
#include <WiFi.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

const byte ROWS = 4;  //four rows
const byte COLS = 4;  //three columns
char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 13, 12, 14, 27 };  //connect to the row pinouts of the keypad
byte colPins[COLS] = { 26, 25, 33, 32 };  //connect to the column pinouts of the keypad

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

String Event_Name = "Attendance";

String Key = "kPABqDk57rSZkTz1bUMG1sZ0jIulbdgG_9o5kYYFbMB";  //nlGjtBEifiySWwQmTgZAipFdSmCgQLXOa4cMcAnin05

// Replace with your unique IFTTT URL resource
String resource = "/trigger/" + Event_Name + "/with/key/" + Key;

// Maker Webhooks IFTTT
const char* server = "maker.ifttt.com";

// Replace with your SSID and Password
const char* ssid = "Dineth";
const char* password = "Dineth2008";

int input;

struct {
  const char* name;
  const int id;
}

Users[] = {
  { "Parrot", 11 },
  { "bla", 12 },
  { "Dineth", 13 },
  { "Aakesh", 14 }
};

size_t UserCount = sizeof Users / sizeof Users[0];

void setup() {
  Serial.begin(9600);

  sim800.begin(BAUD_RATE, SERIAL_8N1, rxPin, txPin);
  Serial.println("SIM800C serial initialize");

  sim800.print("AT+CMGF=1\r"); //SMS text mode
  delay(1000);

  lcd.begin();
  lcd.setCursor(3, 0);
  lcd.print("Attendance");  
  lcd.setCursor(4, 1);
  lcd.print("System"); 
  delay(1600);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Enter Your ID:");

  Serial.print("Connecting to: ");
  Serial.print(ssid);
  WiFi.begin(ssid, password);

  int timeout = 10 * 4;  // 10 seconds
  while (WiFi.status() != WL_CONNECTED && (timeout-- > 0)) {
    delay(250);
    Serial.print(".");
  }
  Serial.println("");

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Failed to connect, going back to sleep");
  }

  Serial.print("WiFi connected in: ");
  Serial.print(millis());
  Serial.print(", IP address: ");
  Serial.println(WiFi.localIP());
}
void loop() {
  char key = keypad.getKey();
  if (key) {
    if (key >= '0' && key <= '9') {
      input *= 10;
      input += key - '0';
    }
      lcd.setCursor(0, 1);
      lcd.print(input);
      Serial.print(input);
    if (key == '#')  // 'enter'
    {
      for (size_t i = 0; i < UserCount; i++) {
        if (input == Users[i].id) {
          SendData(Users[i].name, Users[i].id);
          Message();
          return;
        }
      }
      // User ID not found!
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("No ID Found Like: ");
      lcd.setCursor(0, 1);
      lcd.println(input);
      delay(2000);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter Your ID:");
      input = 0;
      
    }

    if (key == '*') {
      input = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter Your ID:");
    }
  }
}


void makeIFTTTRequest(const char *name, int id) {
  Serial.print("Connecting to ");
  Serial.print(server);

  WiFiClient client;
  int retries = 5;
  while (!!!client.connect(server, 80) && (retries-- > 0)) {
    Serial.print(".");
  }
  Serial.println();
  if (!!!client.connected()) {
    Serial.println("Failed to connect...");
  }

  Serial.print("Request resource: ");
  Serial.println(resource);

  // Temperature in Celsius
  String jsonObject = String("{\"value1\":\"") + name + "\",\"value2\":\"" + String(id) + "\"}";

  client.println(String("POST ") + resource + " HTTP/1.1");
  client.println(String("Host: ") + server);
  client.println("Connection: close\r\nContent-Type: application/json");
  client.print("Content-Length: ");
  client.println(jsonObject.length());
  client.println();
  client.println(jsonObject);

  int timeout = 5 * 10;  // 5 seconds
  while (!!!client.available() && (timeout-- > 0)) {
    delay(100);
  }
  if (!!!client.available()) {
    Serial.println("No response...");
  }
  while (client.available()) {
    Serial.write(client.read());
  }

  Serial.println("\nclosing connection");
  client.stop();
}

void SendData(const char *name, int id) {
  lcd.clear();
  lcd.setCursor(3, 0);
  lcd.print("Attended ;)");
  lcd.setCursor(0, 1);
  lcd.println(name);
  makeIFTTTRequest(name, id);

  sim800.print("AT+CMGF=1\r");
  delay(1000);
  sim800.print("AT+CMGS=\""+PHONE+"\"\r");
  delay(1000);
  sim800.print(name);
  delay(100);
  sim800.write(0x1A); //ascii code for ctrl-26 //sim800.println((char)26); //ascii code for ctrl-26
  delay(1000);
  Serial.println("SMS Sent Successfully.");

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Enter Your ID:");
  
  input = 0;
}

I need to send a message if we get the below as an example, If the user ID entered is 11 need to send the message to an individual number for every ID. Is there any way to save it as in the following array?

  { "Parrot", 11 },
  { "bla", 12 },
  { "Dineth", 13 },
  { "Aakesh", 14 }
};

Something like this?

1 Like

Yes Names,Number and also the ID should be in the Array

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.