Good morning, I have a device that I have recently developed that has an ESP32 on the board and I want to control two relays and a temperature sensor. Separately the codes work and transmit perfectly but I cannot get the two codes together in one code and it works .
I leave attached the codes separately.
Relay code:
#include <Arduino.h>
#include “Thing.h”
#include “WebThingAdapter.h”
// TODO: Hardcode your wifi credentials here (and keep it private)
const char *ssid = “Cr@ck3r”;
const char *password = “Frumusete159753”;
#if defined(LED_BUILTIN)
const int ledPin = LED_BUILTIN;
#else
const int ledPin = 4; // manually configure LED pin
#endif
WebThingAdapter *adapter;
const char *ledTypes[] = {“OnOffSwitch”, “Light”, nullptr};
ThingDevice led(“led”, “Built-in LED”, ledTypes);
ThingProperty ledOn(“on”, “”, BOOLEAN, “OnOffProperty”);
bool lastOn = false;
void setup(void) {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(115200);
Serial.println("");
Serial.print(“Connecting to “”);
Serial.print(ssid);
Serial.println(”"");
#if defined(ESP8266) || defined(ESP32)
WiFi.mode(WIFI_STA);
#endif
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
bool blink = true;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
digitalWrite(ledPin, blink ? LOW : HIGH); // active low led
blink = !blink;
}
digitalWrite(ledPin, LOW); // active low led
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
adapter = new WebThingAdapter(“w25”, WiFi.localIP());
led.addProperty(&ledOn);
adapter->addDevice(&led);
adapter->begin();
Serial.println(“HTTP server started”);
Serial.print(“http://”);
Serial.print(WiFi.localIP());
Serial.print("/things/");
Serial.println(led.id);
}
void loop(void) {
adapter->update();
bool on = ledOn.getValue().boolean;
digitalWrite(ledPin, on ? LOW : HIGH); // active low led
if (on != lastOn) {
Serial.print(led.id);
Serial.print(": ");
Serial.println(on);
}
lastOn = on;
}
Temperature sensor code:
#include “Thing.h”
#include “WebThingAdapter.h”
#include “DHT.h”
const char* ssid = “Cr@ck3r”;
const char* password = “Frumusete159753”;
// minimal change from last sensor reading to
// update our values
const double threshold = 0.1;
WebThingAdapter* adapter;
// MultiLevelSensor and Sensor are mandatory for this type of application.
// See https://iot.mozilla.org/schemas/
const char* deviceTypes[] = {“MultiLevelSensor”, “Sensor”, nullptr};
// Sets up the device info. First two values are free-form.
ThingDevice device(“DHTSensor”, “Temperatura Comedor”, deviceTypes);
// Sets up device properties. Not sure about the second value here. Probably doesn’t make sense.
// Third and fourth argument are not freeform.
ThingProperty temperature(“temperature”, “Input pin”, NUMBER, “LevelProperty”);
// Pin to connect DHT sensor’s data wire (yellow)
const int sensorPin = 4;
DHT dht;
int samplingPeriod = dht.getMinimumSamplingPeriod();
double lastTemperatureValue = 0;
void setup(void) {
Serial.begin(115200);
Serial.println("");
dht.setup(sensorPin);
Serial.print("Sensor model: ");
Serial.println(dht.getModel());
Serial.print(“Connecting to “”);
Serial.print(ssid);
Serial.println(”"");
#if defined(ESP8266) || defined(ESP32)
WiFi.mode(WIFI_STA);
#endif
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
bool blink = true;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Here ‘Comedor’ becomes part of the hostname => Comedor.local
// so make sure it’s unique in your network.
adapter = new WebThingAdapter(“Comedor”, WiFi.localIP());
device.addProperty(&temperature);
adapter->addDevice(&device);
Serial.println(“Starting HTTP server”);
adapter->begin();
Serial.print(“http://”);
Serial.print(WiFi.localIP());
Serial.print("/things/");
Serial.println(device.id);
}
// The loop function makes sure we get up-to-date values.
void loop(void) {
delay(samplingPeriod);
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 have tried it in various ways but there is no way I can get to control the two relays separately and display the temperature on the same device. Besides, on the platform it only shows me 1 On / Off device or temperature, but not both at the same time.
Can someone help me please, because I don’t want to use 1 device for relays and 1 for temperature.
Thank you so much everyone.