#include <WiFi.h>
#include <DHT.h>
#include <HTTPClient.h>
//[email protected]
//kirk930622
//[email protected]
const char* sheetId = "1Yx4Uf4c3kYtsm3d0Gro8oDJapcvuocl6g9yGSMzb_I4";
const char* apiKey = "ba96d9ef509ba71ae4ae8c9973af492722bfde16";
const char* sheetName = "Sheet1"; // Replace with your sheet name
//const int columnIndex; // Replace with your desired column index
//const int rowIndex; // Replace with your desired row index
DHT dht(13, DHT22);
String temp="";
void setup() {
Serial.begin(115200);
//WIFI Setup
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
Serial.println("google sheets Sending\n");
temp = dht.readTemperature();
Serial.println(temp);
sendDataToGoogleSheets(123, 2, 3); // Replace with your data, column index, and row index
delay(1000);
}
}
// google Sheets
void sendDataToGoogleSheets(int data, int columnIndex, int rowIndex) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "https://sheets.googleapis.com/v4/spreadsheets/" + String(sheetId) +
"/values/" + sheetName + "!A" + String(rowIndex) + ":append?valueInputOption=RAW&key=" + String(apiKey);
String payload = "{\"majorDimension\":\"ROWS\",\"values\": [";
payload += "[";
for (int i = 1; i <= columnIndex; i++) {
if (i == columnIndex) {
payload += String(data);
} else {
payload += "\"\",";
}
}
payload += "]";
payload += "]}";
http.begin(url);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
}