ESP32 Google Sheets (Sending Data and Reading Data) I Arduino Code
ESP32 Google Sheets (Sending Data and Reading Data) I Arduino Code
//////////////////////////////////////////////////////////
// Before you use this Arduino code, I hope you have watched the video until the
end. //
// Because there are several configurations and settings for this project and this
Arduino code that is shown or can be seen in the video. //
// And also so that you know how to use this Arduino code.
//
//
//
// If you see some differences between the code I shared and the code that appears
in the video, it's because after I made the video, //
// I fixed several lines of code. So follow the code that I have shared.
//
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> 01_Test_DHT11_and_Switches
// Reference :
https://github.com/adafruit/DHT-sensor-library/blob/master/examples/DHTtester/
DHTtester.ino
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11
#define Switch_1_PIN 13
#define Switch_2_PIN 12
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
pinMode(Switch_1_PIN, INPUT_PULLUP);
pinMode(Switch_2_PIN, INPUT_PULLUP);
dht11.begin();
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
Serial.print(F("Humidity : "));
Serial.print(h);
Serial.print(F("% | Temperature : "));
Serial.print(t);
Serial.print(F("°C | Switch 1 : "));
Serial.print(digitalRead(Switch_1_PIN));
Serial.print(F(" | Switch 2 : "));
Serial.println(digitalRead(Switch_2_PIN));
}
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<
//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> 02_Send_Data_to_Google_Sheets
//----------------------------------------Including the libraries.
#include "WiFi.h"
#include <HTTPClient.h>
#include "DHT.h"
//----------------------------------------
//
________________________________________________________________________________Get
ting_DHT11_Sensor_Data()
// Subroutine for getting temperature and humidity data from the DHT11 sensor.
void Getting_DHT11_Sensor_Data() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
Humd = dht11.readHumidity();
// Read temperature as Celsius (the default)
Temp = dht11.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(Humd) || isnan(Temp)) {
Serial.println();
Serial.println(F("Failed to read from DHT sensor!"));
Serial.println();
Status_Read_Sensor = "Failed";
Temp = 0.00;
Humd = 0;
} else {
Status_Read_Sensor = "Success";
}
Serial.println();
Serial.println("-------------");
Serial.print(F("Status_Read_Sensor : "));
Serial.print(Status_Read_Sensor);
Serial.print(F(" | Humidity : "));
Serial.print(Humd);
Serial.print(F("% | Temperature : "));
Serial.print(Temp);
Serial.println(F("°C"));
Serial.println("-------------");
}
//________________________________________________________________________________
//
________________________________________________________________________________Rea
d_Switches_State()
// Subroutine to get the state of the Switches.
void Read_Switches_State() {
if (digitalRead(Switch_1_PIN) == LOW) Switch_1_State = "On";
if (digitalRead(Switch_1_PIN) == HIGH) Switch_1_State = "Off";
Serial.println();
Serial.println("-------------");
Serial.print(F("Switch_1 : "));
Serial.print(Switch_1_State);
Serial.print(F(" | Switch_2 : "));
Serial.println(Switch_2_State);
Serial.println("-------------");
}
//________________________________________________________________________________
//
________________________________________________________________________________VOI
D SETUP()
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
delay(1000);
pinMode(Switch_1_PIN, INPUT_PULLUP);
pinMode(Switch_2_PIN, INPUT_PULLUP);
pinMode(On_Board_LED_PIN, OUTPUT);
digitalWrite(On_Board_LED_PIN, LOW);
Serial.println();
Serial.println("WiFi connected");
Serial.println("------------");
//::::::::::::::::::
//----------------------------------------
delay(100);
Serial.println();
Serial.println("DHT11 Begin");
Serial.println();
delay(1000);
dht11.begin();
delay(2000);
}
//________________________________________________________________________________
//
________________________________________________________________________________VOI
D LOOP()
void loop() {
// put your main code here, to run repeatedly:
Serial.println();
Serial.println("-------------");
Serial.println("Send data to Google Spreadsheet...");
Serial.print("URL : ");
Serial.println(Send_Data_URL);
http.end();
//::::::::::::::::::
digitalWrite(On_Board_LED_PIN, LOW);
Serial.println("-------------");
}
//----------------------------------------
delay(10000);
}
//________________________________________________________________________________
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<
//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> 03_Test_LCD_and_LEDs
#include <LiquidCrystal_I2C.h>
#define LED_1_PIN 12
#define LED_2_PIN 13
// Initialize the LiquidCrystal_I2C lib as "lcd" and set the LCD I2C address to
0x27 and set the LCD configuration to 16 x 2.
// In general, the address of a 16x2 I2C LCD is "0x27".
// However, if the address "0x27" doesn't work, you can find out the address with
"i2c_scanner". Look here : https://playground.arduino.cc/Main/I2cScanner/
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
delay(1000);
pinMode(LED_1_PIN, OUTPUT);
pinMode(LED_2_PIN, OUTPUT);
digitalWrite(LED_1_PIN, LOW);
digitalWrite(LED_2_PIN, HIGH);
// Initialize lcd.
lcd.init();
// Turn on the LED backlight on the LCD.
lcd.backlight();
// Clean the LCD display.
lcd.clear();
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED_1_PIN, !digitalRead(LED_1_PIN));
digitalWrite(LED_2_PIN, !digitalRead(LED_2_PIN));
lcd.setCursor(0,0);
if (digitalRead(LED_1_PIN) == HIGH) lcd.print("LED 1 : ON ");
if (digitalRead(LED_1_PIN) == LOW) lcd.print("LED 1 : OFF");
lcd.setCursor(0,1);
if (digitalRead(LED_2_PIN) == HIGH) lcd.print("LED 2 : ON ");
if (digitalRead(LED_2_PIN) == LOW) lcd.print("LED 2 : OFF");
delay(2000);
}
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<
//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> 04_Read_Data_from_Google_Sheets
//----------------------------------------Including the libraries.
#include "WiFi.h"
#include <HTTPClient.h>
#include <LiquidCrystal_I2C.h>
//----------------------------------------
// Defines the location number for the custom degree character to be displayed on
the LCD.
#define degree_Char_Num 0
// Initialize the LiquidCrystal_I2C lib as "lcd" and set the LCD I2C address to
0x27 and set the LCD configuration to 16 x 2.
// In general, the address of a 16x2 I2C LCD is "0x27".
// However, if the address "0x27" doesn't work, you can find out the address with
"i2c_scanner". Look here : https://playground.arduino.cc/Main/I2cScanner/
LiquidCrystal_I2C lcd(0x27,16,2);
//
________________________________________________________________________________get
Value()
// String function to process the data (Split String).
// I got this from : https://www.electroniclinic.com/reyax-lora-based-multiple-
sensors-monitoring-using-arduino/
String getValue(String data, char separator, int index) {
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
//
________________________________________________________________________________VOI
D SETUP()
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
delay(1000);
pinMode(On_Board_LED_PIN, OUTPUT);
pinMode(LED_1_PIN, OUTPUT);
pinMode(LED_2_PIN, OUTPUT);
digitalWrite(LED_1_PIN, LOW);
digitalWrite(LED_2_PIN, LOW);
// Initialize lcd.
lcd.init();
// Turn on the LED backlight on the LCD.
lcd.backlight();
// Custom characters for degree symbols.
lcd.createChar(degree_Char_Num, degree_Char);
// Clean the LCD display.
lcd.clear();
// Serial.println();
// Serial.println("-------------");
// Serial.println("WIFI mode : STA");
// Serial.println("-------------");
WiFi.mode(WIFI_STA);
delay(1000);
//----------------------------------------
WiFi.begin(ssid, password);
digitalWrite(On_Board_LED_PIN, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("WiFi connected");
lcd.setCursor(0,1);
lcd.print("successfully.");
delay(1000);
// Serial.println();
// Serial.println("WiFi connected");
// Serial.println("------------");
//::::::::::::::::::
//----------------------------------------
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Get data from");
lcd.setCursor(0,1);
lcd.print("Google Sheets");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Please wait...");
delay(1000);
}
//________________________________________________________________________________
//
________________________________________________________________________________VOI
D LOOP()
void loop() {
// put your main code here, to run repeatedly:
Serial.println();
Serial.println("-------------");
Serial.println("Read data from Google Spreadsheet...");
Serial.print("URL : ");
Serial.println(Read_Data_URL);
http.end();
//::::::::::::::::::
digitalWrite(On_Board_LED_PIN, LOW);
Serial.println("-------------");
if (Status_Read_Sensor == "Success") {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp : ");
lcd.print(Temp);
lcd.print(" ");
lcd.write(degree_Char_Num);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Humd : ");
lcd.print(Humd);
lcd.print(" %");
}
if (Status_Read_Sensor == "Failed") {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp : Err");
lcd.setCursor(0,1);
lcd.print("Humd : Err");
}
delay(10000);
}
//________________________________________________________________________________
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<