#include <SoftwareSerial.h> #define TX_PIN 8 #define RX_PIN 7 SoftwareSerial serialSIM800(TX_PIN, RX_PIN); void setup() { Serial.begin(9600); //HW Serial Monitor while(!Serial); Serial.println("Inisialisasi modul SIM800L"); serialSIM800.begin(9600); delay(1000); Serial.println("Kirim SMS..."); //Set mode teks untuk pengiriman sms serialSIM800.write("AT+CMGF=1\r\n"); delay(1000); //Mulai mengirim Sms ganti nomer tujuan kamu serialSIM800.write("AT+CMGS=\"081234567890\"\r\n"); delay(1000); serialSIM800.write("System ready"); delay(1000); serialSIM800.write((char)26); //CTRL-Z delay(1000); Serial.println("SMS Terkirim!"); } void loop() { }
+CMT :"+628155555555","11/10/01,12 :30 :00+00", pesan sms disini
#include <SoftwareSerial.h>
#include <avr/pgmspace.h> // untuk menghemat RAM gunakan PROGMEM
SoftwareSerial gsm(2, 3); // RX, TX pins ke modem
String message = "";
String senderNumber = ""; // variabel nomer yang akan dibalas
bool newMessage = false; // Flag ada pesan baru
// jokes bapak bapak disimpan di PROGMEM
const char joke1[] PROGMEM = "Kenapa ayam nyebrang jalan? Karena mau ke seberang!";
const char joke2[] PROGMEM = "Kenapa gajah bawa koper? Karena mau pergi liburan!";
const char joke3[] PROGMEM = "Kenapa Indonesia panas banget ya? Padahal negara ASEAN.";
const char joke4[] PROGMEM = "Uang 100 ribu jika dilempar bakal jadi apa? Jadi rebutan.";
const char joke5[] PROGMEM = "Apa yang lebih besar dari dinosaurus? Hutan tempat mereka tinggal.";
const char joke6[] PROGMEM = "Huruf-huruf apa yang sering kedinginan? Huruf B karena ada di tengah-tengah AC.";
const char joke7[] PROGMEM = "Kentang apa yang bisa bikin bayi ketawa? Kentang-tingtung-tingtang-tingtungtang.";
const char joke8[] PROGMEM = "Kesenian apa yang sering dilakukan nasabah bank? Tari tunai.";
const char joke9[] PROGMEM = "Sapi apa yang larinya cepat? Sapida motor.";
const char joke10[] PROGMEM = "Monyet apa yang bikin sebel? Monyetel tivi nggak ada remote.";
const char joke11[] PROGMEM = "Sepeda apa yang tak bisa dicat? Sepeda hilang.";
const char* const jokes[] PROGMEM = {joke1, joke2, joke3, joke4, joke5, joke6, joke7, joke8, joke9, joke10, joke11}; // Array of joke pointers
void setup() {
gsm.begin(9600); // Start GSM communication
Serial.begin(9600); // For serial monitor
delay(1000);
Serial.println("Test GSM Modem Server for UNESA");
// Initialize GSM modem
gsm.println("ATE1");
delay(1000);
gsm.println("AT+CMGF=1"); // Set text mode
delay(1000);
gsm.println("AT+CNMI=2,2"); // Sms langsung baca
delay(1000);
gsm.println("AT+CMGD=31,4"); // Hapus sms
delay(1000);
}
void loop() {
if (gsm.available()) {
char c = gsm.read();
message += c;
if (c == '\n') { // End of a line
Serial.print("Received: ");
Serial.println(message);
// Check for SMS header (+CMT) to know it's an incoming message
if (message.indexOf("+CMT") != -1) {
senderNumber = extractPhoneNumber(message); // Extract sender's number
newMessage = true; // Set flag indicating the next line will be the message body
}
else if (newMessage && message.length() > 1) { // Process the next line after +CMT as the message body
String studentName = message;
studentName.trim(); // Remove any trailing spaces or newlines
// Send greeting with a random joke
String reply = createReply(studentName);
sendSMS(senderNumber, reply); // Use senderNumber extracted earlier
newMessage = false; // Reset flag after sending the SMS
}
message = ""; // Clear the message for the next line
}
}
}
// Extract phone number from the +CMT line
String extractPhoneNumber(String msg) {
int startIndex = msg.indexOf("\"") + 1;
int endIndex = msg.indexOf("\"", startIndex);
return msg.substring(startIndex, endIndex);
}
// Create the reply message with a greeting and a random joke
String createReply(String name) {
String greeting = "Halo " + name + ", ";
// Generate a random index and fetch the joke from PROGMEM
int randomIndex = random(0, 11);
char joke[100]; // Create a buffer to store the joke
strcpy_P(joke, (char*)pgm_read_word(&(jokes[randomIndex]))); // Read the joke from PROGMEM
return greeting + String(joke); // Combine greeting with joke
}
// Function to send an SMS
void sendSMS(String phoneNumber, String message) {
gsm.println("AT+CMGS=\"" + phoneNumber + "\"\r"); // Send the AT command for the recipient's number
delay(100);
gsm.print(message); // Send the SMS body
delay(100);
gsm.write(26); // Send Ctrl+Z to end the SMS
delay(500);
}
if (gsm.available()) {
char c = gsm.read();
message += c;
if (c == '\n') { // End of a line
Serial.print("Received: ");
Serial.println(message);
// Check for SMS header (+CMT) to know it's an incoming message
if (message.indexOf("+CMT") != -1) {
senderNumber = extractPhoneNumber(message); // Extract sender's number
newMessage = true; // Set flag indicating the next line will be the message body
}
else if (newMessage && message.length() > 1) { // Process the next line after +CMT as the message body
String studentName = message;
studentName.trim(); // Remove any trailing spaces or newlines
// Send greeting with a random joke
String reply = createReply(studentName);
sendSMS(senderNumber, reply); // Use senderNumber extracted earlier
newMessage = false; // Reset flag after sending the SMS
}
message = ""; // Clear the message for the next line
}
}
// Extract phone number from the +CMT line
String extractPhoneNumber(String msg) {
int startIndex = msg.indexOf("\"") + 1;
int endIndex = msg.indexOf("\"", startIndex);
return msg.substring(startIndex, endIndex);
}
// Create the reply message with a greeting and a random joke
String createReply(String name) {
String greeting = "Halo " + name + ", ";
// Generate a random index and fetch the joke from PROGMEM
int randomIndex = random(0, 11);
char joke[100]; // Create a buffer to store the joke
strcpy_P(joke, (char*)pgm_read_word(&(jokes[randomIndex]))); // Read the joke from PROGMEM
return greeting + String(joke); // Combine greeting with joke
}
// Function to send an SMS
void sendSMS(String phoneNumber, String message) {
gsm.println("AT+CMGS=\"" + phoneNumber + "\"\r"); // Send the AT command for the recipient's number
delay(100);
gsm.print(message); // Send the SMS body
delay(100);
gsm.write(26); // Send Ctrl+Z to end the SMS
delay(500);
}