123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- #include <stdio.h>
- #include <string.h>
- #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<noOfBytes;i++) {
- while( !uart1serialAvailable() ) {
- if( millis() > 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<strlen((char const *)reply);i++) {
- //send_usart2( reply[i] );
- //}
- printf("\nREPLY -->%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<strlen((char const *)text);i++) {
- send_usart1(text[i]);
- }
-
- send_usart1(13);
- send_usart1(10);
-
- result = getWiFiReply(timeout, responseText, cleanUpAfter);
-
- printf("----------------\n\n");
-
- flushBuffers();
-
- return result;
- }
- static void sendRawToESP(unsigned char *text) {
-
- flushBuffers();
- printf("SEND RAW-->%s<--\n",text);
-
- for(int i=0;i<strlen((char const *)text);i++) {
- send_usart1(text[i]);
- }
-
- }
- static int waitForWiFiConnection() {
-
- int status = 0;
- int retryCount = 10;
- int retries = 5;
-
- while( retries > 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);
- }
|