首頁 > 評測 > ESP8266最佳開發(fā)板--ESP-LAUNCHER開發(fā)板評測

ESP8266最佳開發(fā)板--ESP-LAUNCHER開發(fā)板評測

  • 作者:SATURN
  • 來源:21ic
  • [導(dǎo)讀]
  • 要評選中國影響力十大芯片,ESP8266必在其中。雖然開發(fā)文檔有些混亂,各種生態(tài)紛雜,但它的低價,給了開發(fā)者更低門檻,也讓更多人都參與到WiFi的開發(fā)體驗(yàn)中來。 從這種意義上來說,ESP8266是一款偉大的SoC!可能生態(tài)太繁茂了,所以官方開發(fā)板顯得并不重要,曝光度也不高。但是我們還是想要嘗試一下它的開發(fā)體驗(yàn)。下面正文開始

參考官方原理圖,這里可以使用GPIO12來做試驗(yàn)。

接下來就可以使用ARDUINO的blink程序來測試了,代碼太常見,不上了!

說下插曲,按官方的文檔,直接將GPIO0開關(guān)拔下來,就可以下載代碼了,但是實(shí)際使用的時候沒用,非得先關(guān)閉電源開關(guān),然后再上電,刷刷刷的就開刷了!

第二個程序要來體驗(yàn)一下WIFI連接及測試情況,這個也簡單,參考官方代碼并精簡一下,得到如下代碼

// Visual Micro is in vMicro>General>Tutorial Mode
// 
/*
    Name:       wificlient.ino
    Created:   2018/8/17 17:43:41
    Author:     DESKTOP\hnlsm
*/

// Define User Types below here or use a .h file
//
#include <ESP8266WiFi.h>

const char* ssid = "xxxx";
const char* password = "xxxx";

// Define Function Prototypes that use User Types below here or use a .h file
//


// Define Functions below here or use other .ino or cpp files
//

// The setup() function runs once each time the micro-controller starts
void setup()
{
     Serial.begin(115200);
     delay(10);

     Serial.println("Connecting to Local WiFi.");
     WiFi.mode(WIFI_STA);
     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());

}

// Add the main program code into the continuous loop() function
void loop()
{

}

默認(rèn)使用DHCP方式從路由器獲取IP地址,然后使用串口打印出來,loop()里什么也不干!

得到串口輸出如下

image9.png

成功獲取到IP地址,至此,已成功加入網(wǎng)絡(luò)世界!

然而,這樣并沒有什么用!物聯(lián)的魅力在于遠(yuǎn)程信息的獲取及控制,所以咱得再來點(diǎn)個燈,不同的是,這一次點(diǎn)燈是通過網(wǎng)絡(luò)來進(jìn)行控制的。

網(wǎng)絡(luò)通信的方式很多,低級一點(diǎn)的直接socket來進(jìn)行,高級的則可以使用http來實(shí)現(xiàn),不過這里咱們要來體驗(yàn)一把mqtt方式通信。

mqtt是專門用于物聯(lián)網(wǎng)通信的協(xié)議,底層基于TCP/IP協(xié)議。mqtt通信效率高,可靠并且非常容易實(shí)現(xiàn)。Arduino平臺有許多現(xiàn)成可用的,如PubSubClinet就是Arduino平臺上使用頻率非常高的一個組件。

關(guān)于mqtt,有興趣的可以網(wǎng)上找找資料,非常好的一款物聯(lián)網(wǎng)通信協(xié)議,這里不多介紹,上一段最精簡的代碼,實(shí)現(xiàn)網(wǎng)絡(luò)點(diǎn)燈。

// Visual Micro is in vMicro>General>Tutorial Mode
// 
/*
Name:       wificlient.ino
Created:  2018/8/17 17:43:41
Author:     DESKTOP\hnlsm
*/

// Define User Types below here or use a .h file
//
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "xxxx";
const char* password = "xxxx";
const char* mqtt_server = "192.168.88.22";
#define LED 12
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);

// Define Function Prototypes that use User Types below here or use a .h file
//


// Define Functions below here or use other .ino or cpp files
//

// The setup() function runs once each time the micro-controller starts
void setup()
{
     pinMode(LED, OUTPUT);
     Serial.begin(115200);
     delay(10);

     //setup wifi client
     Serial.println("Connecting to Local WiFi.");
     WiFi.mode(WIFI_STA);
     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());

     //setup mqtt client
     mqttClient.setServer(mqtt_server, 1883);
     mqttClient.setCallback(callback);

}

void callback(char* topic, byte* payload, unsigned int length) {

     //for test only
     char temp[40];
     int i;
     for (i = 0; i < length; i++) {
         temp[i] = payload[i];

     }
     temp[i] = '\0';
     Serial.println(topic);
     Serial.println(temp);

     //program logic goes here
     if (strcmp(topic, "led") == 0) {
         if ((char)payload[0] == '1') {
              digitalWrite(LED, LOW);
         }
         else {
              digitalWrite(LED, HIGH);
         }
     }

}

void reconnect() {
     while (!mqttClient.connected()) {
         if (!mqttClient.connect("ESP8266Client")) {
              delay(5000);
         }

         mqttClient.subscribe("led");
     }

}


// Add the main program code into the continuous loop() function
void loop()
{
     if (!mqttClient.connected()) {
         reconnect();
     }

     mqttClient.loop();


}

mqtt需要一個服務(wù)器,服務(wù)器的作用是檢查用戶發(fā)布的消息,并將之發(fā)布到訂閱了相關(guān)主題(topic)的用戶,在程序的開頭,除了指定WIFI配置相關(guān)參數(shù),還指定了mqtt服務(wù)器,mqtt服務(wù)器默認(rèn)的port號為1883。

reconnect()函數(shù)檢查是否連接正常,如果正常,向用服務(wù)器訂閱topic為led的消息,如果其它結(jié)點(diǎn)發(fā)布消息到該主題,esp8266就會接收到相關(guān)的消息。

代碼的核心在callback函數(shù)里,當(dāng)接收到一個訂閱消息,先檢查其主題是否為led,如果是,檢查消息負(fù)載是0還是1,如果接收到的消息是0,滅燈,否則點(diǎn)亮燈!就這么簡單,程序做了精簡,只是為了演示功能,并沒有考慮容錯處理。

現(xiàn)在可以在另一個結(jié)點(diǎn)上發(fā)布消息來控制LED燈的亮滅。這里使用mosquitto來測試,如下

image10.png

mosquitto_pub命令的-t參數(shù)指定topic,-m參數(shù)指定消息,執(zhí)行命令后在ESP8266的串口可以看到相關(guān)的內(nèi)容輸出

image11.png

當(dāng)然,GPIO12對應(yīng)的LED燈也會相應(yīng)的點(diǎn)亮或熄滅!

程序中也可以將采集到的傳感器如溫度、濕度等信息publish出去,供其它設(shè)備讀取,從而實(shí)現(xiàn)ESP-LAUNCHER真正的物聯(lián)功能!

 

除了支持C/C++之外,ESP8266還支持Javascript,MicroPython等編程語言,有興趣的童鞋可以到官網(wǎng)上查看相關(guān)的支持,http://esp8266.net/上有很多有趣的資源。

  • 本文系21ic原創(chuàng),未經(jīng)許可禁止轉(zhuǎn)載!

網(wǎng)友評論

  • 聯(lián)系人:巧克力娃娃
  • 郵箱:board@21ic.com
  • 我要投稿
  • 歡迎入駐,開放投稿

熱門標(biāo)簽
項(xiàng)目外包 more+