for pet feeder system & add timer function

Fork of httpServer by justin kim

Files at this revision

API Documentation at this revision

Comitter:
justinkim
Date:
Thu Jul 07 23:53:18 2016 +0000
Parent:
5:3c24e937da61
Commit message:
add function about timer alert

Changed in this revision

Handler/FsHandler.cpp Show annotated file Show diff for this revision Revisions of this file
Handler/FsHandler.h Show annotated file Show diff for this revision Revisions of this file
diff -r 3c24e937da61 -r b33c1a450937 Handler/FsHandler.cpp
--- a/Handler/FsHandler.cpp	Wed May 04 00:43:24 2016 +0000
+++ b/Handler/FsHandler.cpp	Thu Jul 07 23:53:18 2016 +0000
@@ -3,12 +3,35 @@
 #include "FsHandler.h"
 //#define DEBUG
 #include "hl_debug.h"
+#include "stdlib.h"
 
 DigitalOut led_red(LED1);
 DigitalOut led_green(LED2);
 DigitalOut led_blue(LED3);
 
-PwmOut myservo(PC_10);
+void SNTP_Connect(void);
+void time_set(void);
+void time_update(char buffer[]);
+
+extern int hour_r, min_r,hm_r, hm_n;
+extern int flag_t;
+extern double duty;
+
+PwmOut myservo(P10);
+datetime ntptime;
+struct tm timeinfo;
+
+uint8_t update_mode = 0;
+uint8_t Set_update[3] = {18, 00, 00};   //Hour,Minute,Second
+
+//ymd_buffer[0]~[3] : Year
+//ymd_buffer[4]~[5] : Month
+//ymd_buffer[6]~[7] : Day
+extern char ymd_buffer[8];
+//hms_buffer[0]~[1] : Hour
+//hms_buffer[2]~[3] : Minute
+//hms_buffer[4]~[5] : Second
+extern char hms_buffer[6];
 
 static int matchstrings(const char* one, const char* two)
 {
@@ -68,7 +91,7 @@
     int retval = 0;   //success
     
     if( std::string::npos != msg.uri.find("get_netinfo.cgi") )
-    {
+    {}/*
         char buf[256];
                 
         sprintf(buf, "NetinfoCallback({\"mac\":\"%s\",\"ip\":\"%s\",\"sn\":\"%s\",\"gw\":\"%s\",\"temp\":\"%s\"});"
@@ -84,7 +107,7 @@
         //sprintf(buf, "AinCallback({\"ain_v0\":\"%.3f\",\"ain_v1\":\"%.3f\"});",ain0.read()*100, ain1.read()*100);
                 
         tcp.send(buf, strlen(buf));
-    }
+    }*/
     
     else //read html pages
     {
@@ -147,8 +170,7 @@
 
 int HTTPFsRequestHandler::handlePostRequest()
 {
-    char ch;
-    double duty;
+    char ch;    
     myservo.period_ms(20);
     int pin = 0;
     
@@ -177,6 +199,7 @@
     else if( std::string::npos != msg.uri.find("set_dio2.cgi") )
     {
         ch = get_http_param_value("pin");
+        printf("ch : %d \r\n",ch);
                               
         if(ch == 0)
         {
@@ -198,6 +221,45 @@
             printf("duty : %lf\r\n", duty);
             //myservo.pulsewidth_ms(1.0); 
         }      
+        else if(ch == 3)
+        {
+            duty = 0.2;
+            myservo = duty;
+            printf("duty : %lf\r\n", duty);
+            wait(5);
+            duty = 1;
+            myservo = duty;
+            printf("duty : %lf\r\n", duty);
+        } 
+    }
+    else if( std::string::npos != msg.uri.find("set_time.cgi") )
+    {
+        hour_r = get_http_param_value("hour");
+        min_r = get_http_param_value("min");
+        
+        printf("hour : %d, min : %d\r\n", hour_r, min_r);
+        
+        SNTP_Connect();
+        time_set();
+        
+        time_t seconds = time(NULL);
+        
+        
+        strftime(ymd_buffer, 8, "%Y%m%d%\n\r", localtime(&seconds)); 
+        
+        if(hms_buffer[4] == '0' && hms_buffer[5] == '0')
+        {
+            time_update(hms_buffer);
+            printf("update HMS : %s\r\n",hms_buffer);
+        }
+        
+        hm_r = (hour_r*100) + min_r;
+        hm_n = atoi(hms_buffer);
+        hm_n = hm_n / 100;
+        printf("HMr : %d\r\n",hm_r);
+        printf("HMn : %d\r\n",hm_n);
+        
+        flag_t = 1;
     }
     else
     {
@@ -238,4 +300,53 @@
     return ret;
 }
 
+void SNTP_Connect(void) {
+    printf("Getting time information by using NTP...\r\n");
+    
+    SNTPClient sntp("time.nist.gov", 40);   // timezone: Korea, Republic of
+    sntp.connect();
+    if(sntp.getTime(&ntptime) == true) {
+          printf("%d-%d-%d, %02d:%02d:%02d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss);
+          printf("Completed Get and Set Time\r\n\r\n");
+    }
+    else {
+        while(sntp.getTime(&ntptime) == true) {
+            printf("%d-%d-%d, %02d:%02d:%02d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss);
+            printf("Completed Get and Set Time\r\n\r\n");
+            break;
+        }
+    }
+    sntp.close();
+}
 
+void time_set(void) {
+    timeinfo.tm_mon = ntptime.mo-1;
+    timeinfo.tm_mday = ntptime.dd;
+    timeinfo.tm_hour = ntptime.hh;
+    timeinfo.tm_min = ntptime.mm;
+    timeinfo.tm_sec = ntptime.ss;
+    timeinfo.tm_year = ntptime.yy-1900;
+    time_t t =mktime(&timeinfo);
+    set_time(t);
+    t = time(NULL);
+}
+
+void time_update(char buffer[]) {
+    uint8_t h_buffer = ((buffer[0]-48)*10) + (buffer[1]-48);
+    uint8_t m_buffer = ((buffer[2]-48)*10) + (buffer[3]-48);
+    uint8_t s_buffer = ((buffer[4]-48)*10) + (buffer[5]-48);
+
+    if(update_mode == 0){
+        if(h_buffer == Set_update[0] && m_buffer == Set_update[1] && s_buffer == Set_update[2]){
+            SNTP_Connect();
+            time_set();
+            update_mode = 1;
+            printf("Time Update Completed.\n\r\n\r");
+        }
+    }
+    else if(update_mode == 1){
+        if(!(h_buffer == Set_update[0] && m_buffer == Set_update[1] && s_buffer == Set_update[2])){
+            update_mode = 0;
+        }
+    }
+}
diff -r 3c24e937da61 -r b33c1a450937 Handler/FsHandler.h
--- a/Handler/FsHandler.h	Wed May 04 00:43:24 2016 +0000
+++ b/Handler/FsHandler.h	Thu Jul 07 23:53:18 2016 +0000
@@ -27,6 +27,7 @@
 #include "HTTPRequestHandler.h"
 #include "HTTPConnection.h"
 #include "EthernetInterface.h"
+#include "SNTPClient.h"
 
 #include <map>
 #include <list>
@@ -89,4 +90,7 @@
         
         uint32_t get_http_param_value(char* param_name);
 };
+
+extern datetime ntptime;
+extern PwmOut myservo;
 #endif // __FSHANDLER_H__