Suchcode

MAX6675 Thermocouple Temperature over Wi-Fi with ESP8266 NodeMCU D1 Mini

MAX6675 Thermocouple Temperature over Wi-Fi with ESP8266 NodeMCU D1 Mini

In this tutorial, we learn how to use a MAX6674 K-type thermocouple sensor with an ESP8266 NodeMCU and D1 Mini to measure temperature over WiFi on a desktop, tablet, and mobile phone. The code can use Ajax, which requires your router to be connected to WiFi, and without Ajax, which does not require WiFi.

Bilder

Browser-Bildschirm auf dem Desktop
Browser-Bildschirm auf dem Desktop
Browser-Bildschirm auf dem Desktop
Browser-Bildschirm auf dem Desktop
Browserbildschirm auf dem Mobiltelefon
Browserbildschirm auf dem Mobiltelefon
Browserbildschirm auf Mobiltelefon
Browserbildschirm auf Mobiltelefon
340-MAX6675 Thermocouple Temperature over WiFi with ESP8266 NodeMCU D1 Mini
Sprache: C++
/*
 * 
 * This is Arduino code to measure a MAX6675 K-Type thermocouple sensor
 * over Wifi using an ESP8266 NodeMCU and D1 Mini.
 * This code can work with and without Ajax. See the video for details.

 * 
 * Watch the video instruction for this code:  https://youtu.be/sS5apP9vqak
 * 
 * Full explanation of this code and wiring diagram is available at
 * my Arduino Course at Udemy.com here: http://robojax.com/L/?id=62

 * Written by Ahmad Shamshiri on July 06, 2020 in Ajax, Ontario, Canada
 * www.robojax.com
 * 

 * Get this code and other Arduino codes from Robojax.com.
Learn Arduino step by step in a structured course with all materials, wiring diagrams and libraries
all in one place. 

If you found this tutorial helpful, please support me so I can continue creating 
content like this. 

or make a donation using PayPal http://robojax.com/L/?id=64
Related videos:
Introduction to MAX6675 K-Type: https://youtu.be/VGqONmUinqA
Using MAX6675 K-Type thermocouple with an LED display: https://youtu.be/cD5oOu4N_AE
Using MAX6675 K-Type thermocouple with an LCD1602-I2C: https://youtu.be/BlhpktgPdKs
Using 2 or more MAX6675 K-Type thermocouples: https://youtu.be/c90NszbNG8c
Using MAX6675 K-Type thermocouple as a heater or cooler controller: [this video]
 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.


*/

//Watch the video for details of the lines below
static const uint8_t D0   = 16;
static const uint8_t D1   = 5;
static const uint8_t D2   = 4;
static const uint8_t D3   = 0;
static const uint8_t D4   = 2;
static const uint8_t D5   = 14;
static const uint8_t D6   = 12;
static const uint8_t D7   = 13;
static const uint8_t D8   = 15;
static const uint8_t D9   = 3;
static const uint8_t D10  = 1;

///////// Start of MAX6675 settings

#include "max6675.h"
//Robojax.com  MAX6675 Thermocouple

int thermoCLK = D4;
int thermoCS = D3;
int thermoDO = D2;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
float tempC, tempF, tempK;
char type='C';//default temperature type C
char *titleMain="Using ESP8266";
char *titleTemp="Temperature: ";
int refreshTime =5000;
const bool useAjax=false;//set to "true" or "false". if set to "true" then internet connection is needed

/////////////// end of MAX6675 settings

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#ifndef STASSID
#define STASSID "Robojax"//your Wifi SSID (case sensitive)
#define STAPSK  "YouTube2020"//your WiFi password
#endif

const char *ssid = STASSID;
const char *password = STAPSK;
ESP8266WebServer server(80);

