【搬运】基于MQTT的聊天系统-Arduino中文社区 - Powered by Discuz! Archiver

vany5921 发表于 2020-4-7 10:55

【搬运】基于MQTT的聊天系统

本项目利用公开的MQTT服务器实现简单的聊天系统,其中用到了M5ez库搭建菜单系统,内置英文输入法,可以配合FaceKit使用,程序使用了迭代器实现消息列表的显示,暂时没有查看历史消息的功能,意味着一旦内容超出屏幕即被新消息刷新。


#include <M5ez.h>
#include <vector>

#include <PubSubClient.h>

WiFiClient espClient;
PubSubClient client(espClient);

//const char* mqtt_server = "broker.mqtt-dashboard.com";
const int mqtt_port = 1883;


std::vector<String> msgList;
bool inchat = false;

// on message received
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
payload = '\0';
String msg = String((char*) payload);
Serial.println(msg);
msgList.push_back(msg);
redraw();
}

void setup() {
ez.begin();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
}

void loop() {
ezMenu mainmenu("Welcome to M5ez");
mainmenu.addItem("chat", chat_menu);
mainmenu.addItem("settings", ez.settings.menu);
mainmenu.run();
}

// true: connected,false: not connected
bool keepMqttConn() {
    if (!client.connected()) {
      Serial.print("Attempting MQTT connection...");

      String clientId = getMacAddr();
      if (client.connect(clientId.c_str())) {
          Serial.print("Connecting done");
          // Once connected, publish an announcement...
          //client.publish("outTopic", "hello world");
          client.subscribe("chat");
          return true;
      } else {
          return false;
      }
    }
    return true;
}

// draw chat messages on LCD
void redraw() {
if(!inchat) {
    return;
}
ez.canvas.clear();
ez.header.show("chat");
ez.canvas.font(&FreeSans9pt7b);
ez.canvas.lmargin(10);

// show latest 8 messages
int total = 0;
for(int i = msgList.size() - 1 ; i >= 0; i--) {
    if(total >= 8) {
      break;
    }
    ez.canvas.println(msgList);
    total++;
}
}

// chat main
void chat_menu() {
if(WiFi.status() != WL_CONNECTED) {
    ez.msgBox("notice", "Wi-Fi is not enabled. Please setting Wi-Fi.");
    return;
}
inchat = true;

ez.buttons.show("up # Back # input # # down #");
redraw();

while(true) {
    if(!keepMqttConn()) {
      ez.msgBox("notice", "MQTT server not found.");
      return;
    }
    client.loop();
   
    String btnpressed = ez.buttons.poll(); // check button status
    if(btnpressed == "Back") {
      break;
    }else if(btnpressed == "input") {
      ez.buttons.clear(true);
      ez.canvas.clear();
      
      String msg = ez.textInput();
      msg.trim();

      // send message
      if(msg.length() > 0) {
      //msgList.push_back(msg);
      keepMqttConn();
      client.publish("chat", msg.c_str());
      }
      
      redraw();
      ez.buttons.clear(true);
      ez.buttons.show("up # Back # input # # down #");
    }else if (btnpressed != "") {

    }
}
inchat = false;
}

// get MAC address of device
String getMacAddr() {
uint8_t baseMac;
esp_read_mac(baseMac, ESP_MAC_WIFI_STA);
char baseMacChr = {0};
sprintf(baseMacChr, "%02X%02X%02X%02X%02X%02X", baseMac, baseMac, baseMac, baseMac, baseMac, baseMac);
return String(baseMacChr);
}

liantianji 发表于 2020-4-8 16:41

大神,佩服牛叉。
页: [1]
查看完整版本: 【搬运】基于MQTT的聊天系统