/* Combined parts of the code for both sensors and their target devices. Cooling(Fan) & lighting components (No MQTT yet). */ #include #include #include "Adafruit_SHT31.h" #include bool enableHeater = false; uint8_t loopCnt = 0; Adafruit_SHT31 sht31 = Adafruit_SHT31(); int light = 15; //LDR readings taken and stored at pin A0. int LEDPIN_1 = 27; //LED pins used to react to light value differences. int LEDPIN_2 = 14; int FAN = 26; //Fan to simulate actuator heat pump, reacts to temperature input. #define relay 4 void setup() { Serial.begin(9600); //does this start both sensors or just one? Serial.print(F("DHT11 Test")); pinMode(relay,OUTPUT); if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr Serial.println("Couldn't find SHT31"); while (1) delay(1); } Serial.print("Heater Enabled State: "); if (sht31.isHeaterEnabled()) Serial.println("ENABLED"); else Serial.println("DISABLED"); pinMode(15, INPUT); pinMode(LEDPIN_1, OUTPUT); //Setting LEDs as outputs, since they are reacting to incoming light data pinMode(LEDPIN_2, OUTPUT); } void loop() { //DHT float temp = sht31.readTemperature(); float h = sht31.readHumidity(); if (! isnan(temp)) { // check if 'is not a number' Serial.print("Temp *C = "); Serial.print(temp); Serial.print("\t\t"); } else { Serial.println("Failed to read temperature"); } if(temp>=30) // Turn the fan on: We are a using a Normally closed connection here for the relay { // Normally Closed (NC) configuration, sends HIGH current signals to stop the current flow digitalWrite(relay, HIGH); Serial.println("Fan is ON"); delay(5000); } else // Turn the fan off { // Normally Closed (NC) configuration, sends LOW signals to stop the current flow digitalWrite(relay, LOW); Serial.println("Fan is OFF"); delay(5000); } } //LDR int light = analogRead(15); //read light value from LDR in Ohms EVALUATE SHOUlD LIGHT NOT 15 Serial.println(light); String lightString = String(light); Serial.println("The string equivalent to integer light is " + lightString + "°C"); if (light < 1300) { Serial.println("It very bright, lots of natural light"); Serial.println("Both LEDs are off"); digitalWrite(LEDPIN_1, LOW); digitalWrite(LEDPIN_2, LOW); } else if (light > 1300 && light < 2500) { //medium amount of light to show one LED at a time). Serial.println("moderate light quantity, quite cloudy"); digitalWrite(LEDPIN_1, HIGH); digitalWrite(LEDPIN_2, LOW); } else { // both LEDs turn on when the surroundings are dark. Serial.println("Its very dark, extremely overcast or nighttime"); digitalWrite(LEDPIN_1, HIGH); digitalWrite(LEDPIN_2, HIGH); } delay(750); //750ms delay between sensor readings. char* lightArr = &lightString[0]; //client.publish needs matching format of topic folder and light value //Serial.println(lightArr); // Serial.print (" this is the array"); }