To retrieve data from a Google Sheet using an ESP32 or ESP8266, you can follow these steps:
Create a Google Sheet and add some data.
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.
Copy the CSV link from the window and save it.
Install the ESPAsyncWebServer library on your ESP32/ESP8266.
Set up an HTTP server on the ESP32/ESP8266 using the ESPAsyncWebServer library.
Set up the server to handle GET requests and parse the CSV link.
Use the HTTPClient library to send a GET request to the CSV URL and obtain the data.
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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-04-10 11:00:00 +0000
Seen: 8 times
Last updated: Aug 05 '22
How can I open a csv file with an unspecified encoding format?
How to remove rows from a CSV file stored in a Google Cloud Storage bucket?
What does it mean to export an empty CSV in Postgres/PGAdmin?
How can spatial CSV data be loaded and organized effectively?
How can data in csv and json format be uploaded and utilized as a data set in openEMR?