Mechatronics LAB
Published © MIT

IoT digital thermometer using nodemcu and lm35

In this tutorial, we will explore more about this interesting MCU and slowly we dive in the Internet of things world by connecting NodeMCU w

BeginnerWork in progress1 hour18
IoT digital thermometer using nodemcu and lm35

Story

Read more

Code

Code

Arduino
#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(" &deg;C</p>");
    client.print("<p style='text-align:center;'><strong>Temperature (Fahrenheit):</strong> ");
    client.print(temp_fahrenheit);
    client.println(" &deg;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 MicroPyuthon

Python
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} &deg;C</p>
<p style='text-align:center;'><strong>Temperature (Fahrenheit):</strong> {temp_fahrenheit} &deg;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()

Credits

Mechatronics LAB
75 projects • 49 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .

Comments