traffic light controller with RPC over http interrupter

Dependencies:   EthernetInterface HTTPClient HTTPServer mbed-rpc mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
daklowprofile
Date:
Fri Aug 03 08:07:51 2018 +0000
Parent:
1:2c577df21639
Commit message:
RPC over http + RPC function + traffic light controller ; (need improvements for http post and get on time, need faster response maybe)

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 2c577df21639 -r bf0da8f925e0 main.cpp
--- a/main.cpp	Tue Jul 17 06:08:30 2018 +0000
+++ b/main.cpp	Fri Aug 03 08:07:51 2018 +0000
@@ -12,14 +12,6 @@
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
 
-//DigitalIn button1(p14);
-//DigitalIn button2(p27);
-//DigitalIn button3(p28);
-
-RpcDigitalOut button1(LED1,"button1");
-RpcDigitalOut button2(LED2,"button2");
-RpcDigitalOut button3(LED3,"button3");
-
 //T1
 DigitalOut t1red(p5);
 DigitalOut t1yellow(p6);
@@ -111,23 +103,14 @@
 
 
 void http_get(){
-    char* Server = "http://192.168.1.111/PDRM/api/api_update.php";
-    //char* Server = "http://demo.thingsboard.io/api/v1/aq8Xr1qkEzo1ANb4VQHa/telemetry";
+    char* Server = "http://192.168.1.103/PDRM/api/api_update.php";
     char buffer[256];
     HTTPClient http;
-    //HTTPMap map;
-    //map.put("t1r", "application/json");
     
     buffer[0] = 0;
     sprintf(buffer,"%s?t1=%d,%d,%d&t2=%d,%d,%d&t3=%d,%d,%d",Server,t1red.read(),t1yellow.read(),t1green.read(),t2red.read(),t2yellow.read(),t2green.read(),t3red.read(),t3yellow.read(),t3green.read());
     printf("Send to %s\r\n", buffer);
     http.get(buffer, buffer , 10); // Execute the URL of urlBuffer
-    /*
-    sprintf(buffer,"{\"t1r\":%d, \"t1y\":%d, \"t1g\":%d, \"t2r\":%d, \"t2y\":%d, \"t2g\":%d, \"t3r\":%d, \"t3y\":%d, \"t3g\":%d, \"active\": false}",t1red.read(),t1yellow.read(),t1green.read(),t2red.read(),t2yellow.read(),t2green.read(),t3red.read(),t3yellow.read(),t3green.read());
-    printf("Send to %s\r\n", buffer);
-    HTTPText inText(buffer, 512);
-    http.post(Server,map,&inText,10);
-    */
 }
 
 void traffic_interrupt (char t1, char t2, char t3, int time){
@@ -138,77 +121,92 @@
     wait(time);  
     }  
 
