Código completo
O código completo será apresentado logo abaixo, lembre de fazer as devidas configurações no sdk config.
#include <stdio.h>
#include <sdkconfig.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/semphr.h>
#include <freertos/queue.h>
#include <esp_lcd_types.h>
#include <driver/gpio.h>
#include <esp_err.h>
#include <esp_log.h>
#include "ESPMI.h"
#include <usb/hid_host.h>
#include <usb/usb_host.h>
#include <esp_netif_ip_addr.h>
#include <esp_netif.h>
#include <esp_eth.h>
#include <esp_event.h>
#include <esp_log.h>
#include <cJSON.h>
#include "ethernet_init.h"
#include "HDMI.h"
#include "lvgl.h"
#include "ebyte_core.h"
#include "ebyte_port.h"
#include "ebyte_callback.h"
#include "ethernet_init.h"
#include "mqtt_client.h"
static const char TAG [] = "Main";
typedef struct {
uint16_t temperature;
uint16_t humidity;
int16_t accX;
int16_t accY;
int16_t accZ;
}SensorBox_TypeDef;
SensorBox_TypeDef* sensors;
uint32_t packetsReceived = 0;
volatile static float temperature;
volatile static float humidity;
static float accX;
static float accY;
static float accZ;
static esp_mqtt_client_handle_t client = NULL;
extern int8_t Ebyte_E220x_GetRssiInst( void );
float lis3dh_from_fs16_hr_to_mg(int16_t lsb){
return ((float)lsb / 16.0f) * 12.0f;
}
void Ebyte_Port_TransmitCallback( uint16_t state ){
}
void Ebyte_Port_ReceiveCallback( uint16_t state, uint8_t *buffer, uint8_t length){
static ESPMI_MCPGpioLevel level;
extern bool mqttIsConnected;
if(state == 0x0002){
sensors = (SensorBox_TypeDef*)buffer;
temperature = -45.0f + 175.0f * (float)sensors->temperature/65535.0f;
humidity = 100 * ((float)sensors->humidity/65535.0f);
accX = lis3dh_from_fs16_hr_to_mg(sensors->accX)/100.0f;
accY = lis3dh_from_fs16_hr_to_mg(sensors->accY)/100.0f;
accZ = lis3dh_from_fs16_hr_to_mg(sensors->accZ)/100.0f;
packetsReceived++;
ESP_LOGI(TAG, "%.1f, %.1f, %.1f, %.1f, %.1f, %i\r\n", temperature, humidity, accX, accY, accZ, Ebyte_E220x_GetRssiInst());
if(client != NULL){
if(mqttIsConnected){
cJSON* root = cJSON_CreateObject();
cJSON_AddNumberToObject(root, "accX", accX);
cJSON_AddNumberToObject(root, "accY", accY);
cJSON_AddNumberToObject(root, "accZ", accZ);
cJSON_AddNumberToObject(root, "temp", temperature);
cJSON_AddNumberToObject(root, "hum", humidity);
cJSON_AddNumberToObject(root, "packets", packetsReceived);
cJSON_AddNumberToObject(root, "rssi", Ebyte_E220x_GetRssiInst());
char* json_str = cJSON_Print(root);
esp_mqtt_client_publish(client, "v1/devices/me/telemetry", json_str, strlen(json_str), 0, 0);
cJSON_Delete(root);
free(json_str);
}
}
level = !level;
ESPMI_MCP23008_SetLevelGpioPin(GPIO_NUM_7, level);
}
}
uint8_t Ebyte_BSP_SpiTransAndRecv(uint8_t data){
spi_transaction_t spiTrans;
const char* TAG = "E220 SPI transmitter";
memset(&spiTrans, 0, sizeof(spi_transaction_t));
spiTrans.length = 8;
spiTrans.tx_data[0] = data;
spiTrans.flags = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA;
if(spi_device_polling_transmit(ESPMI_GetSPIHandle(), &spiTrans) != ESP_OK){
ESP_LOGE(TAG, "SPI transmission failed");
}
return spiTrans.rx_data[0];
}
void Ebyte_BSP_RfSpiUnselected( void ){
gpio_set_level(ESPMI_E220_900MM22S_NSS, 1);
}
void Ebyte_BSP_RfSpiSelected( void ){
gpio_set_level(ESPMI_E220_900MM22S_NSS, 0);
}
void Ebyte_BSP_RfResetIoHigh( void ){
ESPMI_MCP23008_SetLevelGpioPin(ESPMI_E220_900MM22S_RST, True);
}
void Ebyte_BSP_RfResetIoLow( void ){
ESPMI_MCP23008_SetLevelGpioPin(ESPMI_E220_900MM22S_RST, False);
}
void Ebyte_BSP_RfTxIoEnable( void ){
ESPMI_MCP23008_SetLevelGpioPin(ESPMI_E220_900MM22S_TXEN, True);
}
void Ebyte_BSP_RfTxIoDisable( void ){
ESPMI_MCP23008_SetLevelGpioPin(ESPMI_E220_900MM22S_TXEN, False);
}
void Ebyte_BSP_RfRxIoEnable( void ){
ESPMI_MCP23008_SetLevelGpioPin(ESPMI_E220_900MM22S_RXEN, True);
}
void Ebyte_BSP_RfRxIoDisable( void ){
ESPMI_MCP23008_SetLevelGpioPin(ESPMI_E220_900MM22S_RXEN, False);
}
uint8_t Ebyte_BSP_RfBusyIoRead( void ){
Boolean state = False;
ESPMI_MCP23008_ReadLevelPin(ESPMI_E220_900MM22S_BUSY, &state);
return (uint8_t)state;
}
void app_main(void){
extern esp_mqtt_client_handle_t mqtt_app_start(void);
ESPMI_ConfigI2C(ESPMI_I2C_MASTER_NUM);
ESPMI_ConfigSPI(SPI2_HOST);
ESPMI_ConfigGPIO();
ESPMI_MCP23008_ConfigGpioDir(0x71);
ESPMI_Eth_Init(SPI3_HOST);
client = mqtt_app_start();
Ebyte_RF.Init();
Ebyte_RF.EnterReceiveMode(0);
while (1) {
Ebyte_RF.StartPollTask();
vTaskDelay(1/portTICK_PERIOD_MS);
}
}
Dashboard ThingsBoard
Last updated