PIR + CDS + PIR + Ethernet

Dependencies:   WIZnet_Library mbed

Files at this revision

API Documentation at this revision

Comitter:
nanjsk
Date:
Sun May 31 17:24:22 2015 +0000
Commit message:
autu LED + Ethernet

Changed in this revision

WIZnet_Library.lib 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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZnet_Library.lib	Sun May 31 17:24:22 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/nanjsk/code/WIZnet_Library/#c1f1122adf7b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun May 31 17:24:22 2015 +0000
@@ -0,0 +1,194 @@
+#include "mbed.h"
+#include "WIZnetInterface.h"
+
+#define USE_DHCP    0
+
+#define PORT    5000
+
+const char * IP_Addr    = "192.168.1.194";
+const char * IP_Subnet  = "255.255.255.0";
+const char * IP_Gateway = "192.168.1.1";
+const char * Target_IP  = "192.168.1.63";
+unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56};
+
+Serial pc(USBTX, USBRX);
+
+//#ifdef TARGET_FRDM-KL25Z
+SPI spi(D11,D12,D13);
+WIZnetInterface ethernet(&spi,D10,D2);
+//#endif
+
+Timer t;
+
+DigitalOut ledR0(LED_RED);
+DigitalOut ledG0(LED_GREEN);
+//DigitalOut ledB0(LED_BLUE);
+
+DigitalOut led0(D5);
+DigitalOut led1(D6);
+
+DigitalIn PIR(D8);
+AnalogIn CDS(A0);
+
+#define PLED_ON      led0 = led1 = 1
+#define PLED_OFF     led0 = led1 = 0
+
+int CDS_data;
+int PIR_sensor;
+
+int CDS_check()
+{
+    CDS_data = CDS.read()*1000;
+    return CDS_data;
+}
+
+int PIR_check()
+{
+    PIR_sensor = PIR;
+    return PIR_sensor;
+}
+int main()
+{
+    ledR0 = ledG0 = 1;
+    PLED_OFF;
+    //PIR.mode(PullDown);
+    unsigned char flag = 1;
+    int csd_int = 2;
+    int pir_int0 = 2;
+    int pir_int1 = 2;
+    int pir_status = 0;
+
+    pc.baud(115200);
+    wait(0.5f);
+    pc.printf("Hello mbed!\n\r");
+    pc.printf("===========================================\n\r");
+    
+    #if USE_DHCP
+    int ret = ethernet.init(MAC_Addr);
+    #else
+    int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
+    #endif
+    
+    if (!ret) {
+        pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
+        ret = ethernet.connect();
+        if (!ret) {
+            pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
+                      ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
+        } else {
+            pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
+            exit(0);
+        }
+    } else {
+        pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
+        exit(0);
+    }
+    
+    //TCPSocketServer server;
+//    server.bind(PORT);
+//    server.listen();
+    
+    //pc.printf("\nWait for new connection...\r\n");
+    TCPSocketConnection client;
+    
+    //server.accept(client);
+    //client.set_blocking(false, 0); // Timeout=0.
+    //pc.printf("Connection from: %s\r\n", client.get_address());
+    client.connect(Target_IP,5000);
+    
+    if(client.is_connected() == 1){
+        pc.printf("Connected success : %s\r\n", client.get_address());
+        client.send("Hello server!\r\n", 15);
+        PLED_ON;
+        wait(0.2f);
+        PLED_OFF;
+        wait(0.2f);
+        PLED_ON;
+        wait(0.2f);
+        PLED_OFF;
+    }
+    else {
+        pc.printf("Connected fail : %s\r\n", client.get_address());
+        PLED_ON;
+        wait(0.4f);
+        PLED_OFF;
+    }
+    
+    while (true) {
+        //printf("%d\r\n",CDS_check());
+        if(CDS_check() > 880){
+            if(csd_int != 0){
+                pc.printf("Now is dark.\r\n");
+                client.send("Now is dark.\r\n", 14);
+                csd_int = 0;
+            }
+            ledG0 = 0;
+            flag = 1;
+            while(flag){
+                if(PIR_check()){
+                    if(pir_int0 != 0){
+                        pc.printf("The movement is detected.\r\n");
+                        pc.printf("Power LED is turned on.\r\n");
+                        client.send("The movement is detected.\r\n", 27);
+                        client.send("Power LED is turned on.\r\n", 25);
+                        pir_int0 = 0;
+                    }
+                    t.stop();
+                    t.reset();
+                    t.start();
+                    pir_status = 1;
+                    ledR0 = 0;
+                    PLED_ON;
+                }
+                if(pir_status == 0) {
+                    flag = 0;
+                }
+                if(t.read_ms() > 5000 && pir_status == 1){
+                    if(pir_int0 != 1){
+                        pc.printf("The movement is not detected.\r\n");
+                        pc.printf("Power LED is turned off.\r\n");
+                        client.send("The movement is not detected.\r\n", 31);
+                        client.send("Power LED is turned off.\r\n", 26);
+                        pir_int0 = 1;
+                    }
+                    t.stop();
+                    t.reset();
+                    pir_status = 0;
+                    ledR0 = 1;
+                    PLED_OFF;
+                    flag = 0;
+                    wait(0.1f);
+                }
+            }
+        }
+        else{
+            if(csd_int != 1){
+                pc.printf("Now is brighter.\r\n");
+                client.send("Now is brighter.\r\n", 18);
+                csd_int = 1;
+            }
+            
+            ledG0 = 1;
+            if(PIR_check()){
+                if(pir_int1 != 0){
+                    pc.printf("The movement is detected.\r\n");
+                    client.send("The movement is detected.\r\n", 27);
+                    pir_int1 = 0;
+                }
+                ledR0 = 0;
+            }
+            else{
+                if(pir_int1 != 1){
+                    pc.printf("The movement is not detected.\r\n");
+                    client.send("The movement is not detected.\r\n", 31);
+                    pir_int1 = 1;
+                }
+                ledR0 = 1;
+            }
+        }
+        
+        //pc.printf("===========================================\n\r");
+        //ledB0 = !ledB0;
+        //wait(1);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun May 31 17:24:22 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/cbbeb26dbd92
\ No newline at end of file