Fish Feeder

Dependencies:   AX12 WIZnetInterface mbed

Committer:
Ricky_Kwon
Date:
Wed Aug 26 13:33:02 2015 +0000
Revision:
1:30716e221157
Parent:
0:c863ec1eea0f
Fish Feeder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ricky_Kwon 0:c863ec1eea0f 1 #include "mbed.h"
Ricky_Kwon 0:c863ec1eea0f 2 #include "EthernetInterface.h"
Ricky_Kwon 0:c863ec1eea0f 3 #include "AX12.h"
Ricky_Kwon 0:c863ec1eea0f 4 #define WEB_SERVER_PORT 80
Ricky_Kwon 0:c863ec1eea0f 5 char send_dat[1024]={0,};
Ricky_Kwon 0:c863ec1eea0f 6 uint16_t calcu_len(void);
Ricky_Kwon 0:c863ec1eea0f 7 float tempC;
Ricky_Kwon 0:c863ec1eea0f 8 AnalogIn ain(A0); // connect A0(WIZwiki-W7500) to Vout(Temp36)
Ricky_Kwon 0:c863ec1eea0f 9 AX12 feed (PA_13, PA_14, 2);
Ricky_Kwon 0:c863ec1eea0f 10 int main (void)
Ricky_Kwon 0:c863ec1eea0f 11 {
Ricky_Kwon 0:c863ec1eea0f 12 printf("Wait a second...\r\n");
Ricky_Kwon 0:c863ec1eea0f 13 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};
Ricky_Kwon 0:c863ec1eea0f 14 const char ip_addr[] = "192.168.0.222";
Ricky_Kwon 0:c863ec1eea0f 15 const char mask_addr[] = "255.255.255.0";
Ricky_Kwon 0:c863ec1eea0f 16 const char gateway_addr[] = "192.168.0.1";
Ricky_Kwon 0:c863ec1eea0f 17
Ricky_Kwon 0:c863ec1eea0f 18 uint16_t len;
Ricky_Kwon 0:c863ec1eea0f 19 EthernetInterface eth;
Ricky_Kwon 0:c863ec1eea0f 20 //eth.init(mac_addr); //Use DHCP
Ricky_Kwon 0:c863ec1eea0f 21 eth.init(mac_addr, ip_addr, mask_addr, gateway_addr); //Use Static
Ricky_Kwon 0:c863ec1eea0f 22 eth.connect();
Ricky_Kwon 0:c863ec1eea0f 23 printf("Server IP Address is %s\r\n", eth.getIPAddress());
Ricky_Kwon 0:c863ec1eea0f 24
Ricky_Kwon 0:c863ec1eea0f 25 TCPSocketServer server;
Ricky_Kwon 0:c863ec1eea0f 26 server.bind(WEB_SERVER_PORT);
Ricky_Kwon 0:c863ec1eea0f 27 server.listen();
Ricky_Kwon 0:c863ec1eea0f 28 feed.SetGoal(939,1);
Ricky_Kwon 0:c863ec1eea0f 29 while (true)
Ricky_Kwon 0:c863ec1eea0f 30 {
Ricky_Kwon 0:c863ec1eea0f 31 printf("Wait for new connection...\r\n");
Ricky_Kwon 0:c863ec1eea0f 32 TCPSocketConnection client;
Ricky_Kwon 0:c863ec1eea0f 33 server.accept(client);
Ricky_Kwon 0:c863ec1eea0f 34 client.set_blocking(false, 15000); // Timeout after (1.5)s
Ricky_Kwon 0:c863ec1eea0f 35
Ricky_Kwon 0:c863ec1eea0f 36 printf("Connection from: %s\r\n", client.get_address());
Ricky_Kwon 0:c863ec1eea0f 37 char buffer[256];
Ricky_Kwon 0:c863ec1eea0f 38 char dat[128];
Ricky_Kwon 0:c863ec1eea0f 39 char *dat_temp;
Ricky_Kwon 0:c863ec1eea0f 40
Ricky_Kwon 0:c863ec1eea0f 41 memset(dat, 0, sizeof(dat));
Ricky_Kwon 0:c863ec1eea0f 42 memset(send_dat, 0, sizeof(send_dat));
Ricky_Kwon 0:c863ec1eea0f 43 memset(buffer, 0, sizeof(buffer));
Ricky_Kwon 0:c863ec1eea0f 44
Ricky_Kwon 0:c863ec1eea0f 45 while (true) {
Ricky_Kwon 0:c863ec1eea0f 46
Ricky_Kwon 0:c863ec1eea0f 47 wait(0.5);
Ricky_Kwon 0:c863ec1eea0f 48 int n = client.receive_all(buffer, sizeof(buffer));
Ricky_Kwon 0:c863ec1eea0f 49 if (n <= 0) break;
Ricky_Kwon 0:c863ec1eea0f 50 //buffer[n] = '\0';
Ricky_Kwon 0:c863ec1eea0f 51
Ricky_Kwon 0:c863ec1eea0f 52 printf("rev_dat : %s\r\n", buffer);
Ricky_Kwon 0:c863ec1eea0f 53
Ricky_Kwon 0:c863ec1eea0f 54 float V = ain.read() * 3.3; // connect Vs(Temp36) to 3.3V(WIZwiki-W7500)
Ricky_Kwon 0:c863ec1eea0f 55 tempC = (V-0.5) * 100; // calculate temperature C
Ricky_Kwon 0:c863ec1eea0f 56
Ricky_Kwon 0:c863ec1eea0f 57 if((buffer[0]=='G')&&(buffer[1]=='E')&&(buffer[2]=='T')&&(buffer[3]==' '))// GET_Request
Ricky_Kwon 0:c863ec1eea0f 58 {
Ricky_Kwon 0:c863ec1eea0f 59 if((buffer[5]=='?'))
Ricky_Kwon 0:c863ec1eea0f 60 {
Ricky_Kwon 0:c863ec1eea0f 61 feed.SetGoal(554,1);
Ricky_Kwon 0:c863ec1eea0f 62 feed.SetGoal(554,1);
Ricky_Kwon 0:c863ec1eea0f 63 wait(1.0);
Ricky_Kwon 0:c863ec1eea0f 64 feed.SetGoal(939,1);
Ricky_Kwon 0:c863ec1eea0f 65 feed.SetGoal(939,1);
Ricky_Kwon 0:c863ec1eea0f 66 }
Ricky_Kwon 0:c863ec1eea0f 67 dat_temp = "HTTP/1.1 200 OK\r\n"; strcat(dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 68 dat_temp = "Content-Type: text/html\r\n"; strcat(dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 69 dat_temp = "Connection: close\r\n"; strcat(dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 70 //len = calcu_len();
Ricky_Kwon 0:c863ec1eea0f 71 sprintf(dat, "%sContent-Length: %d\r\n\r\n", dat, calcu_len()); strcat(dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 72
Ricky_Kwon 0:c863ec1eea0f 73 client.send(dat, strlen(dat));
Ricky_Kwon 0:c863ec1eea0f 74 printf("send_dat : %s\r\n", dat);
Ricky_Kwon 0:c863ec1eea0f 75
Ricky_Kwon 0:c863ec1eea0f 76 client.send(send_dat, sizeof(send_dat));
Ricky_Kwon 0:c863ec1eea0f 77 printf("send_dat : %s\r\n", send_dat);
Ricky_Kwon 0:c863ec1eea0f 78 //printf("send_dat size : %d\r\n", len);
Ricky_Kwon 0:c863ec1eea0f 79
Ricky_Kwon 0:c863ec1eea0f 80 memset(dat, 0, sizeof(dat));
Ricky_Kwon 0:c863ec1eea0f 81 memset(send_dat, 0, sizeof(send_dat));
Ricky_Kwon 0:c863ec1eea0f 82 memset(buffer, 0, sizeof(buffer));
Ricky_Kwon 0:c863ec1eea0f 83 }
Ricky_Kwon 0:c863ec1eea0f 84
Ricky_Kwon 0:c863ec1eea0f 85 }
Ricky_Kwon 0:c863ec1eea0f 86
Ricky_Kwon 0:c863ec1eea0f 87 client.close();
Ricky_Kwon 0:c863ec1eea0f 88 }
Ricky_Kwon 0:c863ec1eea0f 89
Ricky_Kwon 0:c863ec1eea0f 90 }
Ricky_Kwon 0:c863ec1eea0f 91 uint16_t calcu_len(void)
Ricky_Kwon 0:c863ec1eea0f 92 {
Ricky_Kwon 0:c863ec1eea0f 93 char * dat_temp;
Ricky_Kwon 0:c863ec1eea0f 94 char * forbidden = NULL;
Ricky_Kwon 0:c863ec1eea0f 95 char str[4]={0,};
Ricky_Kwon 0:c863ec1eea0f 96
Ricky_Kwon 0:c863ec1eea0f 97 //dat_temp = "<!DOCTYPE HTML>\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 98 dat_temp = "<html>\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 99 dat_temp = "\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 100 dat_temp = "<head>\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 101 dat_temp = "<meta http-equiv=\"content-type\" content=\"text/html; charset=euc-kr\">\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 102 dat_temp = "<title>fishbowl</title>\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 103 dat_temp = "<meta name=\"generator\" content=\"Namo WebEditor\">\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 104 dat_temp = "</head>\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 105 dat_temp = "\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 106 dat_temp = "<body bgcolor=\"white\" text=\"black\" link=\"blue\" vlink=\"purple\" alink=\"red\">\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 107 dat_temp = "<p>fishbowl</p>\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 108 //dat_temp = "<p>&nbsp;</p>\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 109 dat_temp = "<p>Temperature in the fishbowl = $$$$$</p>\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 110
Ricky_Kwon 0:c863ec1eea0f 111 dat_temp = "<form name=\"form1\">\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 112 dat_temp = "<p><input type=\"hidden\" name=\"do\" value=\"ON\"></p>\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 113 dat_temp = "<p>FEED <input type=\"button\" name=\"formbutton1\" value=\"ON\" onclick=\"submit();\"></p>\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 114 dat_temp = "</form>\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 115
Ricky_Kwon 0:c863ec1eea0f 116 //dat_temp = "<p></p>\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 117 dat_temp = "</body>\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 118 dat_temp = "\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 119 dat_temp = "</html>\r\n"; strcat(send_dat, dat_temp);
Ricky_Kwon 0:c863ec1eea0f 120 forbidden = strstr(send_dat, "$$$$$");
Ricky_Kwon 0:c863ec1eea0f 121 memset(forbidden,0,5);
Ricky_Kwon 0:c863ec1eea0f 122 sprintf((char*)str, "%5.2f", tempC);
Ricky_Kwon 0:c863ec1eea0f 123 memcpy(forbidden,str,5);
Ricky_Kwon 0:c863ec1eea0f 124
Ricky_Kwon 0:c863ec1eea0f 125 return strlen(send_dat);
Ricky_Kwon 0:c863ec1eea0f 126 }