void handleRoot() {
   //Robojax.com MLX90614 with ESP8622 with Ajax
 String HTML ="<!DOCTYPE html>\
  <html>\
  <head>\
  \t\n<title>Robojax MAX6675 Thermocouple</title>\
  \t\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
  \n<style>\
 \nhtml,body{\t\nwidth:100%\;\nheight:100%\;\nmargin:0}\n*{box-sizing:border-box}\n.colorAll{\n\tbackground-color:#90ee90}\n.colorBtn{\n\tbackground-color:#add8e6}\n.angleButtdon,a{\n\tfont-size:30px\;\nborder:1px solid #ccc\;\ndisplay:table-caption\;\npadding:7px 10px\;\ntext-decoration:none\;\ncursor:pointer\;\npadding:5px 6px 7px 10px}a{\n\tdisplay:block}\n.btn{\n\tmargin:5px\;\nborder:none\;\ndisplay:inline-block\;\nvertical-align:middle\;\ntext-align:center\;\nwhite-space:nowrap}\n";
    HTML +=".button {\n";
    HTML +=" border: none\;\n  color: white\;\n  padding: 15px 32px\;\n";
    HTML +="  text-align: center\;\n";
    HTML +="  text-decoration: none\;\n";
    HTML +="  display: inline-block\;\n";
    HTML +="  font-size: 25px\;\n";
    HTML +="  margin: 4px 2px\;\n";
    HTML +="  cursor: pointer\;\n";
    HTML +="} \n";
  HTML +="</style>\n\n";
  if(useAjax)
  {
      HTML +="<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js\"></script>\n\n";
  HTML +="<script>\n";
  HTML +="$(document).ready(function(){\n";
  
  HTML +="function loadTemp(){\n";
  HTML +="    $('#div1').load(";  
  if(type =='C'){
   HTML +="\"\/c\"\n";    
  }else if(type =='F')
  {
   HTML +="\"\/f\"\n";        
  }else{
    HTML +="\"\/k\"\n";         
  }
  HTML +=",function () {\n";  
  HTML +="         $(this).unwrap()\;\n";
  HTML +="    })\;\n";
  HTML +="}\n";

  HTML +="loadTemp()\;\n";
  HTML +="setInterval(function(){\n";
  HTML +="    loadTemp() \n";
  HTML +="}, 5000)\;\n";  
     
  HTML +="})\; </script> \n";
  }
   HTML +="</head>\n\n<body>\n<h1>Robojax MAX6675 Thermocouple</h1>\n";
   HTML +="<h1>"; 
   HTML +=titleMain;
   HTML +="</h1>\n";           
    HTML +="<div id=\"div1\" style=\"background-color: #aecbd6 !important\; display: inline-block\; margin: 10px\"><h2>";   
  if(useAjax)
  {
   HTML +="<div id=\"div1\"><h2>"; 
   HTML +=titleTemp; 
   HTML +="</h2></div>\n";       

  }else{
   
    if(type =='C'){
    HTML +="<span  style=\"font-size: 28px\">\n";  
    HTML +=titleTemp;
    HTML +=tempC;
    HTML +="&deg;C</span><br/>\n";
   
    }

  
    if(type =='F')
    {
    HTML +="<span  style=\"font-size: 28px\">\n";  
    HTML +=titleTemp;
    HTML +=tempF;
    HTML +="&deg;F</span><br/>\n";
  
    }

  
    if(type =='K')
    {
  
    HTML +="<span  style=\"font-size: 28px\">\n";  
    HTML +=titleTemp;
    HTML +=tempK;
    HTML +="&deg;K</span><br/>\n";
  
    }
  }//if ajax
  HTML +="<br/>";

  if(!useAjax)
  {
    HTML +="<a href=\"/c\" class=\"button\" style=\"background-color: #4CAF50\">Get &deg;C</a>\n";
    HTML +="<a href=\"/f\" class=\"button\" style=\"background-color: #4389fa\">Get &deg;F</a>\n";
    HTML +="<a href=\"/k\" class=\"button\" style=\"background-color: #fa5e43\">Get &deg;K</a>\n";            
  }
  HTML +="</div>\n"; //background color div
  HTML +="\t\n</body>\n</html>\n";
  server.send(200, "text/html", HTML);  
   //Robojax.com MAX6675 with ESP8622 with Ajax  
}//handleRoot()

void handleNotFound() {
   //Robojax.com MAX6675 with ESP8622 with Ajax
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";

  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }

  server.send(404, "text/plain", message);

}//handleNotFound()

