Project tutorial
ESP8266 Thermometer

ESP8266 Thermometer

ESP8266-01 and DS18B20 temperature server.

  • 1,744 views
  • 0 comments
  • 8 respects

Components and supplies

About this project

The sketch is loaded to the ROM of the ESP-01, the Arduino is used for programming only, after that the ESP is an independent server. The ESP is connected to the local network. On request from any internet browser connected to the same network, the ESP server responds with a new temperature reading, otherwise the server is listening for requests.

For uploading the sketch to the ESP-01 you need an Arduino Uno or USB to SERIAL adaptor. The Arduino is hard wired Reset so the mc doesn't take part, only the USB to Serial is used. The Serial Monitor in the Arduino IDE is used for debuging and getting the IP address of the ESP. These websites give good guide for preparing and uploading the sketch.

https://medium.com/@aallan/getting-started-with-the-esp8266-270e30feb4d1

http://www.geekstips.com/esp8266-arduino-tutorial-iot-code-example/

Before loading the code enter your SSID and password that you use to connect wifi devices to your router.

type in your browser the IP address that you find on the Serial Monitor in the Arduino IDE. Normally the IP is 192.168.1.3

Code

serverC/C++
/*
    ESP8266-01 WiFi Thermometer project by moty22.co.uk
    This sketch is made from a few examples.
    This sketch demonstrates how to set up a simple HTTP-like server.
    server_ip is the IP address of the ESP8266 module, will be
    printed to Serial when the module is connected.
*/

#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
 
#ifndef STASSID
#define STASSID "your SSID" //your router ID ("VIRGIN-21")
#define STAPSK  "password"  //router password ("12345678")
#endif

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
 
// Setup a oneWire instance to communicate with any OneWire devices 
OneWire oneWire(2);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

const char* ssid = STASSID;
const char* password = STAPSK;

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
  Serial.begin(9600);
  sensors.begin();
  
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print(F("Connecting to "));
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
  }
  Serial.println();
  Serial.println(F("WiFi connected"));

  // Start the server
  server.begin();
  Serial.println(F("Server started"));

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.println(F("new client"));

  client.setTimeout(5000); // default is 1000

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(F("request: "));
  Serial.println(req);

  // read/ignore the rest of the request
  // do not client.flush(): it is for output only, see below
  while (client.available()) {
    // byte by byte is not very efficient
    client.read();
  }

   // call sensors.requestTemperatures() to issue a global temperature
  sensors.requestTemperatures(); // Send the command to get temperatures

  Serial.print("Temperature is: ");
  float temp = sensors.getTempCByIndex(0);
  Serial.println(temp);

  // Send the response to the client
  // it is OK for multiple small client.print/write,
  // because nagle algorithm will group them into one single packet
  client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nTemperature now is: "));
  client.print(temp);
  client.print(F(" Centigrade</html>"));
  
  // The client will actually be *flushed* then disconnected
  // when the function returns and 'client' object is destroyed (out-of-scope)
  // flush = ensure written data are received by the other side
  Serial.println(F("Disconnecting from client"));
}

Schematics

wifi thermo
Server 3qn4g1qsjk

Comments

Similar projects you might like

ESP8266 To Your Website

Project tutorial by Projecter

  • 12,472 views
  • 5 comments
  • 11 respects

ESP8266 ESP-01 Webserver

by Robin Kanattu Thomas

  • 126,445 views
  • 22 comments
  • 163 respects

WiFi ESP8266 and DHT22 Sensor

Project tutorial by Hernanduino

  • 28,726 views
  • 7 comments
  • 27 respects

Telegram Bot with ESP8266

Project tutorial by Shebin Jose Jacob

  • 42,225 views
  • 44 comments
  • 77 respects
Add projectSign up / Login