arudino源码如下:
#include <ESP8266WiFi.h>//默认,加载WIFI头文件
#include "PubSubClient.h"//默认,加载MQTT库文件,mqtt库可以在库管理器直接下载
const char* ssid = "fangguanju"; //修改,修改为你的路由的WIFI名字
const char* password = "xxzxccgt"; //修改为你的WIFI密码
const char* mqtt_server = "bemfa.com"; //默认,MQTT服务器地址
const int mqtt_server_port = 9501; //默认,MQTT服务器端口
#define ID_MQTT "xxxxxxxx" //mqtt客户端ID,修改为你的开发者密钥
const char * dhttopic = "led002"; //主题名字,可在巴法云mqtt控制台创建,尾号002为灯
int B_led = LED_BUILTIN; //控制的led引脚
long timeval = 3 * 1000; //上传的传感器时间间隔,默认3秒
int ENV_D7 = D7 ; // L298N ENV gpio
int M_D1 = D1; // L298N_IN1 ESP_D1
int M_D2 = D2; // L298N_IN2 ESP_D2
String ledstatus = "off";//led状态默认off
long lastMsg = 0;//时间戳
//SimpleDHT11 dht11(pinDHT11);//dht11初始化
WiFiClient espClient;
PubSubClient client(espClient);//mqtt初始化
//灯光函数及引脚定义
void turnOnLed();
void turnOffLed();
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// 设置gpio 为输出模式
pinMode(ENV_D7,OUTPUT);
pinMode(M_D1, OUTPUT);
pinMode(M_D2, OUTPUT);
// 初始化引脚为低电平
digitalWrite(ENV_D7, LOW);
digitalWrite(M_D1, LOW);
digitalWrite(M_D2, LOW);
}
void turnup()
{
analogWrite(ENV_D7, 100);
digitalWrite(M_D1, HIGH);
digitalWrite(M_D2, LOW);
ledstatus="on";
String msg = "#" ledstatus; //数据封装#温度#湿度#开关状态#
client.publish(dhttopic, msg.c_str());//数据上传
}
void turndown()
{
analogWrite(ENV_D7, 100);
digitalWrite(M_D1, LOW);
digitalWrite(M_D2, HIGH);
ledstatus="off";
String msg = "#" ledstatus; //数据封装#温度#湿度#开关状态#
client.publish(dhttopic, msg.c_str());//数据上传
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String Mqtt_Buff = "";
for (int i = 0; i < length; i ) {
Mqtt_Buff = (char)payload[i];
}
Serial.print(Mqtt_Buff);
Serial.println();
// Switch on the LED if an 1 was received as first character
if (Mqtt_Buff == "on") {//如果接收字符on,亮灯
//turnOnLed();//开灯函数
turnup();
} else if (Mqtt_Buff == "off") {//如果接收字符off,亮灯
//turnOffLed();//关灯函数
turndown();
} else if (Mqtt_Buff == "up") {//如果接收字符off,亮灯
turnup();//关灯函数
}
else if (Mqtt_Buff == "down") {//如果接收字符off,亮灯
turndown();//关灯函数
}
else if (Mqtt_Buff == "on#off") {//如果接收字符off,亮灯
turnup();//关灯函数
}
else if (Mqtt_Buff == "off#on") {//如果接收字符off,亮灯
turndown();//关灯函数
}
Mqtt_Buff = "";
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(ID_MQTT)) {//连接mqtt
Serial.println("connected");
client.subscribe(dhttopic);//修改,修改为你的主题
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(B_led, OUTPUT);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, mqtt_server_port);
client.setCallback(callback);
digitalWrite(B_led, HIGH);
}
void loop() {
if (!client.connected()) {//判断mqtt是否连接
reconnect();
}
client.loop();//mqtt客户端
long now = millis();//获取当前时间戳
if (now - lastMsg > timeval) {//如果达到3s,进行数据上传
lastMsg = now;
String msg = "#" ledstatus; //数据封装#温度#湿度#开关状态#
client.publish(dhttopic, msg.c_str());//数据上传
}
}
//打开灯泡
void turnOnLed() {
ledstatus = "on";
Serial.println("turn on light");
digitalWrite(B_led, LOW);
String msg = "#" ledstatus; //数据封装#温度#湿度#开关状态#
client.publish(dhttopic, msg.c_str());//数据上传
}
//关闭灯泡
void turnOffLed() {
ledstatus = "off";
Serial.println("turn off light");
digitalWrite(B_led, HIGH);
analogWrite(ENV_D7, 0);
digitalWrite(M_D1, LOW);
digitalWrite(M_D2, LOW);
String msg = "#" ledstatus; //数据封装#温度#湿度#开关状态#
client.publish(dhttopic, msg.c_str());//数据上传
}
//源码结束
巴法云登录后可以创建mqtt云,创建主题后可以和esp8266通讯,在天猫精灵app中可以关联巴法云,里面就会有你建立的主题了。
,