Ask Your Question

Revision history [back]

To retrieve data from a Google Sheet using an ESP32 or ESP8266, you can follow these steps:

  1. Create a Google Sheet and add some data.

  2. Publish the sheet as a CSV file by going to File > Publish to the web, and choose Comma-separated values (.csv) in the drop-down menu.

  3. Copy the CSV link from the window and save it.

  4. Install the ESPAsyncWebServer library on your ESP32/ESP8266.

  5. Set up an HTTP server on the ESP32/ESP8266 using the ESPAsyncWebServer library.

  6. Set up the server to handle GET requests and parse the CSV link.

  7. Use the HTTPClient library to send a GET request to the CSV URL and obtain the data.

  8. Parse the CSV data and use it as needed.

Here is a basic example code:

#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <HTTPClient.h>
#include <CSV.h>

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

AsyncWebServer server(80);

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

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  // Set up HTTP server
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    HTTPClient http;    
    http.begin("your_CSV_link");
    int httpCode = http.GET();
    if (httpCode > 0) {
      String csvData = http.getString();
      Serial.println(csvData); // Print CSV data to Serial monitor

      // Parse CSV data
      CSV csv;
      int numCols = csv.setRows(csvData.c_str());
      for (int i = 0; i < numCols; ++i){
        String value = csv.getValue(1, i);
        Serial.println(value); // Print each value in row 1
      }
    }
    http.end();
  });

  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
}

Note that this is just a basic example and you may need to modify the code to fit your specific requirements. Also, make sure to keep the CSV link private and secure if it contains sensitive data.