#include <ArduinoJson.h>
#include <WiFi.h>
#include "PubSubClient.h"
#include "DHT.h"
#define DHT22PIN 15
#define ldrPin 2
const int analogInputPin = A0; // Define analog input pin for light sensor
#define ptm 13
int16_t valueptm;
const int pot = 33;
int pot_value;
int lightValue; // Variable to store light sensor reading
int analogValue;
DHT dht(DHT22PIN, DHT22);
// DHT dht(DHT22PIN, DHT22);
const char * MQTTServer = "broker.emqx.io";
const char * MQTT_Topic = "DHT22";
const char * MQTT_ID = "hello";
int Port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
const int DHT_PIN = 15;
const int Led_Pin=25;
const int Maybom=26;
// DHTesp dhtSensor;
//CAM BIEN KHAC:
const int doamdat=12;
const int pir= 19;
void setup() {
pinMode(pir,INPUT);
pinMode(13, INPUT);
pinMode(ptm, INPUT);
pinMode( pot,INPUT);
dht.begin();
Serial.begin(115200);
// dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
WIFIConnect();
client.setServer(MQTTServer, Port);
client.setCallback(callback);
pinMode(Led_Pin, OUTPUT);
pinMode(Maybom, OUTPUT);
}
void loop() {
lightValue = analogRead(analogInputPin);
valueptm= analogRead(ptm);
analogValue = analogRead(0);
pot_value = analogRead(pot);
delay(10);
if (!client.connected()) {
MQTT_Reconnect();
}
client.loop();
sendData();
delay(8000);
}
void WIFIConnect() {
Serial.println("SSID: Wokwi-GUEST");
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("ket noi duoc");
Serial.println(WiFi.localIP());
}
void MQTT_Reconnect() {
while (!client.connected()) {
if (client.connect(MQTT_ID)) {
Serial.print("MQTT Topic: ");
Serial.print(MQTT_Topic);
Serial.print(" connected");
client.subscribe(MQTT_Topic);
Serial.println("");
} else {
Serial.println("error");
delay(5000);
}
}
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Topic: ");
Serial.println(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(stMessage=="on")
{
digitalWrite(Led_Pin, HIGH);
}
if(stMessage=="onMayBom")
{
digitalWrite(Maybom, HIGH);
}
if(stMessage=="off")
{
digitalWrite(Led_Pin, LOW);
}
if(stMessage=="offMayBom")
{
digitalWrite(Maybom, LOW);
}
}
void sendData()
{
float t =dht.readTemperature();
float h = dht.readHumidity();
//
int value=analogRead(13);
int dat = map(value,0,4098,0,255);
//
int chuyendong=digitalRead(pir);
//
int cambienas = analogRead(ldrPin);
float kqanhsang=anhsang(cambienas);
//
if (isnan(t)||isnan(h)||dat<0||kqanhsang<0)
{
Serial.println("Dữ liệu nan");
}else
{
JsonDocument doc;
//int cd=amdat(30, 90);
//int as=amdat(500, 1000);
doc["temp"] = String(t, 2);
doc["humi"] = String(h, 2);
float phantram=pot_value*100/4095;
//doc["doamdat"] = String(phantram+"%");
doc["doamdat"] = String(phantram) + "%";
doc["chuyendong"] = String(chuyendong);
doc["anhsang"] = String(lightValue);
char jsonString[200];
serializeJson(doc, jsonString);
Serial.print(jsonString);
client.publish("SEND_DATA", jsonString);
}
}
float anhsang(int a) {
const float gama = 0.7;
const float rl10 = 50;
a = map(a, 4095, 0, 1024, 0);
float voltase = a / 1024.*5;
float resistansi = 2000 * voltase / (1-voltase/5);
float lux = pow(rl10*1e3*pow(10,gama)/resistansi,(1/gama));
return lux ;
}
int amdat(int minValue, int maxValue) {
return random(minValue, maxValue);
}
int anhsang(int minValue, int maxValue) {
return random(minValue, maxValue);
}