221 lines
6.9 KiB
C
221 lines
6.9 KiB
C
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/semphr.h"
|
|
#include "freertos/timers.h"
|
|
#include "nvs_flash.h"
|
|
#include "esp_random.h"
|
|
#include "esp_event.h"
|
|
#include "esp_netif.h"
|
|
#include "esp_wifi.h"
|
|
#include "esp_log.h"
|
|
#include "esp_mac.h"
|
|
#include "esp_now.h"
|
|
#include "esp_crc.h"
|
|
#include "MagicEspnow.h"
|
|
#include "adc_read.h"
|
|
#include "nvsSave.h"
|
|
#include "pwm_led.h"
|
|
|
|
#define ESPNOW_MAXDELAY 512
|
|
|
|
static const char *TAG = "MagicEspnow";
|
|
bool is_broadcast = true;//是否开始广播
|
|
bool startConnect = false;//是否开始广播数据
|
|
|
|
char command[2][10] = {"connect", "unicast"};
|
|
|
|
bool semAdcData = true;
|
|
bool semBroadcastData = true;
|
|
bool sendStart = false;
|
|
extern struct adcData_t adcData;
|
|
|
|
uint8_t test = 'a';
|
|
|
|
static uint8_t s_example_broadcast_mac[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // 广播数据地址
|
|
// static uint8_t s_example_broadcast_mac[ESP_NOW_ETH_ALEN] = {0xf4,0x12,0xfa,0xcb,0xe8,0x93}; // 广播数据地址
|
|
uint8_t dst_mac[ESP_NOW_ETH_ALEN] = {0}; // 单播数据地址
|
|
|
|
|
|
void PeerAdd(uint8_t *dstMac){
|
|
esp_now_peer_info_t *peer = malloc(sizeof(esp_now_peer_info_t));
|
|
if (peer == NULL)
|
|
{
|
|
ESP_LOGE(TAG, "Malloc peer information fail");
|
|
esp_now_deinit();
|
|
return;
|
|
}
|
|
peer->channel = 1;
|
|
peer->ifidx = ESPNOW_WIFI_IF;
|
|
peer->encrypt = false;
|
|
memcpy(peer->peer_addr, dstMac, ESP_NOW_ETH_ALEN);
|
|
esp_err_t ret = esp_now_add_peer(peer);
|
|
if(ret == ESP_ERR_ESPNOW_EXIST){
|
|
ESP_LOGI(TAG,"mac had exist");
|
|
}
|
|
free(peer);
|
|
}
|
|
|
|
static void example_wifi_init(void)
|
|
{
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(ESPNOW_WIFI_MODE));
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
|
ESP_ERROR_CHECK(esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE));
|
|
}
|
|
|
|
static void example_espnow_send_cb()
|
|
{
|
|
if (is_broadcast && startConnect)
|
|
ESP_LOGI(TAG, "Send baadcast connect data succeed");
|
|
// else if (unicast && sendStart == false)
|
|
// ESP_LOGI(TAG, "Send unicast connect data succeed");
|
|
else if (unicast)
|
|
ESP_LOGI(TAG,"Send unicast adcdata succeed");
|
|
}
|
|
|
|
static void example_espnow_recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *data, int len)
|
|
{
|
|
// ESP_LOGI(TAG, "Received data %s", data);
|
|
if (unicast)//处理收到数据为单播的unicast
|
|
{
|
|
ESP_LOGI(TAG, "Received unicast mac %x:%x:%x:%x:%x:%x", recv_info->src_addr[0], recv_info->src_addr[1], recv_info->src_addr[2], recv_info->src_addr[3], recv_info->src_addr[4], recv_info->src_addr[5]);
|
|
//单播的时候接收到了数据
|
|
printf("unicast: %s\n",(char*) data);
|
|
// if(strcmp((const char*)data,"unicast")==0){
|
|
// if(esp_now_send(dst_mac,&command[1],strlen(command[1]))){
|
|
// ESP_LOGI(TAG,"faled to send");
|
|
// }
|
|
// printf("set send Start True");
|
|
// //当收到车发送的unicast时就开始处理adc的数据
|
|
// // sendStart = true;
|
|
// }
|
|
}
|
|
else if (is_broadcast && strcmp((const char *)data, "connect") == 0)//处理收到的数据为广播的connect
|
|
{
|
|
ESP_LOGI(TAG, "Received connect mac %x:%x:%x:%x:%x:%x", recv_info->src_addr[0], recv_info->src_addr[1], recv_info->src_addr[2], recv_info->src_addr[3], recv_info->src_addr[4], recv_info->src_addr[5]);
|
|
printf("connect: %s\n",(char*)data);
|
|
memcpy(dst_mac, recv_info->src_addr, sizeof(dst_mac));
|
|
ESP_LOGI(TAG, "memcpy dst_mac %x:%x:%x:%x:%x:%x", dst_mac[0], dst_mac[1], dst_mac[2], dst_mac[3], dst_mac[4], dst_mac[5]);
|
|
save_mac_to_nvs("storage","mac",dst_mac);
|
|
read_mac_from_nvs("storage","mac");
|
|
PeerAdd(dst_mac);
|
|
is_broadcast = false;
|
|
}
|
|
else if (is_broadcast)
|
|
{
|
|
// 车发来的数据处理
|
|
}
|
|
|
|
}
|
|
|
|
static void example_espnow_task(void *pvParameter)
|
|
{
|
|
int ret;
|
|
|
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
|
ESP_LOGI(TAG, "Start sending broadcast data");
|
|
|
|
/* Start sending broadcast ESPNOW data. */
|
|
if(is_broadcast)
|
|
if (esp_now_send(s_example_broadcast_mac, &command[0], strlen(command[0])) != ESP_OK)
|
|
{
|
|
ESP_LOGE(TAG, "Send error");
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
while (1)
|
|
{
|
|
if (is_broadcast)
|
|
{
|
|
if(startConnect)
|
|
if (esp_now_send(s_example_broadcast_mac, &command[0], strlen(command[0])) != ESP_OK)
|
|
{
|
|
ESP_LOGE(TAG, "Send boardcast data error");
|
|
}
|
|
vTaskDelay(250 / portTICK_PERIOD_MS);
|
|
}
|
|
else
|
|
{
|
|
// if(sendStart){
|
|
while (semAdcData == false)
|
|
{
|
|
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
}
|
|
semAdcData = false;
|
|
if(esp_now_send(dst_mac,&adcData,sizeof(adcData)));
|
|
semAdcData = true;
|
|
// }
|
|
// else{
|
|
// if(esp_now_send(dst_mac,&command[1],strlen(command[1]))){
|
|
// ESP_LOGE(TAG, "Send unicast connect data error");
|
|
// }
|
|
// }
|
|
|
|
}
|
|
vTaskDelay(20 / portTICK_PERIOD_MS);
|
|
}
|
|
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
static esp_err_t example_espnow_init(void)
|
|
{
|
|
/* Initialize ESPNOW and register sending and receiving callback function. */
|
|
ESP_ERROR_CHECK(esp_now_init());
|
|
ESP_ERROR_CHECK(esp_now_register_send_cb(example_espnow_send_cb));
|
|
ESP_ERROR_CHECK(esp_now_register_recv_cb(example_espnow_recv_cb));
|
|
|
|
/* Add broadcast peer information to peer list. */
|
|
esp_now_peer_info_t *peer = malloc(sizeof(esp_now_peer_info_t));
|
|
if (peer == NULL)
|
|
{
|
|
ESP_LOGE(TAG, "Malloc peer information fail");
|
|
esp_now_deinit();
|
|
return ESP_FAIL;
|
|
}
|
|
peer->channel = 1;
|
|
peer->ifidx = ESPNOW_WIFI_IF;
|
|
peer->encrypt = false;
|
|
memcpy(peer->peer_addr, s_example_broadcast_mac, ESP_NOW_ETH_ALEN);
|
|
ESP_ERROR_CHECK(esp_now_add_peer(peer));
|
|
free(peer);
|
|
|
|
// 一开始先设置为初始广播地址
|
|
|
|
xTaskCreate(example_espnow_task, "example_espnow_task", 2048, NULL, 4, NULL);
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
static void example_espnow_deinit(example_espnow_send_param_t *send_param)
|
|
{
|
|
|
|
esp_now_deinit();
|
|
}
|
|
|
|
void app_main(void)
|
|
{
|
|
// Initialize NVS
|
|
esp_err_t ret = nvs_flash_init();
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
|
{
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
ret = nvs_flash_init();
|
|
}
|
|
ESP_ERROR_CHECK(ret);
|
|
example_wifi_init();
|
|
example_espnow_init();
|
|
read_mac_from_nvs("storage","mac");
|
|
|
|
xTaskCreate(adc_read, "adc_read", 2048, NULL, 4, NULL);
|
|
// xTaskCreate(ButtonHandle,"ButtonHandle",2048,NULL,4,NULL);
|
|
xTaskCreate(LedPwmSet,"LedpwmSet",2048,NULL,4,NULL);
|
|
}
|