template | title | thumbnail | language | tags | |
---|---|---|---|---|---|
true |
Recording data into a google sheet using an ESP32 |
thumbnail.jpg |
en |
|
This uses a "google apps script"(.gs
) to create a HTTP endopoint (a "url") which you can then use with your ESP32 to send data. The .gs
will then timestamp that data and insert it into a google sheet.
- an ESP32
- a router with internet access
- a Google™ account
-
create a blank google sheet and open it
-
go into
Extensions > Apps Script
-
inside this template you will have a
googleAppsScriptEndpoint.gs
file, paste its content into the automatically generatedCode.gs
-
Inside the url of your google sheet you will find its id, for example
1p7Bo8z9aC3FG7HiytJiOzF2Nri0Efc9hn4c69zhF6XE
is the ID inside ofhttps://docs.google.com/spreadsheets/d/1p7Bo8z9aC3FG7HiytJiOzF2Nri0Efc9hn4c69zhF6XE/edit#gid=0
-
Enter that ID of your google sheet into the
sheet_id
variable -
In the "Apps Script" editor, hit the "Deploy > New Deployment" button in the top right corner
-
Select the type "Web app"
-
in "who has access" select "Anyone"
-
copy the "deployment ID" and put it in the ESP32 code, the variable is called
GOOGLE_DEPLOYEMENT_ID
-
Enter the proper credential for your router inside the
ssid
andpassword
variables -
Upload the code on your ESP32
-
🎉🎉🎉
For basic use, you won't need to change the .gs
.
Built as it is, you just need to call the send(String label, String value)
function to send the value of your sensor. The label is used to differenciate between two sensors.
If you're like me, you won't like that your endpoint has to be accessible to "Anyone". In which case you can look into the comments of the .gs
to learn how to enable a simplistic API token. The endpoint will only accept requests accompanied by that token.
Once you are done setting the token into the .gs
(don't forget to re-deploy) enter the token into your ESP32 code (not the hash).
This is still not perfect because the url of your endpoint and the token are both stored in your ESP32 code. Ideally we would need the ESP32 to ask for the token when it starts, that way getting the ESP32 code won't be enough for "Anybody" to be able to inject data into your dataset.
If you change stuff in the .gs
you will need to re-deploy it to apply the changes to your endpoint. This will change the deployement ID and you'll have to paste the new deployment ID into your ESP32 code and then re-upload it onto your board.