Fish Feeder

Dependencies:   AX12 WIZnetInterface mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "AX12.h"
00004 #define WEB_SERVER_PORT   80
00005 char send_dat[1024]={0,};
00006 uint16_t calcu_len(void);
00007 float tempC;
00008 AnalogIn   ain(A0); // connect A0(WIZwiki-W7500) to Vout(Temp36)
00009 AX12 feed (PA_13, PA_14, 2);
00010 int main (void) 
00011 {
00012     printf("Wait a second...\r\n");
00013     uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; 
00014     const char ip_addr[] = "192.168.0.222"; 
00015     const char mask_addr[] = "255.255.255.0"; 
00016     const char gateway_addr[] = "192.168.0.1";
00017     
00018     uint16_t len;
00019     EthernetInterface eth;
00020     //eth.init(mac_addr); //Use DHCP
00021     eth.init(mac_addr, ip_addr, mask_addr, gateway_addr); //Use Static
00022     eth.connect();
00023     printf("Server IP Address is %s\r\n", eth.getIPAddress());
00024     
00025     TCPSocketServer server;
00026     server.bind(WEB_SERVER_PORT);
00027     server.listen();
00028     feed.SetGoal(939,1);
00029     while (true) 
00030     {
00031         printf("Wait for new connection...\r\n");
00032         TCPSocketConnection client;
00033         server.accept(client);
00034         client.set_blocking(false, 15000); // Timeout after (1.5)s
00035         
00036         printf("Connection from: %s\r\n", client.get_address());
00037         char buffer[256];
00038         char dat[128];
00039         char *dat_temp;
00040         
00041         memset(dat, 0, sizeof(dat));
00042         memset(send_dat, 0, sizeof(send_dat));
00043         memset(buffer, 0, sizeof(buffer));
00044         
00045         while (true) {
00046             
00047             wait(0.5);
00048             int n = client.receive_all(buffer, sizeof(buffer));
00049             if (n <= 0) break;
00050             //buffer[n] = '\0';
00051             
00052             printf("rev_dat : %s\r\n", buffer);
00053             
00054             float V = ain.read() * 3.3; // connect Vs(Temp36) to 3.3V(WIZwiki-W7500)      
00055             tempC = (V-0.5) * 100; // calculate temperature C
00056                        
00057             if((buffer[0]=='G')&&(buffer[1]=='E')&&(buffer[2]=='T')&&(buffer[3]==' '))// GET_Request
00058             { 
00059                 if((buffer[5]=='?'))
00060                 {
00061                     feed.SetGoal(554,1);
00062                     feed.SetGoal(554,1);
00063                     wait(1.0);
00064                     feed.SetGoal(939,1);
00065                     feed.SetGoal(939,1);
00066                 }
00067                 dat_temp = "HTTP/1.1 200 OK\r\n";                           strcat(dat, dat_temp);
00068                 dat_temp = "Content-Type: text/html\r\n";                   strcat(dat, dat_temp);
00069                 dat_temp = "Connection: close\r\n";                         strcat(dat, dat_temp);
00070                 //len = calcu_len();
00071                 sprintf(dat, "%sContent-Length: %d\r\n\r\n", dat, calcu_len());     strcat(dat, dat_temp);
00072             
00073                 client.send(dat, strlen(dat));
00074                 printf("send_dat : %s\r\n", dat);
00075             
00076                 client.send(send_dat, sizeof(send_dat));   
00077                 printf("send_dat : %s\r\n", send_dat);
00078                 //printf("send_dat size : %d\r\n", len);
00079                 
00080                 memset(dat, 0, sizeof(dat));
00081                 memset(send_dat, 0, sizeof(send_dat));
00082                 memset(buffer, 0, sizeof(buffer));
00083             }
00084             
00085         }
00086         
00087         client.close();
00088     }
00089     
00090 }
00091 uint16_t calcu_len(void)
00092 {
00093     char * dat_temp;
00094     char * forbidden = NULL;
00095     char str[4]={0,};
00096 
00097     //dat_temp = "<!DOCTYPE HTML>\r\n";                                                                       strcat(send_dat, dat_temp);
00098     dat_temp = "<html>\r\n";                                                                                strcat(send_dat, dat_temp);
00099     dat_temp = "\r\n";                                                                                  strcat(send_dat, dat_temp);
00100     dat_temp = "<head>\r\n";                                                                                strcat(send_dat, dat_temp);
00101     dat_temp = "<meta http-equiv=\"content-type\" content=\"text/html; charset=euc-kr\">\r\n";              strcat(send_dat, dat_temp);
00102     dat_temp = "<title>fishbowl</title>\r\n";                                                               strcat(send_dat, dat_temp);
00103     dat_temp = "<meta name=\"generator\" content=\"Namo WebEditor\">\r\n";                                  strcat(send_dat, dat_temp);
00104     dat_temp = "</head>\r\n";                                                                               strcat(send_dat, dat_temp);
00105     dat_temp = "\r\n";                                                                                      strcat(send_dat, dat_temp);
00106     dat_temp = "<body bgcolor=\"white\" text=\"black\" link=\"blue\" vlink=\"purple\" alink=\"red\">\r\n";  strcat(send_dat, dat_temp);
00107     dat_temp = "<p>fishbowl</p>\r\n";                                                                       strcat(send_dat, dat_temp);
00108     //dat_temp = "<p>&nbsp;</p>\r\n";                                                                         strcat(send_dat, dat_temp);
00109     dat_temp = "<p>Temperature in the fishbowl = $$$$$</p>\r\n";                                            strcat(send_dat, dat_temp);
00110     
00111     dat_temp = "<form name=\"form1\">\r\n";                                                                 strcat(send_dat, dat_temp);
00112     dat_temp = "<p><input type=\"hidden\" name=\"do\" value=\"ON\"></p>\r\n";                               strcat(send_dat, dat_temp);
00113     dat_temp = "<p>FEED <input type=\"button\" name=\"formbutton1\" value=\"ON\" onclick=\"submit();\"></p>\r\n";                   strcat(send_dat, dat_temp);
00114     dat_temp = "</form>\r\n";                                                                               strcat(send_dat, dat_temp);
00115     
00116     //dat_temp = "<p></p>\r\n";                                                                               strcat(send_dat, dat_temp);
00117     dat_temp = "</body>\r\n";                                                                               strcat(send_dat, dat_temp);
00118     dat_temp = "\r\n";                                                                                      strcat(send_dat, dat_temp);
00119     dat_temp = "</html>\r\n";                                                                               strcat(send_dat, dat_temp);
00120     forbidden = strstr(send_dat, "$$$$$");
00121     memset(forbidden,0,5);
00122     sprintf((char*)str, "%5.2f", tempC);
00123     memcpy(forbidden,str,5);
00124 
00125     return strlen(send_dat);
00126 }