I have two different code of functions and I want to combine them together to make it work together. both of code for dht11 sensor.
- first code is for getting sensor data and update in google spreadsheet.
- second code is for getting sensor data and display on OLED screen.
what I want is to work both functions(it's ok if both are not working at the same time. work sequentially is ok..)
----------------------------------- here is first one.
#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#define DHTPIN 27
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
//---------------------------------------------------------------------
const char * ssid = "WIFI"; // type your Wifi name
const char * password = "PW"; // Type your wifi password
String GOOGLE_SCRIPT_ID = "spreadsheetinfo"; // Type your App Script id
const int sendInterval = 50;
WiFiClientSecure client;
void setup() {
pinMode(2,OUTPUT);
dht.begin();
Serial.begin(115200);
delay(100);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(2,LOW);
delay(5000);
Serial.print(".");
digitalWrite(2,HIGH);
delay(5000);
}
Serial.println("OK");
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
write_google_sheet( "value1="+String(h)+"&value2="+String(t));
delay(1000);
}
///////////////////////////////////////////////
void write_google_sheet(String params) {
HTTPClient http;
String url="https://-+GOOGLE_SCRIPT_ID+"/exec?"+params;
Serial.println(url);
Serial.println("Updating Temperature & Humidity Status");
http.begin(url.c_str());
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
int httpCode = http.GET();
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
String payload;
if (httpCode > 0) {
payload = http.getString();
Serial.println("Payload: "+payload);
}
http.end();
delay(600000); // data loading time change in google speadsheet 10min
}
-----------------------------------here is second code for displaying.
#include "DHTesp.h"
#include "heltec.h"
DHTesp dht;
float currentTemp;
float currentHumidity;
void displayReadingsOnOled() {
String temperatureDisplay ="Temperature: " + (String)currentTemp + "°C";
String humidityDisplay = "Humidity: " + (String)currentHumidity + "%";
// Clear the OLED screenw
Heltec.display->clear();
// Prepare to display temperature
Heltec.display->drawString(0, 0, temperatureDisplay);
// Prepare to display humidity
Heltec.display->drawString(0, 12, humidityDisplay);
// Display the readings
Heltec.display->display();
}
void setup()
{
displayReadingsOnOled ;
dht.setup(27, DHTesp::DHT11);
currentTemp = dht.getTemperature();
currentHumidity = dht.getHumidity();
pinMode(LED,OUTPUT);
digitalWrite(LED,HIGH);
Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Enable*/, false /*Serial Enable*/);
displayReadingsOnOled();
Serial.println("OK");
}
void loop()
{
float temperature = dht.getTemperature();
float humidity = dht.getHumidity();
if (temperature != currentTemp || humidity != currentHumidity) {
currentTemp = temperature;
currentHumidity = humidity;
displayReadingsOnOled();
}
delay(2000);
}