Chau Vo / Mbed 2 deprecated F103-Web-Server

Dependencies:   NTPClient W5500Interface Watchdog device_configuration eeprom_flash mbed-rpc-nucleo mbed-rtos mbed

Fork of F103-Serial-to-Ethernet by Chau Vo

Files at this revision

API Documentation at this revision

Comitter:
olympux
Date:
Tue Jun 14 21:25:04 2016 +0000
Parent:
38:f8735ae519aa
Child:
40:c966abbe2d62
Commit message:
New features; ; - Set current time; - Set on/off time for DigitalOut 0 and 1; - Auto on/off do0, do1

Changed in this revision

README.md Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/README.md	Mon Jun 13 23:06:38 2016 +0000
+++ b/README.md	Tue Jun 14 21:25:04 2016 +0000
@@ -6,6 +6,41 @@
 - TCP/UDP server for controlling and monitoring using NNIO v2.0 and RPC protocols.
 - UDP server for discovering and configuring.
 
+# Usage
+
+## Set RTC for timed Digital Ouputs
+
+Use TCP client or UDP to send configuration to the module:
+
+Set the current_time variable
+
+```
+/Time/write abcdef
+```
+
+where abcdef is time from 00:00am in seconds.
+
+Run RPC SetTime() function to execute set_current_time() to set current time to the current_time variable
+
+```
+/SetTime/run x
+```
+
+## Set On/Off time for Digital Outputs
+
+```
+/do0OnTime/write abcdef
+/do0OffTime/write abcdef
+```
+
+where abcdef is time in seconds, start from 00:00am. Check it by sending to read
+
+```
+/do0OnTime/read x
+/do0OffTime/read x
+```
+
+
 # Releases
 
 ## v2.0.0 (04/06/2016)
--- a/main.cpp	Mon Jun 13 23:06:38 2016 +0000
+++ b/main.cpp	Tue Jun 14 21:25:04 2016 +0000
@@ -169,6 +169,39 @@
 // Watchdog
 Watchdog wdt;
 
+
+// Some variable types that can be modified through RPC.
+//int wheelsOn;
+//char lcdBannerMessage;
+//float speed;
+int current_time;
+int do0OnTime, do0OffTime;
+int do1OnTime, do1OffTime;
+ 
+//RPCVariable<int> rpcLights(&wheelsOn, "wheels");
+//RPCVariable<char> rpcBanner(&lcdBannerMessage, "banner");
+//RPCVariable<float> rpcSpeed(&speed, "speed");
+RPCVariable<int> rpcCurrentTime(&current_time, "Time");
+RPCVariable<int> rpcdo0OnTime(&do0OnTime, "do0OnTime");
+RPCVariable<int> rpcdo0OffTime(&do0OffTime, "do0OffTime");
+RPCVariable<int> rpcdo1OnTime(&do0OnTime, "do1OnTime");
+RPCVariable<int> rpcdo1OffTime(&do0OffTime, "do1OffTime");
+
+// RPC function definitions
+// Create a function of the required format
+void set_current_time(Arguments* args, Reply* rep);
+void set_current_time(Arguments* args, Reply* rep){
+    time_t ct = (time_t)current_time; // convert
+    struct tm *st = localtime(&ct);
+    
+    set_time(ct); // set time
+    
+    DBG("Set current time to: %s", ctime(&ct));
+    DBG("Time only: %d:%d:%d", st->tm_hour, st->tm_min, st->tm_sec);
+}
+// Attach it to an RPC object
+RPCFunction rpcSetCurrentTime(&set_current_time, "SetTime");
+
 /*
  * NNIO Protocol
  */
@@ -268,26 +301,105 @@
         wdt.Service();
 }
 
-
-// These are examples of some variable types that can be modified through RPC.
-int wheelsOn;
-char lcdBannerMessage;
-float speed;
- 
-RPCVariable<int> rpcLights(&wheelsOn, "wheels");
-RPCVariable<char> rpcBanner(&lcdBannerMessage, "banner");
-RPCVariable<float> rpcSpeed(&speed, "speed");
-
-/**
- * RPC function definitions
- */
-// Create a function of the required format
-void do_blink(Arguments* args, Reply* rep);
-void do_blink(Arguments* args, Reply* rep){
-    DBG("RPC function called");
+// Timer thread to check on/off time
+void digital_outputs_timer_thread(void const* args)
+{
+    Thread::wait(700);
+    
+    while(true) {
+        // read current time
+        time_t seconds = time(NULL);
+        struct tm *st = localtime(&seconds);
+        int current_time_in_seconds = 3600*(st->tm_hour) + 60*(st->tm_min) + st->tm_sec;
+        DBG("Current time: %d:%d:%d", st->tm_hour, st->tm_min, st->tm_sec);
+        
+        // check do0
+        if (do0OnTime < do0OffTime) {
+            if ((current_time_in_seconds >= do0OnTime) && (current_time_in_seconds < do0OffTime)){
+                if (dout0 == 0) {
+                    dout0 = 1;
+                    DBG("do0 ON");
+                }
+                else {
+                    DBG("do0 ON'ed");
+                }
+            }
+            else {
+                if (dout0 == 1) {
+                    dout0 = 0;
+                    DBG("do0 OFF'ed");
+                }
+                else {
+                    DBG("do0 OFF");
+                }
+            }
+        }
+        else {
+            if ((current_time_in_seconds >= do0OffTime) && ((current_time_in_seconds < do0OnTime))) {
+                if (dout0 == 1) {
+                    dout0 = 0;
+                    DBG("do0 OFF");
+                }
+                else {
+                    DBG("do0 OFF'ed");
+                }
+            }
+            else {
+                if (dout0 == 0) {
+                    dout0 = 1;
+                    DBG("do0 ON");
+                }
+                else {
+                    DBG("do0 ON'ed");
+                }
+            }
+        }
+        
+        // check do1
+        if (do1OnTime < do1OffTime) {
+            if ((current_time_in_seconds >= do1OnTime) && (current_time_in_seconds < do1OffTime)){
+                if (dout1 == 0) {
+                    dout1 = 1;
+                    DBG("do1 ON");
+                }
+                else {
+                    DBG("do1 ON'ed");
+                }
+            }
+            else {
+                if (dout1 == 1) {
+                    dout1 = 0;
+                    DBG("do1 OFF");
+                }
+                else {
+                    DBG("do1 OFF'ed");
+                }
+            }
+        }
+        else {
+            if ((current_time_in_seconds >= do1OffTime) && ((current_time_in_seconds < do1OnTime))) {
+                if (dout1 == 1) {
+                    dout1 = 0;
+                    DBG("do1 OFF");
+                }
+                else {
+                    DBG("do1 OFF'ed");
+                }
+            }
+            else {
+                if (dout1 == 0) {
+                    dout1 = 1;
+                    DBG("do1 ON");
+                }
+                else {
+                    DBG("do1 ON'ed");
+                }
+            }
+        }
+        // wait 1s
+        Thread::wait(10000); // Thread::wait() in ms
+    }
 }
-// Attach it to an RPC object
-RPCFunction blink(&do_blink, "blink");
 
 // Main code
 int main()
@@ -318,6 +430,7 @@
     */
     Thread t2(auto_update_timer_thread);
     Thread t3(wdt_reset_thread);
+    Thread t4(digital_outputs_timer_thread);
 
     /*
     * Ethernet