LED TEMPERATURE HUMIDITY APK Ver2.0 (ESP32. MQTT. Broker. Publish. Subscribe .MIT APP Inventor)
參考來源 https://github.com/HighVoltages/Mqtt-in-Android-app---mit-app-inventor
MIT INVENTOR 程式
https://drive.google.com/file/d/1y1DJz7S6jgSd-jSFo_LUMlmXKUTHnkvP/view?usp=sharing
WOKWI程式
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <DHTesp.h>
//
const int DHTPIN = 15;
const int LED = 13;
DHTesp dhtSensor;
uint32_t delayMS;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char *mqtt_broker = "broker.hivemq.com";
const int mqtt_port = 1883;
//MQTT Credentials
const char* topic = "alex9ufo/Tempdata"; //publish topic
//parameters for using non-blocking delay
unsigned long previousMillis = 0;
const long interval = 5000;
String msgStr = ""; // MQTT message buffer
float temp, hum;
//setting up wifi and mqtt client
WiFiClient espClient;
PubSubClient client(espClient);
//=============================================================================
void setup_wifi() {
delay(10);
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());
}
//=============================================================================
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("Connected");
// Once connected, publish an announcement...
//client.publish("alex9ufo/esp32", "iotfrontier");
// ... and resubscribe
client.subscribe("alex9ufo/lights");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
//=============================================================================
//subscribe call back
void callback(char*topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
String data = "";
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
data += (char)payload[i];
}
Serial.println();
Serial.print("Message size :");
Serial.println(length);
Serial.println();
Serial.println("-----------------------");
Serial.println(data);
if(data=="ON"){
Serial.println("LED =on");
digitalWrite(LED,LOW );
}
else{
Serial.println("LED =off");
digitalWrite(LED,HIGH );
}
}
//=============================================================================
void setup() {
Serial.begin(115200);
// Initialize device.
dhtSensor.setup(DHTPIN, DHTesp::DHT22);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
setup_wifi();
client.setServer(mqtt_broker , 1883); //setting MQTT server
client.setCallback(callback); //defining function which will be called when message is recieved.
}
//=============================================================================
void loop() {
if (!client.connected()) { //if client is not connected
reconnect(); //try to reconnect
}
client.loop();
unsigned long currentMillis = millis(); //read current time
if (currentMillis - previousMillis >= interval) { //if current time - last time > 5 sec
previousMillis = currentMillis;
TempAndHumidity data = dhtSensor.getTempAndHumidity();
String temp = String(data.temperature, 2);
Serial.print("Temperature: ");
Serial.println(temp);
client.publish("alex9ufo/esp32/temperature", temp.c_str());
String hum = String(data.humidity, 1);
Serial.print("Humidity: ");
Serial.println(hum);
msgStr = String(temp) +","+String(hum);
byte arrSize = msgStr.length() + 1;
char msg[arrSize];
Serial.print("PUBLISH DATA:");
Serial.println(msgStr);
msgStr.toCharArray(msg, arrSize);
client.publish(topic, msg);
msgStr = "";
delay(50);
}
}
//=============================================================================
沒有留言:
張貼留言