windown

Dependencies:   SDFileSystem WIZnetInterface mbed-src

main.cpp

Committer:
schenk
Date:
2015-10-05
Revision:
0:1501dea8d383

File content as of revision 0:1501dea8d383:

#include "mbed.h"
#include "EthernetInterface.h"
#include "FsHandler.h"
#include "HTTPServer.h"
#include "SDFileSystem.h"
#include "DHT.h"

#ifdef TARGET_WIZWIKI_W7500
    //Choose one of file system.
    SDFileSystem local(SD_MOSI, SD_MISO, SD_CLK, SD_SEL, "local");//PB_3, PB_2, PB_1, PB_0
    //LocalFileSystem local("local");
#endif

#ifdef TARGET_WIZWIKI_W7500
    uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};
#endif
DHT sensor(D4, DHT11);
DigitalIn Rain(D2);
EthernetInterface eth;
HTTPServer  svr;
char ip_addr[] = "192.168.1.112";
char subnet_mask[] = "255.255.255.0";
char gateway_addr[] = "192.168.1.1";

//#define DHCP //If uncomment, W7500 runs DHCP
int Rain_value=1;
int mode = 0;

int main()
{
    
    
    int error = 0;
    int h = 0, c = 0, Rain_value = 1;
    HTTPFsRequestHandler::mount("/local/", "/");
    svr.addHandler<HTTPFsRequestHandler>("/");

#ifdef TARGET_WIZWIKI_W7500
    
    #ifdef DHCP
        eth.init(mac_addr); //Use DHCP
    #else
        eth.init(mac_addr, ip_addr, subnet_mask, gateway_addr); //Not Use DHCP
    #endif

#else

    #ifdef DHCP
        eth.init(); //Use DHCP
    #else
        eth.init(ip_addr, subnet_mask, gateway_addr); //Not Use DHCP
    #endif

#endif

    printf("Check Ethernet Link\r\n");
    while(1) //Wait link up
    {
        if(eth.link() == true) 
            break;
    }
    printf("Link up\r\n");

    eth.connect();
    printf("Server IP Address is %s\r\n", eth.getIPAddress());

    if (!svr.start(80, &eth)) {

        //error("Server not starting !");
        exit(0);
    }

    while(1) {
        svr.poll();
        Rain_value = Rain.read(); // read the rain value
        wait(0.2);
        error = sensor.readData();
        if (0 == error) {
            c   = sensor.ReadTemperature(CELCIUS);
            h   = sensor.ReadHumidity();
            printf("===============Smart window================\n\r");
            printf("Temperature in Celcius: %d\n",c);
            printf("Humidity : %d\n", h);
            printf("Rain value : %d\n", Rain_value);
            printf("Mode : %d\n", mode);
            printf("===========================================\n\r");
            
        } else {
            printf("Error: %d\n", error);
        }
        
        
        //////// open the window
        if((c>27 || h>50) && (mode == 0&&Rain_value==1)){
          // ((c>30 or h>50) and closed status)
            DigitalOut(D9,0);
            DigitalOut(D8,1);
            wait(0.87);
            DigitalOut(D9,0);
            DigitalOut(D8,0);
            mode = 1 ; // opened status
         
        }
        /////// close the window
        else if((c<20 && h<40) || (mode == 1&&Rain_value==0)){ // ((c<20 and h<40) and opened status)
            DigitalOut(D9,1);
            DigitalOut(D8,0);
            wait(0.8);
            DigitalOut(D9,0);
            DigitalOut(D8,0);
            mode = 0; //closed status
        }
        
         wait(1);  
    }   
}