Last active
July 14, 2022 01:15
-
-
Save gabrielacaesar/f48558aa8025d5cddbc38f0fd69361c3 to your computer and use it in GitHub Desktop.
codigo-do-arduino.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Código do arduino: | |
//Config----------------------------------------------------------- | |
const char* ssid = "gabrielaWiFi"; //Alterar para o nome da sua rede | |
const char* password = "66929415"; //Alterar para sua senha de rede ou "" caso seja livre | |
const char* GScriptId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // alterar para chave copiada na criação do App Script | |
//----------------------------------------------------------------- | |
String payload_base = "{\"command\": \"append_row\", \"sheet_name\": \"Página1\", \"values\": "; //Verificar se seu Sheet se chama Página1 ou alterar | |
String payload = ""; //declarando e inicializando a variavel de payload | |
const char* host = "script.google.com"; | |
const int httpsPort = 443; | |
String url = String("/macros/s/") + GScriptId + "/exec?cal"; | |
//WiFi---------------------- | |
#include <ESP8266WiFi.h> | |
#include "HTTPSRedirect.h" | |
//Objects---------------------------------- | |
HTTPSRedirect* client = nullptr; | |
//----------------------------------------- | |
//Control Variables------------------------ | |
int value0 = 0; | |
int value1 = 0; | |
//----------------------------------------- | |
void setup() // configurações de inicialização | |
{ | |
Serial.begin(115200); | |
//WiFi Setup | |
delay(500); | |
Serial.print("Conectando ao WiFi..."); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED){ Serial.print("."); delay(500); } | |
Serial.println("[OK]"); | |
//HTTPS Redirect Setup | |
client = new HTTPSRedirect(httpsPort); | |
client->setInsecure(); | |
client->setPrintResponseBody(true); | |
client->setContentTypeHeader("application/json"); | |
Serial.print("Conectando ao Google..."); | |
bool flag = false; | |
for (int i=0; i<5; i++) | |
{ | |
int retval = client->connect(host, httpsPort); | |
if (retval == 1) | |
{ | |
flag = true; | |
Serial.println("OK"); | |
break; | |
} | |
else | |
Serial.println("Error"); | |
} | |
if (!flag) | |
{ | |
Serial.print("Error"); | |
Serial.println(host); | |
return; | |
} | |
delete client; | |
client = nullptr; | |
} | |
void loop() // rotina da placa (ler e enviar os dados do sensor) | |
{ | |
Serial.println("Working"); | |
value0 ++; | |
value1 = analogRead(0); //leitura do sensor | |
static bool flag = false; | |
if (!flag) | |
{ | |
client = new HTTPSRedirect(httpsPort); | |
client->setInsecure(); | |
flag = true; | |
client->setPrintResponseBody(true); | |
client->setContentTypeHeader("application/json"); | |
} | |
if (client != nullptr) { if (!client->connected()){ client->connect(host, httpsPort); } } | |
else { Serial.println("Error"); } | |
payload = payload_base + "\"" + value0 + "," + value1 + "\"}"; // adiciona os dados a serem enviados para a planilha no payload | |
Serial.println("Enviando..."); | |
if(client->POST(url, host, payload)){ Serial.println(" OK"); } | |
else { Serial.println("Error"); } | |
delay(10000); | |
} | |
payload = payload_base + "\"" + value0 + "," + value1 + "\"}";// adiciona os dados a serem enviados para a planilha no payload | |
Serial.println("Enviando..."); | |
if(client->POST(url, host, payload)){ Serial.println(" OK"); } | |
else { Serial.println("Error"); } | |
delay(10000); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment