Advertisement
Ruddog

Combo_HTML_C++_NodeMCU

Sep 14th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.81 KB | None | 0 0
  1. //Conbined C++ and HTML for sketch running on NodeMCU
  2.   // A0=ADCO            // D0=GPIO16 & USER & Wake Onboard LED
  3.   // RESERVED           // D1 GPIO5 <<<<<<<Use for Relay K1
  4.   // RESERVED           // D2 GPIO4
  5.   // SD3= GPIO10        // D3=GPIO0 & FLASH<<<<<<<Use for Relay K2
  6.   // SD2= GPIO9         // D4=GPIO2 & TXD1 Onboard LED<<<<<<<<<Connect to Temp Sensor
  7.   // SD1=MOSI & SDD1    // 3.3V=3.3V
  8.   // CMD=CS & SDCMD     // GND=GND
  9.   // SD0=MISO & SDD0    // D5=GPIO14 & HSCLK
  10.   // CLK=SCLK & SDCLK   // D6=GPIO12 & HMISO
  11.   // GND=GND            // D7=GPIO13 & RXD2 &HMOSI
  12.   // 3V3=3.3V           // D8=GPIO15 & TXD2 & HCS
  13.   // EN=EN              // RX=GPIO3 & RXD0
  14.   // RST=RST            // TX=GPIO1 & TXD0
  15.   // GND=GND            // GND=GND
  16.   // Vin=Vin            // 3V3=3.3V
  17.  
  18.   // D1 = GPIO5 = digitalWrite(5,HIGH) = pinMode(5,OUTPUT)
  19. #include <ESP8266WiFi.h>
  20. #include <TimeLib.h>
  21. #include <WiFiUdp.h>
  22. //USE GPIO 5 for this example =D1
  23. String  ClientRequest;
  24. IPAddress staticIP707_10(192,168,86,6);
  25. IPAddress gateway707_10(192,168,86,1);
  26. IPAddress subnet707_10(255,255,255,0);
  27. WiFiServer server(80);
  28. String  waterStatus;
  29. int MT;
  30. int DY;
  31. int YR;
  32. int HR;
  33. int MN;
  34. int SC;
  35. int WD;
  36. int BP;
  37. //int MN_Timer;
  38. //int storeMins;
  39. //int timerMinsSet;
  40. ////////////////////////////////////////////////
  41. int setHR = 9;//START TIMER AT THIS HOUR
  42. int setMN = 30;//TURN OFF GPIO PIN WHEN THIS INTERGER IS EXCEEDED
  43. int setDY;
  44. ///////////////////////////////////////////////
  45. int dur_Timer_MN = setMN + 1;
  46. //int output5;
  47. char ntpServerName[] = "us.pool.ntp.org";
  48.  
  49.  
  50.  
  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(0, OUTPUT);
  145.   pinMode(5,OUTPUT);
  146.   waterStatus = "";
  147.  
  148. //digitalWrite(5,HIGH);//initialize pin it might be on by default
  149. Serial.begin(115200);
  150.   WiFi.disconnect();
  151.   delay(3000);
  152.   Serial.println("START");
  153.    WiFi.begin("WIFI","password");
  154.   while ((!(WiFi.status() == WL_CONNECTED))){
  155.     delay(300);
  156.     Serial.print("..");
  157. BP=0;
  158.  
  159.  
  160.   }
  161.   Serial.println("Connected");
  162.   WiFi.config(staticIP707_10, gateway707_10, subnet707_10);
  163.   Serial.println("Your IP is");
  164.   Serial.println((WiFi.localIP().toString()));
  165.  
  166.   server.begin();
  167.  
  168. Udp.begin(localPort);
  169. setSyncProvider(getNtpTime);
  170. setSyncInterval(300);
  171. }
  172.  
  173.  
  174. void loop()
  175. {
  176.   delay(5000);
  177.   Serial.println();
  178.   (MT=digitalClockDisplay('n'));
  179.   (DY=digitalClockDisplay('j'));
  180.   (YR=digitalClockDisplay('y'));
  181.  
  182.   (HR=digitalClockDisplay('h'));
  183.   (MN=digitalClockDisplay('m'));
  184.   (SC=digitalClockDisplay('s'));
  185.   (WD=digitalClockDisplay('w'));
  186.   Serial.print(MT);
  187.   Serial.print('-');
  188.   Serial.print(DY);
  189.   Serial.print('-');
  190.   Serial.println(YR);
  191.   Serial.print(HR);
  192.   Serial.print(':');
  193.   Serial.print(MN);
  194.   Serial.print(':');
  195.   Serial.println(SC);
  196.   Serial.print("Weekday ");
  197.   Serial.println(WD);
  198.   Serial.println(" ");
  199.   delay(300);
  200.   Serial.println(BP);
  201.   ////////////////////////////////////////////////////////////
  202.  
  203.   if (WD == 1 ) {
  204.     Serial.println("Its Sunday");
  205.     delay(300);
  206.   }
  207.  
  208.   if (WD == 3 || (WD == 5 || WD == 7)) {
  209.     Serial.println("Not a watering day");
  210.     delay(300);
  211.   }
  212.   //BP is Button Pressed and can be either 1 or 0
  213.   //if the button is pressed it will set if statement to
  214.   //false and move onto else portion turn off valve
  215.   if (BP == 0 && (WD == 2 || (WD == 4 || WD == 6))) {
  216.     Serial.println("Conditions Met for Water Vavle to be on");
  217.    }
  218.  
  219.       if (HR == setHR && MN < dur_Timer_MN && BP==0) {
  220.         Serial.println("Turn on water valve");
  221.         digitalWrite(5,HIGH);
  222.         waterStatus = "Water Valve Open";
  223.         Serial.println(waterStatus);
  224.         delay(300);
  225.       } else {
  226.         Serial.println("Turn off water valve");
  227.         digitalWrite(5,LOW);
  228.         waterStatus = "Water Valve Closed";
  229.         Serial.println(waterStatus);
  230.         delay(300);
  231.       }
  232.  
  233.  
  234.   //////////////////////////////////////////////////////////
  235.  
  236.     WiFiClient client = server.available();
  237.     if (!client) { return; }
  238.     while(!client.available()){  delay(1); }
  239.     ClientRequest = (client.readStringUntil('\r'));
  240.     client.flush();
  241.     //////////////////////////////////////////////
  242.     //Added May 8 2018 at 7:33 AM
  243.    //////////////////////////////////////////////
  244.     if (ClientRequest.indexOf("LED=ON") > 0) {
  245.       //digitalWrite(16,HIGH);
  246.       BP=1;
  247.       Serial.println(BP=1);
  248.  
  249.     }
  250.     if (ClientRequest.indexOf("LED=OFF") > 0) {
  251.      // digitalWrite(16,LOW);
  252.      BP=0;
  253.      Serial.println(BP=0);
  254.     }
  255.     //////////////////////////////////////////////
  256.     //Added May 8 2018 at 7:33 AM
  257.    //////////////////////////////////////////////
  258.    
  259.    /////////////////////////////////////////////
  260.    //Web page section Beginning
  261.    ////////////////////////////////////////////
  262.    client.println("HTTP/1.1 200 OK");
  263.     client.println("Content-Type: text/html");
  264.     client.println("");
  265.     client.println("<!DOCTYPE HTML>");
  266.     client.println("<html>");
  267.  
  268.     client.println("<head>");
  269.     client.println("</head>");
  270.  
  271.     client.println("<body>");
  272.       client.println("<h1 style=""color:#ff0000"">");
  273.       client.println("ON or OFF if Timer is running");
  274.       client.println("<h1 style=""color:#ff0000"">");
  275.       client.println(" IP Address is : 192.168.86.10");
  276.       client.println("<h1 style=""color:#ff0000"">");
  277.       client.println(BP);
  278.       client.println("<h1 style=""color:#ff0000"">");
  279.       client.println("Current time is ");
  280.       client.println(HR);
  281.       client.println(":");
  282.       client.println(MN);
  283.       client.println(":");
  284.       client.println(SC);
  285.       client.println("<h1 style=""color:#ff0000"">");
  286.       client.println("Water Valve Status-> ");
  287.       client.println(waterStatus);
  288.       client.println("</h1>");
  289.  
  290.      
  291.    
  292.       client.println("<a href=");
  293.       client.println("LED=ON");
  294.       //Modified button size
  295.       client.println("<button style=\"height:500px;width:500px\">");
  296.       //client.println("<button style=")height:50px;width:50px">");
  297.       //client.println("><button>");
  298.       client.println("ON");
  299.       //client.println("</button></a>");
  300.    
  301.       client.println("<a href=");
  302.       client.println("LED=OFF");
  303.       client.println("><button>");
  304.       client.println("OFF");
  305.       client.println("</button></a>");
  306.    
  307.     client.println("</body>");
  308.     client.println("</html>");
  309.     client.stop();
  310.      delay(1);
  311.   /////////////////////////////////////////////
  312.    //Web page section END
  313.    ////////////////////////////////////////////
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement