wifi.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "sw_fifo_1.h"
  4. #include "sw_fifo_2.h"
  5. #include "app.h"
  6. #include "led_blink.h"
  7. #include "main.h"
  8. #include "queue.h"
  9. #include "temp3.h"
  10. #include "stm32f4xx_hal.h"
  11. #include "stm32f4xx_hal_gpio.h"
  12. #include "wifi.h"
  13. #include "interrupts.h"
  14. static int waitForWiFiConnection();
  15. static int sendToESP(unsigned char *text, int timeout, unsigned char *responseText, int cleanUpAfter);
  16. static char * getData(int timeout);
  17. static int getWiFiReply(int timeout, unsigned char *responseText, int cleanUpAfter);
  18. static void cleanUpRxBuffer(int no);
  19. static void flushBuffers();
  20. static void sendRawToESP(unsigned char *text);
  21. #define MAX_DATA_SIZE 1024
  22. char rcvData[MAX_DATA_SIZE];
  23. void setupWifi() {
  24. printf("Setup ESP8266\n");
  25. printf("Power: Off - ");
  26. deactivateESP8266();
  27. delay(1000);
  28. activateESP8266();
  29. printf("On\n");
  30. getWiFiReply(5000,"ready",1);
  31. cleanUpRxBuffer(0);
  32. delay(2000); // Give the module a chance to connect to the local WiFi network
  33. // Do we have an IP already ?
  34. if( waitForWiFiConnection() == 0 ) {
  35. // No IP, try to connect to WiFi AP
  36. sendToESP("AT+CWMODE=1",5000, NULL, 1);
  37. sendToESP("AT+CWJAP=\"Hemnet3\",\"robingustavchef\"", 10000, NULL, 1);
  38. //sendToESP("AT+CWJAP=\"Thomass iPhone\",\"a3m97hso90d5c\"", 10000, NULL);
  39. sendToESP("AT+CIPMUX=0",5000, NULL, 1);
  40. }
  41. blinkTheLED();
  42. }
  43. int sendDataToServer(int type,unsigned int data) {
  44. static char dataStr[200];
  45. sprintf(dataStr, "GET /input/input.php?type=%d&data=%u", type, data);
  46. return (sendTCPData(dataStr)==0)?0:1;
  47. }
  48. char * sendTCPData(char *data) {
  49. static char data2[200];
  50. char *cp;
  51. // Try to connect to HTTP-Server
  52. if( sendToESP("AT+CIPSTART=\"TCP\",\"192.168.1.110\", 80",10000, "OKLinked",1) == 0 ) {
  53. return 0; // Exit
  54. }
  55. cleanUpRxBuffer(0);
  56. sprintf(data2, "AT+CIPSEND=%d", strlen(data)+13);
  57. if( sendToESP((unsigned char *)data2,5000, "> ",1) == 0 ) {
  58. return 0; // Exit
  59. }
  60. sendRawToESP((unsigned char *)data);
  61. if( sendToESP((unsigned char *)" HTTP/1.0\r\n",5000, "SEND OK",0) == 0 ) {
  62. return 0; // Exit
  63. }
  64. printf("Fetch data reply..\n");
  65. cp = getData(10000);
  66. if( cp == 0 ) {
  67. return 0; // Exit
  68. }
  69. //printf("HTTP Rx Data:\n");
  70. //printf("%s\n -- End data -- \n",rcvData);
  71. flushBuffers();
  72. delay(1000); // Short delay after data transmit, to help stabilize everything
  73. printf("--------------- EXIT sendDataToServer() -------------\n");
  74. return cp;
  75. }
  76. // +IPD,256:HTTP/
  77. static char * getData(int timeout) {
  78. static char bytes[20];
  79. char *bytes_p=bytes;
  80. char c='\0';
  81. int noOfBytes;
  82. int i;
  83. unsigned int timeout_time = millis() + timeout;
  84. getWiFiReply(timeout,"+IPD,",0);
  85. flushBuffers();
  86. *bytes_p='\0';
  87. while( c != ':' ) {
  88. while( !uart1serialAvailable() ) {
  89. if( millis() > timeout_time ) return 0; // Exit if no data arrives
  90. }
  91. c = uart_1_get_byte();
  92. *bytes_p = c;
  93. *bytes_p++;
  94. *bytes_p='\0';
  95. }
  96. sscanf(bytes,"%d",&noOfBytes);
  97. printf("No of data bytes:%d\n",noOfBytes);
  98. for(i=0;i<noOfBytes;i++) {
  99. while( !uart1serialAvailable() ) {
  100. if( millis() > timeout_time ) return 0; // Exit if no data arrives
  101. };
  102. c = uart_1_get_byte();
  103. rcvData[i] = c;
  104. }
  105. rcvData[i] = '\0';
  106. if( getWiFiReply(timeout,"OKOKUnlink",1) == 0 ) {
  107. return 0;
  108. }
  109. return rcvData;
  110. }
  111. static int getWiFiReply(int timeout, unsigned char *responseText, int cleanUpAfter) {
  112. int done = 0;
  113. unsigned long startTime = millis();
  114. unsigned long endTime=0;
  115. static unsigned char reply[1024];
  116. unsigned char *reply_p = reply;
  117. reply[0] = '\0';
  118. flushBuffers();
  119. while( (millis() < (startTime + timeout)) && done != 2 ) {
  120. if( uart1serialAvailable() ) {
  121. unsigned char c = uart_1_get_byte();
  122. if( c == 10 || c == 13 || ( c >= ' ' && c < 127 ) ) {
  123. printf("%c",c);
  124. }
  125. if( done == 0 ) {
  126. if( c >= ' ' && c <= '}' ) {
  127. *reply_p = c;
  128. reply_p++;
  129. *reply_p = '\0';
  130. }
  131. if( responseText == NULL ) {
  132. if( strstr((char const *)reply,"OK") != 0 || strstr((char const *)reply,"ready") != 0 || strstr((char const *)reply,"no change") != 0 ) {
  133. done = 1;
  134. }
  135. }
  136. else {
  137. if( strstr((char const *)reply,(char const *)responseText) != 0 ) {
  138. done = 1;
  139. }
  140. }
  141. if( done == 1 ) {
  142. endTime = millis() + 300; // Keep receiving for 100ms more.....
  143. // In some cases we don't want to receive and display after-data
  144. if( cleanUpAfter == 0 ) {
  145. done = 2;
  146. }
  147. }
  148. }
  149. }
  150. if( done == 1 && millis() > endTime ) {
  151. done = 2;
  152. }
  153. flushBuffers();
  154. }
  155. if( done == 2 ) done=1;
  156. //for(int i=0; i<strlen((char const *)reply);i++) {
  157. //send_usart2( reply[i] );
  158. //}
  159. printf("\nREPLY -->%s<-- Status:%d \n", reply, done);
  160. flushBuffers();
  161. return done;
  162. }
  163. static int sendToESP(unsigned char *text, int timeout, unsigned char *responseText, int cleanUpAfter) {
  164. int result;
  165. flushBuffers();
  166. printf("SEND -->%s<--\n",text);
  167. for(int i=0;i<strlen((char const *)text);i++) {
  168. send_usart1(text[i]);
  169. }
  170. send_usart1(13);
  171. send_usart1(10);
  172. result = getWiFiReply(timeout, responseText, cleanUpAfter);
  173. printf("----------------\n\n");
  174. flushBuffers();
  175. return result;
  176. }
  177. static void sendRawToESP(unsigned char *text) {
  178. flushBuffers();
  179. printf("SEND RAW-->%s<--\n",text);
  180. for(int i=0;i<strlen((char const *)text);i++) {
  181. send_usart1(text[i]);
  182. }
  183. }
  184. static int waitForWiFiConnection() {
  185. int status = 0;
  186. int retryCount = 10;
  187. int retries = 5;
  188. while( retries > 0 && sendToESP("ATE0",2000, NULL, 1) == 0 ) {
  189. retries--;
  190. }
  191. while( retryCount > 0 && status == 0 ) {
  192. if( sendToESP("AT+CIFSR",4000, "192.168.",1) == 0 ) {
  193. retryCount--;
  194. }
  195. else {
  196. status = 1;
  197. }
  198. }
  199. return status;
  200. }
  201. // This functions waits until all tx-buffers are empty
  202. #pragma optimize=none
  203. static void flushBuffers() {
  204. while( uart1_tx_fifo_not_empty_flag );
  205. while( uart2_tx_fifo_not_empty_flag );
  206. }
  207. #pragma optimize=none
  208. static void cleanUpRxBuffer(int no) {
  209. if( no == 0 ) {
  210. while( uart1serialAvailable() ) {
  211. uart_1_get_byte();
  212. }
  213. }
  214. else {
  215. for(int i = no; i>0; i-- ) {
  216. if( uart1serialAvailable() ) {
  217. uart_1_get_byte();
  218. }
  219. }
  220. }
  221. }
  222. void activateESP8266() {
  223. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_15,(GPIO_PinState)1);
  224. }
  225. void deactivateESP8266() {
  226. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_15,(GPIO_PinState)0);
  227. }