Advertisement
Ruddog

HTML and C++

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