Nano 33 IoT cloud climate controller w 8 relays

Hey all,

Im taking a whack at using the IoT cloud and wanted to setup my latest project which is basically an Arduino Nano 33 IoT with a ELEGOO 8-Channel relay board. I have been chiseling away at the code and am about to hook up the sensors and wanted to see if any of the code masters had any feedback or suggestions on the layout/code.

I am going to be using 2 DHT22 temperature and humidity sensors so that adds a little more fun I am trying to read all 4 measurements from the two sensors and convert the temp to degrees Fahrenheit . I started those up with this at the top:

#define DHTPIN1 21
#define DHTPIN2 20

DHT dht[] = {
{DHTPIN1, DHT22},
{DHTPIN2, DHT22},

Then later in the code this:

//does something for sensors???
for (auto& sensor : dht) {
sensor.begin();

Not sure if it is being initialized correctly...

Long story short.. as I progress.. I want to be able to create at least two thermostat type conditions for temp1 and temp2 (via relay1 and relay2) and also be able to control humidity for "hum1" (relay3).. Hopefully building more on there as I go...

Any suggestions or tips? Im not looking for highly sophisticated control... like PID... just ON/OFF for now... in the future it may be cool to ADD a PID style control for one of the heater relays...

Here is a reference code I used for a dual DHT22 Sensor configuration.

/* 
  Sketch generated by the Arduino IoT Cloud Thing

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing
 
  int t_temp2;
  int t_temp1;
  int t_hum1;
  float temp1;
  float temp2;
  float hum1;
  float hum2;
  bool redLED;
  bool relay4;
  bool relay5;
  bool relay3;
  bool relay6;
  bool relay8;
  bool relay1;
  bool relay2;
  bool relay7;
 

  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"
// DHT sensor library - Version: 1.4.3
#include <DHT.h>
#include <DHT_U.h>
//Sensor 1 and 2
#define DHTPIN1 21
#define DHTPIN2 20

DHT dht[] = {
  {DHTPIN1, DHT22},
  {DHTPIN2, DHT22},
};

float hum[2];
float temp[2];
   //Fahrenheit conversion variables
float calc[2];

int myLED = 13;

int relayArray[] = {2, 3, 4, 5, 6, 7, 8, 9};
int temp1TOL= 5; // hysteresis or tolerance for temp1
int temp2TOL = 5; // hysteresis or tolerance for temp1

void setup() {
  //Set relay array as outputs and state
  for (int ir=0;ir<8;ir++) {
  pinMode(relayArray[ir], OUTPUT);
  digitalWrite(relayArray[ir], HIGH);
}
  //does something for sensors???
  for (auto& sensor : dht) {
    sensor.begin();
  }
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Iot generated VARiables as defined in thingProperties.h
  initProperties();

 //CLOUD GENERATED: 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();
  // Your code here 
  
  delay(2000); 
  
   for (int i = 0; i < 2; i++) {
    temp[i] = dht[i].readTemperature();
    hum[i] = dht[i].readHumidity();
  }
  
// convert the temp to deg fahrenheit
 for (int ic = 0; ic < 2; ic++) {
  //T(°F) = T(°C) × 9/5 + 32
  calc[ic] = (temp[ic] * 1.8 ) + 32;
 }
 //condition for relay1 on
 if (calc1 >= t_temp1 + temp1TOL){
   relay1 = 0;
 }else if(calc1 >= t_temp1){
   relay1 = 0;
 }else{
   relay1 = 1;
 }
 //condition for relay2 on
  if (calc2 >= t_temp2 + temp2TOL){
   relay2 = 0;
 }else if(calc2 >= t_temp2){
   relay2 = 0;
 }else{
   relay2 = 1;
 }
 
  
  Serial.print("Temp1 Setpoint = ");
  Serial.print(t_temp1);
  //Print degree symbol
  Serial.write(176); 
  Serial.println("F");
  Serial.print("Temp1 = ");
  Serial.print(calc[0]);
  //Print degree symbol
  Serial.write(176); 
  Serial.println("F");
  
  Serial.print("Temp2 Setpoint = ");
  Serial.print(t_temp2);
  //Print degree symbol
  Serial.write(176); 
  Serial.println("F");
  Serial.print("Temp2 = ");
  Serial.print(calc[1]);
  //Print degree symbol
  Serial.write(176); 
  Serial.println("F");

}

  
/*
  Since Port1 is READ_WRITE variable, onPort1Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onPort1Change()  {
  // Add your code here to act upon Port1 change
}
/*
  Since RedLED is READ_WRITE variable, onRedLEDChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onRedLEDChange()  {
  // Add your code here to act upon RedLED change
  Serial.println(redLED);
  
  if(redLED){
    digitalWrite(myLED,HIGH);
  }else{
     digitalWrite(myLED,LOW);
 }
}

/*
  Since Relay1 is READ_WRITE variable, onRelay1Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay1Change()  {
  // Add your code here to act upon Relay1 change
  if(relay1){
  digitalWrite(relayArray[0],HIGH);
 }else{
  digitalWrite(relayArray[0],LOW);
  }
}

/*
  Since Relay2 is READ_WRITE variable, onRelay2Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay2Change()  {
  // Add your code here to act upon Relay2 change
  if(relay2){
  digitalWrite(relayArray[1],HIGH);
 }else{
  digitalWrite(relayArray[1],LOW);
  }
}

/*
  Since Relay3 is READ_WRITE variable, onRelay3Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay3Change()  {
  // Add your code here to act upon Relay3 change
  if(relay3){
  digitalWrite(relayArray[2],HIGH);
 }else{
  digitalWrite(relayArray[2],LOW);
  }
}

/*
  Since Relay4 is READ_WRITE variable, onRelay4Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay4Change()  {
  // Add your code here to act upon Relay4 change
  if(relay4){
  digitalWrite(relayArray[3],HIGH);
 }else{
  digitalWrite(relayArray[3],LOW);
  }
}

/*
  Since Relay5 is READ_WRITE variable, onRelay5Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay5Change()  {
  // Add your code here to act upon Relay5 change
  if(relay5){
  digitalWrite(relayArray[4],HIGH);
 }else{
  digitalWrite(relayArray[4],LOW);
  }
}

/*
  Since Relay6 is READ_WRITE variable, onRelay6Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay6Change()  {
  // Add your code here to act upon Relay6X change
  if(relay6){
  digitalWrite(relayArray[5],HIGH);
 }else{
  digitalWrite(relayArray[5],LOW);
  }
}

/*
  Since Relay7 is READ_WRITE variable, onRelay7Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay7Change()  {
  // Add your code here to act upon Relay7 change
  if(relay7){
  digitalWrite(relayArray[6],HIGH);
 }else{
  digitalWrite(relayArray[6],LOW);
  }
}

/*
  Since Relay8 is READ_WRITE variable, onRelay8Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onRelay8Change()  {
  // Add your code here to act upon Relay8 change
  if(relay8){
  digitalWrite(relayArray[7],HIGH);
 }else{
  digitalWrite(relayArray[7],LOW);
  }
}


void onTTemp2Change()  {

  
  
}
void onTTemp1Change()  {

  
  
}
void onTHumChange()  {

  
  
}











@revsol, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

1 Like

What you want to do is possible. From what I can find on your ELEGOO 8-Channel relay board (there are several with that description). From what I can see you will need an external 5V power supply for the relays and possibly a level translation. For the conversion I do it the easy way: float TempF = 0; TempF = (TempC * 1.8 ) +32; That should help getting you on your way. Posting links to technical information on hardware items always helps.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.