-void interrupt(int buffer){ 
-    switch(buffer){
+void interrupt(Arguments *in, Reply *out);
+RPCFunction rpcMove(&interrupt, "Interrupt");
+int duration,Id;
+
+
+void interrupt(Arguments *in, Reply *out){
+    Id = in->getArg<int>();
+    duration = in->getArg<int>();
+    pc.printf("Traffic Light %d will turn to green for %d seconds\r\n", Id, duration);
+    
+    switch(Id){
         case 1 :
+        led1 = 1;
         timer_tf.stop();
         if(timer_tf.read() >= 0 && timer_tf.read() <= 51){
-        wait(15);
+        wait(duration);
         }else if(timer_tf.read() >= 52 && timer_tf.read() <= 90){
         traffic_interrupt('R','Y','R',3);
-        traffic_interrupt('G','R','R',15);
+        traffic_interrupt('G','R','R',duration);
         traffic_interrupt('Y','R','R',3);
         }else if(timer_tf.read() >= 91 && timer_tf.read() <= 94){
         wait(3);
-        traffic_interrupt('G','R','R',15);
+        traffic_interrupt('G','R','R',duration);
         traffic_interrupt('Y','R','R',3);
         }else if(timer_tf.read() >= 95 && timer_tf.read() <= 165){
         traffic_interrupt('R','R','Y',3);
-        traffic_interrupt('G','R','R',15);    
+        traffic_interrupt('G','R','R',duration);    
         traffic_interrupt('Y','R','R',3);    
         }else if(timer_tf.read() >= 166 && timer_tf.read() <= 169){
         wait(3);
-        traffic_interrupt('G','R','R',15);
+        traffic_interrupt('G','R','R',duration);
         traffic_interrupt('Y','R','R',3);
         }
+        led1 = 0;
         timer_tf.start();   
         break;
           
         case 2 :
+        led2 = 1;
         timer_tf.stop();
         if(timer_tf.read() >= 0 && timer_tf.read() <= 47){
         traffic_interrupt('Y','R','R',3);
-        traffic_interrupt('R','G','R',15);
+        traffic_interrupt('R','G','R',duration);
         traffic_interrupt('R','Y','R',3);
         }else if(timer_tf.read() >= 48 && timer_tf.read() <= 51){
         wait(3);
-        traffic_interrupt('R','G','R',15);
+        traffic_interrupt('R','G','R',duration);
         traffic_interrupt('R','Y','R',3);
         }else if(timer_tf.read() >= 52 && timer_tf.read() <= 94){
-        wait(15);
+        wait(duration);
         }else if(timer_tf.read() >= 95 && timer_tf.read() <= 165){
         traffic_interrupt('R','R','Y',3);
-        traffic_interrupt('R','G','R',15);
+        traffic_interrupt('R','G','R',duration);
         traffic_interrupt('R','Y','R',3);
         }else if(timer_tf.read() >= 166 && timer_tf.read() <= 169){
         wait(3);
-        traffic_interrupt('R','G','R',15);
+        traffic_interrupt('R','G','R',duration);
         traffic_interrupt('R','Y','R',3);
-        }    
-        timer_tf.start();          
+        }
+        led2 = 0;
+        timer_tf.start(); 
         break;
         
         case 3 :
+        led3 = 1;
         timer_tf.stop();
         if(timer_tf.read() >= 0 && timer_tf.read() <= 47){
         traffic_interrupt('Y','R','R',3);
-        traffic_interrupt('R','R','G',15);
+        traffic_interrupt('R','R','G',duration);
         traffic_interrupt('R','R','Y',3);
         }else if(timer_tf.read() >= 48 && timer_tf.read() <= 51){
         wait(3);
-        traffic_interrupt('R','R','G',15);
+        traffic_interrupt('R','R','G',duration);
         traffic_interrupt('R','R','Y',3);
         }else if(timer_tf.read() >= 52 && timer_tf.read() <= 90){
         traffic_interrupt('R','Y','R',3);
-        traffic_interrupt('R','R','G',15);
+        traffic_interrupt('R','R','G',duration);
         traffic_interrupt('R','R','Y',3);
         }else if(timer_tf.read() >= 91 && timer_tf.read() <= 94){
         wait(3);
-        traffic_interrupt('R','R','G',15);
+        traffic_interrupt('R','R','G',duration);
         traffic_interrupt('R','R','Y',3);
         }else if(timer_tf.read() >= 95 && timer_tf.read() <= 169){
-        wait(15);
+        wait(duration);
         }
+        led3 = 0;
         timer_tf.start();
         break;
         }
@@ -233,8 +231,8 @@
     }else{}
 }
 
-
 void normal_traffic(){
+    change(t1red.read(),t1yellow.read(),t1green.read(),t2red.read(),t2yellow.read(),t2green.read(),t3red.read(),t3yellow.read(),t3green.read());
     if(timer_tf.read() >= 0 && timer_tf.read() <= 47){
     traffic_timer('G','R','R');
     }else if(timer_tf.read() >= 48 && timer_tf.read() <= 51){
@@ -250,31 +248,13 @@
     }else if(timer_tf.read() >= 170){
     timer_tf.reset();
     }
-    
-    if(button1.read()==1){
-        interrupt(1);
-        button1.write(0);
-    }else if(button2.read()==1){
-        interrupt(2);
-        button2.write(0);
-    }else if(button3.read()==1){
-        interrupt(3);
-        button3.write(0);
-    }
-    change(t1red.read(),t1yellow.read(),t1green.read(),t2red.read(),t2yellow.read(),t2green.read(),t3red.read(),t3yellow.read(),t3green.read());
+}
 
-}
 
 
 int main() {
     EthernetInterface eth;  
     HTTPServer svr;
-    
-    button1.write(0);
-    button2.write(0);        
-    button3.write(0);
-     
-    RPC::add_rpc_class<RpcDigitalOut>();
        
     printf("Setting up...\r\n");
     eth.init(); 
@@ -299,18 +279,11 @@
       
   while(1)
   {
-    //while(button1.read()==0&&button2.read()==0&&button3.read()==0){
     normal_traffic();
-    //}
-    //wait(20);
     svr.poll();
     if(tm.read()>.5)
     {
       tm.start();
     }
-
-    
-    
-  }
-  
+  }  
 }