PIR + CDS + PIR + Ethernet
Dependencies: WIZnet_Library mbed
main.cpp@0:c07a663b50be, 2015-05-31 (annotated)
- Committer:
- nanjsk
- Date:
- Sun May 31 17:24:22 2015 +0000
- Revision:
- 0:c07a663b50be
autu LED + Ethernet
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nanjsk | 0:c07a663b50be | 1 | #include "mbed.h" |
nanjsk | 0:c07a663b50be | 2 | #include "WIZnetInterface.h" |
nanjsk | 0:c07a663b50be | 3 | |
nanjsk | 0:c07a663b50be | 4 | #define USE_DHCP 0 |
nanjsk | 0:c07a663b50be | 5 | |
nanjsk | 0:c07a663b50be | 6 | #define PORT 5000 |
nanjsk | 0:c07a663b50be | 7 | |
nanjsk | 0:c07a663b50be | 8 | const char * IP_Addr = "192.168.1.194"; |
nanjsk | 0:c07a663b50be | 9 | const char * IP_Subnet = "255.255.255.0"; |
nanjsk | 0:c07a663b50be | 10 | const char * IP_Gateway = "192.168.1.1"; |
nanjsk | 0:c07a663b50be | 11 | const char * Target_IP = "192.168.1.63"; |
nanjsk | 0:c07a663b50be | 12 | unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56}; |
nanjsk | 0:c07a663b50be | 13 | |
nanjsk | 0:c07a663b50be | 14 | Serial pc(USBTX, USBRX); |
nanjsk | 0:c07a663b50be | 15 | |
nanjsk | 0:c07a663b50be | 16 | //#ifdef TARGET_FRDM-KL25Z |
nanjsk | 0:c07a663b50be | 17 | SPI spi(D11,D12,D13); |
nanjsk | 0:c07a663b50be | 18 | WIZnetInterface ethernet(&spi,D10,D2); |
nanjsk | 0:c07a663b50be | 19 | //#endif |
nanjsk | 0:c07a663b50be | 20 | |
nanjsk | 0:c07a663b50be | 21 | Timer t; |
nanjsk | 0:c07a663b50be | 22 | |
nanjsk | 0:c07a663b50be | 23 | DigitalOut ledR0(LED_RED); |
nanjsk | 0:c07a663b50be | 24 | DigitalOut ledG0(LED_GREEN); |
nanjsk | 0:c07a663b50be | 25 | //DigitalOut ledB0(LED_BLUE); |
nanjsk | 0:c07a663b50be | 26 | |
nanjsk | 0:c07a663b50be | 27 | DigitalOut led0(D5); |
nanjsk | 0:c07a663b50be | 28 | DigitalOut led1(D6); |
nanjsk | 0:c07a663b50be | 29 | |
nanjsk | 0:c07a663b50be | 30 | DigitalIn PIR(D8); |
nanjsk | 0:c07a663b50be | 31 | AnalogIn CDS(A0); |
nanjsk | 0:c07a663b50be | 32 | |
nanjsk | 0:c07a663b50be | 33 | #define PLED_ON led0 = led1 = 1 |
nanjsk | 0:c07a663b50be | 34 | #define PLED_OFF led0 = led1 = 0 |
nanjsk | 0:c07a663b50be | 35 | |
nanjsk | 0:c07a663b50be | 36 | int CDS_data; |
nanjsk | 0:c07a663b50be | 37 | int PIR_sensor; |
nanjsk | 0:c07a663b50be | 38 | |
nanjsk | 0:c07a663b50be | 39 | int CDS_check() |
nanjsk | 0:c07a663b50be | 40 | { |
nanjsk | 0:c07a663b50be | 41 | CDS_data = CDS.read()*1000; |
nanjsk | 0:c07a663b50be | 42 | return CDS_data; |
nanjsk | 0:c07a663b50be | 43 | } |
nanjsk | 0:c07a663b50be | 44 | |
nanjsk | 0:c07a663b50be | 45 | int PIR_check() |
nanjsk | 0:c07a663b50be | 46 | { |
nanjsk | 0:c07a663b50be | 47 | PIR_sensor = PIR; |
nanjsk | 0:c07a663b50be | 48 | return PIR_sensor; |
nanjsk | 0:c07a663b50be | 49 | } |
nanjsk | 0:c07a663b50be | 50 | int main() |
nanjsk | 0:c07a663b50be | 51 | { |
nanjsk | 0:c07a663b50be | 52 | ledR0 = ledG0 = 1; |
nanjsk | 0:c07a663b50be | 53 | PLED_OFF; |
nanjsk | 0:c07a663b50be | 54 | //PIR.mode(PullDown); |
nanjsk | 0:c07a663b50be | 55 | unsigned char flag = 1; |
nanjsk | 0:c07a663b50be | 56 | int csd_int = 2; |
nanjsk | 0:c07a663b50be | 57 | int pir_int0 = 2; |
nanjsk | 0:c07a663b50be | 58 | int pir_int1 = 2; |
nanjsk | 0:c07a663b50be | 59 | int pir_status = 0; |
nanjsk | 0:c07a663b50be | 60 | |
nanjsk | 0:c07a663b50be | 61 | pc.baud(115200); |
nanjsk | 0:c07a663b50be | 62 | wait(0.5f); |
nanjsk | 0:c07a663b50be | 63 | pc.printf("Hello mbed!\n\r"); |
nanjsk | 0:c07a663b50be | 64 | pc.printf("===========================================\n\r"); |
nanjsk | 0:c07a663b50be | 65 | |
nanjsk | 0:c07a663b50be | 66 | #if USE_DHCP |
nanjsk | 0:c07a663b50be | 67 | int ret = ethernet.init(MAC_Addr); |
nanjsk | 0:c07a663b50be | 68 | #else |
nanjsk | 0:c07a663b50be | 69 | int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway); |
nanjsk | 0:c07a663b50be | 70 | #endif |
nanjsk | 0:c07a663b50be | 71 | |
nanjsk | 0:c07a663b50be | 72 | if (!ret) { |
nanjsk | 0:c07a663b50be | 73 | pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress()); |
nanjsk | 0:c07a663b50be | 74 | ret = ethernet.connect(); |
nanjsk | 0:c07a663b50be | 75 | if (!ret) { |
nanjsk | 0:c07a663b50be | 76 | pc.printf("IP: %s, MASK: %s, GW: %s\r\n", |
nanjsk | 0:c07a663b50be | 77 | ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway()); |
nanjsk | 0:c07a663b50be | 78 | } else { |
nanjsk | 0:c07a663b50be | 79 | pc.printf("Error ethernet.connect() - ret = %d\r\n", ret); |
nanjsk | 0:c07a663b50be | 80 | exit(0); |
nanjsk | 0:c07a663b50be | 81 | } |
nanjsk | 0:c07a663b50be | 82 | } else { |
nanjsk | 0:c07a663b50be | 83 | pc.printf("Error ethernet.init() - ret = %d\r\n", ret); |
nanjsk | 0:c07a663b50be | 84 | exit(0); |
nanjsk | 0:c07a663b50be | 85 | } |
nanjsk | 0:c07a663b50be | 86 | |
nanjsk | 0:c07a663b50be | 87 | //TCPSocketServer server; |
nanjsk | 0:c07a663b50be | 88 | // server.bind(PORT); |
nanjsk | 0:c07a663b50be | 89 | // server.listen(); |
nanjsk | 0:c07a663b50be | 90 | |
nanjsk | 0:c07a663b50be | 91 | //pc.printf("\nWait for new connection...\r\n"); |
nanjsk | 0:c07a663b50be | 92 | TCPSocketConnection client; |
nanjsk | 0:c07a663b50be | 93 | |
nanjsk | 0:c07a663b50be | 94 | //server.accept(client); |
nanjsk | 0:c07a663b50be | 95 | //client.set_blocking(false, 0); // Timeout=0. |
nanjsk | 0:c07a663b50be | 96 | //pc.printf("Connection from: %s\r\n", client.get_address()); |
nanjsk | 0:c07a663b50be | 97 | client.connect(Target_IP,5000); |
nanjsk | 0:c07a663b50be | 98 | |
nanjsk | 0:c07a663b50be | 99 | if(client.is_connected() == 1){ |
nanjsk | 0:c07a663b50be | 100 | pc.printf("Connected success : %s\r\n", client.get_address()); |
nanjsk | 0:c07a663b50be | 101 | client.send("Hello server!\r\n", 15); |
nanjsk | 0:c07a663b50be | 102 | PLED_ON; |
nanjsk | 0:c07a663b50be | 103 | wait(0.2f); |
nanjsk | 0:c07a663b50be | 104 | PLED_OFF; |
nanjsk | 0:c07a663b50be | 105 | wait(0.2f); |
nanjsk | 0:c07a663b50be | 106 | PLED_ON; |
nanjsk | 0:c07a663b50be | 107 | wait(0.2f); |
nanjsk | 0:c07a663b50be | 108 | PLED_OFF; |
nanjsk | 0:c07a663b50be | 109 | } |
nanjsk | 0:c07a663b50be | 110 | else { |
nanjsk | 0:c07a663b50be | 111 | pc.printf("Connected fail : %s\r\n", client.get_address()); |
nanjsk | 0:c07a663b50be | 112 | PLED_ON; |
nanjsk | 0:c07a663b50be | 113 | wait(0.4f); |
nanjsk | 0:c07a663b50be | 114 | PLED_OFF; |
nanjsk | 0:c07a663b50be | 115 | } |
nanjsk | 0:c07a663b50be | 116 | |
nanjsk | 0:c07a663b50be | 117 | while (true) { |
nanjsk | 0:c07a663b50be | 118 | //printf("%d\r\n",CDS_check()); |
nanjsk | 0:c07a663b50be | 119 | if(CDS_check() > 880){ |
nanjsk | 0:c07a663b50be | 120 | if(csd_int != 0){ |
nanjsk | 0:c07a663b50be | 121 | pc.printf("Now is dark.\r\n"); |
nanjsk | 0:c07a663b50be | 122 | client.send("Now is dark.\r\n", 14); |
nanjsk | 0:c07a663b50be | 123 | csd_int = 0; |
nanjsk | 0:c07a663b50be | 124 | } |
nanjsk | 0:c07a663b50be | 125 | ledG0 = 0; |
nanjsk | 0:c07a663b50be | 126 | flag = 1; |
nanjsk | 0:c07a663b50be | 127 | while(flag){ |
nanjsk | 0:c07a663b50be | 128 | if(PIR_check()){ |
nanjsk | 0:c07a663b50be | 129 | if(pir_int0 != 0){ |
nanjsk | 0:c07a663b50be | 130 | pc.printf("The movement is detected.\r\n"); |
nanjsk | 0:c07a663b50be | 131 | pc.printf("Power LED is turned on.\r\n"); |
nanjsk | 0:c07a663b50be | 132 | client.send("The movement is detected.\r\n", 27); |
nanjsk | 0:c07a663b50be | 133 | client.send("Power LED is turned on.\r\n", 25); |
nanjsk | 0:c07a663b50be | 134 | pir_int0 = 0; |
nanjsk | 0:c07a663b50be | 135 | } |
nanjsk | 0:c07a663b50be | 136 | t.stop(); |
nanjsk | 0:c07a663b50be | 137 | t.reset(); |
nanjsk | 0:c07a663b50be | 138 | t.start(); |
nanjsk | 0:c07a663b50be | 139 | pir_status = 1; |
nanjsk | 0:c07a663b50be | 140 | ledR0 = 0; |
nanjsk | 0:c07a663b50be | 141 | PLED_ON; |
nanjsk | 0:c07a663b50be | 142 | } |
nanjsk | 0:c07a663b50be | 143 | if(pir_status == 0) { |
nanjsk | 0:c07a663b50be | 144 | flag = 0; |
nanjsk | 0:c07a663b50be | 145 | } |
nanjsk | 0:c07a663b50be | 146 | if(t.read_ms() > 5000 && pir_status == 1){ |
nanjsk | 0:c07a663b50be | 147 | if(pir_int0 != 1){ |
nanjsk | 0:c07a663b50be | 148 | pc.printf("The movement is not detected.\r\n"); |
nanjsk | 0:c07a663b50be | 149 | pc.printf("Power LED is turned off.\r\n"); |
nanjsk | 0:c07a663b50be | 150 | client.send("The movement is not detected.\r\n", 31); |
nanjsk | 0:c07a663b50be | 151 | client.send("Power LED is turned off.\r\n", 26); |
nanjsk | 0:c07a663b50be | 152 | pir_int0 = 1; |
nanjsk | 0:c07a663b50be | 153 | } |
nanjsk | 0:c07a663b50be | 154 | t.stop(); |
nanjsk | 0:c07a663b50be | 155 | t.reset(); |
nanjsk | 0:c07a663b50be | 156 | pir_status = 0; |
nanjsk | 0:c07a663b50be | 157 | ledR0 = 1; |
nanjsk | 0:c07a663b50be | 158 | PLED_OFF; |
nanjsk | 0:c07a663b50be | 159 | flag = 0; |
nanjsk | 0:c07a663b50be | 160 | wait(0.1f); |
nanjsk | 0:c07a663b50be | 161 | } |
nanjsk | 0:c07a663b50be | 162 | } |
nanjsk | 0:c07a663b50be | 163 | } |
nanjsk | 0:c07a663b50be | 164 | else{ |
nanjsk | 0:c07a663b50be | 165 | if(csd_int != 1){ |
nanjsk | 0:c07a663b50be | 166 | pc.printf("Now is brighter.\r\n"); |
nanjsk | 0:c07a663b50be | 167 | client.send("Now is brighter.\r\n", 18); |
nanjsk | 0:c07a663b50be | 168 | csd_int = 1; |
nanjsk | 0:c07a663b50be | 169 | } |
nanjsk | 0:c07a663b50be | 170 | |
nanjsk | 0:c07a663b50be | 171 | ledG0 = 1; |
nanjsk | 0:c07a663b50be | 172 | if(PIR_check()){ |
nanjsk | 0:c07a663b50be | 173 | if(pir_int1 != 0){ |
nanjsk | 0:c07a663b50be | 174 | pc.printf("The movement is detected.\r\n"); |
nanjsk | 0:c07a663b50be | 175 | client.send("The movement is detected.\r\n", 27); |
nanjsk | 0:c07a663b50be | 176 | pir_int1 = 0; |
nanjsk | 0:c07a663b50be | 177 | } |
nanjsk | 0:c07a663b50be | 178 | ledR0 = 0; |
nanjsk | 0:c07a663b50be | 179 | } |
nanjsk | 0:c07a663b50be | 180 | else{ |
nanjsk | 0:c07a663b50be | 181 | if(pir_int1 != 1){ |
nanjsk | 0:c07a663b50be | 182 | pc.printf("The movement is not detected.\r\n"); |
nanjsk | 0:c07a663b50be | 183 | client.send("The movement is not detected.\r\n", 31); |
nanjsk | 0:c07a663b50be | 184 | pir_int1 = 1; |
nanjsk | 0:c07a663b50be | 185 | } |
nanjsk | 0:c07a663b50be | 186 | ledR0 = 1; |
nanjsk | 0:c07a663b50be | 187 | } |
nanjsk | 0:c07a663b50be | 188 | } |
nanjsk | 0:c07a663b50be | 189 | |
nanjsk | 0:c07a663b50be | 190 | //pc.printf("===========================================\n\r"); |
nanjsk | 0:c07a663b50be | 191 | //ledB0 = !ledB0; |
nanjsk | 0:c07a663b50be | 192 | //wait(1); |
nanjsk | 0:c07a663b50be | 193 | } |
nanjsk | 0:c07a663b50be | 194 | } |