Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #include <LiquidCrystal.h> //Inclui a biblioteca do LCD
  2. #include <ESP8266WiFi.h>
  3. #include <time.h>
  4.  
  5. int const reedswitch = 12; // Pino 6
  6.  
  7. const char* ssid = "Escr. Mobile";
  8. const char* senha = "xCt6M6B5";
  9.  
  10. //Data Hora
  11. int timezone = -4;
  12. int dst = 0;
  13.  
  14. int val = 0;
  15. int antigo_val = 0;
  16. int contador = 0; //Contagem comutaçao
  17. float dados;
  18. String horadia;
  19. //String hora = 16;
  20.  
  21.  
  22. String apiKey = "G1BMBS0R40F2SJKK"; //Write API Key
  23. const char *servidor_destino = "api.thingspeak.com";
  24.  
  25. void setup() {
  26.  
  27. pinMode (reedswitch, INPUT_PULLUP);//pullup usado para resistores
  28. Serial.begin(115200);
  29. digitalWrite(12, HIGH);
  30. WiFi.enableAP(0);
  31.  
  32. Serial.print("\n");
  33. Serial.print("Conectando a rede:");
  34. Serial.println(ssid);
  35. WiFi.begin(ssid, senha);
  36.  
  37. while (WiFi.status() != WL_CONNECTED) {
  38. delay(500);
  39. Serial.print(".");
  40.  
  41. }
  42. Serial.println("WiFi conectado");
  43. Serial.print(WiFi.localIP());
  44. Serial.print("\n\n");
  45.  
  46. configTime(timezone * 3600, dst * 0, "pool.ntp.org", "time.nist.gov");
  47. }
  48.  
  49. void loop() {
  50. WiFiClient cliente;
  51. cliente.connect(servidor_destino, 80);
  52. val = digitalRead(reedswitch); //Lendo o Status atual do pino do redswitch
  53.  
  54. time_t now;
  55. struct tm * timeinfo;
  56. time(&now);
  57. timeinfo = localtime(&now);
  58. horadia = (timeinfo->tm_hour);
  59. //Serial.println(horadia);
  60.  
  61. if (horaatual == "10")
  62.  
  63. dados = 0;
  64. Serial.println(dados);
  65. Serial.println("Contador Zerado");
  66. delay(30000);
  67.  
  68. String postStr = apiKey;
  69. postStr += "&field1=";
  70. postStr += String(dados);
  71. postStr += "\r\n\r\n";
  72.  
  73. cliente.print("POST /update HTTP/1.1\n");
  74. cliente.print("Host:api.thingspeak.com\n");
  75. cliente.print("Connection:close\n");
  76. cliente.print("X-THINGSPEAKAPIKEY:" + apiKey + "\n");
  77. cliente.print("Content-Type: application/x-www-form-urlencoded\n");
  78. cliente.print("Content-Length:");
  79. cliente.print(postStr.length());
  80. cliente.print("\n\n");
  81. cliente.print(postStr);
  82. }
  83.  
  84. else if ((val == LOW) && (antigo_val == HIGH)) {
  85. delay(10);
  86.  
  87. contador = contador + 1;
  88. antigo_val = val; //Valor antigo vale o atual
  89.  
  90. dados = (contador * 0.25);
  91.  
  92. String postStr = apiKey;
  93. postStr += "&field1=";
  94. postStr += String(dados);
  95. postStr += "\r\n\r\n";
  96.  
  97. cliente.print("POST /update HTTP/1.1\n");
  98. cliente.print("Host:api.thingspeak.com\n");
  99. cliente.print("Connection:close\n");
  100. cliente.print("X-THINGSPEAKAPIKEY:" + apiKey + "\n");
  101. cliente.print("Content-Type: application/x-www-form-urlencoded\n");
  102. cliente.print("Content-Length:");
  103. cliente.print(postStr.length());
  104. cliente.print("\n\n");
  105. cliente.print(postStr);
  106.  
  107. Serial.print("Medida de chuva: ");
  108.  
  109. Serial.print(dados);
  110. Serial.println(" mm");
  111. digitalWrite(reedswitch, HIGH);
  112. delay(300);
  113. digitalWrite(reedswitch, LOW);
  114. delay(300);
  115. Serial.println("Dados enviados para Nuvem\n\n");
  116.  
  117. }
  118. else {
  119. antigo_val = val;
  120. cliente.stop();
  121.  
  122. }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement