Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <SPI.h>
- char thingSpeakAddress[] = "api.thingspeak.com";
- String writeAPIKey = "[chave_de_escrita]";
- const int updateThingSpeakInterval = 17 * 1000;
- char ssid[] = "[nome_da_rede]";
- char pass[] = "[senha_da_rede]";
- int status = WL_IDLE_STATUS;
- WiFiClient client;
- String stringVal = "";
- long lastConnectionTime = 0;
- boolean lastConnected = false;
- int failedCounter = 0;
- void setup() {
- Serial.begin(115200);
- while (status != WL_CONNECTED) {
- Serial.print("Conectando...");
- status = WiFi.begin(ssid, pass);
- delay(5000);
- }
- Serial.println("Conexão realizada com sucesso!");
- }
- void loop() {
- while (client.available()) {
- char c = client.read();
- Serial.print(c);
- }
- if (!client.connected() && lastConnected ) {
- Serial.println("...disconnected");
- Serial.println();
- client.stop();
- }
- if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
- updateThingSpeak("field1=teste");
- lastConnected = client.connected();
- }
- void updateThingSpeak(String tsdata) {
- if (client.connect(thingSpeakAddress, 80)) {
- client.print("POST /update HTTP/1.1\n");
- client.print("Host: api.thingspeak.com\n");
- client.print("Connection: close\n");
- client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
- client.print("Content-Type: application/x-www-form-urlencoded\n");
- client.print("Content-Length: ");
- client.print(tsdata.length());
- client.print("\n\n");
- client.print(tsdata);
- lastConnectionTime = millis();
- if (client.connected()) {
- Serial.println("Conectando ao ThingSpeak...");
- Serial.println();
- failedCounter = 0;
- } else {
- failedCounter++;
- Serial.println("Connexão ao ThingSpeak falhou ("+String(failedCounter, DEC)+")");
- Serial.println();
- }
- lastConnectionTime = millis();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement