转载请注明出处:小锋学长生活大爆炸[xfxuezhang.cn]
连接MQTT
1、先安装库
2、默认你已有MQTT服务器
3、编写代码(跳过WiFi连接部分)
#include <PubSubClient.h>
// MQTT server details
const char* mqtt_server = "xxxxx.cn";
const int mqtt_port = 1883;
PubSubClient client(espClient);
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP32CAMClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
// and resubscribe
client.subscribe("/cloud/mqtt/esp32_cam/subscribe");
} 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() {
Serial.begin(115200);
// setup_wifi;
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
完整示例:连接WiFi、连接MQTT、OLED显示
结合了这篇博客:https://xfxuezhang.blog.csdn.net/article/details/136050235
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <PubSubClient.h>
#define CAMERA_MODEL_AI_THINKER
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// WiFi credentials
const char* ssid = "xxx";
const char* password = "xxxx";
// MQTT server details
const char* mqtt_server = "xxxx.cn";
const int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
// Define I2C Pins for ESP32-CAM
#define I2C_SDA 14
#define I2C_SCL 15
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup_wifi() {
delay(10);
// Connect to 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.print("IP address: ");
Serial.println(WiFi.localIP());
// Display IP address on OLED
display.clearDisplay();
display.setCursor(0,0);
display.println("WiFi connected");
display.print("IP: ");
display.println(WiFi.localIP());
display.display();
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// Display the incoming message on the OLED display
display.setCursor(2,0);
display.println("MQTT Message:");
display.println(messageTemp);
display.display();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP32CAMClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
// and resubscribe
client.subscribe("/cloud/mqtt/esp32_cam/subscribe");
} 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() {
Serial.begin(115200);
// Initialize I2C
Wire.begin(I2C_SDA, I2C_SCL);
// Initialize OLED
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Use the correct I2C address here (commonly 0x3C or 0x3D for 128x32 displays)
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}