Group Project IoT (CSM3313): Water Temperature Sensor
by s62161 in Circuits > Microcontrollers
326 Views, 4 Favorites, 0 Comments
Group Project IoT (CSM3313): Water Temperature Sensor
Group 10 Members
- Azamuddin bin Mansor (S63718)
- Nur Zulkaedah binti Mahyuddin (S62609)
- Nur Azmina binti Masri (S62161)
Objective:
- To design and develop an IoT system aimed at monitoring water temperature at the "Stesen Penyelidikan Alami Kenyir UMT, Tasik Kenyir."
- To deploy an water temperature sensor at the "House Boat, Stesen Penyelidikan Alami Kenyir UMT, Tasik Kenyir."
- To collect real-time data on water temperature, enabling remote monitoring and analysis.
Scope:
Research and Planning:
- Conduct a thorough analysis of the environment and the specific needs for water temperature monitoring at Kenyir Lake.
- Explore existing solutions and technologies relevant to water temperature sensors and IoT systems for environmental monitoring.
- Plan the deployment location for optimal data collection and devise strategies for ensuring accurate and reliable data transmission.
IoT Device Development:
- Select and integrate a high-precision water temperature sensor suitable for the aquatic environment at Kenyir Lake.
- Design and assemble the IoT device incorporating the chosen sensor, ensuring its functionality and durability in varying water conditions.
- Implement power management solutions to enable the device to operate autonomously for at least one week.
- Configure the device to use MQTT protocol for data transmission to and from the HiveMQ broker.
- Program the microcontroller to control an output (LED) based on subscribed MQTT data, providing a visual indication.
Dashboard and User Interface Development:
- Enable MQTT protocol integration within the dashboard to subscribe and publish data to the HiveMQ broker.
- Implement interactive visualizations for users to interpret and analyze the collected water temperature data effectively.
- Ensure the dashboard allows users to send commands to the IoT device, controlling the LED output for testing or demonstrative purposes.
Deployment and Testing:
- Physically deploy the IoT device containing the water temperature sensor at the designated location in Kenyir Lake.
- Conduct rigorous testing of the entire system, verifying sensor accuracy, data transmission reliability, and dashboard functionality.
- Monitor and record the system's performance over a week-long period, ensuring consistent and accurate data collection.
- Document the deployment process, highlighting challenges faced and solutions implemented to ensure successful implementation.
Supplies
Circuit Supplies
- 1 x Temperature sensor (https://shp.ee/m52de0u)
- 1 x Half Breadboard
- 1 x ESP32 Microcontroller
- 1 x LED
- 1 x Resistor
- 1 x Male to male wire (jumper wire)
- 4 x Male to Female wire (jumper wire)
- 1 x Powerbank (power source)
- 1 x USB Micro Cable
Container Box supplies
- Used polystyrene box
- Plastic container
- Tapes
- Cutter
- Cable Tight
- Hot glue gun
Prepare the Circuit
- The first image shows a visual 2D design for the circuit of water temperature sensor.
- The second and third images shows the 3D circuit that has been done .
- You can see the step-by-step instructions on how to build the circuit on the video above.
Coding
Explanation of continuous coding step by step:
// include the necessary libraries
#include <WiFi.h>
#include <MQTT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <MQTTClient.h>
1. #include statements: These lines include several libraries necessary for the DS18B20 sensor includes OneWire, DallasTemperature, WiFi, MQTT, and MQTTClient. These libraries should be installed in Arduino IDE Libraries to enable communication and temperature sensing.
// define the pin where the temperature sensor is connected
const int SENSOR_PIN = 4;
// define the pin where the LED is connected
int led1_pin = 2;
// initialize the OneWire library to communicate with the temperature sensor
OneWire oneWire(SENSOR_PIN);
// initialize the DallasTemperature library
DallasTemperature tempSensor(&oneWire);
// variable to store the temperature value in Celsius
float tempCelsius;
2. const int SENSOR_PIN = 4 and int led1_pin = 2: This line defines the pin number on ESP32, (GPIO 18) and (GPIO 14) to which the temperature sensor and LED are connected.
3. OneWire oneWire(SENSOR_PIN): This line creates a OneWire object called oneWire and associates it with the temperature sensor's pin.
4. DallasTemperature tempSensor(&oneWire): This line creates a DallasTemperature object called tempSensor and links it to the oneWire object to communicate with the DS18B20 temperature sensor.
5. float tempCelsius: This line declares a variable to store the temperature in Celsius.
// define the Wi-Fi and MQTT parameters
#define WIFI_SSID "FSKMPocketWiFi"
#define WIFI_PASSWORD "88888888"
#define MQTT_HOST "broker.hivemq.com"
#define MQTT_PREFIX_TOPIC "csm3313_umt/group10"
#define MQTT_PUBLISH_TOPIC1 "/watertemp"
#define MQTT_SUBSCRIBE_TOPIC1 "/led01"
6. #define statements: These lines define constants for WiFi credentials, MQTT configuration, and topics used for publishing and subscribe the data read by the sensor.
// create a Wi-Fi client object and an MQTT client object
WiFiClient net;
MQTTClient mqtt(1024);
// variable to store the time of the last MQTT message publication
unsigned long lastMillis = 0;
7. WiFiClient net: This line creates a WiFiClient object named net, which is used to establish a connection ESP32 to the Wi-Fi network.
8. MQTTClient mqtt(1024): This line creates an MQTTClient object named mqtt with a buffer size of 1024 bytes for MQTT communication.
9. unsigned long lastMillis = 0: This line initializes a variable to store the last time a temperature reading was published.
// function to connect to the Wi-Fi network
void connectToWiFi() {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
10. connectToWiFi(): This function establishes a connection to the specified Wi-Fi network using the provided WIFI_SSID and WIFI_PASSWORD.
// function to handle incoming MQTT messages
void messageReceived(String topic, String payload) {
if (topic == (String(MQTT_PREFIX_TOPIC) + String(MQTT_SUBSCRIBE_TOPIC1))) {
if (payload == "1") {
digitalWrite(led1_pin, HIGH);
} else if (payload == "0") {
digitalWrite(led1_pin, LOW);
}
}
}
11. messageReceived(String topic, String payload): This function is called whenever a message is received from the MQTT broker. It checks the topic and payload to control an LED (LED01) based on the received message.
// function to connect to the MQTT broker
void connectToMqttBroker() {
mqtt.begin(MQTT_HOST, net);
mqtt.onMessage(messageReceived);
String uniqueString = String(WIFI_SSID) + "-" + String(random(1, 98)) + String(random(99, 999));
char uniqueClientID[uniqueString.length() + 1];
uniqueString.toCharArray(uniqueClientID, uniqueString.length() + 1);
while (!mqtt.connect(uniqueClientID)) {
delay(500);
}
mqtt.subscribe(String(MQTT_PREFIX_TOPIC) + String(MQTT_SUBSCRIBE_TOPIC1));
}
12. connectToMqttBroker(): This function connects to the MQTT broker (broker.hivemq.com) and subscribes to a specific MQTT topic. It generates a unique client ID based on the SSID and a random number to avoid conflicts with other MQTT clients
// setup function to initialize the program
void setup(void) {
Serial.begin(115200);
pinMode(led1_pin, OUTPUT);
digitalWrite(led1_pin, LOW);
connectToWiFi();
connectToMqttBroker();
}
13. Serial.begin(115200): initializes serial communication with a baud rate of 115200. This is often used for debugging and monitoring the program's output.
14. connectToWiFi() and connectToMqttBroker(): This is the function calls to set up and establish connections to WiFi and an MQTT broker, respectively.
// loop function to run the program
void loop() {
mqtt.loop();
delay(10);
if (WiFi.status() != WL_CONNECTED) {
connectToWiFi();
}
if (!mqtt.connected()) {
connectToMqttBroker();
}
if (millis() - lastMillis > 900000) {
lastMillis = millis();
tempSensor.requestTemperatures();
tempCelsius = tempSensor.getTempCByIndex(0);
mqtt.publish(String(MQTT_PREFIX_TOPIC) + String(MQTT_PUBLISH_TOPIC1), String(tempCelsius));
}
}
15. mqtt.loop(): is called to allow the MQTT client to handle incoming message.
16. delay(10): introduces a short delay to prevent excessive loop iterations.
17. The subsequent 'if' statement: check the status of WiFi and MQTT connection. If not connected, the program attempts to reconnect.
18. The final 'if' statement checks whether 15 minutes (900,000 milliseconds) have passed since the last temperature reading was published. If so, it request a temperature reading from the sensor and published it to the MQTT topic.
Set Up a Node-RED Dashboard to Subscribe to a Broker for Sensor Data
1. Configure the MQTT In Node Red: (please refer on figure 2 above)
Drag an MQTT In node onto the flow canvas. Double-click the MQTT In node to open its configuration dialog. In the configuration dialog, establish a connection to the MQTT broker. Enter the details for the server and topic under which the water temperature data is being published. Rename this node "Water Temperature" for clarity and Click "Done" to save the MQTT In node configuration.
2. Set Up the Debug Node: (please refer on figure 3 above)
To help with debugging and verifying incoming data, drag a Debug node onto the flow canvas from the palette. Connect the output of the MQTT In node to the input of the Debug node. Double-click the Debug node to configure it. In the dialog, ensure that it is set to display the entire message payload. Click "Done" to save the Debug node configuration.
3. Create a Dashboard Gauge: (please refer on figure 4 above)
To visualize the water temperature data on the Node-RED dashboard, add a Gauge node from the dashboard category on the left. Connect the output of the MQTT In node to the input of the Gauge node. Configure the Gauge node by double-clicking it. Set it up to represent the temperature data appropriately. Create a new dashboard group or choose the existing group. Define the range of values for temperature sensor and sectors, then label the Gauge node as "Water Temperature" to make its purpose clear. Click "Done" to save the Gauge node configuration.
4. Add a Dashboard Text Display: (please refer on figure 5 above)
To display the temperature data as text on the dashboard, add a Text node from the dashboard category onto the flow canvas. Connect the output of the MQTT In node to the input of the Text node. Configure the Text node by double-clicking it. Set it up to display the temperature data as follows and click "Done" to save the Text node configuration.
5. Implement a Chart Node: (please refer on figure 6 above)
To create a time series graph of the temperature data, use a Chart node from the dashboard category. Connect the output of the MQTT In node to the chart node and configure the node as follows in figure 6.
Set Up a Node-RED Dashboard to Publish Data to a Broker for LED Control
1. Add a Switch Node: (please refer on figure 2 above)
Drag a switch node onto the Node-RED canvas. This will be the control for sending LED commands. Configure the node by setting its properties such as group, label, topic, and payload to correspond with the LED state to control as follows in the figure 2.
2. Insert a Function Node: (please refer on figure 3 above)
Place a Function Node on the canvas to define the logic for the LED control message. Double-click the Function Node to open its configuration and write the JavaScript code needed to process the incoming message from the dashboard node as shown in the image. This code will format the message payload as required by the LED control mechanism. Connect the output of the Switch Node to the input of this Function Node.
3. Set Up the MQTT Publish Node: (please refer on figure 4 above)
Add an MQTT Out Node that will handle publishing messages to the MQTT broker. Open the configuration for the MQTT Out Node and specify the MQTT server details along with the topic that the LED is subscribed to, such as "csm3313_umt/group10/led01". Ensure that the MQTT Out Node is correctly connected to the broker by checking for a "connected" status once configured. Link the output of the Function Node to the input of the MQTT Out Node.
Deploy the Node-RED Flow
Once all nodes are configured and connected, click the "Deploy" button in the top right corner of the Node-RED interface to activate your flow.
View the Dashboard of the Node Red That Had Been Deploy
Access the Node-RED dashboard by clicking the dashboard tab in the Node-RED interface or by navigating to the dashboard URL.
~ For your reference, I have broken down the Node Red application development process into steps 3 through 6 above so that the contents may be read more easily with the accompanying visuals diagram.
~ or as alternative, you can view the step-by-step in a single document by downloading the PDF below:
Downloads
Prepare the Container Box for the Circuit
We have made discussion on how to make sure the circuit for this IoT system project will be secure and not easy to damage in the water. Therefore, we used two type of container which is: -
1) Plastic Container (please refer to image 2, and 3)
- We modified the plastic container by making a hole to make sure the sensor can get out through the plastic container and will be able to read the water temperature.
- Then, we arrange the complete circuit that has been connected to the adapter with power bank in the plastic container.
2) Polystyrene box (please refer to image 1 and 4 and the video above)
- We have combined with Group 1 to use the same polystyrene box.
- Based on the video, you can see how we organized the circuit that has been put in plastic container into the polystyrene box in order to make sure our project will survive and secure for one week period in the water at the "House Boat, Stesen Penyelidikan Alami Kenyir UMT, Tasik Kenyir".
Water Temperature Sensor Deployment's Venue
We have decided to deploy our IoT Group's Project which is Water Temperature sensor near the Houseboat together with the Group 1's Project.
- The video shows how we deploy our project which is water temperature sensor.
- The image shows the project that has done been deployed.
Data Analysis
Explanation:
The line chart above shows the average of water temperature that has been deploy at "Houseboat, Stesen Penyelidikan Alami Kenyir UMT, Tasik Kenyir" over a one-week period which is from 6 January 2024 until 12 January 2024. The temperature readings were taken at regular intervals throughout the day and night.
The chart shows that the water temperature fluctuated throughout the week, with the highest temperature of 28.56 degrees Celsius recorded on 11 January 2024, at 7:54 PM. The lowest temperature of 24.25 degrees Celsius was recorded on 6 January 2024, at 9:09 AM.
Overall, the water temperature sensor that has been deployed remained relatively stable over the one-week period, with an average temperature of around 27.48 degrees Celsius.
Discussion from the gained data of Water Temperature:
- The temperature readings tend to be higher at night and lower during the day. This is likely due to the fact that the water absorbs heat from the sun during the day and releases it back into the atmosphere at night.
- There is a slight upward trend in the temperature readings over the course of the week. This could be due to a number of factors, such as changes in the weather or an increase in the water depth.