app.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. **********************************************************************************
  3. * WiFi Gateway Project 100MHz STM32-F411RE
  4. **********************************************************************************
  5. * This project works as a wireless sensor receiver using a 433MHz receiver module.
  6. * The data is then sent to a Raspberry Pi using TCP/IP via a ESP8266 module.
  7. * System clock is 100MHz
  8. *
  9. * SysTick-counter is setup to handle the LED-flashing
  10. * The systick int is 1mS and it calles the LED-Handler
  11. * Systick is changed to the lowest possible priority
  12. *
  13. * TIM2 is set to sample the Rx-input in a 10uS period. TIM2_IRQHandler()
  14. * HW is set to 32 prescaler + 10 counter period
  15. * TIM2 also handles the micros()-functions, and counts mS in millis()
  16. *
  17. * TIM3 is not used
  18. *
  19. * The USART1 is used for comm to WiFi-module. Handled with interrupts.
  20. *
  21. * The USART2 is used for communicating to the ST/Link v2 as a simulated
  22. * USB Serial Port. This is handled with interrupts.
  23. *
  24. * Compiler options: Turned off common subexpression elimination.
  25. * If this on is one, then while-loop can be optimized away. Results in very
  26. * strange code. After this change, I could remove all volatile keywords.
  27. *
  28. * IO-Pins:
  29. * PA0 Input RX_DATA
  30. * PA5 Output GREEN_LED
  31. *
  32. * Notes:
  33. * Had one awful bug. It crashed into HardFault_Handler. The problem was found
  34. * after many hours on a forum. It was regarding string-functions which wrote to
  35. * a local stack-char-array belonging to a calling function ! Not fun !
  36. *
  37. **********************************************************************************
  38. */
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include "sw_fifo_1.h"
  42. #include "sw_fifo_2.h"
  43. #include "app.h"
  44. #include "led_blink.h"
  45. #include "main.h"
  46. #include "queue.h"
  47. #include "temp3.h"
  48. #include "stm32f4xx_hal.h"
  49. #include "stm32f4xx_hal_gpio.h"
  50. #include "wifi.h"
  51. #include "receiver.h"
  52. #include "interrupts.h"
  53. void printTime();
  54. void setRTCTime();
  55. extern RTC_HandleTypeDef hrtc;
  56. void setup() {
  57. printf("\n\n\n\n\n100MHz 433.92MHz to WiFi Gateway\n");
  58. printf("--------------------------------\n\n");
  59. deactivateESP8266(); // Turn off the WiFi-Board, ESP8266
  60. rcvInitQueue();
  61. wifi_rcvInitQueue();
  62. setupWifi();
  63. handle_RTC_interrupt();
  64. setRTCTime();
  65. printf("Init completed\n\n");
  66. receiverEnabled = 1;
  67. }
  68. void printClock() {
  69. unsigned long time=millis();
  70. printf("%lu,%lu",(time/1000),(time%1000));
  71. }
  72. extern int sec,min,hour;
  73. void loop() {
  74. static int noOfCodesReceived=0;
  75. unsigned int type, value;
  76. int code;
  77. int retrys = 0;
  78. //receiver();
  79. // This takes from the receive-function (in receiver.c)
  80. // And calls the WiFi-function to send data to the http-server
  81. while( wifi_rcvDeQueue( &type, &value) ) {
  82. switch( type ) {
  83. case 0: // Nexa
  84. printClock();printf(" <NR%08lX>\n\r",value);
  85. break;
  86. case 1:
  87. printClock();printf(" No 1:%u \n\n",value);
  88. break;
  89. case 2:
  90. printClock();printf(" No 2:%u \n\n",value);
  91. break;
  92. case 3:
  93. noOfCodesReceived++;
  94. printTime();printf(" No 3:%u ",value);
  95. code = (value & 0xFC000000) >> 26;
  96. printf("Code:%d \n\n",code);
  97. break;
  98. default:
  99. printf("Unknown type\n");
  100. break;
  101. }
  102. if( type == 3 ) {
  103. // Send data to server
  104. while( retrys < 3 && sendDataToServer(type,value ) == 0 ) {
  105. retrys++;
  106. printf("********** ERROR **********\n");
  107. setupWifi(); // We have run into problems.....need to reset WiFi-module
  108. }
  109. }
  110. }
  111. // Handle logging
  112. /*
  113. if( loggingReady == 1 ) {
  114. static char logTxtData[2000]; // CIP-Send-string is max 2048 bytes, according to ESP8266 Docs
  115. char str[20];
  116. loggingReady = 0;
  117. int logItems = (startLoggingPos<stopLoggingPos) ? stopLoggingPos-startLoggingPos : (LOG_SIZE-startLoggingPos)+stopLoggingPos;
  118. // Do something about the saved logging data
  119. printf("LOG items:%d\n",logItems);
  120. sprintf(logTxtData,"GET /input/errloginput.php?data=");
  121. for(;logItems>0;logItems--) {
  122. sprintf(str,"%d,",logData[startLoggingPos]);
  123. if( strlen(logTxtData)+20 < 2000 ) {
  124. strcat(logTxtData,str);
  125. }
  126. startLoggingPos++;
  127. if( startLoggingPos == LOG_SIZE ) {
  128. startLoggingPos = 0;
  129. }
  130. }
  131. sendTCPData(logTxtData);
  132. }
  133. if( newMinuteEvent ) {
  134. static char logTxtData[100];
  135. RTC_TimeTypeDef time;
  136. RTC_DateTypeDef date;
  137. newMinuteEvent = 0;
  138. HAL_RTC_GetTime(&hrtc,&time,RTC_FORMAT_BIN);
  139. HAL_RTC_GetDate(&hrtc,&date,RTC_FORMAT_BIN);
  140. sprintf(logTxtData,"GET /input/wifiLogInput.php?time=%d:%d:%d&rxcnt=%d",time.Hours,time.Minutes,time.Seconds,noOfCodesReceived);
  141. sendTCPData(logTxtData);
  142. noOfCodesReceived = 0;
  143. }
  144. */
  145. //=================================== SERIAL =================================
  146. // Serial handling to/from the WiFi-module ESP8266
  147. //============================================================================
  148. /*
  149. // ========================= From USB to WiFi ================================
  150. while( uart2serialAvailable() ) {
  151. unsigned char c = uart_2_get_byte();
  152. send_usart1( c );
  153. }
  154. // ========================= From WiFi to USB ================================
  155. while( uart1serialAvailable() ) {
  156. unsigned char c = uart_1_get_byte();
  157. send_usart2( c );
  158. }
  159. */
  160. }
  161. void printTime() {
  162. RTC_TimeTypeDef time;
  163. RTC_DateTypeDef date;
  164. HAL_RTC_GetTime(&hrtc,&time,RTC_FORMAT_BIN);
  165. HAL_RTC_GetDate(&hrtc,&date,RTC_FORMAT_BIN);
  166. printf("%02d:%02d:%02d",time.Hours,time.Minutes,time.Seconds);
  167. }
  168. void setRTCTime() {
  169. static char logTxtData[100];
  170. char *rx;
  171. int h,m,s;
  172. sprintf(logTxtData,"GET /info/currenttime.php");
  173. rx = sendTCPData(logTxtData);
  174. if( rx != 0 ) {
  175. rx = strstr(rx,"Time:");
  176. if( rx != 0 ) {
  177. rx += 5;
  178. *(rx + 8) = '\0';
  179. //printf("%s\n",rx);
  180. if( sscanf(rx,"%d:%d:%d",&h,&m,&s) ) {
  181. RTC_TimeTypeDef sTime;
  182. int retVal=0;
  183. printf("Set time..");
  184. sTime.Hours = h;
  185. sTime.Minutes = m;
  186. sTime.Seconds = s;
  187. sTime.SubSeconds = 0;
  188. sTime.TimeFormat = RTC_HOURFORMAT12_AM;
  189. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  190. sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  191. retVal = HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BIN);
  192. printf("R:%d\n",retVal);
  193. }
  194. }
  195. }
  196. }