Created
October 3, 2024 09:48
-
-
Save cyrusmeh/59578c6164b82ca049ef39dab87b6ea0 to your computer and use it in GitHub Desktop.
We’ll be building a smart device utilizing the TTGO T-Call development board with the ESP32 and SIM800L chip. You'll learn about the essential components required, including a 2.4GHz Wi-Fi antenna, a nano SIM card, an XL4015 step-down DC-DC converter, and a 100A solid-state relay.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define TINY_GSM_MODEM_SIM800 // Define SIM800 GSM modem | |
| #define RELAY_PIN 14 // Pin connected to relay | |
| // Include the necessary libraries | |
| #include <Wire.h> | |
| #include <TinyGsmClient.h> | |
| // TTGO T-Call pins | |
| #define MODEM_RST 5 | |
| #define MODEM_PWKEY 4 | |
| #define MODEM_POWER_ON 23 | |
| #define MODEM_TX 27 | |
| #define MODEM_RX 26 | |
| // Define serial communication for the modem and debugging | |
| #define SerialMon Serial | |
| #define SerialAT Serial1 | |
| // SIM card PIN (leave empty if not defined) | |
| const char simPIN[] = ""; | |
| // Your phone number to send SMS (use + and country code) | |
| #define SMS_TARGET "+989393569424" | |
| // Initialize modem | |
| TinyGsm modem(SerialAT); | |
| void setup() { | |
| // Initialize serial communication | |
| SerialMon.begin(115200); | |
| SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX); | |
| // Initialize relay pin | |
| pinMode(RELAY_PIN, OUTPUT); | |
| // Power up the modem | |
| pinMode(MODEM_PWKEY, OUTPUT); | |
| pinMode(MODEM_RST, OUTPUT); | |
| pinMode(MODEM_POWER_ON, OUTPUT); | |
| digitalWrite(MODEM_PWKEY, LOW); | |
| digitalWrite(MODEM_RST, HIGH); | |
| digitalWrite(MODEM_POWER_ON, HIGH); | |
| // Initialize the modem | |
| SerialMon.println("Initializing modem..."); | |
| if (!modem.restart()) { | |
| SerialMon.println("Failed to restart modem"); | |
| while (true); | |
| } | |
| SerialMon.println("Modem initialized successfully"); | |
| // Unlock SIM card if necessary | |
| if (strlen(simPIN) && modem.getSimStatus() != 3) { | |
| modem.simUnlock(simPIN); | |
| } | |
| // Set SMS text mode | |
| SerialAT.println("AT+CMGF=1"); | |
| delay(100); | |
| // Set SMS handling mode to deliver immediately to serial | |
| SerialAT.println("AT+CNMI=2,2,0,0,0"); | |
| delay(100); | |
| SerialMon.println("Setup completed, waiting for SMS..."); | |
| } | |
| void loop() { | |
| // Handle incoming SMS messages | |
| if (SerialAT.available()) { | |
| String sms = SerialAT.readStringUntil('\n'); | |
| sms.trim(); | |
| SerialMon.println("Received SMS: " + sms); | |
| // Check for "ON" or "OFF" commands in the SMS | |
| if (sms.indexOf("ON") >= 0) { | |
| digitalWrite(RELAY_PIN, HIGH); // Relay ON (adjust if active-low) | |
| SerialMon.println("Relay turned ON via SMS"); | |
| } else if (sms.indexOf("OFF") >= 0) { | |
| digitalWrite(RELAY_PIN, LOW); // Relay OFF (adjust if active-low) | |
| SerialMon.println("Relay turned OFF via SMS"); | |
| } | |
| } | |
| // Manual control from Serial Monitor | |
| if (Serial.available()) { | |
| String input = Serial.readString(); | |
| input.trim(); | |
| SerialMon.println("Received input: " + input); | |
| if (input.equalsIgnoreCase("ON")) { | |
| digitalWrite(RELAY_PIN, HIGH); // Relay ON (adjust if active-low) | |
| SerialMon.println("Relay turned ON"); | |
| } else if (input.equalsIgnoreCase("OFF")) { | |
| digitalWrite(RELAY_PIN, LOW); // Relay OFF (adjust if active-low) | |
| SerialMon.println("Relay turned OFF"); | |
| } else { | |
| SerialMon.println("Unknown command. Please type ON or OFF."); | |
| } | |
| } | |
| delay(1000); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment