In this tutorial, we'll create a digital thermometer using NodeMCU and the LM35 temperature sensor. The NodeMCU will host a web server to display real-time temperature readings on a web browser. This project is a great introduction to IoT (Internet of Things) applications.
Components RequiredIntroduction to LM35 Temperature SensorThe LM35 is an analog linear temperature sensor that outputs a voltage proportional to the temperature in Celsius. It can operate from both 5V and 3.3V power supplies, making it suitable for use with the NodeMCU.
Circuit DiagramConnect the components as shown below:
- Connect the LM35's output to Analog Pin A0 on the NodeMCU.
- Use 3.3V from the NodeMCU to power the LM35.
- Ground the GND pin of the LM35.
#include <ESP8266WiFi.h>
const char *ssid = "your_wifi_ssid"; // Replace with your WiFi SSID
const char *password = "your_wifi_password"; // Replace with your WiFi password
float temp_celsius = 0;
float temp_fahrenheit = 0;
WiFiServer server(80);
void setup() {
Serial.begin(115200);
pinMode(A0, INPUT);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi is connected");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
}
void loop() {
temp_celsius = (analogRead(A0) * 330.0) / 1023.0; // Convert analog values to Celsius
temp_fahrenheit = temp_celsius * 1.8 + 32.0; // Convert Celsius to Fahrenheit
Serial.print("Temperature = ");
Serial.print(temp_celsius);
Serial.print(" Celsius, ");
Serial.print(temp_fahrenheit);
Serial.println(" Fahrenheit");
WiFiClient client = server.available();
if (client) {
Serial.println("New client");
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println("Refresh: 10"); // Update the page every 10 seconds
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head><title>Digital Thermometer</title></head>");
client.println("<body>");
client.println("<h1 style='text-align:center;'>Digital Thermometer</h1>");
client.print("<p style='text-align:center;'><strong>Temperature (Celsius):</strong> ");
client.print(temp_celsius);
client.println(" °C</p>");
client.print("<p style='text-align:center;'><strong>Temperature (Fahrenheit):</strong> ");
client.print(temp_fahrenheit);
client.println(" °F</p>");
client.println("</body>");
client.println("</html>");
delay(5000); // Delay to allow the client to receive the webpage
client.stop(); // Close the connection
Serial.println("Client disconnected");
}
}
Code Explanation (Arduino)- Setup: Initializes serial communication, connects to Wi-Fi, and starts the server.
- Loop: Reads temperature from LM35, converts it to Celsius and Fahrenheit, and serves a web page with updated temperature readings.
python
Copy code
import machine
import network
import time
# Replace with your WiFi credentials
SSID = 'your_wifi_ssid'
PASSWORD = 'your_wifi_password'
# Initialize WiFi connection
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(SSID, PASSWORD)
while not station.isconnected():
pass
print('WiFi connected!')
print('IP address:', station.ifconfig()[0])
# Configure ADC (Analog-to-Digital Converter)
adc = machine.ADC(0)
# Create a server socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('', 80))
server_socket.listen(5)
print('Server started')
# HTML content for displaying temperature
html = """
<!DOCTYPE html>
<html>
<head><title>Digital Thermometer</title></head>
<body>
<h1 style='text-align:center;'>Digital Thermometer</h1>
<p style='text-align:center;'><strong>Temperature (Celsius):</strong> {temp_celsius} °C</p>
<p style='text-align:center;'><strong>Temperature (Fahrenheit):</strong> {temp_fahrenheit} °F</p>
</body>
</html>
"""
while True:
client_socket, addr = server_socket.accept()
print('Got a connection from %s' % str(addr))
request = client_socket.recv(1024)
print('Request:')
print(request)
temp_reading = adc.read() * 3.3 / 1024.0
temp_celsius = (temp_reading - 0.5) * 100.0
temp_fahrenheit = temp_celsius * 1.8 + 32.0
response = html.format(temp_celsius=temp_celsius, temp_fahrenheit=temp_fahrenheit)
client_socket.send(response)
client_socket.close()
Code Explanation (MicroPython)- WiFi Connection: Connects NodeMCU to the specified Wi-Fi network.
- ADC Configuration: Sets up Analog-to-Digital Conversion for reading LM35 sensor values.
- Server Setup: Initializes a server socket on port 80 to listen for incoming client connections.
- Temperature Reading: Reads analog sensor values, converts them to Celsius and Fahrenheit, and formats the HTML response to display on the web page.
After uploading the code to NodeMCU using MicroPython, access the NodeMCU's IP address in a web browser on the same network to view real-time temperature readings. This project demonstrates the flexibility of using MicroPython for IoT applications with NodeMCU.
Comments