// Libraries that we used #include #include #include #include #include #include int soilmoisturevalue = 0; // for moisture value readings char soilmoisturearray[16]; // to store the moisture value float humidityvalue = 0; // for humidity value readings char humidityarray[16]; // to store the humidity value float temperaturevalue = 0; // for temperature readings char temperaturearray[16]; // to store the temperature value int PIN = 4; // the relay is connected to pin 4 on the ESP32 Adafruit_SHT31 sht31 = Adafruit_SHT31(); const char* ssid = "******"; // The network's name const char* password = "*******"; // password for the network const char* mqtt_server = "hsrw.space"; // MQTT Broker IP address const char* mqtt_username = "user"; const char* mqtt_password = "mqtt"; const char* myname = "rajshree"; const char* soilmoisture_topic = "/amc2021/rajshree/soil_moisture"; // topic to publish moisture values const char* humidity_topic = "/amc2021/rajshree/humidity"; // topic to publish humidity values const char* temperature_topic = "/amc2021/rajshree/temperature"; // topic to publish temperature values const char* water_pump_topic = "amc2021/rajshree/water_pump"; // topic to turn on water pump based on the soil moisture values void setup_wifi() { delay(10); // connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); //use the defined SSID and password while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); // as long as there is no connection to the Wifi, print one dot with 500ms delay } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); // when the connection is established, print two messages and the IP of the Wifi } void callback(char* topic, byte* payload, unsigned int length) { 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() { Serial.begin(57600); setup_wifi(); client.setServer(mqtt_server, 1883); //MQTT data transmission using port 1883 client.setCallback(callback); delay(1500); // Allow the hardware to sort itself out Serial.println("SHT31 test"); if (! sht31.begin(0x44)) { // 0x44 is the i2c address Serial.println("Couldn't find SHT31"); while (1) delay(1); } pinMode (PIN, OUTPUT); // sets digital pin 4(the relay) as an output client.subscribe (water_pump_topic); // subscribe to water pump topic } void loop() { if (!client.connected()) { reconnect(); } client.loop(); reading_data (); // reading data from the different sensors from MQTT_publishing delay(2000); // delay by 2 sec }