-
-
Save lcsouzamenezes/e5fa9d5205a55fb58a800021b249400b to your computer and use it in GitHub Desktop.
Send and receive SMS using SIM7600 GSM module and Arduino.
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
/* | |
Project: Interfacing SIM7600 GSM Module with Maker UNO | |
Item used: | |
- Maker UNO https://www.cytron.io/p-maker-uno | |
- SIM7600 https://www.cytron.io/p-4g-3g-2g-gsm-gprs-gnss-hat-for-raspberry-pi | |
*/ | |
#include <SoftwareSerial.h> | |
#include "Adafruit_FONA.h" | |
#define BUTTON 2 | |
#define BUZZER 8 | |
#define LED13 13 | |
#define GSM_RX 4 | |
#define GSM_TX 3 | |
#define GSM_PWR 5 | |
#define GSM_RST 20 // Dummy | |
#define GSM_BAUD 9600 | |
char replybuffer[255]; | |
SoftwareSerial SIM7600SS = SoftwareSerial(GSM_TX, GSM_RX); | |
SoftwareSerial *SIM7600Serial = &SIM7600SS; | |
Adafruit_FONA SIM7600 = Adafruit_FONA(GSM_RST); | |
#define NOTE_G3 196 | |
#define NOTE_C4 262 | |
#define NOTE_E4 330 | |
#define NOTE_F4 349 | |
#define NOTE_G4 392 | |
#define NOTE_C5 523 | |
#define NOTE_D5 587 | |
#define NOTE_G5 784 | |
#define playStartMelody() playTone(melody1, melody1Dur, 2) | |
#define playReadyMelody() playTone(melody2, melody2Dur, 5) | |
#define playErrorMelody() playTone(melody3, melody3Dur, 2) | |
#define playSmsMelody() playTone(melody4, melody4Dur, 5) | |
#define playButtonMelody() playTone(melody5, melody5Dur, 4) | |
int melody1[] = {NOTE_C4, NOTE_G4}; | |
int melody1Dur[] = {12, 8}; | |
int melody2[] = {NOTE_C4, NOTE_F4, NOTE_G4, NOTE_E4, NOTE_C5}; | |
int melody2Dur[] = {6, 16, 6, 6, 3}; | |
int melody3[] = {NOTE_C4, NOTE_G3}; | |
int melody3Dur[] = {6, 3}; | |
int melody4[] = {NOTE_G4, NOTE_C5, 0, NOTE_G4, NOTE_C5}; | |
int melody4Dur[] = {12, 12, 12, 12, 12}; | |
int melody5[] = {NOTE_C5, NOTE_G5, NOTE_C5, NOTE_D5}; | |
int melody5Dur[] = {6, 6, 6, 6}; | |
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0); | |
char SIM7600InBuffer[64]; // For notifications from the FONA | |
char callerIDbuffer[32]; // We'll store the SMS sender number in here | |
char SMSbuffer[32]; // We'll store the SMS content in here | |
uint16_t SMSLength; | |
String SMSString = ""; | |
boolean buttonEnable = false; | |
void setup() | |
{ | |
pinMode(BUTTON, INPUT_PULLUP); | |
pinMode(BUZZER, OUTPUT); | |
pinMode(LED13, OUTPUT); | |
pinMode(GSM_PWR, OUTPUT); | |
delay(1000); | |
pinMode(GSM_PWR, INPUT_PULLUP); | |
Serial.begin(115200); | |
Serial.println("Interfacing SIM7600 GSM Module with Maker UNO"); | |
Serial.println("Initializing... (May take a minute)"); | |
playStartMelody(); | |
delay(15000); | |
// Make it slow so its easy to read! | |
SIM7600Serial->begin(GSM_BAUD); | |
if (!SIM7600.begin(*SIM7600Serial)) { | |
Serial.println("Couldn't find SIM7600"); | |
playErrorMelody(); | |
while (1); | |
} | |
Serial.println(F("SIM7600 is OK")); | |
// Print SIM card IMEI number. | |
char imei[16] = {0}; // MUST use a 16 character buffer for IMEI! | |
uint8_t imeiLen = SIM7600.getIMEI(imei); | |
if (imeiLen > 0) { | |
Serial.print("SIM card IMEI: "); Serial.println(imei); | |
} | |
SIM7600Serial->print("AT+CNMI=2,1\r\n"); // Set up the SIM800L to send a +CMTI notification when an SMS is received | |
playReadyMelody(); | |
Serial.println("GSM is ready!"); | |
delay(1000); | |
} | |
void loop() | |
{ | |
char* bufPtr = SIM7600InBuffer; // Handy buffer pointer | |
if (SIM7600.available()) { // Any data available from the SIM800L | |
int slot = 0; // This will be the slot number of the SMS | |
int charCount = 0; | |
// Read the notification into fonaInBuffer | |
do { | |
*bufPtr = SIM7600.read(); | |
Serial.write(*bufPtr); | |
delay(1); | |
} while ((*bufPtr++ != '\n') && (SIM7600.available()) && (++charCount < (sizeof(SIM7600InBuffer) - 1))); | |
// Add a terminal NULL to the notification string | |
*bufPtr = 0; | |
// Scan the notification string for an SMS received notification. | |
// If it's an SMS message, we'll get the slot number in 'slot' | |
if (1 == sscanf(SIM7600InBuffer, "+CMTI: \"SM\",%d", &slot)) { | |
playSmsMelody(); | |
Serial.print("slot: "); Serial.println(slot); | |
// Retrieve SMS sender address/phone number. | |
if (!SIM7600.getSMSSender(slot, callerIDbuffer, 31)) { | |
Serial.println("Didn't find SMS message in slot!"); | |
} | |
Serial.print("FROM: "); Serial.println(callerIDbuffer); | |
if (!SIM7600.readSMS(slot, SMSbuffer, 250, &SMSLength)) { // pass in buffer and max len! | |
Serial.println("Failed!"); | |
} | |
else { | |
SMSString = String(SMSbuffer); | |
Serial.print("SMS: "); Serial.println(SMSString); | |
} | |
// Compare SMS string | |
if (SMSString == "ENABLE BUTTON") { | |
Serial.print("Button is enable."); | |
buttonEnable = true; | |
delay(100); | |
// Send SMS for status | |
if (!SIM7600.sendSMS(callerIDbuffer, "Button is enable.")) { | |
Serial.println("Failed"); | |
} | |
else { | |
Serial.println(F("Sent!")); | |
} | |
} | |
else if (SMSString == "LED13 ON") { | |
Serial.print("Setting LED 13 to on."); | |
digitalWrite(LED13, HIGH); | |
delay(100); | |
// Send SMS for status | |
if (!SIM7600.sendSMS(callerIDbuffer, "Setting LED to on.")) { | |
Serial.println("Failed"); | |
} | |
else { | |
Serial.println(F("Sent!")); | |
} | |
} | |
else if (SMSString == "LED13 OFF") { | |
Serial.print("Setting LED 13 to off."); | |
digitalWrite(LED13, LOW); | |
delay(100); | |
// Send SMS for status | |
if (!SIM7600.sendSMS(callerIDbuffer, "Setting LED to off.")) { | |
Serial.println("Failed"); | |
} | |
else { | |
Serial.println(F("Sent!")); | |
} | |
} | |
else { | |
Serial.print("Invalid command."); | |
playErrorMelody(); | |
} | |
// Delete the original msg after it is processed | |
// otherwise, we will fill up all the slots | |
// and then we won't be able to receive SMS anymore | |
while (1) { | |
boolean deleteSMSDone = SIM7600.deleteSMS(slot); | |
if (deleteSMSDone == true) { | |
Serial.println("OK!"); | |
break; | |
} | |
else { | |
Serial.println("Couldn't delete, try again."); | |
} | |
} | |
} | |
} | |
if (digitalRead(BUTTON) == LOW && buttonEnable == true) { | |
buttonEnable = false; | |
playButtonMelody(); | |
// Send SMS for status | |
if (!SIM7600.sendSMS(callerIDbuffer, "Button is pressed!")) { | |
Serial.println("Failed"); | |
} | |
else { | |
Serial.println(F("Sent!")); | |
} | |
} | |
} | |
void playTone(int *melody, int *melodyDur, int notesLength) | |
{ | |
for(int i = 0; i < notesLength; i++) { | |
int noteDuration = 1000/melodyDur[i]; | |
tone(BUZZER, melody[i], noteDuration); | |
delay(noteDuration); | |
noTone(BUZZER); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment