/* Sketch was generated by motoblockly
Website: http://www.motoblockly.com
Author: www.motoduino.com
Date: Tue Dec 19 2023 13:51:02 GMT+0800
*/
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
//#include "esp_DHT.h" motoduino_code
//使用變數的方式
//const char *mqtt_broker = "broker.MQTTGO.io";
//const char *topic = "emqx/esp32";
WiFiClient espclient;
PubSubClient mqtt_client(espclient);
DHT dht(14, DHT22);
//DHT dht(14, DHT11); motoduino_code
void setup()
{
Serial.begin(9600);
pinMode(14, INPUT);
dht.begin();
//setup MQTT
Serial.print("Connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin("Wokwi-GUEST", "", 6);
// WiFi.begin(String("SSID").c_str(),String("Password").c_str()); motoduino_code
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected!");
//setup MQTT
Serial.print("Connecting to MQTT");
mqtt_client.setServer("broker.MQTTGO.io",1883);
//mqtt_client.setServer("https://broker.mqttgo.io",1883); motoduino_code
while (!mqtt_client.connect("7935364904")) {
Serial.print(".");
delay(1000);
}
Serial.println("Connected !");
}
void loop()
{
mqtt_client.loop();
mqtt_client.publish(String("emqx/esp32").c_str(),String(dht.readTemperature()).c_str());
delay(1000);
}