Wokwi ESP32 DHT22 + MqttGo.io Broker / Client
免費MQTT Broker https://broker.mqttgo.io/
MQTTGO.io (20220608新增)
尤老大公司出品
mqttgo.io
TCP Port:1883
WebsocketPort:8000
網址:mqttgo.io:8000/mqtt
(2023/01/16更新)
tcp:mqttgo.io:1883
tcp/tls:mqttgo.io:8883
ws://mqttgo.io:8000/mqtt
wss://broker.mqttgo.io:8084/mqtt
圖片來源:https://swf.com.tw/images/books/IoT/MQTT/publisher_broker_subscriber.pngw
WOKWI ESP32程式 檔名:AS-ESP32-DHT22 MQTTgo.io
#include "DHT.h"
#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>
char ssid[] = "Wokwi-GUEST"; // your network SSID (name)
char pass[] = ""; // your network password
const int LED_PIN = 13;
#define DHTPIN 4
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
const size_t BUFFER_SIZE = 100;
char data[BUFFER_SIZE + 1]; // Extra one for NULL terminator.
long now = millis();
long last_time = 0;
// MQTT client
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
char *mqttServer = "broker.mqttgo.io";
int mqttPort = 1883;
String json1 = "";
String json2 = "";
//========================================================
void connectToWiFi() {
Serial.print("Connectiog to ");
WiFi.begin(ssid, pass);
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.print("Connected.");
}
//========================================================
void setupMQTT() {
mqttClient.setServer(mqttServer, mqttPort);
// set the callback function
mqttClient.setCallback(callback);
mqttClient.subscribe("alex9ufo/esp32/led");
}
//========================================================
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Callback - ");
Serial.print("Message:");
String message="";
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
message += ((char)payload[i]);
}
Serial.println();
message.trim();
// Switch on the LED if an 1 was received as first character
if (String(topic) == "alex9ufo/esp32/led") {
Serial.print("Changing output to ");
Serial.println(message);
if(message == "on"){
digitalWrite(LED_PIN, LOW);
}
else if(message == "off"){
digitalWrite(LED_PIN, HIGH);
}
}
}
//========================================================
void reconnect() {
Serial.println("Connecting to MQTT Broker...");
while (!mqttClient.connected()) {
Serial.println("Reconnecting to MQTT Broker..");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
if (mqttClient.connect(clientId.c_str())) {
Serial.println("Connected.");
// subscribe to topic
mqttClient.subscribe("alex9ufo/esp32/led");
}
}
}
//========================================================
void setup() {
pinMode(LED_PIN, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
Serial.println(F("DHTxx test!"));
connectToWiFi();
dht.begin();
setupMQTT();
}
//========================================================
void loop() {
if (!mqttClient.connected())
reconnect();
mqttClient.loop();
now = millis();
if (now - last_time > 5000) {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
//TempAndHumidity data = dhtSensor.getTempAndHumidity();
//float h = dht.readHumidity();
//float t = dht.readTemperature();
String temp = String(dht.readTemperature(), 2);
json1= temp;
mqttClient.publish("alex9ufo/esp32/temp", temp.c_str());
Serial.print("Temperature: ");
Serial.println(temp);
String hum = String(dht.readHumidity() , 1);
json2=hum;
mqttClient.publish("alex9ufo/esp32/humi", hum.c_str());
Serial.print("Humidity: ");
Serial.println(hum);
last_time = now;
}
}
//========================================================
沒有留言:
張貼留言