//#include "DHT.h" //library relevant for dht sensor operation commands. #include //command repository for basic sensor communication functions. #include //librarz facilitating communication with SPI devices (eg. RTC). #include #include //library allows sending and receiving MQTT messages. #include //internet connection enabling library. #include //communication with I2C devices. #include "Adafruit_SHT31.h" /* int DHTPIN = 33; //DHT pin to store and take readings at A1.*/ int LEDPIN_1 = 27; //LED pins used to react to light value differences. int LEDPIN_2 = 14; int FAN = 32; //Fan to simulate actuator of fan, reacts to temperature input. char lightarray[16]; // store the light value. char temperaturearray[16]; // store the temperature value. Adafruit_SHT31 sht31 = Adafruit_SHT31(); // WiFi const char* ssid = "iotlab"; // The network's name // CNXK004A2418 const char* password = "iotlab18"; // password for the network // **************** // MQTT Broker const char* mqtt_broker = "broker.hivemq.com"; // //const char* mqtt_server = "hsrw.space"; // MQTT Broker IP address const char *mqtt_username = "emqx"; //const char* mqtt_username = "user"; const char *mqtt_password = "public"; //const char* mqtt_password = "mqtt"; const int mqtt_port = 1883; const char* myname = "Adiel"; const char* topic_up_temperature = "amc2022/groupD/up/Temperature"; const char* topic_up_Light = "amc2022/groupD/up/Light"; const char* topic_up_Fan_Switch = "amc2022/groupD/up/FanSwitch"; const char* topic_up_LEDPIN_1 = "amc2022/groupD/up/LEDPIN_1"; const char* topic_up_LEDPIN_2 = "amc2022/groupD/up/LEDPIN_2"; void initWiFi() { //initiate wifi connections void WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.print("Connecting to WiFi .."); while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(1000); } Serial.println(WiFi.localIP()); } void callback(char* topic, byte* payload, unsigned int length) { //callback setup to ensure functtion doesnt run before task is completed Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); } WiFiClient ethClient; PubSubClient client(ethClient); void reconnect() { //It will reconnect to the server if the connection is lost using a blocking reconnect function while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("arduinoClient", mqtt_username, mqtt_password)) { Serial.println("connected"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void setup() { //basic setting for iniating serial, sensors, setting baud rate. Serial.begin(115200); initWiFi(); Serial.print("RRSI: "); Serial.println(WiFi.RSSI()); //Ensure & check if wifi connection is quality. ESP32 has low range, comparative to other devices. client.setServer(mqtt_broker, 1883); //MQTT data transmission using port 1883 client.setCallback(callback); delay(1500); // Allow the hardware to sort itself out, avoid clashes of commands Serial.println("SHT31 test"); if (! sht31.begin(0x44)) { // 0x44 is the i2c address Serial.println("Couldn't find SHT31"); while (1) delay(1); } /* pinMode (DHTPIN, INPUT); // sets digital pin 33(DHT) as an input. */ pinMode(33, INPUT); //Sets LDR as an input on pin . pinMode(LEDPIN_1, OUTPUT); //Setting LEDs as outputs, since they are reacting to incoming light data. pinMode(LEDPIN_2, OUTPUT); pinMode(FAN, OUTPUT); // sets digital pin 32 (the relay) as an output, to temperature data. client.subscribe (topic_up_temperature); // subscribe to temperature topic client.subscribe (topic_up_Light); // subscribe to temperature topic client.subscribe (topic_up_Fan_Switch); // subscribe to temperature topic client.subscribe (topic_up_LEDPIN_1); // subscribe to temperature topic client.subscribe (topic_up_LEDPIN_2); // subscribe to temperature topic } void reading_data(){ //LDR int light = analogRead(33); //read light value from LDR in Ohms(Physical pin D33) Serial.println(light); if (light > 3500) { Serial.println("Its very dark, extremely overcast or night time"); Serial.println("Both LEDs are ON"); digitalWrite(LEDPIN_1, HIGH); digitalWrite(LEDPIN_2, HIGH); } else if (light > 1100 && light < 3500) { //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("It very bright, lots of natural light"); Serial.println("Both LEDs are OFF"); digitalWrite(LEDPIN_1, LOW); digitalWrite(LEDPIN_2, LOW); } delay(10); //750ms delay between sensor readings. //SHT int temperature = sht31.readTemperature(); // read Temperature in °C Serial.print(temperature); Serial.println(F("°C")); delay (50); if (temperature > 30) { Serial.println("Its becoming hot in here - The fan is on"); digitalWrite(FAN, HIGH); } else { Serial.println("Its cold or tolerable; lets save money!"); digitalWrite(FAN, LOW); } if (! isnan(temperature)) { // check if 'is not a number' Serial.print("Temp *C = "); Serial.print(temperature); Serial.print("\t\t"); } else { Serial.println("Failed to read temperature"); delay (50); } /* if(temperature>=30) { // Turn the fan on: We are a using a Normally closed connection here for the relay (normally open grove-relay). // Normally Closed (NC) configuration, sends HIGH current signals to stop the current flow digitalWrite(FAN, HIGH); Serial.println("Fan is ON"); delay(50); } else { // Turn the fan off // Normally Closed (NC) configuration, sends LOW signals to stop the current flow digitalWrite(FAN, LOW); Serial.println("Fan is OFF"); delay(50); }*/ String temperaturestr = String(temperature);// for MQTT transmission temperaturestr.toCharArray(temperaturearray, temperaturestr.length() + 1); client.publish(topic_up_temperature, temperaturearray);// To publish the topic under Serial.println("Publish temperature"); String lightstr = String(light); // for MQTT transmission lightstr.toCharArray(lightarray, lightstr.length() + 1); client.publish(topic_up_Light, lightarray); // light values are published under the topic light //client.publish(topic_up_LEDPIN_1, lightarray); delay(2000); } void loop() { //MQTT if (!client.connected()) { reconnect(); } client.loop(); reading_data(); // reading data from the different sensors from MQTT_publishing /* delay(2000); // delay by 2 sec */ }