コード例 #1
0
ファイル: user_main.c プロジェクト: icamgo/noduino-sdk
void ICACHE_FLASH_ATTR node_group_init(void)
{
	if (esp_now_init() == 0) {
		os_printf("esp_now init ok\n");
		esp_now_register_recv_cb(simple_cb);

		u8 ch = wifi_get_channel();
		os_printf("current wifi channel is: %d\n", ch);

		esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
		esp_now_add_peer(ctrl_mac, ESP_NOW_ROLE_CONTROLLER, ch, key, 16);
		esp_now_add_peer(slave_mac, ESP_NOW_ROLE_SLAVE, ch, key, 16);
	} else {
		os_printf("esp_now init failed\n");
	}
}
コード例 #2
0
void ICACHE_FLASH_ATTR sp_PairedDevLoadAll(PairedButtonParam* buttonParam)
{
    int i;
	for(i=0;i<buttonParam->PairedNum;i++){
		esp_now_add_peer(buttonParam->PairedList[i].mac_t,ESP_NOW_ROLE_CONTROLLER,0,buttonParam->PairedList[i].key_t,ESPNOW_KEY_LEN);
	}

}
コード例 #3
0
ファイル: esp_now_netdev.c プロジェクト: gebart/RIOT
static bool _esp_now_add_peer (uint8_t* bssid, uint8_t channel, uint8_t* key)
{
    if (esp_now_is_peer_exist(bssid)) {
        return false;
    }

    esp_now_peer_info_t peer = {};

    memcpy(peer.peer_addr, bssid, ESP_NOW_ETH_ALEN);
    peer.channel = channel;
    peer.ifidx = ESP_IF_WIFI_AP;

    if (esp_now_params.key) {
        peer.encrypt = true;
        memcpy(peer.lmk, esp_now_params.key, ESP_NOW_KEY_LEN);
    }

    esp_err_t ret = esp_now_add_peer(&peer);
    DEBUG("esp_now_add_peer node %02x:%02x:%02x:%02x:%02x:%02x "
          "added with return value %d\n",
          bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5], ret);
    return (ret == ESP_OK);
}
コード例 #4
0
ファイル: main.cpp プロジェクト: cho45/esp-wattmeter
void setup() {
	pinMode(13, OUTPUT);

	Serial.begin(74880);
	Serial.println("Initializing...");

	WiFi.mode(WIFI_AP_STA);
	WiFi.softAP("foobar", "12345678", 1, 0);

	if (startWifi(30)) {
	} else {
		Serial.println("failed to start wifi");
		ESP.restart();
	}

	uint8_t macaddr[6];
	wifi_get_macaddr(STATION_IF, macaddr);
	Serial.print("mac address (STATION_IF): ");
	printMacAddress(macaddr);

	wifi_get_macaddr(SOFTAP_IF, macaddr);
	Serial.print("mac address (SOFTAP_IF): ");
	printMacAddress(macaddr);

	if (esp_now_init() == 0) {
		Serial.println("init");
	} else {
		Serial.println("init failed");
		ESP.restart();
		return;
	}

	esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
	esp_now_register_recv_cb([](uint8_t *macaddr, uint8_t *data, uint8_t len) {
//		Serial.print("data: ");
//		for (int i = 0; i < len; i++) {
//			Serial.print(" 0x");
//			Serial.print(data[i], HEX);
//		}
//
//		Serial.println("");
//		Serial.print("recv_cb length=");
//		Serial.print(len);
//		Serial.print(" from mac address: ");
//		printMacAddress(macaddr);
//

		// exception...
		//send_data_t* send_data = static_cast<send_data_t*>(static_cast<void*>(data));

		send_data_t send_data;
		memcpy(&send_data, data, sizeof(send_data));

//		uint8_t data_len = (len - sizeof(send_data.chipId) - sizeof(send_data.battery)) / sizeof(uint32_t);
//		Serial.print("data_len: ");
//		Serial.println(data_len);

		data_queue.push_back(send_data);

	});
	esp_now_register_send_cb([](uint8_t* macaddr, uint8_t status) {
		Serial.println("send_cb");

		Serial.print("mac address: ");
		printMacAddress(macaddr);

		Serial.print("status = "); Serial.println(status);
	});

	int res = esp_now_add_peer(mac, (uint8_t)ESP_NOW_ROLE_CONTROLLER,(uint8_t)WIFI_DEFAULT_CHANNEL, NULL, 0);

//	esp_now_unregister_recv_cb();
//	esp_now_deinit();
}
コード例 #5
0
void ICACHE_FLASH_ATTR
sp_LightPairState(void)
{

#if 1
    switch(light_state){
        case (SP_LIGHT_IDLE):
            os_printf("status:SP_LIGHT_IDLE\n");
			//pair start command, correct
            if(USER_PBULIC_BUTTON_INFO==buttonPairingInfo.simple_pair_state){
                os_printf("statue:Get button Info,next wait button request\n");
				sp_LightPairTimerStart();
                sp_LightPairAccept();
                light_state=SP_LIGHT_WAIT_BUTTON_REQUEST;
				light_shadeStart(HINT_WHITE,500,3,1,NULL);
            }
			//exception
            else{
                light_state=SP_LIGHT_ERROR_HANDLE;
				light_hint_stop(HINT_RED);
                os_printf("buttonPairingInfo.simple_pair_state=%d\n",buttonPairingInfo.simple_pair_state);
				sp_LightPairState();
            }
            break;
        case (SP_LIGHT_WAIT_BUTTON_REQUEST):
            os_printf("status:SP_LIGHT_WAIT_BUTTON_REQUEST\n");
			light_shadeStart(HINT_GREEN,500,3,0,NULL);
			
			//receive button pair request, send req to phone
            if(LIGHT_RECV_BUTTON_REQUSET==buttonPairingInfo.simple_pair_state){
                os_printf("statue:Get button request,next wait user permit or refuse\n"); 
                sp_LightPairRequestPermission();
                light_state=SP_LIGHT_WAIT_USER_INDICATE_PERMIT;
            }
			//receive pair start command again before timeout , right now, restart state machine
			else if(USER_PBULIC_BUTTON_INFO==buttonPairingInfo.simple_pair_state){
                os_printf("statue:Get button Info,restart state machine,wait button request\n");
				sp_LightPairTimerStart();
                sp_LightPairAccept();
                light_state=SP_LIGHT_WAIT_BUTTON_REQUEST;
            }
			//error if other states
			else{
                //simple_pair_deinit();
                light_state=SP_LIGHT_ERROR_HANDLE;
				light_hint_stop(HINT_RED);
                os_printf("err in SP_LIGHT_WAIT_BUTTON_REQUEST ;simple_pair_state=%d\n",buttonPairingInfo.simple_pair_state);
				sp_LightPairState();
            }
            break;
        case (SP_LIGHT_WAIT_USER_INDICATE_PERMIT):
			light_shadeStart(HINT_BLUE,500,3,0,NULL);
            os_printf("status:SP_LIGHT_WAIT_USER_INDICATE_PERMIT\n");
			//phone user permit pairing
            if(USER_PERMIT_SIMPLE_PAIR==buttonPairingInfo.simple_pair_state){
                os_printf("statue:User permit simple pair,next wait simple result\n"); 
				sp_PairedDevRmAll(&PairedDev);//
                sp_LightPairStart();
                light_state=SP_LIGHT_WAIT_SIMPLE_PAIR_RESULT;
            }
			//phone user refuse pairing, END
			else if(USER_REFUSE_SIMPLE_PAIR==buttonPairingInfo.simple_pair_state){
                os_printf("statue:User refuse simple pair,state clear\n");
                //check state
                sp_LightPairRefuse();
                //light_state=SP_LIGHT_IDLE;
                //buttonPairingInfo.simple_pair_state=LIGHT_PAIR_IDLE;
                light_state = SP_LIGHT_END;
				light_shadeStart(HINT_RED,500,2,0,NULL);
				sp_LightPairState();
				
            }
			//exceptions
			else{
                //simple_pair_deinit();
                light_state=SP_LIGHT_ERROR_HANDLE;
				light_hint_stop(HINT_RED);
                os_printf("buttonPairingInfo.simple_pair_state=%d\n",buttonPairingInfo.simple_pair_state);
				sp_LightPairState();
            }
            break;
        case (SP_LIGHT_WAIT_SIMPLE_PAIR_RESULT):
            os_printf("status:SP_LIGHT_WAIT_SIMPLE_PAIR_RESULT\n");
			//pairing finished , END
            if(LIGHT_SIMPLE_PAIR_SUCCED==buttonPairingInfo.simple_pair_state){
                os_printf("status:sp_LightPairSucced\n");  
				light_shadeStart(HINT_WHITE,500,2,0,NULL);

				//1.get key
				//buttonPairingInfo.espnowKey
				//2.add peer and save
            	int res = esp_now_add_peer((uint8*)(buttonPairingInfo.button_mac), (uint8)ESP_NOW_ROLE_CONTROLLER,(uint8)wifi_get_channel(), (uint8*)(buttonPairingInfo.espnowKey), (uint8)ESPNOW_KEY_LEN);
            	os_printf("INIT RES: %d ; MAC:"MACSTR"\r\n",res,MAC2STR(((uint8*)(buttonPairingInfo.button_mac))));
				//3.save
                sp_AddPairedDev(&PairedDev,buttonPairingInfo.button_mac,buttonPairingInfo.espnowKey,wifi_get_channel());
                sp_DispPairedDev(&PairedDev);

                //simple_pair_deinit();//check
                //light_state=SP_LIGHT_IDLE;
                //buttonPairingInfo.simple_pair_state=LIGHT_PAIR_IDLE;
                sp_LightPairReportResult(true);
				light_state = SP_LIGHT_END;
				sp_LightPairState();
            }
			//pairing failed , END
			else if(LIGHT_SIMPLE_PAIR_FAIL==buttonPairingInfo.simple_pair_state){
                os_printf("status:sp_LightPairFail\n");
                //simple_pair_deinit();//check
                //light_state=SP_LIGHT_IDLE;
                //buttonPairingInfo.simple_pair_state=LIGHT_PAIR_IDLE;
                sp_LightPairReportResult(false);
				light_state = SP_LIGHT_END;
				light_hint_stop(HINT_RED);
				sp_LightPairState();
            }
			//exception
			else{
                simple_pair_deinit();
				light_hint_stop(HINT_RED);
                light_state=SP_LIGHT_ERROR_HANDLE;
                os_printf("buttonPairingInfo.simple_pair_state=%d\n",buttonPairingInfo.simple_pair_state);
				sp_LightPairState();
            }
            break; 
			
		case (SP_LIGHT_ERROR_HANDLE):
			os_printf("status:simpaire in Err!!!\r\nBACK TO IDLE\r\n"); 
			light_hint_stop(HINT_RED);
        case (SP_LIGHT_END):
			//reset state, deinit simplepair, stop tout timer
			sp_LightPairTimerStop();
			simple_pair_deinit();
			light_state=SP_LIGHT_IDLE;
			buttonPairingInfo.simple_pair_state=LIGHT_PAIR_IDLE;
			sp_PairedDevLoadAll(&PairedDev);
			break;
			  
        default:
            os_printf("status:unsafe param: %d\n",light_state);
            break;
    }

#endif
}
コード例 #6
0
ファイル: esp_now_netdev.c プロジェクト: gebart/RIOT
static void esp_now_setup (esp_now_netdev_t* dev)
{
    DEBUG("%s: %p\n", __func__, dev);

    /*
     * Init the WiFi driver. TODO It is not only required before ESP_NOW is
     * initialized but also before other WiFi functions are used. Once other
     * WiFi functions are realized it has to be moved to a more common place.
     */
    extern portMUX_TYPE g_intr_lock_mux;
    mutex_init(&g_intr_lock_mux);

    esp_system_event_add_handler (_esp_system_event_handler, NULL);

    esp_err_t result;
    #if CONFIG_ESP32_WIFI_NVS_ENABLED
    result = nvs_flash_init();
    if (result != ESP_OK) {
        LOG_TAG_ERROR("esp_now",
                      "nfs_flash_init failed with return value %d\n", result);
        return;
    }
    #endif

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    result = esp_wifi_init(&cfg);
    if (result != ESP_OK) {
        LOG_TAG_ERROR("esp_now",
                      "esp_wifi_init failed with return value %d\n", result);
        return;
    }

    #ifdef CONFIG_WIFI_COUNTRY
    /* TODO */
    #endif

    /* we use predefined station configuration */
    wifi_config_t wifi_config_sta = {
        .sta = {
            .ssid = CONFIG_WIFI_STA_SSID,
            .password = CONFIG_WIFI_STA_PASSWORD,
            .channel = CONFIG_WIFI_STA_CHANNEL,
            .scan_method = CONFIG_WIFI_STA_SCAN_METHOD,
            .sort_method = CONFIG_WIFI_STA_SORT_METHOD,
            .threshold.rssi = CONFIG_WIFI_STA_RSSI,
            .threshold.authmode = CONFIG_WIFI_STA_AUTHMODE
        }
    };

    /* get SoftAP interface mac address and store it as device addresss */
    esp_read_mac(dev->addr, ESP_MAC_WIFI_SOFTAP);

    /* prepare the ESP_NOW configuration for SoftAP */
    wifi_config_t wifi_config_ap = {};

    strcpy ((char*)wifi_config_ap.ap.password, esp_now_params.softap_pass);
    sprintf((char*)wifi_config_ap.ap.ssid, "%s%02x%02x%02x%02x%02x%02x",
            ESP_NOW_AP_PREFIX,
            dev->addr[0], dev->addr[1], dev->addr[2],
            dev->addr[3], dev->addr[4], dev->addr[5]);
    wifi_config_ap.ap.ssid_len = strlen((char*)wifi_config_ap.ap.ssid);

    wifi_config_ap.ap.channel = esp_now_params.channel;
    wifi_config_ap.ap.authmode = CONFIG_WIFI_AP_AUTH;
    wifi_config_ap.ap.ssid_hidden = CONFIG_WIFI_AP_HIDDEN;
    wifi_config_ap.ap.max_connection = CONFIG_WIFI_AP_MAX_CONN;
    wifi_config_ap.ap.beacon_interval = CONFIG_WIFI_AP_BEACON;

    /* set the WiFi interface to Station + SoftAP mode without DHCP */
    result = esp_wifi_set_mode(WIFI_MODE_STA | WIFI_MODE_AP);
    if (result != ESP_OK) {
        LOG_TAG_ERROR("esp_now",
                      "esp_wifi_set_mode failed with return value %d\n",
                      result);
        return;
    }

    /* set the Station and SoftAP configuration */
    result = esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config_sta);
    if (result != ESP_OK) {
        LOG_TAG_ERROR("esp_now", "esp_wifi_set_config station failed with "
                      "return value %d\n", result);
        return;
    }
    result = esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config_ap);
    if (result != ESP_OK) {
        LOG_TAG_ERROR("esp_now",
                      "esp_wifi_set_mode softap failed with return value %d\n",
                      result);
        return;
    }

    /* start the WiFi driver */
    result = esp_wifi_start();
    if (result != ESP_OK) {
        LOG_TAG_ERROR("esp_now",
                      "esp_wifi_start failed with return value %d\n", result);
        return;
    }

    #if ESP_NOW_UNICAST==0 /* TODO */
    /* all ESP-NOW nodes get the shared mac address on their station interface */
    wifi_set_macaddr(STATION_IF, (uint8_t*)_esp_now_mac);
    #endif

    /* set the netdev driver */
    dev->netdev.driver = &_esp_now_driver;

    /* initialize netdev data structure */
    dev->peers_all = 0;
    dev->peers_enc = 0;
    mutex_init(&dev->dev_lock);

    /* initialize ESP-NOW and register callback functions */
    result = esp_now_init();
    if (result != ESP_OK) {
        LOG_TAG_ERROR("esp_now", "esp_now_init failed with return value %d\n",
                      result);
        return;
    }
    esp_now_register_send_cb (esp_now_send_cb);
    esp_now_register_recv_cb (esp_now_recv_cb);

    #if ESP_NOW_UNICAST
    /* create the ESP_NOW event handler thread */
    esp_now_event_handler_pid = thread_create(esp_now_event_handler_stack,
                                             sizeof(esp_now_event_handler_stack),
                                             ESP_NOW_PRIO + 1,
                                             THREAD_CREATE_WOUT_YIELD |
                                             THREAD_CREATE_STACKTEST,
                                             esp_now_event_handler,
                                             NULL, "net-esp-now-event");

    /* timer for peer scan initialization */
    _esp_now_scan_peers_done = false;
    _esp_now_scan_peers_timer.callback = &esp_now_scan_peers_timer_cb;
    _esp_now_scan_peers_timer.arg = dev;

    /* execute the first scan */
    esp_now_scan_peers_done();

    #else /* ESP_NOW_UNICAST */
    #if 0
    int res = esp_now_add_peer((uint8_t*)_esp_now_mac, ESP_NOW_ROLE_COMBO,
                               esp_now_params.channel, NULL, 0);
    DEBUG("%s: multicast node added %d\n", __func__, res);
    #endif
    #endif /* ESP_NOW_UNICAST */
}