Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////
- // Generated with a lot of love//
- // with TUNIOT FOR ESP8266 //
- // Website: Easycoding.tn //
- /////////////////////////////////
- // Antenna End of board
- // A0=ADCO // D0=GPIO16 & USER & Wake Onboard LED
- // RESERVED // D1 GPIO5
- // RESERVED // D2 GPIO4
- // SD3= GPIO10 // D3=GPIO0 & FLASH
- // SD2= GPIO9 // D4=GPIO2 & TXD1 Onboard LED
- // SD1=MOSI & SDD1 // 3.3V=3.3V
- // CMD=CS & SDCMD // GND=GND
- // SD0=MISO & SDD0 // D5=GPIO14 & HSCLK
- // CLK=SCLK & SDCLK // D6=GPIO12 & HMISO
- // GND=GND // D7=GPIO13 & RXD2 &HMOSI
- // 3V3=3.3V // D8=GPIO15 & TXD2 & HCS
- // EN=EN // RX=GPIO3 & RXD0
- // RST=RST // TX=GPIO1 & TXD0
- // GND=GND // GND=GND
- // Vin=Vin // 3V3=3.3V
- #include <ESP8266WiFi.h>
- #include <TimeLib.h>
- #include <WiFiUdp.h>
- //USE GPIO 5 for this example =D1
- String ClientRequest;
- IPAddress staticIP707_10(192,168,86,10);
- IPAddress gateway707_10(192,168,86,1);
- IPAddress subnet707_10(255,255,255,0);
- WiFiServer server(80);
- String waterStatus;
- int MT;
- int DY;
- int YR;
- int HR;
- int MN;
- int SC;
- int WD;
- int BP;
- //int MN_Timer;
- //int storeMins;
- //int timerMinsSet;
- ////////////////////////////////////////////////
- int setHR = 9;//START TIMER AT THIS HOUR
- int setMN = 59;//TURN OFF GPIO PIN WHEN THIS INTERGER IS EXCEEDED
- int setDY;
- ///////////////////////////////////////////////
- int dur_Timer_MN = setMN + 1;
- int output5;
- char ntpServerName[] = "us.pool.ntp.org";
- int timeZone = -7;
- WiFiUDP Udp;
- unsigned int localPort = 8888;
- time_t getNtpTime();
- void printDigits(int digits);
- void sendNTPpacket(IPAddress &address);
- int digitalClockDisplay(char m){
- int interm;
- if (m=='h'){
- interm=hour();
- return interm;
- }
- if (m=='m'){
- interm=minute();
- return interm;
- }
- if (m=='s'){
- interm=second();
- return interm;
- }
- if (m=='j'){
- interm=day();
- return interm;
- }
- if (m=='n'){
- interm=month();
- return interm;
- }
- if (m=='y'){
- interm=year();
- return interm;
- }
- if (m=='w'){
- interm=weekday();
- return interm;
- }
- }
- /*-------- NTP code ----------*/
- const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
- byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
- time_t getNtpTime()
- {
- IPAddress ntpServerIP; // NTP server's ip address
- while (Udp.parsePacket() > 0) ; // discard any previously received packets
- Serial.println("Transmit NTP Request");
- // get a random server from the pool
- WiFi.hostByName(ntpServerName, ntpServerIP);
- Serial.print(ntpServerName);
- Serial.print(":");
- Serial.println(ntpServerIP);
- sendNTPpacket(ntpServerIP);
- uint32_t beginWait = millis();
- while (millis() - beginWait < 1500) {
- int size = Udp.parsePacket();
- if (size >= NTP_PACKET_SIZE) {
- Serial.println("Receive NTP Response");
- Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
- unsigned long secsSince1900;
- // convert four bytes starting at location 40 to a long integer
- secsSince1900 = (unsigned long)packetBuffer[40] << 24;
- secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
- secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
- secsSince1900 |= (unsigned long)packetBuffer[43];
- return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
- }
- }
- Serial.println("No NTP Response :-(");
- return 0; // return 0 if unable to get the time
- }
- void sendNTPpacket(IPAddress &address)
- {
- memset(packetBuffer, 0, NTP_PACKET_SIZE);
- packetBuffer[0] = 0b11100011; // LI, Version, Mode
- packetBuffer[1] = 0; // Stratum, or type of clock
- packetBuffer[2] = 6; // Polling Interval
- packetBuffer[3] = 0xEC; // Peer Clock Precision
- packetBuffer[12] = 49;
- packetBuffer[13] = 0x4E;
- packetBuffer[14] = 49;
- packetBuffer[15] = 52;
- Udp.beginPacket(address, 123); //NTP requests are to port 123
- Udp.write(packetBuffer, NTP_PACKET_SIZE);
- Udp.endPacket();
- }
- void setup()
- {
- ClientRequest = "";
- pinMode(2, OUTPUT);
- waterStatus = "";
- //digitalWrite(5,HIGH);//initialize pin it might be on by default
- Serial.begin(115200);
- WiFi.disconnect();
- delay(3000);
- Serial.println("START");
- WiFi.begin("xxxxx","xxxxxxx");
- while ((!(WiFi.status() == WL_CONNECTED))){
- delay(300);
- Serial.print("..");
- BP=0;
- pinMode(5,OUTPUT);
- digitalWrite(output5, HIGH);
- }
- Serial.println("Connected");
- WiFi.config(staticIP707_10, gateway707_10, subnet707_10);
- Serial.println("Your IP is");
- Serial.println((WiFi.localIP().toString()));
- server.begin();
- Udp.begin(localPort);
- setSyncProvider(getNtpTime);
- setSyncInterval(300);
- }
- void loop()
- {
- delay(5000);
- Serial.println();
- (MT=digitalClockDisplay('n'));
- (DY=digitalClockDisplay('j'));
- (YR=digitalClockDisplay('y'));
- (HR=digitalClockDisplay('h'));
- (MN=digitalClockDisplay('m'));
- (SC=digitalClockDisplay('s'));
- (WD=digitalClockDisplay('w'));
- Serial.print(MT);
- Serial.print('-');
- Serial.print(DY);
- Serial.print('-');
- Serial.println(YR);
- Serial.print(HR);
- Serial.print(':');
- Serial.print(MN);
- Serial.print(':');
- Serial.println(SC);
- Serial.print("Weekday ");
- Serial.println(WD);
- Serial.println(" ");
- delay(300);
- Serial.println(BP);
- ////////////////////////////////////////////////////////////
- if (WD == 1 ) {
- Serial.println("Its Sunday");
- delay(300);
- }
- if (WD == 3 || (WD == 5 || WD == 7)) {
- Serial.println("Not a watering day");
- delay(300);
- }
- //BP is Button Pressed and can be either 1 or 0
- //if the button is pressed it will set if statement to
- //false and move onto else portion turn off valve
- if (BP == 0 && (WD == 2 || (WD == 4 || WD == 6))) {
- Serial.println("Allows entry to timer setting");
- }
- if (HR == setHR && MN < dur_Timer_MN && BP==0) {
- Serial.println("Turn on water valve");
- digitalWrite(2,HIGH);
- waterStatus = "Water Valve Open";
- Serial.println(waterStatus);
- delay(300);
- } else {
- Serial.println("Turn off water valve");
- digitalWrite(2,LOW);
- waterStatus = "Water Valve Closed";
- Serial.println(waterStatus);
- delay(300);
- }
- //////////////////////////////////////////////////////////
- WiFiClient client = server.available();
- if (!client) { return; }
- while(!client.available()){ delay(1); }
- ClientRequest = (client.readStringUntil('\r'));
- client.flush();
- //////////////////////////////////////////////
- //Added May 8 2018 at 7:33 AM
- //////////////////////////////////////////////
- if (ClientRequest.indexOf("LED=ON") > 0) {
- //digitalWrite(16,HIGH);
- BP=1;
- Serial.println(BP=1);
- }
- if (ClientRequest.indexOf("LED=OFF") > 0) {
- // digitalWrite(16,LOW);
- BP=0;
- Serial.println(BP=0);
- }
- //////////////////////////////////////////////
- //Added May 8 2018 at 7:33 AM
- //////////////////////////////////////////////
- /////////////////////////////////////////////
- //Web page section Beginning
- ////////////////////////////////////////////
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("");
- client.println("<!DOCTYPE HTML>");
- client.println("<html>");
- client.println("<head>");
- client.println("</head>");
- client.println("<body>");
- client.println("<h1 style=""color:#ff0000"">");
- client.println("ON or OFF if Timer is running");
- client.println("<h1 style=""color:#ff0000"">");
- client.println(" IP Address is : 192.168.86.10"); \<========How does one change the font size?
- client.println("<h1 style=""color:#ff0000"">");
- client.println(BP);
- client.println("<h1 style=""color:#ff0000"">");
- client.println("Current time is ");
- client.println(HR);
- client.println(":");
- client.println(MN);
- client.println(":");
- client.println(SC);
- client.println("<h1 style=""color:#ff0000"">");
- client.println("Water Valve Status-> ");
- client.println(waterStatus);
- client.println("</h1>");
- client.println("<a href=");
- client.println("LED=ON");
- client.println("><button>"); \<========Also how does one change the size of the button?
- client.println("ON");
- client.println("</button></a>");
- client.println("<a href=");
- client.println("LED=OFF");
- client.println("><button>");
- client.println("OFF");
- client.println("</button></a>");
- client.println("</body>");
- client.println("</html>");
- client.stop();
- delay(1);
- /////////////////////////////////////////////
- //Web page section END
- ////////////////////////////////////////////
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement