Hi I have followed this tutorial to get my DS18B20 reading on a website at home.
I have also used this tutorial to get some BME280 working online.
https://randomnerdtutorials.com/visualize-esp32-esp8266-sensor-readings-from-anywhere/
I want to try to get 2 DS18B20 to show temp and 1 DHT22 to show temp and humidity remotely and use similar code and process to the second tutorial.
Currently my code is exactly as per the tutorial.
My question is how do I change the code for the both the ESP and the php code, to read the 2 DS18B20 and the DHT22 onto the web.
Cheers
DS18B20 uses one-wire bus to communicate so you only need one GPIO pin. Connect as many as you want. From what I can tell DHT22 requires one GPIO for every sensor. Use the DS18B20 tutorial and the DHT22 tutorial to cobble together what you need for both.
Hi Steve
Thanks for that info. I now have code that runs both the sensors to the serial monitor. see below
How would I adapt that to send it to the web server from the above tutorial.
Sorry I’m a newby
[code]
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#include “DHT.h”
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define ONE_WIRE_BUS 15 // Data wire is connected to GPIO15
// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
DeviceAddress sensor1 = { 0x28, 0x6A, 0x48, 0x7C, 0x0C, 0x00, 0x00, 0xC4 };
DeviceAddress sensor2 = { 0x28, 0xFA, 0x14, 0x7B, 0x0C, 0x00, 0x00, 0x47 };
DHT dht(DHTPIN, DHTTYPE);
void setup(void)
{
Serial.begin(9600);
sensors.begin();
dht.begin();
Serial.println(“Starting…”);
}
void loop(void){
// Wait a few seconds between measurements.
delay(20000);
//Serial.print(“Requesting temperatures…”);
sensors.requestTemperatures(); // Send the command to get temperatures
// Serial.println(“DONE”);
Serial.print(“8 Frame Hive: “);
Serial.print(sensors.getTempC(sensor1));
Serial.print(“°C: “);
Serial.print(“10 Frame Hive: “);
Serial.print(sensors.getTempC(sensor2));
Serial.print(“°C: “);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F(“Failed to read from DHT sensor!”));
return;
}
Serial.print(F(“8 Frame Roof Temp: “));
Serial.print(t);
Serial.print(F(“°C “));
Serial.print(F(“8 Frame Roof Humidity: “));
Serial.print(h);
Serial.print(F(“% “));
Serial. print(‘\n’);
}
[/code]
Hi.
The php code is already prepared to display three charts. So, it’s ideal for your scenario because you want to display three measurements. You just need to change the headings of the charts.
On the Arduino side, after being able to get the readings, you just need to change the following lines to send your temperature values instead of the BME280 readings:
// Prepare your HTTP POST request data
String httpRequestData = "api_key=" + apiKeyValue + "&value1="
+ String(bme.readTemperature())
+ "&value2=" + String(bme.readHumidity())
+ "&value3=" + String(bme.readPressure()/100.0F) + "";
I hope this helps.
Regards,
Sara
Hi Sara
Would this work?
[code]
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#include “DHT.h”
#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#endif
// Replace with your network credentials
const char* ssid = “”;
const char* password = “”;
// REPLACE with your Domain name and URL path or IP address with path
const char* serverName = “”;
// Keep this API Key value to be compatible with the PHP code provided in the project page.
// If you change the apiKeyValue value, the PHP file /post-data.php also needs to have the same key
String apiKeyValue = “”;
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define ONE_WIRE_BUS 15 // Data wire is connected to GPIO15
// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
DeviceAddress sensor1 = { 0x28, 0x6A, 0x48, 0x7C, 0x0C, 0x00, 0x00, 0xC4 };
DeviceAddress sensor2 = { 0x28, 0xFA, 0x14, 0x7B, 0x0C, 0x00, 0x00, 0x47 };
DHT dht(DHTPIN, DHTTYPE);
// Get Sensor Readings
String getSensorReadings(){
sensors.requestTemperatures();
}
void setup() {
Serial.begin(9600);
sensors.begin();
dht.begin();
Serial.println(“Starting…”);
WiFi.begin(ssid, password);
Serial.println(“Connecting”);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.print(“Connected to WiFi network with IP Address: “);
Serial.println(WiFi.localIP());
}
void loop(){
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Specify content-type header
http.addHeader(“Content-Type”, “application/x-www-form-urlencoded”);
// Prepare your HTTP POST request data
String httpRequestData = “api_key=” + apiKeyValue + “&value1=” + String(dht.readTemperature())
+ “&value2=” + String(dht.readHumidity()) + “&value3=” + String(sensors.getTempC(sensor1)) + “”;
Serial.print(“httpRequestData: “);
Serial.println(httpRequestData);
// You can comment the httpRequestData variable above
// then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
//String httpRequestData = “api_key=xxxx&value1=24.75&value2=49.54&value3=1005.14”;
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
// If you need an HTTP request with a content type: text/plain
//http.addHeader(“Content-Type”, “text/plain”);
//int httpResponseCode = http.POST(“Hello, World!”);
// If you need an HTTP request with a content type: application/json, use the following:
//http.addHeader(“Content-Type”, “application/json”);
//int httpResponseCode = http.POST(“{\”value1\”:\”19\”,\”value2\”:\”67\”,\”value3\”:\”78\”}”);
if (httpResponseCode>0) {
Serial.print(“HTTP Response code: “);
Serial.println(httpResponseCode);
}
else {
Serial.print(“Error code: “);
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println(“WiFi Disconnected”);
}
//Send an HTTP POST request every 30 seconds
delay(30000);
// Wait a few seconds between measurements.
delay(20000);
//Serial.print(“Requesting temperatures…”);
sensors.requestTemperatures(); // Send the command to get temperatures
// Serial.println(“DONE”);
Serial.print(“8 Frame Hive: “);
Serial.print(sensors.getTempC(sensor1));
Serial.print(“°C: “);
Serial.print(“10 Frame Hive: “);
Serial.print(sensors.getTempC(sensor2));
Serial.print(“°C: “);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F(“Failed to read from DHT sensor!”));
return;
}
Serial.print(F(“8 Frame Roof Temp: “));
Serial.print(t);
Serial.print(F(“°C “));
Serial.print(F(“8 Frame Roof Humidity: “));
Serial.print(h);
Serial.print(F(“% “));
Serial. print(‘\n’);
}
[/code]
Hi.
You don’t need this:
// Get Sensor Readings
String getSensorReadings(){
sensors.requestTemperatures();
}
Simply call :
sensors.requestTemperatures();
before sending the request.
Regards,
Sara