Advertisement
Ruddog

RevC

Jun 12th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.76 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. WiFiServer server(80);
  31. String  waterStatus;
  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.  
  52.  
  53.  
  54. int timeZone = -7;
  55. WiFiUDP Udp;
  56. unsigned int localPort = 8888;
  57. time_t getNtpTime();
  58.  
  59. void printDigits(int digits);
  60. void sendNTPpacket(IPAddress &address);
  61. int digitalClockDisplay(char m){
  62. int interm;
  63.  
  64. if (m=='h'){
  65. interm=hour();
  66. return interm;
  67. }
  68. if (m=='m'){
  69. interm=minute();
  70. return interm;
  71. }
  72. if (m=='s'){
  73. interm=second();
  74. return interm;
  75. }
  76. if (m=='j'){
  77. interm=day();
  78. return interm;
  79. }
  80. if (m=='n'){
  81. interm=month();
  82. return interm;
  83. }
  84. if (m=='y'){
  85. interm=year();
  86. return interm;
  87. }
  88. if (m=='w'){
  89. interm=weekday();
  90. return interm;
  91. }
  92. }
  93.  
  94.  /*-------- NTP code ----------*/
  95. const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
  96. byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
  97. time_t getNtpTime()
  98. {
  99. IPAddress ntpServerIP; // NTP server's ip address
  100. while (Udp.parsePacket() > 0) ; // discard any previously received packets
  101. Serial.println("Transmit NTP Request");
  102. // get a random server from the pool
  103. WiFi.hostByName(ntpServerName, ntpServerIP);
  104. Serial.print(ntpServerName);
  105. Serial.print(":");
  106. Serial.println(ntpServerIP);
  107. sendNTPpacket(ntpServerIP);
  108. uint32_t beginWait = millis();
  109. while (millis() - beginWait < 1500) {
  110. int size = Udp.parsePacket();
  111. if (size >= NTP_PACKET_SIZE) {
  112. Serial.println("Receive NTP Response");
  113. Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
  114. unsigned long secsSince1900;
  115. // convert four bytes starting at location 40 to a long integer
  116. secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
  117. secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
  118. secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
  119. secsSince1900 |= (unsigned long)packetBuffer[43];
  120. return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
  121. }
  122. }
  123. Serial.println("No NTP Response :-(");
  124. return 0; // return 0 if unable to get the time
  125. }
  126. void sendNTPpacket(IPAddress &address)
  127. {
  128. memset(packetBuffer, 0, NTP_PACKET_SIZE);
  129. packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  130. packetBuffer[1] = 0;     // Stratum, or type of clock
  131. packetBuffer[2] = 6;     // Polling Interval
  132. packetBuffer[3] = 0xEC;  // Peer Clock Precision
  133. packetBuffer[12] = 49;
  134. packetBuffer[13] = 0x4E;
  135. packetBuffer[14] = 49;
  136. packetBuffer[15] = 52;
  137. Udp.beginPacket(address, 123); //NTP requests are to port 123
  138. Udp.write(packetBuffer, NTP_PACKET_SIZE);
  139. Udp.endPacket();
  140.  
  141. }
  142.  
  143. void setup()
  144.  
  145. {
  146.   ClientRequest = "";
  147.   pinMode(2, OUTPUT);
  148.   waterStatus = "";
  149.  
  150. //digitalWrite(5,HIGH);//initialize pin it might be on by default
  151. Serial.begin(115200);
  152.   WiFi.disconnect();
  153.   delay(3000);
  154.   Serial.println("START");
  155.    WiFi.begin("xxxxx","xxxxxxx");
  156.   while ((!(WiFi.status() == WL_CONNECTED))){
  157.     delay(300);
  158.     Serial.print("..");
  159. BP=0;
  160. pinMode(5,OUTPUT);
  161. digitalWrite(output5, HIGH);
  162.   }
  163.   Serial.println("Connected");
  164.   WiFi.config(staticIP707_10, gateway707_10, subnet707_10);
  165.   Serial.println("Your IP is");
  166.   Serial.println((WiFi.localIP().toString()));
  167.  
  168.   server.begin();
  169.  
  170. Udp.begin(localPort);
  171. setSyncProvider(getNtpTime);
  172. setSyncInterval(300);
  173. }
  174.  
  175.  
  176. void loop()
  177. {
  178.   delay(5000);
  179.   Serial.println();
  180.   (MT=digitalClockDisplay('n'));
  181.   (DY=digitalClockDisplay('j'));
  182.   (YR=digitalClockDisplay('y'));
  183.  
  184.   (HR=digitalClockDisplay('h'));
  185.   (MN=digitalClockDisplay('m'));
  186.   (SC=digitalClockDisplay('s'));
  187.   (WD=digitalClockDisplay('w'));
  188.   Serial.print(MT);
  189.   Serial.print('-');
  190.   Serial.print(DY);
  191.   Serial.print('-');
  192.   Serial.println(YR);
  193.   Serial.print(HR);
  194.   Serial.print(':');
  195.   Serial.print(MN);
  196.   Serial.print(':');
  197.   Serial.println(SC);
  198.   Serial.print("Weekday ");
  199.   Serial.println(WD);
  200.   Serial.println(" ");
  201.   delay(300);
  202.   Serial.println(BP);
  203.   ////////////////////////////////////////////////////////////
  204.  
  205.   if (WD == 1 ) {
  206.     Serial.println("Its Sunday");
  207.     delay(300);
  208.   }
  209.  
  210.   if (WD == 3 || (WD == 5 || WD == 7)) {
  211.     Serial.println("Not a watering day");
  212.     delay(300);
  213.   }
  214.   //BP is Button Pressed and can be either 1 or 0
  215.   //if the button is pressed it will set if statement to
  216.   //false and move onto else portion turn off valve
  217.   if (BP == 0 && (WD == 2 || (WD == 4 || WD == 6))) {
  218.     Serial.println("Allows entry to timer setting");
  219.    }
  220.  
  221.       if (HR == setHR && MN < dur_Timer_MN && BP==0) {
  222.         Serial.println("Turn on water valve");
  223.         digitalWrite(2,HIGH);
  224.         waterStatus = "Water Valve Open";
  225.         Serial.println(waterStatus);
  226.         delay(300);
  227.       } else {
  228.         Serial.println("Turn off water valve");
  229.         digitalWrite(2,LOW);
  230.         waterStatus = "Water Valve Closed";
  231.         Serial.println(waterStatus);
  232.         delay(300);
  233.       }
  234.  
  235.  
  236.   //////////////////////////////////////////////////////////
  237.  
  238.     WiFiClient client = server.available();
  239.     if (!client) { return; }
  240.     while(!client.available()){  delay(1); }
  241.     ClientRequest = (client.readStringUntil('\r'));
  242.     client.flush();
  243.     //////////////////////////////////////////////
  244.     //Added May 8 2018 at 7:33 AM
  245.    //////////////////////////////////////////////
  246.     if (ClientRequest.indexOf("LED=ON") > 0) {
  247.       //digitalWrite(16,HIGH);
  248.       BP=1;
  249.       Serial.println(BP=1);
  250.  
  251.     }
  252.     if (ClientRequest.indexOf("LED=OFF") > 0) {
  253.      // digitalWrite(16,LOW);
  254.      BP=0;
  255.      Serial.println(BP=0);
  256.     }
  257.     //////////////////////////////////////////////
  258.     //Added May 8 2018 at 7:33 AM
  259.    //////////////////////////////////////////////
  260.    
  261.    /////////////////////////////////////////////
  262.    //Web page section Beginning
  263.    ////////////////////////////////////////////
  264.    client.println("HTTP/1.1 200 OK");
  265.     client.println("Content-Type: text/html");
  266.     client.println("");
  267.     client.println("<!DOCTYPE HTML>");
  268.     client.println("<html>");
  269.  
  270.     client.println("<head>");
  271.     client.println("</head>");
  272.  
  273.     client.println("<body>");
  274.       client.println("<h1 style=""color:#ff0000"">");
  275.       client.println("ON or OFF if Timer is running");
  276.       client.println("<h1 style=""color:#ff0000"">");
  277.       client.println(" IP Address is : 192.168.86.10"); \<========How does one change the font size?
  278.       client.println("<h1 style=""color:#ff0000"">");
  279.       client.println(BP);
  280.       client.println("<h1 style=""color:#ff0000"">");
  281.       client.println("Current time is ");
  282.       client.println(HR);
  283.       client.println(":");
  284.       client.println(MN);
  285.       client.println(":");
  286.       client.println(SC);
  287.       client.println("<h1 style=""color:#ff0000"">");
  288.       client.println("Water Valve Status-> ");
  289.       client.println(waterStatus);
  290.       client.println("</h1>");
  291.  
  292.      
  293.    
  294.       client.println("<a href=");
  295.       client.println("LED=ON");
  296.       client.println("><button>");  \<========Also how does one change the size of the button?
  297.       client.println("ON");
  298.       client.println("</button></a>");
  299.    
  300.       client.println("<a href=");
  301.       client.println("LED=OFF");
  302.       client.println("><button>");
  303.       client.println("OFF");
  304.       client.println("</button></a>");
  305.    
  306.     client.println("</body>");
  307.     client.println("</html>");
  308.     client.stop();
  309.      delay(1);
  310.   /////////////////////////////////////////////
  311.    //Web page section END
  312.    ////////////////////////////////////////////
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement