138 lines
3.7 KiB
C++
138 lines
3.7 KiB
C++
/*
|
|
* ESP8266.cpp
|
|
*
|
|
* Created on: Mar 7, 2023
|
|
* Author: Gabriel
|
|
*/
|
|
|
|
#include "ESP8266.hpp"
|
|
#include <cstdio>
|
|
|
|
ESP8266::ESP8266(UART_HandleTypeDef* huart) : huart(huart){
|
|
// TODO Auto-generated constructor stub
|
|
|
|
}
|
|
|
|
ESP8266::~ESP8266() {
|
|
// TODO Auto-generated destructor stub
|
|
}
|
|
|
|
void ESP8266::init(){
|
|
command("AT+CWMODE=1,1\r\n");
|
|
command("AT+CWJAP=\"Gabriel_2G\",\"gabrielwifi\"\r\n");
|
|
command("AT+CWLAP\r\n");
|
|
command("AT+CIPMUX=1\r\n");
|
|
command("AT+CIPSTART=0,\"UDP\",\"239.0.0.1\",10001,11001,0\r\n");
|
|
}
|
|
|
|
ESP8266::statusTypeDef ESP8266::command(const char* command) {
|
|
HAL_UART_Transmit(huart, (uint8_t*)command, strlen(command), 100);
|
|
return wait_until_ok();
|
|
}
|
|
|
|
ESP8266::statusTypeDef ESP8266::send(const char* data) {
|
|
HAL_UART_Transmit(huart, (uint8_t*)data, strlen(data), 100);
|
|
return wait_until_send_ok();
|
|
}
|
|
|
|
void ESP8266::send_uint32(const char* name, uint32_t value){
|
|
char commandBuffer[256];
|
|
char payloadBuffer[256];
|
|
sprintf(payloadBuffer, ">%s:%lu\r\n", name, value);
|
|
sprintf(commandBuffer, "AT+CIPSEND=0,%u\r\n", strlen(payloadBuffer));
|
|
command(commandBuffer);
|
|
send(payloadBuffer);
|
|
}
|
|
|
|
HAL_StatusTypeDef ESP8266::wait_on_flag_until_timeout(uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout) {
|
|
/* Wait until flag is set */
|
|
while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status) {
|
|
/* Check for the Timeout */
|
|
if (Timeout != HAL_MAX_DELAY) {
|
|
if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout)) {
|
|
/* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
|
|
CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
|
|
CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
|
|
|
|
huart->gState = HAL_UART_STATE_READY;
|
|
huart->RxState = HAL_UART_STATE_READY;
|
|
|
|
/* Process Unlocked */
|
|
__HAL_UNLOCK(huart);
|
|
|
|
return HAL_TIMEOUT;
|
|
}
|
|
}
|
|
}
|
|
return HAL_OK;
|
|
}
|
|
|
|
HAL_StatusTypeDef ESP8266::uart_receive_until_termination_byte(uint8_t *pData, uint16_t Size, uint8_t terminationByte, uint32_t Timeout) {
|
|
uint32_t tickstart = 0U;
|
|
|
|
/* Check that a Rx process is not already ongoing */
|
|
if (huart->RxState == HAL_UART_STATE_READY) {
|
|
|
|
/* Process Locked */
|
|
__HAL_LOCK(huart);
|
|
|
|
huart->ErrorCode = HAL_UART_ERROR_NONE;
|
|
huart->RxState = HAL_UART_STATE_BUSY_RX;
|
|
huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
|
|
|
|
/* Init tickstart for timeout management */
|
|
tickstart = HAL_GetTick();
|
|
|
|
huart->RxXferSize = Size;
|
|
huart->RxXferCount = Size;
|
|
|
|
/* Process Unlocked */
|
|
__HAL_UNLOCK(huart);
|
|
|
|
/* Check the remain data to be received */
|
|
while (huart->RxXferCount > 0U) {
|
|
if (wait_on_flag_until_timeout(UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) {
|
|
return HAL_TIMEOUT;
|
|
}
|
|
*pData = (uint8_t) (huart->Instance->DR & (uint8_t) 0x00FF);
|
|
huart->RxXferCount--;
|
|
if (*pData == terminationByte) {
|
|
pData[1] = 0; //Coloca o zero no final da string
|
|
break;
|
|
}
|
|
pData++;
|
|
}
|
|
|
|
/* At end of Rx process, restore huart->RxState to Ready */
|
|
huart->RxState = HAL_UART_STATE_READY;
|
|
|
|
return HAL_OK;
|
|
} else {
|
|
return HAL_BUSY;
|
|
}
|
|
}
|
|
|
|
ESP8266::statusTypeDef ESP8266::wait_until_ok() {
|
|
while(uart_receive_until_termination_byte(buffer, MAX_BUFFER_SIZE, '\n', OK_TIMEOUT) != HAL_TIMEOUT){
|
|
if(!strcmp((char*) buffer, "OK\r\n")){
|
|
return STATUS_OK;
|
|
}
|
|
if(!strcmp((char*) buffer, "ERROR\r\n")){
|
|
return STATUS_ERROR;
|
|
}
|
|
}
|
|
return STATUS_TIMEOUT;
|
|
}
|
|
|
|
ESP8266::statusTypeDef ESP8266::wait_until_send_ok() {
|
|
while(uart_receive_until_termination_byte(buffer, MAX_BUFFER_SIZE, '\n', OK_TIMEOUT) != HAL_TIMEOUT){
|
|
if(!strcmp((char*) buffer, "SEND OK\r\n")){
|
|
return STATUS_OK;
|
|
}
|
|
if(!strcmp((char*) buffer, "SEND ERROR\r\n")){
|
|
return STATUS_ERROR;
|
|
}
|
|
}
|
|
return STATUS_TIMEOUT;
|
|
}
|