Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1.  
  2. #include <NTPtimeESP.h>
  3. #include <ESP8266WiFi.h>
  4.  
  5. #define DEBUG_ON
  6.  
  7. #ifdef DEBUG_ON
  8.   #define Sprint(a) (Serial.print(a))
  9.   #define Sprintln(a) (Serial.println(a)
  10.  
  11. #else
  12.   #define Sprint(a)
  13.   #define Sprintln(a)
  14. #endif
  15.  
  16. #ifdef SECONDS_MODE
  17.   #define dateTime.hour dateTime.second
  18. #endif
  19.  
  20. //NTP-clock
  21. NTPtime NTPch("nl.pool.ntp.org");   // Choose server pool as required
  22. strDateTime dateTime;
  23. int previousHour;
  24.  
  25. //Wifi & thingspeak
  26. String apiKey = "IKOLI389JPQ71CKZ";
  27. const char *ssid =  "Catharijnesingel93b";
  28. const char *pass =  "Catharijnesingel93b!";
  29. const char* server = "api.thingspeak.com";
  30. WiFiClient client;
  31.  
  32. const int MOISTURE_PIN = A0;
  33. const char WATERPUMP_PIN = 12;
  34. const int WATER_THRESHOLD = 650; //higher means dryer
  35. const int WATERING_TIME = 15000;
  36. const int WATERING_HOUR = 20;
  37.  
  38. //AnalogRead
  39. int reading = -1;
  40.  
  41. void setup() {
  42.   Serial.begin(115200);
  43.   pinMode(A0, INPUT);
  44.   pinMode(MOISTURE_PIN, INPUT);
  45.   pinMode(WATERPUMP_PIN, OUTPUT);
  46.  
  47.   //Connect to wifi
  48.   WiFi.mode(WIFI_STA);
  49.   WiFi.begin(ssid, pass);
  50.   while (WiFi.status() != WL_CONNECTED) {
  51.     delay(500);
  52.     Serial.print(".");
  53.   }
  54.   //Connection succesfull
  55.   Serial.println("");
  56.   Serial.print("Connected to ");
  57.   Serial.println(ssid);
  58.   Serial.print("IP address: ");
  59.   Serial.println(WiFi.localIP());  //IP address assigned to your ESP
  60.  
  61.   //Get NTP time
  62.   Serial.println("Getting NTP time");
  63.   dateTime = NTPch.getNTPtime(1.0, 1);
  64.   while (!(dateTime.valid)) {
  65.     Serial.println(".");
  66.     dateTime = NTPch.getNTPtime(1.0, 1);
  67.     delay(10);
  68.   }
  69.   //NTP time retrieved
  70.   Serial.println("");
  71.   previousHour = dateTime.hour;
  72.   Serial.print("Fetched currenLt hour is: ");
  73.   Serial.println(previousHour);
  74.   digitalWrite(WATERPUMP_PIN, LOW);
  75. }
  76.  
  77. void waterPlants() {
  78.   //activates waterpump
  79.   digitalWrite(WATERPUMP_PIN, HIGH);
  80.   delay(WATERING_TIME);
  81.   digitalWrite(WATERPUMP_PIN, LOW);
  82.   delay(30000); //Let water get into soil before measuring
  83. }
  84.  
  85. void updateWeb(int value) {
  86.   //uploads 'value' to thingspeak
  87.   if (client.connect(server, 80)) { //   "184.106.153.149" or api.thingspeak.com
  88.     String postStr = apiKey;
  89.     postStr += "&field1=";
  90.     postStr += value;
  91.     postStr += "\r\n\r\n";
  92.     client.print("POST /update HTTP/1.1\n");
  93.     client.print("Host: api.thingspeak.com\n");
  94.     client.print("Connection: close\n");
  95.     client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
  96.     client.print("Content-Type: application/x-www-form-urlencoded\n");
  97.     client.print("Content-Length: ");
  98.     client.print(postStr.length());
  99.     client.print("\n\n");
  100.     client.print(postStr);
  101.   }
  102.   client.stop();
  103. }
  104.  
  105. void executeCycle(int pCurrentHour) {
  106.   Serial.println("new hour, executing cycle");
  107.   Serial.print("previous hour: " + previousHour);
  108.   Serial.println("     current hour: " + pCurrentHour);
  109.   int reading = analogRead(MOISTURE_PIN);
  110.   Serial.println("current reading: " + reading);
  111.   updateWeb(reading);
  112.  
  113.   if (pCurrentHour == WATERING_HOUR) {
  114.     Serial.println("It's watering time: " + dateTime.hour);
  115.     if (reading > WATER_THRESHOLD) {
  116.       Serial.print("threshold broken, threshold: "+WATER_THRESHOLD);
  117.       Serial.println("      reading: "+reading);
  118.       waterPlants();
  119.       reading = analogRead(MOISTURE_PIN);
  120.       delay(1000); //Small delay for analogRead
  121.       Serial.println("reading after watering: "+reading);
  122.       updateWeb(reading); //Upload again after watering
  123.     }
  124.   }
  125.   Serial.print("Update 'previousHour' -> previousHour:"+previousHour);
  126.   Serial.println("    currentHour:"+pCurrentHour);
  127.   previousHour = pCurrentHour;
  128.   Serial.println("updated 'previousHour' is now: " +previousHour);
  129. }
  130.  
  131. void loop() {
  132.   dateTime = NTPch.getNTPtime(1.0, 1);
  133.  
  134.   if (dateTime.valid) { //check if time in in right format
  135.     int currentHour = dateTime.hour;
  136.     if (currentHour == 0 && previousHour == 23 || currentHour == previousHour + 1) { // the only correct hour transitions
  137.       executeCycle(currentHour);
  138.     }
  139.       else if (currentHour == previousHour) {
  140.         Serial.print("not a new hour, currentHour: " + currentHour);
  141.         delay(100);
  142.         //TODO: deepsleep
  143.         //delay(100); //remove this delay after deepsleep has been implemented
  144.       }
  145.       else {
  146.         Serial.println("faulty time detected");
  147.       }
  148.   }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement