SMS remote control (SIM800L)
Switch a pump by text message through a SIM800L modem, reply with its state, and send an alert when a float switch trips.
Communication · Advanced
In a real product: Remote pump controller (Agriculture): Farmers switch an irrigation pump by text message.
A GSM modem lets a project be reached anywhere there is a mobile network. The sketch talks to a SIM800L with AT commands over SoftwareSerial: it waits for the network, then obeys text messages (ON, OFF, STATUS) and sends an alert if the tank's float switch trips.
What you'll learn
- The AT command dialogue: a command, then an answer that ends with OK or ERROR
- Waiting for network registration (
AT+CREG?answering 0,1) before doing anything else - Parsing unsolicited lines the modem sends on its own, such as an incoming message (
+CMT:)
How it works
sendCommand() writes a line ending in \r and collects the answer until OK or ERROR (or a timeout). After ATE0 (no echo), the sketch polls AT+CREG? until the modem is registered, then sets text mode (AT+CMGF=1) and asks for incoming SMS to be shown at once (AT+CNMI=2,2,0,0,0). Each +CMT: header is followed by the message's text on the next line; the sketch acts on it and replies with AT+CMGS, the text and Ctrl+Z. The float switch on pin 4 sends one alert each time it goes HIGH.
Try this
- Type
ONin the box under the modem while it runs: the pump LED lights and the reply shows under the part. - Slide the float switch: an alert goes out.
- Set the modem's network to
noneand watch the sketch wait; then make it give up after a minute and blink the LED.
sketch.ino
// SMS control through a SIM800L on SoftwareSerial (modem TXD to pin 7, RXD to pin 8): texts ON, OFF and STATUS switch
// the pump (LED on pin 13) and are answered; the float switch on pin 4 going HIGH sends an alert, all to OWNER.
#include <SoftwareSerial.h>
SoftwareSerial modem(7, 8);
const char OWNER[] = "+15551234567";
const int PUMP = 13, FLOAT_SWITCH = 4;
bool pumpOn = false, floatHigh = false, smsNext = false;
String line;
// A command and its answer, until OK or ERROR or the timeout.
String sendCommand(const char *command, unsigned long timeout = 2000) {
modem.print(command);
modem.print('\r');
String answer;
unsigned long t0 = millis();
while (millis() - t0 < timeout) {
while (modem.available()) answer += (char)modem.read();
if (answer.indexOf("OK\r\n") >= 0 || answer.indexOf("ERROR") >= 0) break;
}
return answer;
}
void sendSms(const char *to, const String &text) {
modem.print("AT+CMGS=\"");
modem.print(to);
modem.print("\"\r");
delay(200); // wait for the > prompt
modem.print(text);
modem.write(26); // Ctrl+Z sends it
sendCommand("", 5000);
Serial.print("SMS to ");
Serial.print(to);
Serial.print(": ");
Serial.println(text);
}
void handle(String text) {
text.trim();
text.toUpperCase();
if (text == "ON") pumpOn = true;
else if (text == "OFF") pumpOn = false;
else if (text != "STATUS") return;
digitalWrite(PUMP, pumpOn);
sendSms(OWNER, pumpOn ? "Pump is ON" : "Pump is OFF");
}
void setup() {
Serial.begin(9600);
modem.begin(9600);
pinMode(PUMP, OUTPUT);
pinMode(FLOAT_SWITCH, INPUT);
sendCommand("AT");
sendCommand("ATE0");
Serial.print("Waiting for the network");
while (sendCommand("AT+CREG?").indexOf("+CREG: 0,1") < 0) {
Serial.print('.');
delay(1000);
}
Serial.println(" registered");
sendCommand("AT+CMGF=1");
sendCommand("AT+CNMI=2,2,0,0,0");
}
void loop() {
while (modem.available()) {
char c = modem.read();
if (c != '\n') { if (c != '\r') line += c; continue; }
if (smsNext) { smsNext = false; Serial.print("SMS received: "); Serial.println(line); handle(line); }
else if (line.startsWith("+CMT:")) smsNext = true;
line = "";
}
bool high = digitalRead(FLOAT_SWITCH) == HIGH;
if (high && !floatHigh) sendSms(OWNER, "ALERT: tank level high");
floatHigh = high;
}