当前位置:网站首页>An alternative method of sending fixed format Chinese short message in Arduino: taking dht22 + GSM module as an example
An alternative method of sending fixed format Chinese short message in Arduino: taking dht22 + GSM module as an example
2022-04-22 07:57:00 【Ki1381】
Suppose there's a need , use DHT22 Regularly detect temperature and humidity , Use the results GSM Send the module to the specified mobile phone .
Yes Arduino Come on , use DHT22 Detecting temperature and humidity is not difficult ; Send... Through chip serial port GSM AT Instructions are also easy to implement . The key technical point is how to Arduino Send the encoded text message content to PDU. But this job is not Arduino The strengths of . I found some articles on the Internet , It's too complicated to implement .
For the sake of simplifying the code , Whether the content of the short message to be sent can be saved in advance by other means “ Templates ”, Replace only part of the content when sending ?
such as , We stipulate that the SMS format can only be :
temperature :12.3 ℃; humidity :45.6 %
( Only the temperature value and humidity value can change dynamically according to the actual measured value , And these values can't be negative . Force one decimal place , Integer part value range 0-99)
Use it yourself first C# Write a piece of code to generate this message :( For the convenience of reading, a space is added , In fact, there is no )
089168 BADCFEHGJIFK 11000D9168 badcfehgjiFk 0008FF 26 6E295EA6FF1A 0031 0032 002E 0033 00202103FF1B6E7F5EA6FF1A 0034 0035 002E 0036 0020FF05
53
Explain the parts with thick subscripts a little .“BADCFEHGJIFK” It refers to the encoded SMS center number ,“badcfehgjiFk” It refers to the encoded mobile phone number of the receiver . The original center number is ABCDEFGHIJK(11 position , There is no need to 86 Prefix ), The original receiving phone is abcdefghijk( It's also 11 position , Again, there is no need to 86 Prefix ). The coding rules are simple , If the length of the mobile phone number is singular , Finally add F, Then work in pairs , The positions of odd and even digits are interchanged .ABCD It becomes BADC. give an example : The number of Shanghai Unicom SMS center is 13010314500, So after coding is 3110304105F0, When actually sending, use this string of text to substitute .
53 Yes, send later at+cmgs=XX Instructions need to be used , Example SMS , After calculation, this value is 53. When the integral parts of temperature and humidity are single digits, this value is 49, One digit, one two digit is 51. Another thing to note 0008FF hinder 26( Length of user information ). When the integral parts of temperature and humidity are single digits, it is 22, One digit, one two digit is 24.
The rest is easy to handle . be aware 0031 0032 002E 0033 and 0034 0035 002E 0036. In the example of this article, it can be regarded as a simple and rough substitution relationship .0030 Corresponding “0”,0031 Corresponding “1”,0032 Corresponding “2”... And so on ,0039 Corresponding “9”.002E Corresponding decimal point . So this is the encoded 12.3 and 45.6.
In this way, the general framework of SMS can be determined , The actual content can also be customized according to the value , Next, you can send through the serial port .
AT+CSCS="GSM"
Successfully returns “OK”
AT+CMGF=0
Successfully returns “OK”
AT+CMGS= length
Successfully returns “> ”( Space greater than sign )
Now you can send PDU Format message , Last send 0x1A As the Terminator . Successfully returns OK, End of the process .
As a test , This paper assumes that each serial port transmission will be successful , Ignore the return value and send it directly .
// DHT sensor library by Adafruit
// Search the above libraries in library management , Use directly after installation
#include "DHT.h"
#define DHT22_PIN A0 // DHT22 The data line is connected to A0
#define DHTTYPE DHT22
#define LEDPIN 13
DHT dht(DHT22_PIN, DHTTYPE);
String codedCenterNumber = "3110304105F0"; // Coded Shanghai Unicom
String codedMPNumber = "31********F0"; // The encoded mobile phone number for receiving SMS
void setup() {
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, HIGH);
dht.begin();
// bright 5.5 The second light indicates that initialization is in progress
// Then turn off the light
Serial.begin(115200, SERIAL_8N1);
delay(5000);
Serial.println("at");
delay(500);
Serial.flush();
digitalWrite(LEDPIN, LOW);
}
int i = 0;
void loop() {
if (i == 1)
{
float t = dht.readTemperature();
float h = dht.readHumidity();
SendSMS(t, h);
}
i++;
// Modify this judgment value to change the transmission frequency
if (i == 60)
i = 1;
delay(60000);
}
// =======================
void SendSMS(float tVal, float hVal)
{
String cmgs = "";
String smsPDU = "";
if (tVal < 10.0 && hVal < 10.0)
{
cmgs = "AT+CMGS=49";
smsPDU = GetPDU(codedCenterNumber, codedMPNumber, GetPDUFormatValue(tVal), GetPDUFormatValue(hVal), "22");
}
if (tVal >= 10.0 && hVal >= 10.0)
{
cmgs = "AT+CMGS=53";
smsPDU = GetPDU(codedCenterNumber, codedMPNumber, GetPDUFormatValue(tVal), GetPDUFormatValue(hVal), "26");
}
if ((tVal < 10.0 && hVal >= 10.0) || (tVal >= 10.0 && hVal < 10.0))
{
cmgs = "AT+CMGS=51";
smsPDU = GetPDU(codedCenterNumber, codedMPNumber, GetPDUFormatValue(tVal), GetPDUFormatValue(hVal), "24");
}
digitalWrite(LEDPIN, HIGH);
Serial.println("AT");
delay(500);
Serial.flush();
Serial.println("AT+CSCS=\"GSM\"");
delay(500);
Serial.flush();
Serial.println("AT+CMGF=0");
delay(500);
Serial.flush();
Serial.println(cmgs); //
delay(500);
Serial.flush();
Serial.print(smsPDU);
delay(500);
Serial.flush();
Serial.write(0x1a);
delay(500);
Serial.flush();
digitalWrite(LEDPIN, LOW);
}
/*
// If you love it , You can dial your cell phone directly
// Of course , It's impossible to talk
void CallMP(String MP)
{
digitalWrite(LEDPIN, HIGH);
Serial.println("at");
delay(500);
Serial.flush();
Serial.println("atd" + MP);
delay(500);
Serial.flush();
digitalWrite(LEDPIN, LOW);
}
*/
// SMS center and mobile phone number do not need to add 86 Prefix
String GetPDU(String codedCenterNumber, String codedMpNumber, String tValue, String hValue, String len)
{
String rtn = "";
rtn += "089168";
rtn += codedCenterNumber;
rtn += "11000D9168";
rtn += codedMpNumber;
rtn += "0008FF" + len + "6E295EA6FF1A";
rtn += tValue;
rtn += "00202103FF1B6E7F5EA6FF1A";
rtn += hValue;
rtn += "0020FF05";
return rtn;
}
// The value changes to PDU The form needed
String GetPDUFormatValue(float val)
{
String rtn = "";
String v = String(val, 1);
for (int i = 0; i < v.length(); i++)
{
char c = v.charAt(i);
switch (c)
{
case '0':
rtn += "0030";
break;
case '1':
rtn += "0031";
break;
case '2':
rtn += "0032";
break;
case '3':
rtn += "0033";
break;
case '4':
rtn += "0034";
break;
case '5':
rtn += "0035";
break;
case '6':
rtn += "0036";
break;
case '7':
rtn += "0037";
break;
case '8':
rtn += "0038";
break;
case '9':
rtn += "0039";
break;
case '.':
rtn += "002E";
break;
default:
break;
}
}
return rtn;
}
The final effect is as shown in the figure :

版权声明
本文为[Ki1381]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220624207751.html
边栏推荐
- 关于执行sudo pip3 install --upgrade pip时报错xcode-select: Failed to locate ‘pip3‘, requesting installation
- Run program ~ customize similar CMD commands to open non system software
- 循环详解和各种小细节
- 树莓派4:自定义网络时间来源
- Vmware 设置固定ip地址--桥接模式
- [TCP / IP link layer 2]
- Call another function within a shell function (without return value and with return value)
- 观察者模式--ApplicationContext
- QT advertising screen (multi display split screen + full screen display picture)
- 软件开发规范
猜你喜欢
随机推荐
Solve the problem that the message notification is blocked by the El dialog ($message, $alert, $notify, $confirm)
树莓派mono上跨平台运行一个C#自制的简易图片处理器
List intersection, union, duplicate removal, extraction, attribute modification and other common operations
Instructions for functional interface @ functionalinterface
Call another function within a shell function (without return value and with return value)
Unityjson file creation and reading
STM32 peripherals [iv] RCC
继续树莓派4B+OLED:开机自动显示IP地址
循环详解和各种小细节
C-9结构体:计算输入日期是该年的第几天
[TCP / IP four IP internet protocol]
关于信息收集
JS wdatepicker to get the selected date
Use of C variable and precautions
基於卷積神經網絡LeNet-5模型的mnist手寫數字識別
Stm32外设篇 [一]IO
简单c语言练习:学生数据的存取
Unity update obtains the difference between the current frame and the previous frame
QT学习汇总
六一节,赋诗一首
![RT thread [i] create project](/img/cd/9a2a9c7381caaf150b3700e348e38a.png)

![Stm32外设篇 [一]IO](/img/75/61ea3ed2936eea9b55d59e96253995.png)






