Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #include <Arduino.h>
  2.  
  3. #include <ESP8266WiFi.h>
  4. #include <ESP8266WiFiMulti.h>
  5.  
  6. #include <ESP8266HTTPClient.h>
  7.  
  8. #define USE_SERIAL Serial
  9.  
  10. ESP8266WiFiMulti WiFiMulti;
  11.  
  12. unsigned long startTime;
  13. unsigned long finishTime;
  14. unsigned long totalTime;
  15. int data1;
  16. int data2;
  17. bool isStarted = false;
  18. bool isReady = false;
  19. String url = "http://iot.knowbase.ee/get_data.php?device_id=1&time=";
  20.  
  21. void setup() {
  22. USE_SERIAL.begin(115200);
  23.  
  24. USE_SERIAL.println();
  25. USE_SERIAL.println();
  26. USE_SERIAL.println();
  27.  
  28. for(uint8_t t = 4; t > 0; t--) {
  29. USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
  30. USE_SERIAL.flush();
  31. delay(1000);
  32. }
  33.  
  34. WiFi.mode(WIFI_STA);
  35. WiFiMulti.addAP("TLU", "");
  36. // put your setup code here, to run once:
  37. pinMode(D3, INPUT);
  38. pinMode(D4, INPUT);
  39. Serial.begin(115200);
  40.  
  41. }
  42.  
  43.  
  44.  
  45. void loop() {
  46. // put your main code here, to run repeatedly:
  47. data1 = digitalRead(D3);
  48. data2 = digitalRead(D4);
  49.  
  50.  
  51. if(data1 == 0){
  52. isReady = true;
  53. Serial.println("IS READY");
  54. }
  55.  
  56. if(data1 == 1 && isStarted == false && isReady == true){
  57. startTime = millis();
  58. Serial.println("Started at: ");
  59. Serial.println(startTime);
  60. isStarted = true;
  61. isReady = false;
  62. }
  63. if(data2 == 0 && isStarted == true){
  64. Serial.println("Finished at");
  65. finishTime = millis();
  66. Serial.println(finishTime);
  67. totalTime = finishTime - startTime;
  68. Serial.println("Total time: ");
  69. Serial.println(totalTime);
  70. isStarted = false;
  71. // wait for WiFi connection
  72. if((WiFiMulti.run() == WL_CONNECTED && totalTime > 0)) {
  73.  
  74. HTTPClient http;
  75.  
  76. USE_SERIAL.print("[HTTP] begin...\n");
  77. // configure traged server and url
  78. //http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
  79. http.begin(url+totalTime); //HTTP
  80.  
  81. USE_SERIAL.print("[HTTP] GET...\n");
  82. // start connection and send HTTP header
  83. int httpCode = http.GET();
  84.  
  85. // httpCode will be negative on error
  86. if(httpCode > 0) {
  87. // HTTP header has been send and Server response header has been handled
  88. USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
  89.  
  90. // file found at server
  91. if(httpCode == HTTP_CODE_OK) {
  92. String payload = http.getString();
  93. USE_SERIAL.println(payload);
  94. }
  95. } else {
  96. USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  97. }
  98.  
  99. http.end();
  100. }
  101. }
  102. delay(10);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement