Advertisement
Ruddog

sprinkler sketch

May 23rd, 2018
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.46 KB | None | 0 0
  1. /////////////////////////////////
  2. // Generated with a lot of love//
  3. // with TUNIOT FOR ESP8266     //
  4. // Website: Easycoding.tn      //
  5. /////////////////////////////////
  6. // Antenna End of board
  7.   // A0=ADCO            // D0=GPIO16 & USER & Wake Onboard LED
  8.   // RESERVED           // D1 GPIO5
  9.   // RESERVED           // D2 GPIO4
  10.   // SD3= GPIO10        // D3=GPIO0 & FLASH
  11.   // SD2= GPIO9         // D4=GPIO2 & TXD1 Onboard LED
  12.   // SD1=MOSI & SDD1    // 3.3V=3.3V
  13.   // CMD=CS & SDCMD     // GND=GND
  14.   // SD0=MISO & SDD0    // D5=GPIO14 & HSCLK
  15.   // CLK=SCLK & SDCLK   // D6=GPIO12 & HMISO
  16.   // GND=GND            // D7=GPIO13 & RXD2 &HMOSI
  17.   // 3V3=3.3V           // D8=GPIO15 & TXD2 & HCS
  18.   // EN=EN              // RX=GPIO3 & RXD0
  19.   // RST=RST            // TX=GPIO1 & TXD0
  20.   // GND=GND            // GND=GND
  21.   // Vin=Vin            // 3V3=3.3V
  22. #include <ESP8266WiFi.h>
  23. #include <TimeLib.h>
  24. #include <WiFiUdp.h>
  25. //USE GPIO 5 for this example =D1
  26. String  ClientRequest;
  27. IPAddress staticIP707_10(192,168,86,10);
  28. IPAddress gateway707_10(192,168,86,1);
  29. IPAddress subnet707_10(255,255,255,0);
  30.  
  31. WiFiServer server(80);
  32. int MT;
  33. int DY;
  34. int YR;
  35. int HR;
  36. int MN;
  37. int SC;
  38. int WD;
  39. int BP;
  40. //int MN_Timer;
  41. //int storeMins;
  42. //int timerMinsSet;
  43. ////////////////////////////////////////////////
  44. int setHR = 9;//START TIMER AT THIS HOUR
  45. int setMN = 59;//TURN OFF GPIO PIN WHEN THIS INTERGER IS EXCEEDED
  46. int setDY;
  47. ///////////////////////////////////////////////
  48. int dur_Timer_MN = setMN + 1;
  49. int output5;
  50. char ntpServerName[] = "us.pool.ntp.org";
  51. int timeZone = -7;
  52. WiFiUDP Udp;
  53. unsigned int localPort = 8888;
  54. time_t getNtpTime();
  55.  
  56. void printDigits(int digits);
  57. void sendNTPpacket(IPAddress &address);
  58. int digitalClockDisplay(char m){
  59. int interm;
  60.  
  61. if (m=='h'){
  62. interm=hour();
  63. return interm;
  64. }
  65. if (m=='m'){
  66. interm=minute();
  67. return interm;
  68. }
  69. if (m=='s'){
  70. interm=second();
  71. return interm;
  72. }
  73. if (m=='j'){
  74. interm=day();
  75. return interm;
  76. }
  77. if (m=='n'){
  78. interm=month();
  79. return interm;
  80. }
  81. if (m=='y'){
  82. interm=year();
  83. return interm;
  84. }
  85. if (m=='w'){
  86. interm=weekday();
  87. return interm;
  88. }
  89. }
  90.  
  91.  /*-------- NTP code ----------*/
  92. const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
  93. byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
  94. time_t getNtpTime()
  95. {
  96. IPAddress ntpServerIP; // NTP server's ip address
  97. while (Udp.parsePacket() > 0) ; // discard any previously received packets
  98. Serial.println("Transmit NTP Request");
  99. // get a random server from the pool
  100. WiFi.hostByName(ntpServerName, ntpServerIP);
  101. Serial.print(ntpServerName);
  102. Serial.print(":");
  103. Serial.println(ntpServerIP);
  104. sendNTPpacket(ntpServerIP);
  105. uint32_t beginWait = millis();
  106. while (millis() - beginWait < 1500) {
  107. int size = Udp.parsePacket();
  108. if (size >= NTP_PACKET_SIZE) {
  109. Serial.println("Receive NTP Response");
  110. Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
  111. unsigned long secsSince1900;
  112. // convert four bytes starting at location 40 to a long integer
  113. secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
  114. secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
  115. secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
  116. secsSince1900 |= (unsigned long)packetBuffer[43];
  117. return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
  118. }
  119. }
  120. Serial.println("No NTP Response :-(");
  121. return 0; // return 0 if unable to get the time
  122. }
  123. void sendNTPpacket(IPAddress &address)
  124. {
  125. memset(packetBuffer, 0, NTP_PACKET_SIZE);
  126. packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  127. packetBuffer[1] = 0;     // Stratum, or type of clock
  128. packetBuffer[2] = 6;     // Polling Interval
  129. packetBuffer[3] = 0xEC;  // Peer Clock Precision
  130. packetBuffer[12] = 49;
  131. packetBuffer[13] = 0x4E;
  132. packetBuffer[14] = 49;
  133. packetBuffer[15] = 52;
  134. Udp.beginPacket(address, 123); //NTP requests are to port 123
  135. Udp.write(packetBuffer, NTP_PACKET_SIZE);
  136. Udp.endPacket();
  137.  
  138. }
  139.  
  140. void setup()
  141.  
  142. {
  143.   ClientRequest = "";
  144.   pinMode(2, OUTPUT);
  145.  
  146.  
  147. //digitalWrite(5,HIGH);//initialize pin it might be on by default
  148. Serial.begin(115200);
  149.   WiFi.disconnect();
  150.   delay(3000);
  151.   Serial.println("START");
  152.    WiFi.begin("xxxxxx","xxxxxxxxxxx");
  153.   while ((!(WiFi.status() == WL_CONNECTED))){
  154.     delay(300);
  155.     Serial.print("..");
  156. BP=0;
  157. pinMode(5,OUTPUT);
  158. digitalWrite(output5, HIGH);
  159.   }
  160.   Serial.println("Connected");
  161.   WiFi.config(staticIP707_10, gateway707_10, subnet707_10);
  162.   Serial.println("Your IP is");
  163.   Serial.println((WiFi.localIP().toString()));
  164.  
  165.   server.begin();
  166.  
  167. Udp.begin(localPort);
  168. setSyncProvider(getNtpTime);
  169. setSyncInterval(300);
  170. }
  171.  
  172.  
  173. void loop()
  174. {
  175.   delay(5000);
  176.   Serial.println();
  177.   (MT=digitalClockDisplay('n'));
  178.   (DY=digitalClockDisplay('j'));
  179.   (YR=digitalClockDisplay('y'));
  180.  
  181.   (HR=digitalClockDisplay('h'));
  182.   (MN=digitalClockDisplay('m'));
  183.   (SC=digitalClockDisplay('s'));
  184.   (WD=digitalClockDisplay('w'));
  185.   Serial.print(MT);
  186.   Serial.print('-');
  187.   Serial.print(DY);
  188.   Serial.print('-');
  189.   Serial.println(YR);
  190.   Serial.print(HR);
  191.   Serial.print(':');
  192.   Serial.print(MN);
  193.   Serial.print(':');
  194.   Serial.println(SC);
  195.   Serial.print("Weekday ");
  196.   Serial.println(WD);
  197.   Serial.println(" ");
  198.   delay(300);
  199.   Serial.println(BP);
  200.   ////////////////////////////////////////////////////////////
  201.  
  202.   if (WD == 1 ) {
  203.     Serial.println("Its Sunday");
  204.     delay(300);
  205.   }
  206.  
  207.   if (WD == 3 || (WD == 5 || WD == 7)) {
  208.     Serial.println("Not a watering day");
  209.     delay(300);
  210.   }
  211.   //BP is Button Pressed and can be either 1 or 0
  212.   //if the button is pressed it will set if statement to
  213.   //false and move onto else portion turn off valve
  214.   if (BP == 0 && (WD == 2 || (WD == 4 || WD == 6))) {
  215.     Serial.println("Allows entry to timer setting");
  216.    }
  217.  
  218.       if (HR == setHR && MN < dur_Timer_MN && BP==0) {
  219.         Serial.println("Turn on water valve");
  220.         digitalWrite(2,HIGH);
  221.         delay(300);
  222.       } else {
  223.         Serial.println("Turn off water valve");
  224.         digitalWrite(2,LOW);
  225.         delay(300);
  226.       }
  227.  
  228.  
  229.   //////////////////////////////////////////////////////////
  230.  
  231.     WiFiClient client = server.available();
  232.     if (!client) { return; }
  233.     while(!client.available()){  delay(1); }
  234.     ClientRequest = (client.readStringUntil('\r'));
  235.     client.flush();
  236.     //////////////////////////////////////////////
  237.     //Added May 8 2018 at 7:33 AM
  238.    //////////////////////////////////////////////
  239.     if (ClientRequest.indexOf("LED=ON") > 0) {
  240.       //digitalWrite(16,HIGH);
  241.       BP=1;
  242.       Serial.println(BP=1);
  243.  
  244.     }
  245.     if (ClientRequest.indexOf("LED=OFF") > 0) {
  246.      // digitalWrite(16,LOW);
  247.      BP=0;
  248.      Serial.println(BP=0);
  249.     }
  250.     //////////////////////////////////////////////
  251.     //Added May 8 2018 at 7:33 AM
  252.    //////////////////////////////////////////////
  253.    
  254.    /////////////////////////////////////////////
  255.    //Web page section Beginning
  256.    ////////////////////////////////////////////
  257.    client.println("HTTP/1.1 200 OK");
  258.     client.println("Content-Type: text/html");
  259.     client.println("");
  260.     client.println("<!DOCTYPE HTML>");
  261.     client.println("<html>");
  262.  
  263.     client.println("<head>");
  264.     client.println("</head>");
  265.  
  266.     client.println("<body>");
  267.       client.println("<h1 style=""color:#ff0000"">");
  268.       client.println("ON or OFF if Timer is running");
  269.       client.println("<h1 style=""color:#ff0000"">");
  270.       client.println(" IP Address is : 192.168.86.10");
  271.       client.println("<h1 style=""color:#ff0000"">");
  272.       client.println(BP);
  273.       client.println("<h1 style=""color:#ff0000"">");
  274.       client.println("Current time is ");
  275.       client.println(HR);
  276.       client.println(":");
  277.       client.println(MN);
  278.       client.println(":");
  279.       client.println(SC);
  280.       client.println("<h1 style=""color:#ff0000"">");
  281.       client.println("Duration of watering ");
  282.       client.println(dur_Timer_MN);
  283.       client.println("</h1>");
  284.  
  285.      
  286.    
  287.       client.println("<a href=");
  288.       client.println("LED=ON");
  289.       client.println("><button>");
  290.       client.println("ON");
  291.       client.println("</button></a>");
  292.    
  293.       client.println("<a href=");
  294.       client.println("LED=OFF");
  295.       client.println("><button>");
  296.       client.println("OFF");
  297.       client.println("</button></a>");
  298.    
  299.     client.println("</body>");
  300.     client.println("</html>");
  301.     client.stop();
  302.      delay(1);
  303.   /////////////////////////////////////////////
  304.    //Web page section END
  305.    ////////////////////////////////////////////
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement