#include "DHT.h"
#include <WiFiClientSaecure.h>
#define DHTPIN 33 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
String GAS_ID = "AKfycbwVqQvqETG9tNbK-BE0yRJNNIDRjD7caxVrzFxM7KeW_5cVCiiMnA_iH1oGiVTACg5TtQ"; //--> spreadsheet script ID
//Your Domain name with URL path or IP address with path
const char* host = "script.google.com";
// ----------------------------------------------------------------------------------------------
#define UPDATE_INTERVAL_HOUR (0)
#define UPDATE_INTERVAL_MIN (0)
#define UPDATE_INTERVAL_SEC (30)
#define UPDATE_INTERVAL_MS ( ((UPDATE_INTERVAL_HOUR*60*60) + (UPDATE_INTERVAL_MIN * 60) + UPDATE_INTERVAL_SEC ) * 1000 )
// ----------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------
void update_google_sheet()
{
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClientSecure client;
const int httpPort = 443; // 80 is for HTTP / 443 is for HTTPS!
client.setInsecure(); // this is the magical line that makes everything work
if (!client.connect(host, httpPort)) { //works!
Serial.println("connection failed");
return;
}
//----------------------------------------Processing data and sending data
String url = "/macros/s/" + GAS_ID + "/exec?temperature=";
url += String(t);
url += "&humidity=";
url += String(h);
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");
Serial.println();
Serial.println("closing connection");
}
// ----------------------------------------------------------------------------------------------
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(9600);
setup_wifi();
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// 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();
// Read temperature as Fahrenheit (isFahrenheit = true)
//float f = dht.readTemperature(true);
// 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;
}
// Compute heat index in Fahrenheit (the default)
// float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.println(F("°C "));
update_google_sheet();
}