Hi there,
I am trying to control a heater and a humidifier using a couple solid state relays and a DHT22.
I need some assistance with the "read_write" section of the code. I am trying to turn on the relay when the humidity (DHT22) reads less than the new number that is entered through the dashboard (in the onChange function). I am struggling with how to write the if statement in this section based on the new value that is entered. I want it to turn on/off based on the DHT reading, but also when a new value is entered through the cloud.
Thank you so much! (some of this code is irrelevant because of the scale, but I figured everyone would want the full code).
Below is my code:
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled 3"
https://create.arduino.cc/cloud/things/6088350c-964d-4523-af55-120d2ea9d56b
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float weightCloudVariable;
CloudRelativeHumidity humidity;
bool relay;
CloudTemperature temperature;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include <DHT.h>
#include <DHT_U.h>
//for ssr
#define relayPin 7
//declare DHT22
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN,DHTTYPE);
//Declare softwareSerial
auto & scaleSerial = Serial1;
//Timing Variables
float previousWeight = 0;
float weightThreshold = 0.001;
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
//Initialize software serial
dht.begin();
//initialize software serial
scaleSerial.begin(9600);
//pin mode for ssr
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
humidity = dht.readHumidity();
Serial.print(" Humidity: ");
Serial.println(humidity);
temperature = dht.readTemperature();
Serial.print("Temperature: ");
Serial.print(temperature);
delay(9600);
// onRelayChange();
onHumidityChange();
if (onHumidityChange()<humidity))
{
digitalWrite(relayPin, HIGH);
}
else {
digitalWrite(relayPin, LOW);
}
// Scale Code
//Get the current time
unsigned long currentMillis = millis();
// Check if it's time to send data
if (currentMillis - previousMillis >= interval) {
// Save the current time as the last time we sent data
previousMillis = currentMillis;
// Read and process the scale data
if (scaleSerial.available()) {
String weightData = "";
// Read data from the scale
while (scaleSerial.available()) {
char c = scaleSerial.read();
weightData += c;
}
float currentWeight = extractWeight(weightData);
if(abs(currentWeight - previousWeight) > weightThreshold){
previousWeight = currentWeight;
Serial.println(currentWeight);
Serial.println(weightData);
}
// Print the full raw data from the scale (for debugging purposes)
Serial.println("Raw Data: " + weightData);
// Extract only the numeric part of the data (the weight)
float weightValue = extractWeight(weightData);
// Print the extracted weight value
Serial.println("Weight Value: " + String(weightValue));
//Send the extracted weight value to the cloud
weightCloudVariable = currentWeight;
}
}
}
float extractWeight(String data){
//data.trim();
String numericPart = "";
for (int i = 0; i < data.length(); i++) {
if (isDigit(data[i]) || data[i] == '.'){
numericPart += data[i];
}
}
return numericPart.toFloat();
}
/*
Since Relay is READ_WRITE variable, onRelayChange() is
executed every time a new value is received from IoT Cloud.
*/
void onRelayChange() {
//if(relay){
//digitalWrite(relayPin, HIGH);
//}
//else {
//digitalWrite(relayPin, LOW);
}
/*
Since Humidity is READ_WRITE variable, onHumidityChange() is
executed every time a new value is received from IoT Cloud.
*/
void onHumidityChange() {
if ???
}
/*
Since Temperature is READ_WRITE variable, onTemperatureChange() is
executed every time a new value is received from IoT Cloud.
*/
void onTemperatureChange() {
// Add your code here to act upon Temperature change
}
/*
Since WeightCloudVariable is READ_WRITE variable, onWeightCloudVariableChange() is
executed every time a new value is received from IoT Cloud.
*/