123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- /**
- **********************************************************************************
- * WiFi Gateway Project 100MHz STM32-F411RE
- **********************************************************************************
- * This project works as a wireless sensor receiver using a 433MHz receiver module.
- * The data is then sent to a Raspberry Pi using TCP/IP via a ESP8266 module.
- * System clock is 100MHz
- *
- * SysTick-counter is setup to handle the LED-flashing
- * The systick int is 1mS and it calles the LED-Handler
- * Systick is changed to the lowest possible priority
- *
- * TIM2 is set to sample the Rx-input in a 10uS period. TIM2_IRQHandler()
- * HW is set to 32 prescaler + 10 counter period
- * TIM2 also handles the micros()-functions, and counts mS in millis()
- *
- * TIM3 is not used
- *
- * The USART1 is used for comm to WiFi-module. Handled with interrupts.
- *
- * The USART2 is used for communicating to the ST/Link v2 as a simulated
- * USB Serial Port. This is handled with interrupts.
- *
- * Compiler options: Turned off common subexpression elimination.
- * If this on is one, then while-loop can be optimized away. Results in very
- * strange code. After this change, I could remove all volatile keywords.
- *
- * IO-Pins:
- * PA0 Input RX_DATA
- * PA5 Output GREEN_LED
- *
- * Notes:
- * Had one awful bug. It crashed into HardFault_Handler. The problem was found
- * after many hours on a forum. It was regarding string-functions which wrote to
- * a local stack-char-array belonging to a calling function ! Not fun !
- *
- **********************************************************************************
- */
- #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 "receiver.h"
- #include "interrupts.h"
- void printTime();
- void setRTCTime();
- extern RTC_HandleTypeDef hrtc;
- void setup() {
-
- printf("\n\n\n\n\n100MHz 433.92MHz to WiFi Gateway\n");
- printf("--------------------------------\n\n");
-
- deactivateESP8266(); // Turn off the WiFi-Board, ESP8266
-
- rcvInitQueue();
- wifi_rcvInitQueue();
-
- setupWifi();
-
- handle_RTC_interrupt();
-
- setRTCTime();
-
- printf("Init completed\n\n");
-
- receiverEnabled = 1;
- }
- void printClock() {
-
- unsigned long time=millis();
-
- printf("%lu,%lu",(time/1000),(time%1000));
- }
- extern int sec,min,hour;
- void loop() {
- static int noOfCodesReceived=0;
- unsigned int type, value;
- int code;
- int retrys = 0;
-
- //receiver();
-
- // This takes from the receive-function (in receiver.c)
- // And calls the WiFi-function to send data to the http-server
- while( wifi_rcvDeQueue( &type, &value) ) {
-
-
- switch( type ) {
- case 0: // Nexa
- printClock();printf(" <NR%08lX>\n\r",value);
- break;
- case 1:
- printClock();printf(" No 1:%u \n\n",value);
- break;
- case 2:
- printClock();printf(" No 2:%u \n\n",value);
- break;
- case 3:
- noOfCodesReceived++;
- printTime();printf(" No 3:%u ",value);
- code = (value & 0xFC000000) >> 26;
- printf("Code:%d \n\n",code);
- break;
- default:
- printf("Unknown type\n");
- break;
- }
-
- if( type == 3 ) {
- // Send data to server
- while( retrys < 3 && sendDataToServer(type,value ) == 0 ) {
-
- retrys++;
- printf("********** ERROR **********\n");
- setupWifi(); // We have run into problems.....need to reset WiFi-module
- }
- }
- }
-
- // Handle logging
- /*
- if( loggingReady == 1 ) {
-
- static char logTxtData[2000]; // CIP-Send-string is max 2048 bytes, according to ESP8266 Docs
- char str[20];
-
- loggingReady = 0;
-
- int logItems = (startLoggingPos<stopLoggingPos) ? stopLoggingPos-startLoggingPos : (LOG_SIZE-startLoggingPos)+stopLoggingPos;
- // Do something about the saved logging data
- printf("LOG items:%d\n",logItems);
-
- sprintf(logTxtData,"GET /input/errloginput.php?data=");
-
- for(;logItems>0;logItems--) {
-
- sprintf(str,"%d,",logData[startLoggingPos]);
- if( strlen(logTxtData)+20 < 2000 ) {
- strcat(logTxtData,str);
- }
- startLoggingPos++;
- if( startLoggingPos == LOG_SIZE ) {
- startLoggingPos = 0;
- }
- }
-
- sendTCPData(logTxtData);
- }
-
-
- if( newMinuteEvent ) {
- static char logTxtData[100];
- RTC_TimeTypeDef time;
- RTC_DateTypeDef date;
- newMinuteEvent = 0;
-
- HAL_RTC_GetTime(&hrtc,&time,RTC_FORMAT_BIN);
- HAL_RTC_GetDate(&hrtc,&date,RTC_FORMAT_BIN);
-
- sprintf(logTxtData,"GET /input/wifiLogInput.php?time=%d:%d:%d&rxcnt=%d",time.Hours,time.Minutes,time.Seconds,noOfCodesReceived);
- sendTCPData(logTxtData);
-
- noOfCodesReceived = 0;
- }
- */
-
- //=================================== SERIAL =================================
- // Serial handling to/from the WiFi-module ESP8266
- //============================================================================
-
- /*
- // ========================= From USB to WiFi ================================
- while( uart2serialAvailable() ) {
- unsigned char c = uart_2_get_byte();
- send_usart1( c );
- }
-
- // ========================= From WiFi to USB ================================
- while( uart1serialAvailable() ) {
- unsigned char c = uart_1_get_byte();
- send_usart2( c );
- }
- */
- }
- void printTime() {
-
- RTC_TimeTypeDef time;
- RTC_DateTypeDef date;
-
- HAL_RTC_GetTime(&hrtc,&time,RTC_FORMAT_BIN);
- HAL_RTC_GetDate(&hrtc,&date,RTC_FORMAT_BIN);
-
- printf("%02d:%02d:%02d",time.Hours,time.Minutes,time.Seconds);
- }
- void setRTCTime() {
-
- static char logTxtData[100];
- char *rx;
- int h,m,s;
-
- sprintf(logTxtData,"GET /info/currenttime.php");
- rx = sendTCPData(logTxtData);
-
- if( rx != 0 ) {
- rx = strstr(rx,"Time:");
- if( rx != 0 ) {
- rx += 5;
- *(rx + 8) = '\0';
- //printf("%s\n",rx);
- if( sscanf(rx,"%d:%d:%d",&h,&m,&s) ) {
- RTC_TimeTypeDef sTime;
- int retVal=0;
- printf("Set time..");
- sTime.Hours = h;
- sTime.Minutes = m;
- sTime.Seconds = s;
- sTime.SubSeconds = 0;
- sTime.TimeFormat = RTC_HOURFORMAT12_AM;
- sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
- sTime.StoreOperation = RTC_STOREOPERATION_RESET;
- retVal = HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BIN);
- printf("R:%d\n",retVal);
- }
- }
- }
- }
|