Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266WiFiMulti.h>
- #include <ESP8266HTTPClient.h>
- #define USE_SERIAL Serial
- ESP8266WiFiMulti WiFiMulti;
- unsigned long startTime;
- unsigned long finishTime;
- unsigned long totalTime;
- int data1;
- int data2;
- bool isStarted = false;
- bool isReady = false;
- String url = "http://iot.knowbase.ee/get_data.php?device_id=1&time=";
- void setup() {
- USE_SERIAL.begin(115200);
- USE_SERIAL.println();
- USE_SERIAL.println();
- USE_SERIAL.println();
- for(uint8_t t = 4; t > 0; t--) {
- USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
- USE_SERIAL.flush();
- delay(1000);
- }
- WiFi.mode(WIFI_STA);
- WiFiMulti.addAP("TLU", "");
- // put your setup code here, to run once:
- pinMode(D3, INPUT);
- pinMode(D4, INPUT);
- Serial.begin(115200);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- data1 = digitalRead(D3);
- data2 = digitalRead(D4);
- if(data1 == 0){
- isReady = true;
- Serial.println("IS READY");
- }
- if(data1 == 1 && isStarted == false && isReady == true){
- startTime = millis();
- Serial.println("Started at: ");
- Serial.println(startTime);
- isStarted = true;
- isReady = false;
- }
- if(data2 == 0 && isStarted == true){
- Serial.println("Finished at");
- finishTime = millis();
- Serial.println(finishTime);
- totalTime = finishTime - startTime;
- Serial.println("Total time: ");
- Serial.println(totalTime);
- isStarted = false;
- // wait for WiFi connection
- if((WiFiMulti.run() == WL_CONNECTED && totalTime > 0)) {
- HTTPClient http;
- USE_SERIAL.print("[HTTP] begin...\n");
- // configure traged server and url
- //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
- http.begin(url+totalTime); //HTTP
- USE_SERIAL.print("[HTTP] GET...\n");
- // start connection and send HTTP header
- int httpCode = http.GET();
- // httpCode will be negative on error
- if(httpCode > 0) {
- // HTTP header has been send and Server response header has been handled
- USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
- // file found at server
- if(httpCode == HTTP_CODE_OK) {
- String payload = http.getString();
- USE_SERIAL.println(payload);
- }
- } else {
- USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
- }
- http.end();
- }
- }
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement