The DS18B20 thing is still puzzling tho.
The DS18B20 thing is still puzzling tho.
Explore... Chat... Share...
Moderator: igrr
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
ADC_MODE(ADC_VCC);
ESP8266WebServer server ( 80 );
#define DHTPIN 12 // The Temperature/Humidity sensor
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define ONE_WIRE_BUS 5
#define LEDPIN 13
const char* ssid = "xxxxxxx";
const char* password = "xxxxxxxx";
const char* host = "xxxxxxxx";
String url = "/tempRecord.php?";
const int httpPort = 80;
float t = 0; // temperature celsius
float t_ds18b20 = 0; // temperature from the ds18b20 sensor
float h = 0; // humidity
float f = 0; // temperature farenheit
float hic = 0;
float hif = 0;
float b = 0; // barometric pressure
float bSea = 0; // barometric pressure sea level
float m = 0; // height above sea level
unsigned int l = 800; // LED Strip strength on start
int lstep = 1; // LED step size when changing
boolean tickerClicked = false;
unsigned long dht_time;
unsigned long msMarker = millis() + 20000;
// DHT startup
DHT dht(DHTPIN, DHTTYPE);
// Temperature sensor DS18B20
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
// Ticker setup
//Ticker tickerTemp;
// ************************** LED section ******************************
void LEDoff() {
server.send(200, "text/plain","off");
setLED(0);
}
void LEDmin() {
server.send(200, "text/plain","min");
setLED(200);
}
void LEDmed() {
server.send(200, "text/plain","medium");
setLED(600);
}
void LEDmax() {
server.send(200, "text/plain","max");
setLED(800);
}
void LEDfull() {
server.send(200, "text/plain","full");
setLED(1022);
}
void LEDblink(int reps = 1) {
server.send(200, "text/plain", "blink");
int temp_l = l;
if (reps < 1 || reps > 5) { reps = 1; }
for (int r = 1 ; r <= reps ; r+=1 ) {
setLED(1);
delay(50);
setLED(1023);
delay(50);
}
setLED(1);
delay(50);
setLED(temp_l);
}
void setLED(int newLightValue) {
if (newLightValue > 1023) { newLightValue = 1023; }
if (newLightValue < 0) { newLightValue = 0; }
Serial.print("Setting LED Strip from ");
Serial.print(l);
Serial.print(" to ");
Serial.println(newLightValue);
if (newLightValue > l) {
for (int fadeValue = l ; fadeValue <= newLightValue ; fadeValue += lstep) {
analogWrite(LEDPIN, fadeValue);
delay(1);
}
} else {
for (int fadeValue = l ; fadeValue >= newLightValue ; fadeValue -= lstep) {
analogWrite(LEDPIN, fadeValue);
delay(1);
}
}
l = newLightValue;
}
void setLight() {
server.send(200, "text/plain", "Light set");
if ( server.argName(0) == "val" ) {
setLED(server.arg(0).toInt());
}
}
void lightValue() {
server.send(200, "text/plain", String(l) );
}
// ************************** DHT section ******************************
void readDHT() {
// Wait a few seconds between measurements.
delay(500);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
// Read temperature as Celsius (the default)
t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.println(" %");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
Serial.print(f);
Serial.print(" *F");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.println(" *C ");
Serial.print(hif);
Serial.println(" *F");
}
// ************************** DS18B20 section ******************************
void getTemp() {
DS18B20.requestTemperatures();
delay(10);
t_ds18b20 = DS18B20.getTempCByIndex(0);
server.send(200, "text/plain", String(t_ds18b20) );
Serial.print("Temperature Dallas DS18B20: ");
Serial.println(t_ds18b20);
}
void readDS18B20() {
DS18B20.requestTemperatures();
delay(10);
t_ds18b20 = DS18B20.getTempCByIndex(0);
Serial.print("Temperature Dallas DS18B20: ");
Serial.println(t_ds18b20);
}
// ************************** Call IoT server section **********************
void callIoT() {
Serial.println("Getting weather data....");
readDS18B20();
readDHT();
// Use WiFiClient class to create TCP connections
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
url += "temp=";
url += t_ds18b20;
url += "&hum=";
url += h;
url += "&hic=";
url += hic;
url += "&room=livingroom";
url += "&sensor=DHT11,ds18b20";
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
client.stop();
delay(10);
tickerClicked = false;
}
// ************************** Ticker section ******************************
void tickerClick() {
tickerClicked = true;
}
// ************************** Web Responses ******************************
void handleRoot() {
char temp[400];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
snprintf ( temp, 800,
"<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>ESP8266 Demo</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>Hello from ESP8266!</h1>\
<p>Uptime: %02d:%02d:%02d</p>\
</body>\
</html>",
hr, min % 60, sec % 60
);
server.send ( 200, "text/html", temp );
}
void handleNotFound() {
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 );
}
// ************************** Volage *****************************
void getVolt() {
server.send(200, "text/plain", String(ESP.getVcc()) );
}
// ************************** Setup ******************************
void setup() {
//starttime = millis();
Serial.begin(115200);
// Connecting to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Chip ID: ");
Serial.println(ESP.getChipId());
Serial.print("Flash Chip ID: ");
Serial.println(ESP.getFlashChipId());
Serial.print("Flash Chip size:");
Serial.println(ESP.getFlashChipSize());
Serial.print("Free Heap: ");
Serial.println(ESP.getFreeHeap());
Serial.print("VCC: ");
Serial.println(ESP.getVcc());
setLED(l);
delay(500);
dht.begin();
DS18B20.begin();
//tickerTemp.attach(180, tickerClick);
server.on ( "/", handleRoot );
server.onNotFound ( handleNotFound );
server.on ( "/light", setLight );
server.on ( "/off", LEDoff );
server.on ( "/min", LEDmin );
server.on ( "/med", LEDmed );
server.on ( "/max", LEDmax );
server.on ( "/full", LEDfull );
server.on ( "/lightVal", lightValue );
server.on ( "/blinkOne", []() { LEDblink(1); });
server.on ( "/blinkTwo", []() { LEDblink(2); });
server.on ( "/blinkThree", []() { LEDblink(3); });
server.on ( "/blinkFour", []() { LEDblink(4); });
server.on ( "/getTemp", getTemp );
server.on ( "/getVoltage", getVolt );
server.begin();
}
// ************************** Loop ******************************
void loop() {
server.handleClient();
if ( millis() > msMarker ) {
callIoT();
msMarker = millis() + 180000;
t_ds18b20 = 0;
}
}
It takes about 20-25 seconds for home assistant c[…]
I tried to upgrade tof my sonoff basic R2 with the[…]
a problem Perhaps you want to define "Probl[…]
Rebooting your router will not give you a faster I[…]
There are no other notifications from esptool.py i[…]
Using the Arduino IDE, you'll learn how to set up […]
In this project, you will post to Twitter using an[…]
In this project, we will build a water level contr[…]
I guess I'm late, but I had the same problem and f[…]
Last night I received my first D1 Minis for a lear[…]
Although I am aware that this is an old post, I fe[…]