feat: implement OneNet communication and ESP8266 integration
break: switch to c8t6
This commit is contained in:
140
Core/Src/main.c
140
Core/Src/main.c
@ -30,6 +30,8 @@
|
||||
#include "retarget.h"
|
||||
#include "oled.h"
|
||||
#include "rc522.h"
|
||||
#include "esp8266.h"
|
||||
#include "OneNet.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@ -50,6 +52,11 @@
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
extern unsigned short esp8266_cnt;
|
||||
extern unsigned char esp8266_buf[128];
|
||||
uint8_t* dataPtr;
|
||||
uint8_t UART2_RxData;
|
||||
|
||||
uint8_t rf_status;
|
||||
uint8_t rf_card_type[3];
|
||||
uint8_t rf_card_id[5];
|
||||
@ -103,28 +110,60 @@ int main(void)
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_I2C1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
MX_SPI1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
MX_USART2_UART_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
// Retarget uart
|
||||
RetargetInit(&huart1);
|
||||
|
||||
// Init RC522
|
||||
MFRC_Init();
|
||||
PCD_Reset();
|
||||
|
||||
// Init OLED
|
||||
OLED_Init();
|
||||
OLED_Clear();
|
||||
|
||||
OLED_ShowString(0,0,"scanning...",12);
|
||||
OLED_ShowString(0,0,"initializing...",12);
|
||||
|
||||
HAL_UART_Receive_IT(&huart2, (uint8_t *) &UART2_RxData, 1);
|
||||
ESP8266_Init();
|
||||
while (OneNet_DevLink()) {}
|
||||
OneNET_Subscribe();
|
||||
|
||||
// Init RC522
|
||||
MFRC_Init();
|
||||
PCD_Reset();
|
||||
|
||||
OLED_ShowString(0,0,"OneNet online ",12);
|
||||
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
// if (!OneNet_DevLink())
|
||||
// {
|
||||
// OLED_ShowString(0,0,"OneNet online ",12);
|
||||
// HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
|
||||
// OneNet_SendData();
|
||||
// HAL_Delay(1000);
|
||||
// ESP8266_Clear();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// OLED_ShowString(0,0,"OneNet offline ",12);
|
||||
// HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_SET);
|
||||
// }
|
||||
OneNet_SendData();
|
||||
HAL_Delay(1000);
|
||||
ESP8266_Clear();
|
||||
|
||||
dataPtr = ESP8266_GetIPD(0);
|
||||
if (dataPtr != NULL)
|
||||
{
|
||||
OneNet_RevPro(dataPtr);
|
||||
}
|
||||
|
||||
rf_status = PCD_Request(PICC_REQALL, rf_card_type);
|
||||
|
||||
if (!rf_status) {
|
||||
@ -135,9 +174,9 @@ int main(void)
|
||||
|
||||
if (!rf_status) {
|
||||
rf_status = PCD_ERR;
|
||||
printf(SEP);
|
||||
printf("[i] Card Type: %02X %02X %02X\r\n", rf_card_type[0], rf_card_type[1], rf_card_type[2]);
|
||||
printf("[i] Card ID : %02X-%02X-%02X-%02X\r\n", rf_card_id[0], rf_card_id[1], rf_card_id[2], rf_card_id[3]);
|
||||
// printf(SEP);
|
||||
// printf("[i] Card Type: %02X %02X %02X\r\n", rf_card_type[0], rf_card_type[1], rf_card_type[2]);
|
||||
// printf("[i] Card ID : %02X-%02X-%02X-%02X\r\n", rf_card_id[0], rf_card_id[1], rf_card_id[2], rf_card_id[3]);
|
||||
|
||||
OLED_ShowString(0, 2, "Typ:", 12);
|
||||
uint8_t card_type_buffer[9];
|
||||
@ -156,24 +195,24 @@ int main(void)
|
||||
rf_status = PCD_ERR;
|
||||
|
||||
rf_status = PCD_AuthState(PICC_AUTHENT1A, ADDR, KEY_A, rf_card_id);
|
||||
if(rf_status == PCD_OK)
|
||||
{
|
||||
printf("[*] Key A pass\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[!] Key A verification failed\r\n");
|
||||
}
|
||||
// if(rf_status == PCD_OK)
|
||||
// {
|
||||
// printf("[*] Key A pass\r\n");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// printf("[!] Key A verification failed\r\n");
|
||||
// }
|
||||
|
||||
rf_status = PCD_AuthState(PICC_AUTHENT1B, ADDR, KEY_B, rf_card_id);
|
||||
if(rf_status == PCD_OK)
|
||||
{
|
||||
printf("[*] Key B pass\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[!] Key B verification failed\r\n");
|
||||
}
|
||||
// if(rf_status == PCD_OK)
|
||||
// {
|
||||
// printf("[*] Key B pass\r\n");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// printf("[!] Key B verification failed\r\n");
|
||||
// }
|
||||
}
|
||||
|
||||
if(rf_status == PCD_OK)
|
||||
@ -181,19 +220,19 @@ int main(void)
|
||||
rf_status = PCD_ERR;
|
||||
rf_status = PCD_ReadBlock(ADDR, rf_data);
|
||||
|
||||
if(rf_status == PCD_OK)
|
||||
{
|
||||
printf("[+] Data in sector %d block %d: ", ADDR/4, ADDR%4);
|
||||
for(int i = 0; i < 16; i++)
|
||||
{
|
||||
printf("%02x", rf_data[i]);
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Read card failed\r\n");
|
||||
}
|
||||
// if(rf_status == PCD_OK)
|
||||
// {
|
||||
// printf("[+] Data in sector %d block %d: ", ADDR/4, ADDR%4);
|
||||
// for(int i = 0; i < 16; i++)
|
||||
// {
|
||||
// printf("%02x", rf_data[i]);
|
||||
// }
|
||||
// printf("\r\n");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// printf("Read card failed\r\n");
|
||||
// }
|
||||
HAL_GPIO_WritePin(LED0_GPIO_Port, LED0_Pin, GPIO_PIN_RESET);
|
||||
HAL_Delay(3000);
|
||||
}
|
||||
@ -246,9 +285,30 @@ void SystemClock_Config(void)
|
||||
/* USER CODE BEGIN 4 */
|
||||
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
||||
{
|
||||
if(GPIO_Pin == KEY1_Pin)
|
||||
if(GPIO_Pin == KEY1_Pin || GPIO_Pin == KEY2_Pin || GPIO_Pin == KEY3_Pin || GPIO_Pin == KEY4_Pin)
|
||||
{
|
||||
OLED_Clear();
|
||||
HAL_GPIO_TogglePin(RELAY1_GPIO_Port, RELAY1_Pin);
|
||||
if(HAL_GPIO_ReadPin(RELAY1_GPIO_Port, RELAY1_Pin) == GPIO_PIN_RESET)
|
||||
{
|
||||
OLED_ShowString(0, 6, "Relay1: ON ", 12);
|
||||
}
|
||||
else
|
||||
{
|
||||
OLED_ShowString(0, 6, "Relay1: OFF", 12);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
||||
{
|
||||
if (huart == &huart2)
|
||||
{
|
||||
if (esp8266_cnt >= sizeof(esp8266_buf))
|
||||
{
|
||||
esp8266_cnt = 0;
|
||||
}
|
||||
esp8266_buf[esp8266_cnt++] = UART2_RxData;
|
||||
HAL_UART_Receive_IT(&huart2, (uint8_t *)&UART2_RxData, 1);
|
||||
}
|
||||
}
|
||||
/* USER CODE END 4 */
|
||||
|
Reference in New Issue
Block a user