Hi Godzilla,
Here is the code I am using. I thank “Mrstegeman” for helping me with this when I had problems.
Code:
#define LARGE_JSON_BUFFERS 1
#include <Arduino.h>
#include “Thing.h”
#include “WebThingAdapter.h”
#include “DHT.h”
const char* ssid = “";
const char password = "**”;
// minimal change from last sensor reading to
// update our values
const double threshold = 0.1;
const int Rele1 = 2;
const int Rele2 = 4;
// Pin to connect DHT sensor’s data wire (yellow)
const int sensorPin = 14;
DHT dht;
int samplingPeriod = dht.getMinimumSamplingPeriod();
double lastTemperatureValue = 0;
WebThingAdapter *adapter = NULL;
const char* sensorDeviceTypes[] = {“MultiLevelSensor”, “Sensor”, nullptr};
ThingDevice sensor(“device:esp32:DHTSensor”, “Temperatura Comedor”, sensorDeviceTypes);
ThingProperty temperature(“temperature”, “Input pin”, NUMBER, “LevelProperty”);
const char *switchDeviceTypes[] = {“OnOffSwitch”, nullptr};
ThingDevice switch1(“device:esp32:Rele1”, “Rele1”, switchDeviceTypes);
ThingProperty rele1On(“Rele1On”, “Rele1”, BOOLEAN, “OnOffProperty”);
ThingDevice switch2(“device:esp32:Rele2”, “Rele2”, switchDeviceTypes);
ThingProperty rele2On(“Rele2On”, “Rele2”, BOOLEAN, “OnOffProperty”);
void setup(void) {
pinMode(Rele1, OUTPUT);
pinMode(Rele2, OUTPUT);
digitalWrite(Rele1, LOW);
digitalWrite(Rele2, LOW);
Serial.begin(115200);
//sensor type
dht.setup(sensorPin);
Serial.print("Sensor model: ");
Serial.println(dht.getModel());
// Connect to WiFi access point
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(”.”);
}
// Show connection details
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: “);
Serial.println(WiFi.localIP());
Serial.println(” ");
// create adapter instance
adapter = new WebThingAdapter(“esp32”, WiFi.localIP());
// set up devices and properties
temperature.title = “Temperatura”;
temperature.minimum = dht.getLowerBoundTemperature();
temperature.maximum = dht.getUpperBoundTemperature();
temperature.multipleOf = dht.getNumberOfDecimalsTemperature() == 1 ? 0.1 : 1;
temperature.unit = “degree celsius”;
temperature.readOnly = true;
sensor.addProperty(&temperature);
adapter->addDevice(&sensor);
rele1On.title = “Rele1 On”;
switch1.addProperty(&rele1On);
adapter->addDevice(&switch1);
rele2On.title = “Rele2 On”;
switch2.addProperty(&rele2On);
adapter->addDevice(&switch2);
// Start HTTP server
adapter->begin();
Serial.println(“HTTP server started”);
Serial.print(“http://”);
Serial.print(WiFi.localIP());
}
void loop(void) {
delay(samplingPeriod);
// check relay values
bool on1 = rele1On.getValue().boolean;
digitalWrite(Rele1, on1 ? HIGH : LOW);
bool on2 = rele2On.getValue().boolean;
digitalWrite(Rele2, on2 ? HIGH : LOW);
// update temperature value
float temperatureVal = dht.getTemperature();
double temperatureValDouble = (double) temperatureVal;
if (abs(temperatureValDouble - lastTemperatureValue) >= threshold) {
Serial.print("log: Temperature: ");
Serial.print(temperatureValDouble);
Serial.println(“°C”);
ThingPropertyValue temperatureLevelValue;
temperatureLevelValue.number = temperatureValDouble;
temperature.setValue(temperatureLevelValue);
lastTemperatureValue = temperatureValDouble;
}
adapter->update();
}
I hope you can get an idea of how it is written and that it works for you.
I await your answer if it worked for you.