void setup() {
  //Robojax.com  MAX6675 Thermocouple
  Serial.begin(115200);
  Serial.println("MAX6675 with ESP8266");

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: http://");
  Serial.println(WiFi.localIP());

//multicast DNS   //Robojax.com Tutorial
  if (MDNS.begin("robojaxESP8266")) {
    Serial.println("MDNS responder started");
    Serial.println("access via http://robojaxESP8266");
  }

  server.on("/", handleRoot);
  if(useAjax)
  {
    server.on("/c", displayTempCA);  
    server.on("/f", displayTempFA);  
    server.on("/k", displayTempKA);       
  }else{
    server.on("/c", displayTempC);  
    server.on("/f", displayTempF);  
    server.on("/k", displayTempK);     
  } 
  server.on("/inline", []() {
    server.send(200, "text/plain", "this works as well");
 });
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");  
     //Robojax.com MLX90614 with ESP8622 
  // wait for MAX chip to stabilize
  delay(500);
//Robojax.com MAX6675 Thermocouple  
}// end of setup

void loop() {
  //Robojax.com  MAX6675 Thermocouple
  server.handleClient();
   MDNS.update();    
   readTemperature();

   if(tempC >=89.5)
   {
    ///do something here
   }

   handleRoot();
   delay(1000);
//Robojax.com MAX6675 Thermocouple   
}//loop


/*
 *  readTemperature()
 * @brief Reads the temperature based on the TEMPERATURE_UNIT
 * @param Average temperature
 * @return Returns one of the values above
 * Written by Ahmad Shamshiri for robojax.com
 * on May 20, 2020 at 15:23 in Ajax, Ontario, Canada
 */
void readTemperature()
{
 //Robojax.com with MAX6675 Thermocouple 

   tempF = thermocouple.readFahrenheit();//convert to Fahrenheit 
   tempK= thermocouple.readCelsius() + 273.15;//convert to Kelvin
   tempC = thermocouple.readCelsius();// return Celsius
  
 //Robojax.com  MAX6675 Thermocouple 
}// readTemperature()


void displayTempC()
{
     //Robojax.com MAX6675 with ESP8622 
  type ='C';
  handleRoot();

}//displayTempC()

void displayTempF()
{
     //Robojax.com MAX6675 with ESP8622 
 type ='F';
  handleRoot(); 

}//displayTempF()

void displayTempK()
{
     //Robojax.com MAX6675 with ESP8622 
  type ='K';
  handleRoot();  

}//displayTempK()


/////////////////// functions for Ajax

void displayTempCA()
{
   //Robojax.com MAX6675 with ESP8622

String HTML ="<span  style=\"font-size: 28px\">\n";  
  HTML +=titleTemp;
  HTML +=tempC;
  HTML +="&deg;C</span><br/>\n";

  server.send(200, "text/html", HTML);   
}//displayTemp()

void displayTempFA()
{
   //Robojax.com MAX6675 with ESP8622

String HTML ="<span  style=\"font-size: 28px\">\n";  
  HTML +=titleTemp;
  HTML +=tempF;
  HTML +="&deg;F</span><br/>\n";

  server.send(200, "text/html", HTML);   
}//displayTemp()

void displayTempKA()
{
   //Robojax.com MAX6675 with ESP8622 with Ajax

String HTML ="<span  style=\"font-size: 28px\">\n";  
  HTML +=titleTemp;
  HTML +=tempK;
  HTML +="&deg;K</span><br/>\n";

  server.send(200, "text/html", HTML);   
}//displayTempKA()

Dateien📁

Keine Dateien verfügbar.