思路同上一篇:
本例程实现了以下功能:
- 通过SIM800L连接网络
- 连接OneNet平台(通过新版MQTTs协议)
- 调用SHT20的库采集温湿度
- 将温湿度信息上传至OneNet
- 每5分钟采集一次, 采集完成就休眠
#include <Arduino.h>
#include "PubSubClient.h"
#include "Ticker.h"
#include "uFire_SHT20.h"
/*-------------------------------SIM800L 硬件定义----------------------------------*/
#define MODEM_RST 5 //SIM800L复位引脚接在GPIO5
#define MODEM_PWRKEY 4 //SIM800L开关机引脚接在GPIO4
#define MODEM_POWER_ON 23 //SIM800L电源引脚接在GPIO23
#define MODEM_TX 27 //SIM800L串口TX引脚接在GPIO27
#define MODEM_RX 26 //SIM800L串口RX引脚接在GPIO26
/*-------------------------------其他硬件定义-------------------------------------*/
#define LED_G 13
uFire_SHT20 sht20;
#define SerialMon Serial //调试串口为UART0
#define SerialAT Serial1 //AT串口为UART1
/*-------------------------------Modem相关定义-------------------------------------*/
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
//引入TinyGSM库. 在引入之前要定义好TINY_GSM_MODEM_SIM800,让它知道我们用的模块型号
#include <TinyGsmClient.h>
// 创建一个关联到SerialAT的SIM800L模型
TinyGsm modem(SerialAT);
// 创建一个GSM型的网络客户端
TinyGsmClient gsmclient(modem);
PubSubClient client(gsmclient);
// Your GPRS credentials (leave empty, if missing)
const char apn[] = "CMNET"; // Your APN
const char gprsUser[] = ""; // User
const char gprsPass[] = ""; // Password
const char simPIN[] = ""; // SIM card PIN code, if any
/*-------------------------------云平台相关定义-------------------------------------*/
const char *mqtt_server = "183.230.40.96"; //onenet 的 IP地址
const int port = 1883; //端口号
#define mqtt_devid "esp_device001" //设备ID
#define mqtt_pubid "370098" //产品ID
//鉴权信息
#define mqtt_password "version=2018-10-31&res=products%2F370098%2Fdevices%2Fesp_device001&et=4092512761&method=md5&sign=MUV%2BKFLzv81a4Bw6BDrChQ%3D%3D"
char msgJson[75]; //要发送的json格式的数据
char dataTemplate[] = "{\"id\":123,\"dp\":{\"temp\":[{\"v\":%.2f}],\"humi\":[{\"v\":%.2f}]}}";
/*-------------------------------初始化SIM800L-------------------------------------*/
void setupModem()
{
pinMode(MODEM_POWER_ON, OUTPUT); //电源引脚
pinMode(MODEM_PWRKEY, OUTPUT); //开关机键引脚
// 先打开SIM800L的电源
digitalWrite(MODEM_POWER_ON, HIGH);
//根据手册要求拉下PWRKEY 1秒钟以上 可以开机
digitalWrite(MODEM_PWRKEY, HIGH);
delay(100);
digitalWrite(MODEM_PWRKEY, LOW);
delay(1000);
digitalWrite(MODEM_PWRKEY, HIGH);
SerialMon.println("Initializing modem...");
modem.init(); //开机后modem初始化一下
}
/*-------------------------------SIM800L连接GPRS-------------------------------------*/
void modemToGPRS()
{
//连接网络
SerialMon.print("Waiting for network...");
while (!modem.waitForNetwork(240000L))
{
SerialMon.print(".");
delay(500);
}
SerialMon.println(" OK");
//连接GPRS接入点
SerialMon.print(F("Connecting to APN: "));
SerialMon.print(apn);
while (!modem.gprsConnect(apn, gprsUser, gprsPass))
{
SerialMon.print(".");
delay(10000);
}
SerialMon.println(" OK");
}
/*-------------------------------向云平台发送温湿度数据-------------------------------*/
void sendTempAndHumi()
{
if (client.connected())
{
snprintf(msgJson, 75, dataTemplate, sht20.temperature(), sht20.humidity()); //将模拟温湿度数据套入dataTemplate模板中, 生成的字符串传给msgJson
Serial.print("public the data:");
Serial.println(msgJson);
client.publish("$sys/370098/esp_device001/dp/post/json", (uint8_t *)msgJson, strlen(msgJson));
//发送数据到主题
}
}
/*-------------------------------向云平台断线重连-------------------------------------*/
void clientReconnect()
{
if (modem.isNetworkConnected())
{
while (!client.connected()) //再重连客户端
{
Serial.println("reconnect MQTT...");
if (client.connect(mqtt_devid, mqtt_pubid, mqtt_password))
{
Serial.println("connected");
}
else
{
Serial.println("failed");
Serial.println(client.state());
Serial.println("try again in 5 sec");
delay(5000);
}
}
}
else
{
Serial.println("网络断了,不可能连上了, 把数据存在本地等待下一次联网吧");
}
}
void setup()
{
pinMode(LED_G, OUTPUT);
Wire.begin();
sht20.begin();
SerialMon.begin(115200); //初始化调试串口
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX); //初始化AT串口
setupModem(); //SIM800L物理开机
modemToGPRS(); //modem连接GPRS
//连接OneNet并上传数据
Serial.println("connecting to OneNet IOT...");
client.setServer(mqtt_server, port); //设置客户端连接的服务器,连接Onenet服务器, 使用6002端口
client.connect(mqtt_devid, mqtt_pubid, mqtt_password); //客户端连接到指定的产品的指定设备.同时输入鉴权信息
if (client.connected())
{
client.subscribe("$sys/370098/esp_device001/cmd/request/#"); //订阅命令下发主题
sendTempAndHumi();
}
else //如果客户端没连接ONENET, 重新连接
clientReconnect();
delay(500);
//进入睡眠
esp_sleep_enable_timer_wakeup(300000000);
Serial.println("now sleep");
esp_deep_sleep_start();
}
void loop()
{
client.loop();
}
网友评论