Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Rui Santos
- Complete project details at https://RandomNerdTutorials.com/esp32-datalogging-google-sheets/
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
- Adapted from the examples of the Library Google Sheet Client Library for Arduino devices: https://github.com/mobizt/ESP-Google-Sheet-Client
- */
- #include <Arduino.h>
- #include <WiFi.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_BME280.h>
- #include "time.h"
- #include <ESP_Google_Sheet_Client.h>
- // For SD/SD_MMC mounting helper
- #include <GS_SDHelper.h>
- #define WIFI_SSID "REPLACE_WITH_YOUR_SSID"
- #define WIFI_PASSWORD "REPLACE_WITH_YOUR_PASSWORD"
- // Google Project ID
- #define PROJECT_ID "REPLACE_WITH_YOUR_PROJECT_ID"
- // Service Account's client email
- #define CLIENT_EMAIL "REPLACE_WITH_YOUR_CLIENT_EMAIL"
- // Service Account's private key
- const char PRIVATE_KEY[] PROGMEM = "-----BEGIN PRIVATE KEY-----\ REPLACE_WITH_YOUR_PRIVATE_KEY\n-----END PRIVATE KEY-----\n";
- // The ID of the spreadsheet where you'll publish the data
- const char spreadsheetId[] = "YOUR_SPREADSHEET_ID";
- // Timer variables
- unsigned long lastTime = 0;
- unsigned long timerDelay = 30000;
- // Token Callback function
- void tokenStatusCallback(TokenInfo info);
- // BME280 I2C
- Adafruit_BME280 bme;
- // Variables to hold sensor readings
- float temp;
- float hum;
- float pres;
- // NTP server to request epoch time
- const char* ntpServer = "pool.ntp.org";
- // Variable to save current epoch time
- unsigned long epochTime;
- // Function that gets current epoch time
- unsigned long getTime() {
- time_t now;
- struct tm timeinfo;
- if (!getLocalTime(&timeinfo)) {
- //Serial.println("Failed to obtain time");
- return(0);
- }
- time(&now);
- return now;
- }
- void setup(){
- Serial.begin(115200);
- Serial.println();
- Serial.println();
- //Configure time
- configTime(0, 0, ntpServer);
- // Initialize BME280 sensor
- if (!bme.begin(0x76)) {
- Serial.println("Could not find a valid BME280 sensor, check wiring!");
- while (1);
- }
- GSheet.printf("ESP Google Sheet Client v%s\n\n", ESP_GOOGLE_SHEET_CLIENT_VERSION);
- // Connect to Wi-Fi
- WiFi.setAutoReconnect(true);
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- Serial.print("Connecting to Wi-Fi");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(1000);
- }
- Serial.println();
- Serial.print("Connected with IP: ");
- Serial.println(WiFi.localIP());
- Serial.println();
- // Set the callback for Google API access token generation status (for debug only)
- GSheet.setTokenCallback(tokenStatusCallback);
- // Set the seconds to refresh the auth token before expire (60 to 3540, default is 300 seconds)
- GSheet.setPrerefreshSeconds(10 * 60);
- // Begin the access token generation for Google API authentication
- GSheet.begin(CLIENT_EMAIL, PROJECT_ID, PRIVATE_KEY);
- }
- void loop(){
- // Call ready() repeatedly in loop for authentication checking and processing
- bool ready = GSheet.ready();
- if (ready && millis() - lastTime > timerDelay){
- lastTime = millis();
- FirebaseJson response;
- Serial.println("\nAppend spreadsheet values...");
- Serial.println("----------------------------");
- FirebaseJson valueRange;
- // New BME280 sensor readings
- temp = bme.readTemperature();
- //temp = 1.8*bme.readTemperature() + 32;
- hum = bme.readHumidity();
- pres = bme.readPressure()/100.0F;
- // Get timestamp
- epochTime = getTime();
- valueRange.add("majorDimension", "COLUMNS");
- valueRange.set("values/[0]/[0]", epochTime);
- valueRange.set("values/[1]/[0]", temp);
- valueRange.set("values/[2]/[0]", hum);
- valueRange.set("values/[3]/[0]", pres);
- // For Google Sheet API ref doc, go to https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append
- // Append values to the spreadsheet
- bool success = GSheet.values.append(&response /* returned response */, spreadsheetId /* spreadsheet Id to append */, "Sheet1!A1" /* range to append */, &valueRange /* data range to append */);
- if (success){
- response.toString(Serial, true);
- valueRange.clear();
- }
- else{
- Serial.println(GSheet.errorReason());
- }
- Serial.println();
- Serial.println(ESP.getFreeHeap());
- }
- }
- void tokenStatusCallback(TokenInfo info){
- if (info.status == token_status_error){
- GSheet.printf("Token info: type = %s, status = %s\n", GSheet.getTokenType(info).c_str(), GSheet.getTokenStatus(info).c_str());
- GSheet.printf("Token error: %s\n", GSheet.getTokenError(info).c_str());
- }
- else{
- GSheet.printf("Token info: type = %s, status = %s\n", GSheet.getTokenType(info).c_str(), GSheet.getTokenStatus(info).c_str());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement