/*
This example sketch shows how a value can be sent from the Arduino to the Cayenne Dashboard at automatic intervals.
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
Steps:
1. Set the Cayenne authentication info to match the authentication info from the Dashboard.
2. Compile and upload the sketch.
3. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
*/
#include "WiFi.h"
#include <DHT.h>
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP32.h>
#include <ESP32Servo.h>
#define DHTPIN 15
#define DHTTYPE DHT22
#define TIMEDHT 1000
#define PIN_TRIG 27
#define PIN_ECHO 14
const int servoPin = 13;
DHT dht(DHTPIN, DHTTYPE);
Servo servo;
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "69996d10-3ce6-11ec-9f5b-45181495093e";
char password[] = "9db4fd71d49b955193d2379ba76bdd68c9925053";
char clientID[] = "cdcc2360-501f-11ee-9ab8-d511caccfe8c";
// Use Virtual Channel 5 for uptime display.
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
int pir = 12; // connect pir in D12
int value = 0;
void setup()
{
Serial.begin(115200);
pinMode(pir, INPUT);
servo.attach(servoPin, 500, 2400);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
//Inicialização do DHT
dht.begin();
//Conexão wifi
WiFi.begin("Wokwi-GUEST", "", 6);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Cayenne.begin(username, password, clientID);
}
void loop()
{
Cayenne.loop();
value = digitalRead(pir);
Serial.println(value);
DHT();
if (value == 1) {
Cayenne.virtualWrite(1, HIGH, TYPE_DIGITAL_SENSOR, UNIT_DIGITAL);
distancia();
}
else {
Cayenne.virtualWrite(1, LOW, TYPE_DIGITAL_SENSOR, UNIT_DIGITAL);
}
delay(1000);
}
CAYENNE_IN(4) {
servo.write(getValue.asInt());
}
void DHT() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again)
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
} else {
Cayenne.virtualWrite(2, t, TYPE_TEMPERATURE, UNIT_CELSIUS);
Cayenne.virtualWrite(3, h, TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT);
}
// Where: HIGH = Temperature, LOW = Humidity
Serial.print(F("Temperature: "));
Serial.println(t);
Serial.print(F("% Humidity: "));
Serial.println(h);
}
void distancia(){
// Inicia uma nova medição:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Leia o resultado:
int duration = pulseIn(PIN_ECHO, HIGH);
Serial.print("Distância em CM: ");
Serial.println(duration / 58);
Serial.print("Distância em polegadas: ");
Serial.println(duration / 148);
}