/*---------------------------------------------------------------------------- Copyright: Radig Ulrich mailto: mail@ulrichradig.de Author: Radig Ulrich Remarks: known Problems: none Version: 12.11.2007 Description: HTTP-Client (empfang einer Webseite) Dieses Programm ist freie Software. Sie können es unter den Bedingungen der GNU General Public License, wie von der Free Software Foundation veröffentlicht, weitergeben und/oder modifizieren, entweder gemäß Version 2 der Lizenz oder (nach Ihrer Option) jeder späteren Version. Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License. Sie sollten eine Kopie der GNU General Public License zusammen mit diesem Programm erhalten haben. Falls nicht, schreiben Sie an die Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. ------------------------------------------------------------------------------*/ #include "config.h" #include "http_get.h" #include #include #include #include #include "stack.h" #include "usart.h" #include "timer.h" //PROGMEM char WEATHER_GET_STRING[] = {"GET /koch.htm HTTP/1.1\r\n" //PROGMEM char WEATHER_GET_STRING[] = {"GET /temp/insert.php?Mainboard_Sensors_Id=c81e728d9d4c2f636f067f89cc14862c&Mainboard_Sensors_Log_Temperature=13 HTTP/1.1\r\n" //PROGMEM char WEATHER_GET_STRING[] = {"GET /temp/insert.php?Mainboard_Sensors_Id_1=%d&Mainboard_Sensors_Log_Temperature_1=%d&Mainboard_Sensors_Id_2=%d&Mainboard_Sensors_Log_Temperature_2=%d&Mainboard_Sensors_Id_3=%d&Mainboard_Sensors_Log_Temperature_3=%d&Mainboard_Sensors_Id_4=%d&Mainboard_Sensors_Log_Temperature_4=%d HTTP/1.1\r\n" //PROGMEM char WEATHER_GET_STRING[] = {"GET /temp/insert.php?Id=%d&Temperature=%d&DateTimeGenerated=%s&blaat=1 HTTP/1.1\r\n" //PROGMEM char WEATHER_GET_STRING[] = {"GET /temp/insert.php?Temperature1=%d&Temperature2=%d&Temperature3=%d&Temperature4=%d&Time=%s&Date=%s HTTP/1.1\r\n" //PROGMEM char WEATHER_GET_STRING[] = {"GET /temp/insert.php?Sensor1=%s&Temperature1=%d&Sensor2=%s&Temperature2=%d&Sensor3=%s&Temperature3=%d&Sensor4=%s&Temperature4=%d&Time=%s&Date=%s HTTP/1.1\r\n" //PROGMEM char WEATHER_GET_STRING[] = {"GET /temp/insert.php?S1=1&T1=0&S2=2&T2=195&S3=3&T3=0&S4=4&T4=223&Time=10:30:53&Date=2010-10-04 HTTP/1.1\r\n" PROGMEM char WEATHER_GET_STRING[] = {"GET /temp/insert.php?S1=%s&T1=%d&S2=%s&T2=%d&S3=%s&T3=%d&S4=%s&T4=%d&Time=%s&Date=%s HTTP/1.1\r\n" //"GET / HTTP/1.1\r\n" "Host: www.webservicex.net\r\n" //"Host: www.ulrichradig.de\r\n" "Connection: close\r\n\r\n"}; volatile unsigned int http_get_state = -1; unsigned int my_http_cp = LOCAL_HTTP_PORT; char buffer_string[230]={0}; //---------------------------------------------------------------------------- //HTTP_GET INIT void httpg_init (void) { ////unsigned int my_http_cp = LOCAL_HTTP_PORT; add_tcp_app (my_http_cp, (void(*)(unsigned char))test); } //---------------------------------------------------------------------------- //Abfrage starten //void run_http_request (void) void run_http_request (char *sensor1, int16_t *LM92_CurrentTemp1, char *sensor2, int16_t *LM92_CurrentTemp2, char *sensor3, int16_t *LM92_CurrentTemp3, char *sensor4, int16_t *LM92_CurrentTemp4, char *sql_date, char *sql_time) { sprintf_P(buffer_string,WEATHER_GET_STRING,sensor1, *LM92_CurrentTemp1,sensor2, *LM92_CurrentTemp2,sensor3, *LM92_CurrentTemp3,sensor4, *LM92_CurrentTemp4,sql_time,sql_date); if (http_get_state==-1) { http_get_state=0; //usart_write("DataLog: [Sensor: %i] [Temperature: %i] [Time: %s] [Date: %s]\r\n",*sensor_id,*LM92_CurrentTemp,sql_time,sql_date); //usart_write("DataLog: [Temperature1: %i] [Temperature2: %i] [Temperature3: %i] [Temperature4: %i] [Time: %s] [Date: %s]\r\n",*LM92_CurrentTemp1,*LM92_CurrentTemp2,*LM92_CurrentTemp3,*LM92_CurrentTemp4,sql_time,sql_date); usart_write("DataLog: sensor: %s %s %s %s [Temperature1: %i] [Temperature2: %i] [Temperature3: %i] [Temperature4: %i] [Time: %s] [Date: %s]\r\n",sensor1, sensor2, sensor3, sensor4, *LM92_CurrentTemp1,*LM92_CurrentTemp2,*LM92_CurrentTemp3,*LM92_CurrentTemp4,sql_time,sql_date); usart_write("HTTP GET URL: %s\r\n",buffer_string); HTTPC_DEBUG("buffer: %s\r\n", buffer_string); } else { //usart_write("DataLog Fail: [Sensor: %i] [Temperature: %i] [Time: %s] [Date: %s]\r\n",*sensor_id,*LM92_CurrentTemp,sql_time,sql_date); usart_write("DataLog Fail: %s %s %s %s [Temperature1: %i] [Temperature2: %i] [Temperature3: %i] [Temperature4: %i] [Time: %s] [Date: %s]\r\n",sensor1, sensor2, sensor3, sensor4, *LM92_CurrentTemp1,*LM92_CurrentTemp2,*LM92_CurrentTemp3,*LM92_CurrentTemp4,sql_time,sql_date); usart_write("HTTP GET URL: %s\r\n",buffer_string); usart_write("-------\r\n"); //savetosd() HTTPC_DEBUG("ERROR - HTTP Request BUSY\n\r"); } } //---------------------------------------------------------------------------- //Daten kommen von einem Webserver an!! void test (unsigned char index) { char buffer[10]={0}; for (int a = TCP_DATA_START_VAR;a < TCP_DATA_END_VAR;a++) { HTTPC_DEBUG("%c",eth_buffer[a]); if (eth_buffer[a] == '$') //look for #$ string { if (eth_buffer[a+1] == '#') { if (eth_buffer[a+2] != '1') { usart_write("SQL insert failed\r\n",eth_buffer[a+2]); } } } } tcp_entry[index].time = TCP_TIME_OFF; if(tcp_entry[index].status & FIN_FLAG) { http_get_state=-1; usart_write("HTTP Request - EOF\n\r"); usart_write("-------\r\n"); return; } if (TCP_DATA_START_VAR == TCP_DATA_END_VAR) { return; } //else ////changed to else //{ tcp_entry[index].status = ACK_FLAG; create_new_tcp_packet(0,index); //} } //---------------------------------------------------------------------------- //HTTP Request an einen Webserver stelle void http_request (void) { unsigned long index = MAX_TCP_ENTRY; // if (http_get_state > 1 && http_get_state < 20) http_get_state++; if (http_get_state == 0) { //offnet eine Verbindung zu meinem Webserver HTTPC_DEBUG("HTTP Request\n\r"); //unsigned int my_http_cp = LOCAL_HTTP_PORT; //usart_write("time: %i\r\n",time); //unsigned int my_httpget_cp_new = my_http_cp + time; unsigned int my_httpget_cp_new = my_http_cp + 1; if (my_httpget_cp_new < 1000) my_httpget_cp_new +=1000; if (my_httpget_cp_new == 65535) my_httpget_cp_new =1001; change_port_tcp_app (my_http_cp, my_httpget_cp_new); my_http_cp = my_httpget_cp_new; //ARP Request senden if(arp_request (WEATHER_SERVER_IP)) { for(unsigned long a=0;a<2000000;a++){asm("nop");}; usart_write("debug 1\r\n"); tcp_port_open (WEATHER_SERVER_IP,HTONS(80),HTONS(my_http_cp)); usart_write("debug 2\r\n"); unsigned char tmp_counter = 0; usart_write("debug 3\r\n"); while((index >= MAX_ARP_ENTRY) && (tcp_entry[index].app_status != 1)) { index = tcp_entry_search (WEATHER_SERVER_IP,HTONS(80)); if (tmp_counter++ > 30) { HTTPC_DEBUG("TCP Eintrag nicht gefunden (HTTP_CLIENT)!\r\n"); http_get_state = -1; return; } } usart_write("debug 4\r\n"); HTTPC_DEBUG("TCP Eintrag gefunden (HTTP_CLIENT)!\r\n"); tcp_entry[index].first_ack = 1; http_get_state = 2; //usart_write("tcp_entry[index].app_status %i\r\n",tcp_entry[index].app_status); } else { http_get_state = -1; } } //if (http_get_state == 10) /* if ((http_get_state > 10 && http_get_state < 20) && (tcp_entry[index].app_status == 1)) { usart_write("tcp_entry[index].app_status2 %i\r\n",tcp_entry[index].app_status); HTTPC_DEBUG("\r\n\r\n\r\nDaten Anfordern\r\n"); index = tcp_entry_search (WEATHER_SERVER_IP,HTONS(80)); //memcpy_P(ð_buffer[TCP_DATA_START],WEATHER_GET_STRING,(sizeof(WEATHER_GET_STRING)-1)); memcpy(ð_buffer[TCP_DATA_START],buffer_string,(sizeof(buffer_string)-1)); tcp_entry[index].status = ACK_FLAG | PSH_FLAG; create_new_tcp_packet((sizeof(buffer_string)-1),index); http_get_state=21; usart_write("index: %i\r\n", index); } */ if (tcp_entry[index].app_status == 1) usart_write("tcp_entry[index].app_status4 %i\r\n",tcp_entry[index].app_status); //NEW-CHANGE if (http_get_state > 3) { index = tcp_entry_search (WEATHER_SERVER_IP,HTONS(80)); if ((tcp_entry[index].app_status == 1) && (http_get_state < 20)) { //if (http_get_state > 8) //{ HTTPC_DEBUG("\r\n\r\n\r\nDaten Anfordern\r\n"); //index = tcp_entry_search (WEATHER_SERVER_IP,HTONS(80)); //memcpy_P(ð_buffer[TCP_DATA_START],WEATHER_GET_STRING,(sizeof(WEATHER_GET_STRING)-1)); memcpy(ð_buffer[TCP_DATA_START],buffer_string,(sizeof(buffer_string)-1)); tcp_entry[index].status = ACK_FLAG | PSH_FLAG; create_new_tcp_packet((sizeof(buffer_string)-1),index); http_get_state=21; usart_write("index: %i\r\n", index); //} } } //NEW //if (http_get_state == 20) //20 or more (if waittime to short) but you must change some values more //{ //DEBUG "Error - no answer from server // http_get_state = -1; //} }