Suchen

Registrieren

Vielen Dank für Deine Anmeldung! Du bekommst nun eine Email mit einem Aktivierungslink.

username already taken
Du hast bereits einen Account? .

Login

Passwort vergessen?
Du hast noch keinen Account? Registrieren um den UI-Designer und das Forum zu benutzen oder eigene Projekte anzulegen.
Log dich ein um Beiträge schreiben zu können!



ESP8266-01 with DHT22 Example Code

#1 Juli 30, 2018 20:34:39 by meccip

ESP8266-01 with DHT22 Example Code

Code that works with netIO http protocol.
After upload into ESP-01;
Set up a connection in NetIO on global page
Name: ESP
host: 192,168,1,124
port: 2564
protocol: http

Create two label and define the attributes for each label
Temperature Label
reads: /temp
interval:2000
connection:esp
formatResponse:{0} °C

Humidity Label
reads: /humi
interval:2000
connection:esp
formatResponse:{0} %

This line in the code it is important for DHT22 to work. You must add “15” to work with ESP or else you will have a "nan" value.
DHT dht(DHTPIN, DHTTYPE,15); // add 15 for esp8266 40mhz !!

// include library 
#include <ESP8266WiFi.h>
#include "DHT.h"
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
// end include library


const char* ssid = "RAKKA";
const char* password = "carisima";

WiFiServer androidServer(2564); // set port number for android netIO

DHT dht(DHTPIN, DHTTYPE,15); // add 15 for esp8266 40mhz !!

// setup
void setup(){
Serial.begin(115200);
delay(10);

// prepare GPIO2
// pinMode(2, OUTPUT);
// digitalWrite(2, LOW);

// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.config(IPAddress(192,168,1,124), IPAddress(255,255,255,0), IPAddress(192,168,1,1)); // static ip,dns,gateway
WiFi.begin(ssid, password);

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

// Start the server
androidServer.begin(); // start the server for android netIO
dht.begin();

Serial.println("Server started");

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

}
// end setup

// main loop
void loop() {

WiFiClient client = androidServer.available();
if (!client) {
return;
}

// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}

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

// Return the response
client.println("HTTP/1.1 200 OK");
client.println();

// Match the request

if (request.indexOf("/temp") != -1){
float t = dht.readTemperature(); // Read temperature as Celsius
client.print(t);
Serial.print("Temperature is: ");
Serial.println(t);
}
if (request.indexOf("/humi") != -1){
float h = dht.readHumidity();
client.print(h);
Serial.print("Humidity is: ");
Serial.println(h);
}

delay(1);
Serial.println("Client disconnected");
Serial.println("");

}
// end loop

Editiert meccip (Juli 30, 2018 20:36:30)

#2 Aug. 1, 2018 10:37:23 by Kalle

ESP8266-01 with DHT22 Example Code

Thanks for the example, good job.

I think a interval of 60.000 (1 minute)in NetIO would be better for a temp and humidity sensor because this value does not change constantly within a short time and it will also minimize the data traffic.

A other option is to send a UDP message from the ESP to a port on where NetIO listen when a value has changed. This is also a good solution for relays - in this way you will have a real time feedback.

Kalle

Editiert Kalle (Aug. 1, 2018 10:43:07)