// Servo Sweep example for the ESP32
#include <WiFi.h>
#include "PubSubClient.h"
#include "DHTesp.h"
#include <WiFi.h>
#include <ESP32Servo.h>
const int DHT_PIN = 15;
DHTesp dhtSensor;//
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqttServer = "broker.hivemq.com";
int port = 1883;
String stMac;
char mac[50];
char clientId[50];
WiFiClient espClient;
PubSubClient client(espClient);
//
int ledPin = 12; // choose the pin for the LED
int inputPin = 14; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0;
const float BETA = 3950;
const int servoPin = 18;
Servo servo;
int pos = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
servo.attach(servoPin, 500, 2400);
analogReadResolution(10);
pinMode(19,INPUT);
pinMode(15,INPUT);
pinMode(14,OUTPUT);
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
wifiConnect();
client.setServer(mqttServer, port);
}
void wifiConnect() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
void mqttReconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
long r = random(1000);
sprintf(clientId, "clientId-%ld", r);
if (client.connect(clientId)) {
Serial.print(clientId);
Serial.println(" connected");
client.subscribe("topicName/led");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String stMessage;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
stMessage += (char)message[i];
}
Serial.println();
if (String(topic) == "servo") {
Serial.print("Changing output to ");
if(servoPin == "on"){
Serial.println("on");
digitalWrite(servoPin, 1);
}
else if(stMessage == "off"){
Serial.println("off");
digitalWrite(servoPin, 0);
}
}
}
void loop() {
delay(1000);
if (!client.connected()) {
mqttReconnect();
}
client.loop();
//สว่นของ air move
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
for (pos = 0; pos <= 180; pos += 1) {
servo.write(pos);
delay(15);
}
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}//
//ส่วนของ temperture
TempAndHumidity data = dhtSensor.getTempAndHumidity();
//int analogValue = analogRead(15);
float celsius = data.temperature ;
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
(1000);
if(celsius>=35){
digitalWrite(15, HIGH);
for (pos = 0; pos <= 180; pos += 1){
servo.write(pos);
delay(15); }
} else {
digitalWrite(14, LOW);
delay(1000);}//
client.publish("tem",String(celsius).c_str(),true);
client.publish("motion",String(val).c_str(),true);
//for (pos = 0; pos <= 180; pos += 1) {
//servo.write(pos);
//delay(15);
//}
//for (pos = 180; pos >= 0; pos -= 1) {
//servo.write(pos);
//delay(15);
//}
}