Skip to content

Instantly share code, notes, and snippets.

@xpj
Last active June 12, 2022 19:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xpj/d75307a4833c7e62c6906c682d38e3b9 to your computer and use it in GitHub Desktop.
Save xpj/d75307a4833c7e62c6906c682d38e3b9 to your computer and use it in GitHub Desktop.
Arduino Mega with SIM800L
#include <SoftwareSerial.h>
/* Tutorial link: https://pijaeducation.com/arduino/gsm/send-receive-messages-using-sim800l-with-arduino/
Create software serial pins: pin 2 as RX & 3 as TX
Connect SIM800L module Rx to Pin 3 (Tx) of Arduino & Tx to Pin 2 (Rx) of Arduino
*/
/*
Not all pins on the Mega and Mega 2560 boards support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69). Not all pins on the Leonardo and Micro boards support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
*/
#define rx 10
#define tx 11
SoftwareSerial mySerial(rx, tx);
void setup() {
pinMode(rx, INPUT);
pinMode(tx, OUTPUT);
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("Initializing...");
for (int i =0; i < 30; i++) {
Serial.print(".");
delay(1000);
}
Serial.println(".");
Serial.println("Send attention command to check if all fine, it returns OK");
mySerial.println("AT");
updateSerial();
Serial.println("Signal quality test, value range is 0 - 31 , 31 is the Excellent");
mySerial.println("AT+CSQ");
updateSerial();
Serial.println("Used to read the ICCID from the SIM, if returns means SIM is plugged");
mySerial.println("AT+CCID");
updateSerial();
Serial.println("Check whether it has registered on the network");
mySerial.println("AT+CREG?");
updateSerial();
Serial.println("Configuring module in TEXT mode");
mySerial.println("AT+CMGF=1");
updateSerial();
Serial.println("Decides how newly arrived SMS messages should be handled");
mySerial.println("AT+CNMI=1,2,0,0,0");
updateSerial();
}
void loop() {
updateSerial();
}
// For data transmission from Serial to Software Serial port & vice versa
void updateSerial() {
delay(500);
while (mySerial.available()) {
Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
}
while (Serial.available()) {
mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment