Free sending SMS from Arduino without GSM module
It took me to send SMS from arduinina to my phone. Yes, so that without troubles with GSM, SIM cards and payment. Under the cut, what came of it.
Since GSM is not suitable, you will have to send via the Internet. In the bins there was a super cheap Wi-Fi module ESP8266. You can read about its pre-configuration in this excellent article link .
First things first, Google climbed various Internet services to send SMS. There were many services, but there was no suitable one among them. Either 10 SMS per day, or they want money. Yes, and captcha arduinine too tough. But then, quite by accident, I came across an interesting service of my mobile operator (MTS BY) - for only $ 1 you can activate the email reception service as an SMS link. Well, it didn’t work completely free, and after payment I received a mailing address of the form 375XXYYYYYYY@sms.mts.by (where XX and YYYYYYY are the network code and number). That is, it all came down to the banal sending of email from arduino.
But it was not there. It turns out recently all self-respecting mail servers refuse to accept mail on port 25 and without encryption. And I didn’t want to communicate with others, as well as become attached to my home server. Googled here such a service link . In the basic free version they give 6k letters per month, which is enough for my home use. After registration, go to the settings and see the smtp server address, port, login and the password generated to us.
Quickly drew a sketch, send an email to 375XXYYYYYYY@sms.mts.by and ... Bummer. Send to regular mail - comes. I tried to redo the header this way and that - all the same, letters disappear into the abysses of MTS servers, clearly getting into the boiler of the spam filter.
So you need to "wash" the mail. I chose mail.ru. (quietly hated by me for advertising) as the washing service. For washing in the mail settings, in the "Filters and Forwarding" section, I created a new rule:
Settings
After creating the filter, it was necessary to activate it by entering the code that came in the form of email-SMS to the phone. This is the moment of triumph - I send an email from arduinine, and after a minute the phone rings with a joyful alert signal, while frightening a cat passing by.
Further boring technical details.
I took the Arduino Mega 2560 as a board, since it has as many as three additional serial ports (although you can use the usual UNO, it will only be harder to debut).
ESP8266 connected: GND -> GND, VCC and CH_PD -> + 3.3V, RX -> TX3, TX -> RX3. The speed of the ESP8266 is set to 115200 baud.
To communicate with the smtp server, you need to encode your username and password in Base64.
You can use the Linux console:
openssl enc -base64 <<< 'email@gmail.com'
openssl enc -base64 <<< 'password'
Or by some online service, for example a link .
Well, actually the sketch code. After switching on, an SMS is sent and the code goes into an eternal cycle. Additional libraries are not used.
The code
#define SSID "wi-fi_login" // ваш SSID
#define PASS "wi-fi_password" // ваш пароль Wi-Fi
#define SMTPServer "s02.atomsmtp.com" //smtp сервер
#define SMTPPort "2525" // smtp порт
#define MailLogin "smtp_example@gmail.com" // логин для smtp
#define MailLoginBase64 "dWd1LCBrb25lNG5vCg==" //логин для smtp в Base64
#define MailPasswordBase64 "aHJlbiB0YW0K" // пароль для smtp в Base64
#define MailRelay "example@mail.ru" // промежуточная почта для "отмывания" email
#define PhoneNumber "375290000000" // номер телефона
#define Message "Hello from Arduino!" //сообщение
#define SERIAL_RX_BUFFER_SIZE 256
#define SERIAL_TX_BUFFER_SIZE 256
void setup()
{
delay(2000);
Serial3.begin(115200);
Serial3.setTimeout(5000);
Serial.begin(115200); // для отладки
Serial.println("Init");
Serial3.println("AT+RST"); // сброс и проверка, что модуль готов
if(WaiteString("Ready", 5000)) {
while(Serial3.available()) { Serial3.read();}
Serial.println("WiFi - Module is ready");
}else{
Serial.println("Module dosn't respond.");
while(1);
}
delay(100);
Serial3.println(" AT+CIPMODE=0");
WaiteString("OK");
while(Serial3.available()) { Serial3.read();}
Serial3.println("AT+CIPMUX=1");
WaiteString("OK");
while(Serial3.available()) { Serial3.read();}
// try to connect to wifi
boolean connected = false;
for(int i=0;i<5;i++) {
if(connectWiFi()) {
connected = true;
break;
}
}
if (!connected) {
while(1);
}
}
void loop()
{
String cmd = "AT+CIPSTART=0,\"TCP\",\"";
cmd += String(SMTPServer);
cmd += "\"," + String(SMTPPort);
Serial3.println(cmd);
if(WaiteString("Linked", 5000)) {
while(Serial3.available()) { Serial3.read();}
Serial.println("Link");
}
else {
Serial.println("Link fail");
while (1);
}
if (WaiteString("OK", 2000)) {
while(Serial3.available()) { Serial3.read();}
}
else {
while (1);
}
Send("HELO 1.2.3.4", true);
Send("AUTH LOGIN", true);
Send(MailLoginBase64, true);
Send(MailPasswordBase64, true);
Send("MAIL FROM:<" + String(MailLogin) + ">", true);
Send("RCPT TO:<" + String(MailRelay) + ">", true);
Send("DATA", true);
Send("Subject:SMS", false);
Send("To:\"" + String(PhoneNumber) + "\" <" + String(PhoneNumber) + "@sms.mts.by>", false);
Send("From: <" + String(MailLogin) + ">", false);
Send("", false);
Send(Message, false);
Send(".", true);
Send("QUIT", true);
while(1) {};
}
boolean connectWiFi()
{
Serial3.println("AT+CWMODE=1");
while (!Serial3.available()) { delay(10);}
while (Serial3.available()) {Serial3.read();}
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
Serial3.println(cmd);
if(WaiteString("OK", 8000)){
Serial.println("Connected to WiFi.");
return true;
}else{
Serial.println("Can not connect to the WiFi.");
return false;
}
}
bool Send(String S, bool wait) {
Serial3.print("AT+CIPSEND=0,");
Serial3.println(S.length()+2);
while (!Serial3.available()) { delay(10);}
if(Serial3.find(">")){
}else{
Serial3.println("AT+CIPCLOSE=0");
delay(1000);
return false;
}
Serial3.print(S + "\r\n");//добаяляем перевод строки
if (WaitString("OK", 15000)) {
if (wait) {
WaitString("+IPD", 15000);
while(Serial3.available()) {
Serial3.read();}}
return true;}
else {
return false;}
}
void WaiteString(String S) {
int L = S.length();
String T = String(" ");
while(1) {
if (Serial3.available()) {
char c = Serial3.read();
T = T + String(c);
if (T.length() > L) T = T.substring(1);
if (S.charAt(0) == T.charAt(0))
if (S.compareTo(T) == 0) return;
}
else {
delay(1);
}
}
}
bool WaiteString(String S, int Time) {
int L = S.length();
String T = String(" ");
while(Time>0) {
if (Serial3.available()) {
char c = Serial3.read();
T = T + String(c);
if (T.length() > L) T = T.substring(1);
if (S.charAt(0) == T.charAt(0))
if (S.compareTo(T) == 0) return true;
}
else {
delay(1);
Time--;
}
}
return false;
}
String WaiteString(int Time) {
String T = String("");
while(Time>0) {
if (Serial3.available()) {
char c = Serial3.read();
T = T + String(c);
}
else {
delay(1);
Time--;
}
}
return T;
}