以前用esp8266作的東西是經過tcp鏈接來和服務器端通訊的,服務器端須要本身管理全部的鏈接,每一個鏈接要作心跳包,還要考慮通訊消息的可靠性。偶然看到了mqtt協議,發現能夠拿來用。html
MQTT協議介紹git
ESP8266能夠用的MQTT客戶端github
//注意我這邊用的是esp12e模塊,淘寶16塊左右,因此有16引腳,esp8266也能夠燒製如下程序
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "........";
const char* password = "........";
const char* mqttServer = ".....";
const int mqttPort = 9999;
const char* mqttUserName = "...";
const char* mqttPassword = "...";
const char* lightTopic = "...";
const char* willTopic = "...";
const char* onlineTopic = "...";
const char* clientId = "...";
WiFiClient espClient;
PubSubClient client(espClient);
int lightPin = 16;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
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());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println(topic);
String command = "";
for (int i = 0; i < length; i++) {
command += (char)payload[i];
}
Serial.println(command);
handlePayload(String(topic), command);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
//詳細參數說明請查看文檔
if (client.connect(clientId,mqttUserName,mqttPassword,willTopic,1,0,clientId)) {
Serial.println("connected");
client.publish(onlineTopic, clientId);
client.subscribe(lightTopic, 1);
} 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(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
//處理命令
String handlePayload(String topic, String payload) {
if (String(lightTopic).equals(topic)) {
//light command
if (String("lightOn").equals(payload)) {
digitalWrite(lightPin, HIGH);
} else if (String("lightOff").equals(payload)) {
digitalWrite(lightPin, LOW);
}
}
}
服務器列表 中的服務器都是能夠支持mqtt的,你們能夠根據我的狀況選擇。我這邊用的 activemqweb
下載activemq
官方地址apache
解壓縮,而後進入bin目錄 運行如下命令啓動小程序
activemq.bat start微信小程序
3.打開examples\mqtt\websocket目錄下的index.html
能夠進行測試
服務器
4.配置端口和用戶名密碼
打開config/activemq.xml文件微信
<!-- 找到一下內容,便是配置不一樣協議端口 -->
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
<!-- 在broker節點的最後添加 -->
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="admin" password="admin" groups="users,admins"/>
<authenticationUser username="user" password="password" groups="users"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
給esp12e模塊的16引腳接個繼電器,通電,就能夠經過向esp12e模塊訂閱的主題發送命令控制繼電器了。目前我已經能夠經過天貓精靈和微信小程序控制我臥室的燈了。websocket