另一种参数
#include <WiFi.h>
#include "DHT.h"
#include "PubSubClient.h"
#include "Ticker.h"
#define DHTPIN 21
//`#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHT11);
int god=0;
const int led =15; //灯的接口
const char *ssid = "yaoyao"; //wifi名称
const char *password = "love123456";//wifi密码
const char *mqtt_server = "183.230.40.96"; //onenet 的 IP地址
const int port = 6002; //端口号
#define mqtt_devid "1056007204" //设备ID
#define mqtt_pubid "578961" //产品ID
//鉴权信息
#define mqtt_password "dada" //鉴权信息
//#define onenet_apiKey "tnTFSvTHqR0Zxt=CU7AtDZ4Zr60=" //产品API_KEY
WiFiClient espClient; //创建一个WIFI连接客户端
PubSubClient client(espClient); // 创建一个PubSub客户端, 传入创建的WIFI客户端
Ticker tim1; //定时器,用来循环上传数据
void setupWifi(){
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("正在连接中----\n");
}
Serial.print("WIFI已连接\n");
Serial.println(WiFi.localIP());
}
/* 发布多个传感器信息
String sendmsg 打包数据
{"senseor1":1213,"senseor2":313.3,"senseor3":543.09,"senseor4":645.0}
注意 ” 转义字符串 \“
*/
void pubMQTTmsg_more_name_value(String sendmsg_in)
{
long lastMsg = 0;
int sendlen=800;//28 //数组大小
char msg[sendlen+22]; //数组大小
char tmp[sendlen];
char d[3];
String sendmsg=sendmsg_in;
sendmsg.toCharArray(tmp, sendlen);
//Serial.println(tmp);
uint16_t streamLen= strlen(tmp);
d[0]='\x03';
d[1] = (streamLen >> 8);
d[2] = (streamLen & 0xFF);
snprintf(msg,sizeof(msg),"%c%c%c%s",d[0],d[1],d[2],tmp);
client.publish("$dp", (uint8_t*)msg,streamLen+3,false);
//发送数据到主题
//delay(500);
}
//收到主题下发的回调, 注意这个回调要实现三个形参 1:topic 主题, 2: payload: 传递过来的信息 3: length: 长度
void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
//for (int i = 0; i < length; i++) {
//Serial.print((char)payload[i]);
//}
Serial.println();
if ((char)payload[0] == '0') {
digitalWrite(led, LOW); //
god=0;
}
if ((char)payload[0] == '1') {
digitalWrite(led, HIGH); //
god=1;
}
else{}
}
void clientReconnect()
{
while (!client.connected()) //再重连客户端
{
Serial.print("reconnect MQTT...");
if (client.connect(mqtt_devid, mqtt_pubid, mqtt_password))
{
Serial.println("connected");
}
else
{
Serial.print("failed");
Serial.print(client.state());
Serial.print("try again in 5 sec");
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
pinMode(led,OUTPUT);//输出
setupWifi(); //调用函数连接WIFI
Serial.println(F("DHT11 test!"));
dht.begin();
client.setServer(mqtt_server, port); //设置客户端连接的服务器,连接Onenet服务器, 使用6002端口
client.connect(mqtt_devid, mqtt_pubid, mqtt_password); //客户端连接到指定的产品的指定设备.同时输入鉴权信息
if (client.connected())
{
Serial.println("OneNet is connected!");//判断以下是不是连好了.
}
//设置好客户端收到信息是的回调
// client.setCallback(callback); //订阅命令下发主题
// tim1.attach(5, sendTempAndHumi); //定时每5秒调用一次发送数据函数sendTempAndHumi
}
void loop(){
// int h = int(dht.readHumidity());
// int t = int(dht.readTemperature());
// if (isnan(h) || isnan(t) ) {
//
// h=0;
// t=0;
// Serial.println("error t h");
//
// }
//Serial.println(h);
if (!WiFi.isConnected()) //先看WIFI是否还在连接
{
setupWifi();
}
if (!client.connected()) //如果客户端没连接ONENET, 重新连接
{
clientReconnect();
delay(100);
}else{
String msg=String("")+"{"
+"\"空气质量\":"+String(21)+","
+"\"湿度\":"+String(21)+","
+"\"温度\":"+String(21)
+"}";
pubMQTTmsg_more_name_value(msg);
client.loop(); //客户端循环检测
Serial.print("public the data:");
Serial.println(msg);
delay(3000);
}
}
综合例程
#include <WiFi.h>
#include "PubSubClient.h"
#include "Ticker.h"
int god=0;
const int led =15; //灯的接口
const int pin_mq=34;
#include "DHT.h"
#define DHTPIN 16
//`#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHT11);
const char *ssid = "yaoyao"; //wifi名称
const char *password = "love123456";//wifi密码
const char *mqtt_server = "183.230.40.96"; //onenet 的 IP地址
const int port = 6002; //端口号
#define mqtt_devid "1056007204" //设备ID
#define mqtt_pubid "578961" //产品ID
//鉴权信息
#define mqtt_password "dada" //鉴权信息
//#define onenet_apiKey "tnTFSvTHqR0Zxt=CU7AtDZ4Zr60=" //产品API_KEY
WiFiClient espClient; //创建一个WIFI连接客户端
PubSubClient client(espClient); // 创建一个PubSub客户端, 传入创建的WIFI客户端
Ticker tim1; //定时器,用来循环上传数据
void setupWifi(){
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("正在连接中----\n");
}
Serial.print("WIFI已连接\n");
Serial.println(WiFi.localIP());
}
/* 发布多个传感器信息
String sendmsg 打包数据
{"senseor1":1213,"senseor2":313.3,"senseor3":543.09,"senseor4":645.0}
注意 ” 转义字符串 \“
*/
void pubMQTTmsg_more_name_value(String sendmsg_in)
{
long lastMsg = 0;
int sendlen=800;//28 //数组大小
char msg[sendlen+22]; //数组大小
char tmp[sendlen];
char d[3];
String sendmsg=sendmsg_in;
sendmsg.toCharArray(tmp, sendlen);
//Serial.println(tmp);
uint16_t streamLen= strlen(tmp);
d[0]='\x03';
d[1] = (streamLen >> 8);
d[2] = (streamLen & 0xFF);
snprintf(msg,sizeof(msg),"%c%c%c%s",d[0],d[1],d[2],tmp);
client.publish("$dp", (uint8_t*)msg,streamLen+3,false);
//发送数据到主题
//delay(500);
}
//收到主题下发的回调, 注意这个回调要实现三个形参 1:topic 主题, 2: payload: 传递过来的信息 3: length: 长度
void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
//for (int i = 0; i < length; i++) {
//Serial.print((char)payload[i]);
//}
Serial.println();
if ((char)payload[0] == '0') {
digitalWrite(led, LOW); //
god=0;
}
if ((char)payload[0] == '1') {
digitalWrite(led, HIGH); //
god=1;
}
else{}
}
void clientReconnect()
{
while (!client.connected()) //再重连客户端
{
Serial.print("reconnect MQTT...");
if (client.connect(mqtt_devid, mqtt_pubid, mqtt_password))
{
Serial.println("connected");
}
else
{
Serial.print("failed");
Serial.print(client.state());
Serial.print("try again in 5 sec");
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
pinMode(led,OUTPUT);//输出
//pinMode(pin_mq,INPUT);//输出
//set the resolution to 12 bits (0-4096)
analogReadResolution(12);
setupWifi(); //调用函数连接WIFI
Serial.println(F("DHT11 test!"));
dht.begin();
client.setServer(mqtt_server, port); //设置客户端连接的服务器,连接Onenet服务器, 使用6002端口
client.connect(mqtt_devid, mqtt_pubid, mqtt_password); //客户端连接到指定的产品的指定设备.同时输入鉴权信息
if (client.connected())
{
Serial.println("OneNet is connected!");//判断以下是不是连好了.
}
//设置好客户端收到信息是的回调
// client.setCallback(callback); //订阅命令下发主题
// tim1.attach(5, sendTempAndHumi); //定时每5秒调用一次发送数据函数sendTempAndHumi
}
void loop(){
// 读取温湿度
float h = float(dht.readHumidity());
float t = float(dht.readTemperature());
if (isnan(h) || isnan(t) ) {
h=0;
t=0;
Serial.println("error t h");
}
//String showms=String(h)+" "+String(t);
//Serial.println(showms);
//读取气体
int mq_value= analogRead(pin_mq);
if (!WiFi.isConnected()) //先看WIFI是否还在连接
{
setupWifi();
}
if (!client.connected()) //如果客户端没连接ONENET, 重新连接
{
clientReconnect();
delay(100);
}else{
String msg=String("")+"{"
+"\"空气质量\":"+String(mq_value)+","
+"\"湿度\":"+String(h)+","
+"\"温度\":"+String(t)
+"}";
pubMQTTmsg_more_name_value(msg);
client.loop(); //客户端循环检测
Serial.print("public the data:");
Serial.println(msg);
delay(3000);
}
}