Пример #1
0
STATIC mp_obj_t esp_active(size_t n_args, const mp_obj_t *args) {
    wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);

    wifi_mode_t mode;
    if (!wifi_started) {
        mode = WIFI_MODE_NULL;
    } else {
        ESP_EXCEPTIONS(esp_wifi_get_mode(&mode));
    }

    int bit = (self->if_id == WIFI_IF_STA) ? WIFI_MODE_STA : WIFI_MODE_AP;

    if (n_args > 1) {
        bool active = mp_obj_is_true(args[1]);
        mode = active ? (mode | bit) : (mode & ~bit);
        if (mode == WIFI_MODE_NULL) {
            if (wifi_started) {
                ESP_EXCEPTIONS(esp_wifi_stop());
                wifi_started = false;
            }
        } else {
            ESP_EXCEPTIONS(esp_wifi_set_mode(mode));
            if (!wifi_started) {
                ESP_EXCEPTIONS(esp_wifi_start());
                wifi_started = true;
            }
        }
    }

    return (mode & bit) ? mp_const_true : mp_const_false;
}
Пример #2
0
/**
 * get WiFi mode
 * @return WiFiMode
 */
wifi_mode_t WiFiGenericClass::getMode()
{
    if(!_esp_wifi_started){
        return WIFI_MODE_NULL;
    }
    wifi_mode_t mode;
    if(esp_wifi_get_mode(&mode) == ESP_ERR_WIFI_NOT_INIT){
        log_w("WiFi not started");
        return WIFI_MODE_NULL;
    }
    return mode;
}
Пример #3
0
/******************************************************************************
 * FunctionName : espconn_connect
 * Description  : The function given as the connect
 * Parameters   : espconn -- the espconn used to listen the connection
 * Returns      : none
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR
espconn_connect(struct espconn *espconn)
{
	ip_addr_t ipaddr;
	tcpip_adapter_ip_info_t ipinfo;
	uint8 connect_status = 0;
	sint8 value = ESPCONN_OK;
	espconn_msg *plist = NULL;
	// remot_info *pinfo = NULL;

    if (espconn == NULL) {
        return ESPCONN_ARG;
    } else if (espconn ->type != ESPCONN_TCP)
    	return ESPCONN_ARG;

    /*Check the active node count whether is the limit or not*/
    if (espconn_get_acticve_num(ESPCONN_TCP) >= espconn_tcp_get_max_con())
    {
    	printf("espconn_tcp_get_max_con \n");
    	return ESPCONN_ISCONN;
    }

    /*Check the IP address whether is zero or not in different mode*/
    esp_wifi_get_mode((wifi_mode_t*)&value);
    if (value == WIFI_MODE_STA){
    	wifi_get_ip_info(TCPIP_ADAPTER_IF_STA,&ipinfo);
    	if (ipinfo.ip.addr == 0){
   	 		return ESPCONN_RTE;
   	 	}
    } else if(value == WIFI_MODE_AP){
    	wifi_get_ip_info(TCPIP_ADAPTER_IF_AP,&ipinfo);
    	if (ipinfo.ip.addr == 0){
    		return ESPCONN_RTE;
    	}
    } else if(value == WIFI_MODE_APSTA){
    	IP4_ADDR(&ipaddr.u_addr.ip4, espconn->proto.tcp->remote_ip[0],
    	    	    		espconn->proto.tcp->remote_ip[1],
    	    	    		espconn->proto.tcp->remote_ip[2],
    	    	    		espconn->proto.tcp->remote_ip[3]);
    	ipaddr.u_addr.ip4.addr <<= 8;
    	wifi_get_ip_info(TCPIP_ADAPTER_IF_AP,&ipinfo);
    	ipinfo.ip.addr <<= 8;
    	// espconn_printf("softap_addr = %x, remote_addr = %x\n", ipinfo.ip.addr, ipaddr.u_addr.ip4.addr);

    	if (ipaddr.u_addr.ip4.addr != ipinfo.ip.addr){

    		connect_status = wifi_station_get_connect_status();
    		
			if (connect_status == SYSTEM_EVENT_STA_GOT_IP){
				wifi_get_ip_info(TCPIP_ADAPTER_IF_STA,&ipinfo);
    // espconn_printf("wifi_get_ip_info:(ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR ")\n",
    //         IP2STR(&ipinfo.ip), IP2STR(&ipinfo.netmask), IP2STR(&ipinfo.gw));



				if (ipinfo.ip.addr == 0)
					return ESPCONN_RTE;
			} else if (connect_status != SYSTEM_EVENT_STA_GOT_IP){
				return ESPCONN_RTE;
			} else {
				return connect_status;
			}
    	}
    }
    /*check the active node information whether is the same as the entity or not*/
    for (plist = plink_active; plist != NULL; plist = plist->pnext){
    	if (plist->pespconn && plist->pespconn->type == ESPCONN_TCP){
    		if (espconn->proto.tcp->local_port == plist->pespconn->proto.tcp->local_port){
    			return ESPCONN_ISCONN;
    		}
    	}
    }

	espconn_set_opt(espconn, ESPCONN_COPY|ESPCONN_NODELAY);
    value = espconn_tcp_client(espconn);
    espconn_printf("espconn_connect err value %d \n",value);

    return value;
}
Пример #4
0
// This function is called by the system-event task and so runs in a different
// thread to the main MicroPython task.  It must not raise any Python exceptions.
static esp_err_t event_handler(void *ctx, system_event_t *event) {
   switch(event->event_id) {
    case SYSTEM_EVENT_STA_START:
        ESP_LOGI("wifi", "STA_START");
        break;
    case SYSTEM_EVENT_STA_CONNECTED:
        ESP_LOGI("network", "CONNECTED");
        break;
    case SYSTEM_EVENT_STA_GOT_IP:
        ESP_LOGI("network", "GOT_IP");
        wifi_sta_connected = true;
        wifi_sta_disconn_reason = 0; // Success so clear error. (in case of new error will be replaced anyway)
        break;
    case SYSTEM_EVENT_STA_DISCONNECTED: {
        // This is a workaround as ESP32 WiFi libs don't currently
        // auto-reassociate.
        system_event_sta_disconnected_t *disconn = &event->event_info.disconnected;
        char *message = "";
        wifi_sta_disconn_reason = disconn->reason;
        switch (disconn->reason) {
            case WIFI_REASON_BEACON_TIMEOUT:
                // AP has dropped out; try to reconnect.
                message = "\nbeacon timeout";
                break;
            case WIFI_REASON_NO_AP_FOUND:
                // AP may not exist, or it may have momentarily dropped out; try to reconnect.
                message = "\nno AP found";
                break;
            case WIFI_REASON_AUTH_FAIL:
                message = "\nauthentication failed";
                wifi_sta_connect_requested = false;
                break;
            default:
                // Let other errors through and try to reconnect.
                break;
        }
        ESP_LOGI("wifi", "STA_DISCONNECTED, reason:%d%s", disconn->reason, message);

        wifi_sta_connected = false;
        if (wifi_sta_connect_requested) {
            wifi_mode_t mode;
            if (esp_wifi_get_mode(&mode) == ESP_OK) {
                if (mode & WIFI_MODE_STA) {
                    // STA is active so attempt to reconnect.
                    esp_err_t e = esp_wifi_connect();
                    if (e != ESP_OK) {
                        ESP_LOGI("wifi", "error attempting to reconnect: 0x%04x", e);
                    }
                }
            }
        }
        break;
    }
    case SYSTEM_EVENT_GOT_IP6:
        ESP_LOGI("network", "Got IPv6");
        break;
    case SYSTEM_EVENT_ETH_START:
        ESP_LOGI("ethernet", "start");
        break;
    case SYSTEM_EVENT_ETH_STOP:
        ESP_LOGI("ethernet", "stop");
        break;
    case SYSTEM_EVENT_ETH_CONNECTED:
        ESP_LOGI("ethernet", "LAN cable connected");
        break;
    case SYSTEM_EVENT_ETH_DISCONNECTED:
        ESP_LOGI("ethernet", "LAN cable disconnected");
        break;
    case SYSTEM_EVENT_ETH_GOT_IP:
        ESP_LOGI("ethernet", "Got IP");
        break;
    default:
        ESP_LOGI("network", "event %d", event->event_id);
        break;
    }
    return ESP_OK;
}