HOme Sheriff And Lamp

Dependencies:   CameraC328 HCSR04 SDFileSystem WIZnetInterface mbed

Fork of HoSAL by pi bae

Committer:
uasonice
Date:
Thu Aug 13 19:35:01 2015 +0000
Revision:
13:cefc034543a5
Parent:
12:974f7a96d6ab
update: web server image view

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uasonice 0:35211a622a44 1 #include "mbed.h"
uasonice 4:ca368c50f8c9 2 #include "EthernetInterface.h"
uasonice 4:ca368c50f8c9 3 #include "SDFileSystem.h"
uasonice 4:ca368c50f8c9 4 #include <stdio.h>
uasonice 4:ca368c50f8c9 5 #include <string.h>
uasonice 0:35211a622a44 6
uasonice 0:35211a622a44 7 #define DEBUG_TYPE 1
uasonice 0:35211a622a44 8 #define P_ uart.printf
uasonice 0:35211a622a44 9 #include "rev_config.h"
uasonice 0:35211a622a44 10
uasonice 3:8c4e0e7c8cea 11 #include "rev_Camera.h"
uasonice 4:ca368c50f8c9 12 #include "rev_httpFile.h"
uasonice 3:8c4e0e7c8cea 13 #include "rev_Hcsr04.h"
uasonice 0:35211a622a44 14
uasonice 0:35211a622a44 15
uasonice 3:8c4e0e7c8cea 16 #define MAC "\x00\x08\xDC\x11\x34\x78"
uasonice 3:8c4e0e7c8cea 17 #define IP "192.168.5.5"
uasonice 3:8c4e0e7c8cea 18 #define MASK "255.255.255.0"
uasonice 3:8c4e0e7c8cea 19 #define GATEWAY "192.168.5.1"
uasonice 3:8c4e0e7c8cea 20
uasonice 3:8c4e0e7c8cea 21 #define HTTPD_SERVER_PORT 80
uasonice 0:35211a622a44 22
uasonice 12:974f7a96d6ab 23
uasonice 0:35211a622a44 24 Serial uart(USBTX, USBRX); // tx, rx
uasonice 0:35211a622a44 25
uasonice 4:ca368c50f8c9 26 #if defined(USE_SDCARD)
uasonice 4:ca368c50f8c9 27 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500
uasonice 4:ca368c50f8c9 28 #endif // defined(USE_SDCARD)
uasonice 4:ca368c50f8c9 29
uasonice 4:ca368c50f8c9 30 #if defined(USE_HTTP_FILE_SERVER)
uasonice 4:ca368c50f8c9 31 EthernetInterface eth;
uasonice 4:ca368c50f8c9 32 TCPSocketServer server;
uasonice 4:ca368c50f8c9 33 TCPSocketConnection client;
uasonice 4:ca368c50f8c9 34 #endif // defined(USE_HTTP_FILE_SERVER)
uasonice 4:ca368c50f8c9 35
uasonice 3:8c4e0e7c8cea 36 #if defined(USE_CAMERA)
uasonice 3:8c4e0e7c8cea 37 //CameraC328 cam(PA_14, PA_13, CameraC328::Baud14400);
uasonice 3:8c4e0e7c8cea 38 CameraC328 *g_pCam;
uasonice 3:8c4e0e7c8cea 39 #endif // defined(USE_CAMERA)
uasonice 3:8c4e0e7c8cea 40
uasonice 3:8c4e0e7c8cea 41 #if defined(USE_MEASURE_DISTANCE)
uasonice 2:3c7526a1893a 42 //HCSR04 sensor(D12, D11);
uasonice 4:ca368c50f8c9 43 HCSR04 *g_pHcsr;
uasonice 3:8c4e0e7c8cea 44 #define CHECK_DISTANCE 50
uasonice 3:8c4e0e7c8cea 45 #endif // defined(USE_MEASURE_DISTANCE)
uasonice 0:35211a622a44 46
uasonice 12:974f7a96d6ab 47
uasonice 0:35211a622a44 48 DigitalOut led1(LED1); //server listning status
uasonice 0:35211a622a44 49 DigitalOut led2(LED2); //socket connecting status
uasonice 0:35211a622a44 50
galucpia 11:7db34a66a751 51 DigitalOut myled_R(D7);
galucpia 11:7db34a66a751 52 DigitalOut myled_G(D6);
galucpia 11:7db34a66a751 53 DigitalOut myled_B(D5);
galucpia 11:7db34a66a751 54
uasonice 0:35211a622a44 55 //////////////////////////////////////////////////////////////////////////
uasonice 13:cefc034543a5 56 Ticker tickLed;
uasonice 0:35211a622a44 57
uasonice 0:35211a622a44 58 //////////////////////////////////////////////////////////////////////////
uasonice 0:35211a622a44 59 void ledTickfunc()
uasonice 0:35211a622a44 60 {
uasonice 0:35211a622a44 61 led1 = !led1;
uasonice 0:35211a622a44 62 }
uasonice 0:35211a622a44 63
uasonice 12:974f7a96d6ab 64 void LedSet(int nSet)
uasonice 12:974f7a96d6ab 65 {
uasonice 12:974f7a96d6ab 66 myled_R = nSet;
uasonice 12:974f7a96d6ab 67 myled_G = nSet;
uasonice 12:974f7a96d6ab 68 myled_B = nSet;
uasonice 12:974f7a96d6ab 69 }
uasonice 12:974f7a96d6ab 70
uasonice 0:35211a622a44 71 //////////////////////////////////////////////////////////////////////////
uasonice 0:35211a622a44 72 int main()
uasonice 0:35211a622a44 73 {
uasonice 3:8c4e0e7c8cea 74 Timer tm1;
uasonice 7:58b14840531c 75 char strFile[32];
uasonice 7:58b14840531c 76 uint32_t cntImage=1;
uasonice 3:8c4e0e7c8cea 77
uasonice 13:cefc034543a5 78 tickLed.attach(&ledTickfunc,0.5);
uasonice 13:cefc034543a5 79 //ledTickfunc();
uasonice 0:35211a622a44 80
uasonice 0:35211a622a44 81 uart.baud(115200);
galucpia 11:7db34a66a751 82
uasonice 3:8c4e0e7c8cea 83 DM_fLN("INIT DEVICE");
uasonice 12:974f7a96d6ab 84 LedSet(0);
uasonice 4:ca368c50f8c9 85 #if defined(USE_SDCARD)
uasonice 4:ca368c50f8c9 86 // Check File System
uasonice 4:ca368c50f8c9 87 DM_fLN("Checking File System");
uasonice 4:ca368c50f8c9 88 DIR *d = opendir("/sd/");
uasonice 4:ca368c50f8c9 89 if (d != NULL) {
uasonice 4:ca368c50f8c9 90 DM_fLN("SD Card Present");
uasonice 4:ca368c50f8c9 91 } else {
uasonice 4:ca368c50f8c9 92 DM_fLN("SD Card Root Directory Not Found");
uasonice 4:ca368c50f8c9 93 }
uasonice 4:ca368c50f8c9 94 #endif // defined(USE_SDCARD)
uasonice 4:ca368c50f8c9 95
uasonice 4:ca368c50f8c9 96 #if defined(USE_HTTP_FILE_SERVER)
uasonice 4:ca368c50f8c9 97 // EthernetInterface eth;
uasonice 4:ca368c50f8c9 98 DM_fLN("Init Ethernet");
uasonice 4:ca368c50f8c9 99 //eth.init(); //Use DHCP
uasonice 4:ca368c50f8c9 100 eth.init((uint8_t*)MAC,IP,MASK,GATEWAY); //IP,mask,Gateway
uasonice 4:ca368c50f8c9 101 DM_fLN("Connecting");
uasonice 4:ca368c50f8c9 102 eth.connect();
uasonice 4:ca368c50f8c9 103 DM_fLN("IP Address is %s", eth.getIPAddress());
uasonice 4:ca368c50f8c9 104
uasonice 4:ca368c50f8c9 105 // TCPSocketServer server;
uasonice 4:ca368c50f8c9 106 server.bind(HTTPD_SERVER_PORT);
uasonice 4:ca368c50f8c9 107 server.listen();
uasonice 4:ca368c50f8c9 108 DM_fLN("Server Listening");
uasonice 4:ca368c50f8c9 109
uasonice 4:ca368c50f8c9 110 // FIXME: no work - non-block mode
uasonice 4:ca368c50f8c9 111 server.set_blocking(false, 1000);
uasonice 4:ca368c50f8c9 112 #endif // defined(USE_HTTP_FILE_SERVER)
uasonice 4:ca368c50f8c9 113
uasonice 3:8c4e0e7c8cea 114 #if defined(USE_CAMERA)
uasonice 3:8c4e0e7c8cea 115 DM_fLN("Init camera C328");
uasonice 9:416cbcabbddd 116 g_pCam = new CameraC328(PA_13, PA_14, CameraC328::Baud14400);
uasonice 9:416cbcabbddd 117 //g_pCam = new CameraC328(PA_13, PA_14, CameraC328::Baud38400);
uasonice 3:8c4e0e7c8cea 118 revSync(g_pCam);
uasonice 7:58b14840531c 119 #if 0
uasonice 3:8c4e0e7c8cea 120 tm1.reset();
uasonice 3:8c4e0e7c8cea 121 tm1.start();
uasonice 7:58b14840531c 122 revJpeg_snapshot(g_pCam, "/sd/test_shoot.jpg", CameraC328::JpegResolution640x480);
uasonice 3:8c4e0e7c8cea 123 tm1.stop();
uasonice 3:8c4e0e7c8cea 124 DM_fLN("time of capture: %d", tm1.read_ms());
uasonice 7:58b14840531c 125 #endif
uasonice 3:8c4e0e7c8cea 126 #endif // defined(USE_CAMERA)
uasonice 3:8c4e0e7c8cea 127 #if defined(USE_MEASURE_DISTANCE)
uasonice 4:ca368c50f8c9 128 g_pHcsr = new HCSR04(D12, D11);
uasonice 3:8c4e0e7c8cea 129 #endif // defined(USE_MEASURE_DISTANCE)
uasonice 3:8c4e0e7c8cea 130
uasonice 13:cefc034543a5 131 //tickLed.attach(&fileServer,1.2);
uasonice 12:974f7a96d6ab 132
uasonice 3:8c4e0e7c8cea 133 DM_fLN("start main loop");
uasonice 0:35211a622a44 134 while(1) {
uasonice 4:ca368c50f8c9 135 int dist_cm = 0;
uasonice 3:8c4e0e7c8cea 136 #if defined(USE_MEASURE_DISTANCE)
uasonice 4:ca368c50f8c9 137 if( (dist_cm = get_distance(g_pHcsr)) < CHECK_DISTANCE )
uasonice 3:8c4e0e7c8cea 138 #endif // defined(USE_MEASURE_DISTANCE)
uasonice 4:ca368c50f8c9 139 {
uasonice 4:ca368c50f8c9 140 DM_fLN("capture image: %d", dist_cm);
uasonice 4:ca368c50f8c9 141 #if defined(USE_CAMERA)
uasonice 7:58b14840531c 142 memset(strFile, 0, 32);
uasonice 7:58b14840531c 143 sprintf(strFile, "/sd/file_%03d.jpg", cntImage++);
uasonice 12:974f7a96d6ab 144 LedSet(1);
uasonice 7:58b14840531c 145 revJpeg_snapshot(g_pCam, strFile, CameraC328::JpegResolution640x480);
uasonice 12:974f7a96d6ab 146 LedSet(0);
uasonice 4:ca368c50f8c9 147 #endif // defined(USE_CAMERA)
uasonice 4:ca368c50f8c9 148 }
uasonice 12:974f7a96d6ab 149 #if 1
uasonice 13:cefc034543a5 150 fileServer();
uasonice 12:974f7a96d6ab 151 #else
uasonice 12:974f7a96d6ab 152 wait_ms(1000);
uasonice 12:974f7a96d6ab 153 #endif
uasonice 0:35211a622a44 154 }
uasonice 4:ca368c50f8c9 155 //return 0;
uasonice 0:35211a622a44 156 }