Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h> //Inclui a biblioteca do LCD
- #include <ESP8266WiFi.h>
- #include <time.h>
- int const reedswitch = 12; // Pino 6
- const char* ssid = "Escr. Mobile";
- const char* senha = "xCt6M6B5";
- //Data Hora
- int timezone = -4;
- int dst = 0;
- int val = 0;
- int antigo_val = 0;
- int contador = 0; //Contagem comutaçao
- float dados;
- String horadia;
- //String hora = 16;
- String apiKey = "G1BMBS0R40F2SJKK"; //Write API Key
- const char *servidor_destino = "api.thingspeak.com";
- void setup() {
- pinMode (reedswitch, INPUT_PULLUP);//pullup usado para resistores
- Serial.begin(115200);
- digitalWrite(12, HIGH);
- WiFi.enableAP(0);
- Serial.print("\n");
- Serial.print("Conectando a rede:");
- Serial.println(ssid);
- WiFi.begin(ssid, senha);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("WiFi conectado");
- Serial.print(WiFi.localIP());
- Serial.print("\n\n");
- configTime(timezone * 3600, dst * 0, "pool.ntp.org", "time.nist.gov");
- }
- void loop() {
- WiFiClient cliente;
- cliente.connect(servidor_destino, 80);
- val = digitalRead(reedswitch); //Lendo o Status atual do pino do redswitch
- time_t now;
- struct tm * timeinfo;
- time(&now);
- timeinfo = localtime(&now);
- horadia = (timeinfo->tm_hour);
- //Serial.println(horadia);
- if (horaatual == "10")
- dados = 0;
- Serial.println(dados);
- Serial.println("Contador Zerado");
- delay(30000);
- String postStr = apiKey;
- postStr += "&field1=";
- postStr += String(dados);
- postStr += "\r\n\r\n";
- cliente.print("POST /update HTTP/1.1\n");
- cliente.print("Host:api.thingspeak.com\n");
- cliente.print("Connection:close\n");
- cliente.print("X-THINGSPEAKAPIKEY:" + apiKey + "\n");
- cliente.print("Content-Type: application/x-www-form-urlencoded\n");
- cliente.print("Content-Length:");
- cliente.print(postStr.length());
- cliente.print("\n\n");
- cliente.print(postStr);
- }
- else if ((val == LOW) && (antigo_val == HIGH)) {
- delay(10);
- contador = contador + 1;
- antigo_val = val; //Valor antigo vale o atual
- dados = (contador * 0.25);
- String postStr = apiKey;
- postStr += "&field1=";
- postStr += String(dados);
- postStr += "\r\n\r\n";
- cliente.print("POST /update HTTP/1.1\n");
- cliente.print("Host:api.thingspeak.com\n");
- cliente.print("Connection:close\n");
- cliente.print("X-THINGSPEAKAPIKEY:" + apiKey + "\n");
- cliente.print("Content-Type: application/x-www-form-urlencoded\n");
- cliente.print("Content-Length:");
- cliente.print(postStr.length());
- cliente.print("\n\n");
- cliente.print(postStr);
- Serial.print("Medida de chuva: ");
- Serial.print(dados);
- Serial.println(" mm");
- digitalWrite(reedswitch, HIGH);
- delay(300);
- digitalWrite(reedswitch, LOW);
- delay(300);
- Serial.println("Dados enviados para Nuvem\n\n");
- }
- else {
- antigo_val = val;
- cliente.stop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement