#include #include #include "sw_fifo_1.h" #include "sw_fifo_2.h" #include "app.h" #include "led_blink.h" #include "main.h" #include "queue.h" #include "temp3.h" #include "stm32f4xx_hal.h" #include "stm32f4xx_hal_gpio.h" #include "wifi.h" #include "interrupts.h" static int waitForWiFiConnection(); static int sendToESP(unsigned char *text, int timeout, unsigned char *responseText, int cleanUpAfter); static char * getData(int timeout); static int getWiFiReply(int timeout, unsigned char *responseText, int cleanUpAfter); static void cleanUpRxBuffer(int no); static void flushBuffers(); static void sendRawToESP(unsigned char *text); #define MAX_DATA_SIZE 1024 char rcvData[MAX_DATA_SIZE]; void setupWifi() { printf("Setup ESP8266\n"); printf("Power: Off - "); deactivateESP8266(); delay(1000); activateESP8266(); printf("On\n"); getWiFiReply(5000,"ready",1); cleanUpRxBuffer(0); delay(2000); // Give the module a chance to connect to the local WiFi network // Do we have an IP already ? if( waitForWiFiConnection() == 0 ) { // No IP, try to connect to WiFi AP sendToESP("AT+CWMODE=1",5000, NULL, 1); sendToESP("AT+CWJAP=\"Hemnet3\",\"robingustavchef\"", 10000, NULL, 1); //sendToESP("AT+CWJAP=\"Thomass iPhone\",\"a3m97hso90d5c\"", 10000, NULL); sendToESP("AT+CIPMUX=0",5000, NULL, 1); } blinkTheLED(); } int sendDataToServer(int type,unsigned int data) { static char dataStr[200]; sprintf(dataStr, "GET /input/input.php?type=%d&data=%u", type, data); return (sendTCPData(dataStr)==0)?0:1; } char * sendTCPData(char *data) { static char data2[200]; char *cp; // Try to connect to HTTP-Server if( sendToESP("AT+CIPSTART=\"TCP\",\"192.168.1.110\", 80",10000, "OKLinked",1) == 0 ) { return 0; // Exit } cleanUpRxBuffer(0); sprintf(data2, "AT+CIPSEND=%d", strlen(data)+13); if( sendToESP((unsigned char *)data2,5000, "> ",1) == 0 ) { return 0; // Exit } sendRawToESP((unsigned char *)data); if( sendToESP((unsigned char *)" HTTP/1.0\r\n",5000, "SEND OK",0) == 0 ) { return 0; // Exit } printf("Fetch data reply..\n"); cp = getData(10000); if( cp == 0 ) { return 0; // Exit } //printf("HTTP Rx Data:\n"); //printf("%s\n -- End data -- \n",rcvData); flushBuffers(); delay(1000); // Short delay after data transmit, to help stabilize everything printf("--------------- EXIT sendDataToServer() -------------\n"); return cp; } // +IPD,256:HTTP/ static char * getData(int timeout) { static char bytes[20]; char *bytes_p=bytes; char c='\0'; int noOfBytes; int i; unsigned int timeout_time = millis() + timeout; getWiFiReply(timeout,"+IPD,",0); flushBuffers(); *bytes_p='\0'; while( c != ':' ) { while( !uart1serialAvailable() ) { if( millis() > timeout_time ) return 0; // Exit if no data arrives } c = uart_1_get_byte(); *bytes_p = c; *bytes_p++; *bytes_p='\0'; } sscanf(bytes,"%d",&noOfBytes); printf("No of data bytes:%d\n",noOfBytes); for(i=0;i timeout_time ) return 0; // Exit if no data arrives }; c = uart_1_get_byte(); rcvData[i] = c; } rcvData[i] = '\0'; if( getWiFiReply(timeout,"OKOKUnlink",1) == 0 ) { return 0; } return rcvData; } static int getWiFiReply(int timeout, unsigned char *responseText, int cleanUpAfter) { int done = 0; unsigned long startTime = millis(); unsigned long endTime=0; static unsigned char reply[1024]; unsigned char *reply_p = reply; reply[0] = '\0'; flushBuffers(); while( (millis() < (startTime + timeout)) && done != 2 ) { if( uart1serialAvailable() ) { unsigned char c = uart_1_get_byte(); if( c == 10 || c == 13 || ( c >= ' ' && c < 127 ) ) { printf("%c",c); } if( done == 0 ) { if( c >= ' ' && c <= '}' ) { *reply_p = c; reply_p++; *reply_p = '\0'; } if( responseText == NULL ) { if( strstr((char const *)reply,"OK") != 0 || strstr((char const *)reply,"ready") != 0 || strstr((char const *)reply,"no change") != 0 ) { done = 1; } } else { if( strstr((char const *)reply,(char const *)responseText) != 0 ) { done = 1; } } if( done == 1 ) { endTime = millis() + 300; // Keep receiving for 100ms more..... // In some cases we don't want to receive and display after-data if( cleanUpAfter == 0 ) { done = 2; } } } } if( done == 1 && millis() > endTime ) { done = 2; } flushBuffers(); } if( done == 2 ) done=1; //for(int i=0; i%s<-- Status:%d \n", reply, done); flushBuffers(); return done; } static int sendToESP(unsigned char *text, int timeout, unsigned char *responseText, int cleanUpAfter) { int result; flushBuffers(); printf("SEND -->%s<--\n",text); for(int i=0;i%s<--\n",text); for(int i=0;i 0 && sendToESP("ATE0",2000, NULL, 1) == 0 ) { retries--; } while( retryCount > 0 && status == 0 ) { if( sendToESP("AT+CIFSR",4000, "192.168.",1) == 0 ) { retryCount--; } else { status = 1; } } return status; } // This functions waits until all tx-buffers are empty #pragma optimize=none static void flushBuffers() { while( uart1_tx_fifo_not_empty_flag ); while( uart2_tx_fifo_not_empty_flag ); } #pragma optimize=none static void cleanUpRxBuffer(int no) { if( no == 0 ) { while( uart1serialAvailable() ) { uart_1_get_byte(); } } else { for(int i = no; i>0; i-- ) { if( uart1serialAvailable() ) { uart_1_get_byte(); } } } } void activateESP8266() { HAL_GPIO_WritePin(GPIOA,GPIO_PIN_15,(GPIO_PinState)1); } void deactivateESP8266() { HAL_GPIO_WritePin(GPIOA,GPIO_PIN_15,(GPIO_PinState)0); }