Posting DHT11 Sensor to Google Sheets

Percobaan ESP32 kali ini adalah mengupload bacaan sensor ke google sheets!

Sumbernya dari:
https://randomnerdtutorials.com/esp32-esp8266-publish-sensor-readings-to-google-sheets/

Nah, di sumbernya diperintahkan membuat applet baru di IFTTT. Kebetulan UI nya sudah berubah, jadi caranya beda dari instrusksi sumber di atas, jadi saya menggunakan cara ini:
https://help.ifttt.com/hc/en-us/articles/360021401373-Creating-your-own-Applet

Nah, yang lainnya cukup menjelaskan kokk, dari sumber tersebut.

Ini tester event IFTTT saya.

Hasilnya kira-kira begini:

Karena saya menggunakan DHT11, jadi tidak bisa mengukur tekanan. Nilai yang pertama merupakan temperatur dalam celcius, nilai kedua merupakan temperatur dalam fahrenheit, dan yang ketiga adalah kelembapan.
Lalu, dibuat sensor dapat menerima data setiap 5 menit.
Data pertama: hanya  tes, diinput di IFTTT
Data kedua dan ketiga: Masih percobaan interval waktu menerima data
Data keempat-keenam: Sensor menerima data setiap 5 menit


Nah, ini program yang sudah saya modifikasi jadi dapat menggunakan sensor DHT11:

#ifdef ESP32
  #include <WiFi.h>
#else
  #include <ESP8266WiFi.h>
#endif

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "DHT.h"

// Replace with your SSID and Password
const char* ssid     = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

// Replace with your unique IFTTT URL resource
const char* resource = "REPLACE_WITH_YOUR_IFTTT_URL_RESOURCE";

// How your resource variable should look like, but with your own API KEY (that API KEY below is just an example):
//const char* resource = "/trigger/sensor_readings/with/key/nAZjOphL3d-ZO4N3k64-1A7gTlNSrxMJdmqy3";

// Maker Webhooks IFTTT
const char* server = "maker.ifttt.com";

// Time to sleep
uint64_t uS_TO_S_FACTOR = 1000000;  // Conversion factor for micro seconds to seconds
// sleep for 30 minutes = 1800 seconds
// sleep for 5 minutes = 300 seconds
uint64_t TIME_TO_SLEEP = 300;

#define DHTPIN 4
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE); // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

void setup() {
  Serial.begin(115200);
  delay(2000);

  dht.begin();

  initWifi();
  makeIFTTTRequest();
   
  #ifdef ESP32
    // enable timer deep sleep
    esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);   
    Serial.println("Going to sleep now");
    // start deep sleep for 3600 seconds (60 minutes)
    esp_deep_sleep_start();
  #else
    // Deep sleep mode for 3600 seconds (60 minutes)
    Serial.println("Going to sleep now");
    ESP.deepSleep(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  #endif
}

void loop() {
  // sleeping so wont get here
}

// Establish a Wi-Fi connection with your router
void initWifi() {
  Serial.print("Connecting to: ");
  Serial.print(ssid);
  WiFi.begin(ssid, password); 

  int timeout = 10 * 4; // 10 seconds
  while(WiFi.status() != WL_CONNECTED  && (timeout-- > 0)) {
    delay(250);
    Serial.print(".");
  }
  Serial.println("");

  if(WiFi.status() != WL_CONNECTED) {
     Serial.println("Failed to connect, going back to sleep");
  }

  Serial.print("WiFi connected in: ");
  Serial.print(millis());
  Serial.print(", IP address: ");
  Serial.println(WiFi.localIP());
}

// Make an HTTP request to the IFTTT web service
void makeIFTTTRequest() {
  Serial.print("Connecting to ");
  Serial.print(server);
 
  WiFiClient client;
  int retries = 5;
  while(!!!client.connect(server, 80) && (retries-- > 0)) {
    Serial.print(".");
  }
  Serial.println();
  if(!!!client.connected()) {
    Serial.println("Failed to connect...");
  }
 
  Serial.print("Request resource: ");
  Serial.println(resource);

  // Temperature in Celsius
  String jsonObject = String("{\"value1\":\"") + dht.readTemperature() + "\",\"value2\":\"" + (dht.readTemperature(true))
                      + "\",\"value3\":\"" + dht.readHumidity() + "\"}";
                     
  // Comment the previous line and uncomment the next line to publish temperature readings in Fahrenheit                   
  /*String jsonObject = String("{\"value1\":\"") + (1.8 * dht.readTemperature() + 32) + "\",\"value2\":\""
                      + (dht.readPressure()/100.0F) + "\",\"value3\":\"" + dht.readHumidity() + "\"}";*/
                     
  client.println(String("POST ") + resource + " HTTP/1.1");
  client.println(String("Host: ") + server);
  client.println("Connection: close\r\nContent-Type: application/json");
  client.print("Content-Length: ");
  client.println(jsonObject.length());
  client.println();
  client.println(jsonObject);
       
  int timeout = 5 * 10; // 5 seconds           
  while(!!!client.available() && (timeout-- > 0)){
    delay(100);
  }
  if(!!!client.available()) {
    Serial.println("No response...");
  }
  while(client.available()){
    Serial.write(client.read());
  }
 
  Serial.println("\nclosing connection");
  client.stop();
}

Komentar

Postingan populer dari blog ini

DHT 11 dan Sensor LCD

Rumah Anti Maling dengan Sensor Ultrasonic dan ESP32

Bluetooth for ESP32