void setup() { pinMode(LDR, INPUT); pinMode(BUTTON, INPUT); pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT); delay(1000); Serial.begin(115200); Serial.println("Wifi Connecting"); WiFi.begin(wifiSSID, wifiPassword); wifi_station_set_auto_connect(true); wifi_station_set_hostname(wiFiHostname); wifiConnectCounter = 1; while (WiFi.status() != WL_CONNECTED) { delay(wifiRepeatInterval); wifiConnectCounter++; if (wifiConnectCounter > wifiMaxTries) { delay(wifiRepeatInterval * 1000 * 1000); wifiConnectCounter = 0; } } Serial.println("Wifi Connected"); server.on("/", handleRoot); server.begin(); }
/*JSON{ "type" : "staticmethod", "class" : "ESP8266WiFi", "name" : "setDHCPHostname", "generate" : "jswrap_ESP8266WiFi_setDHCPHostname", "params" : [ ["hostname", "JsVar", "The new DHCP hostname."] ] } * Set the DHCP hostname. */ void jswrap_ESP8266WiFi_setDHCPHostname( JsVar *jsHostname //!< The hostname to set for device. ) { char hostname[256]; jsvGetString(jsHostname, hostname, sizeof(hostname)); os_printf("> jswrap_ESP8266WiFi_setDHCPHostname: %s\n", hostname); wifi_station_set_hostname(hostname); }
void ICACHE_FLASH_ATTR user_init() { uart_init(BIT_RATE_9600, BIT_RATE_9600); wifi_station_set_hostname( "Threatbutt IoT Protector" ); system_init_done_cb(initDone); os_timer_setfn(&myTimer, timerCallback, NULL); os_timer_arm(&myTimer, 10000, 1); }
/*JSON{ "type" : "staticmethod", "class" : "ESP8266WiFi", "name" : "init", "generate" : "jswrap_ESP8266WiFi_init" }*/ void jswrap_ESP8266WiFi_init() { os_printf("> jswrap_ESP8266WiFi_init\n"); // register the state change handler so we get debug printout for sure wifi_set_phy_mode(2); wifi_set_event_handler_cb(wifiEventHandler); os_printf("Wifi init, mode=%d\n", wifi_get_opmode()); wifi_station_set_hostname("espruino"); netInit_esp8266_board(); setupJsNetwork(); networkState = NETWORKSTATE_ONLINE; os_printf("< jswrap_ESP8266WiFi_init\n"); }
// configure Wifi, specifically DHCP vs static IP address based on flash config void ICACHE_FLASH_ATTR configWifiIP() { if (flashConfig.staticip == 0) { // let's DHCP! wifi_station_set_hostname(flashConfig.hostname); if (wifi_station_dhcpc_status() == DHCP_STARTED) wifi_station_dhcpc_stop(); wifi_station_dhcpc_start(); DBG("Wifi uses DHCP, hostname=%s\n", flashConfig.hostname); } else { // no DHCP, we got static network config! wifi_station_dhcpc_stop(); struct ip_info ipi; ipi.ip.addr = flashConfig.staticip; ipi.netmask.addr = flashConfig.netmask; ipi.gw.addr = flashConfig.gateway; wifi_set_ip_info(0, &ipi); DBG("Wifi uses static IP %d.%d.%d.%d\n", IP2STR(&ipi.ip.addr)); } #ifdef DEBUGIP debugIP(); #endif }
void user_init( void ) { positiondebut = 0; positionfin = 1; positiondebutUDP = 0; positionfinUDP = 0; static struct station_config config; uart_div_modify( 0, UART_CLK_FREQ / ( 115200 ) ); os_printf( "%s\n", __FUNCTION__ ); uart_init(115200,9600); spi_slave_init(HSPI,SPI_BUFF); wifi_station_set_hostname( HOSTNAME ); wifi_set_opmode_current( STATIONAP_MODE ); gpio_init(); spi_gpio_init(); //os_printf sur Uart0 espconn_init() ; uart0_tx_buffer("init\n", 5); config.bssid_set = 1; os_memcpy( &config.ssid, SSID, 14 ); os_memcpy( &config.password, PASSWORD, 41); wifi_station_set_config( &config ); wifi_station_connect(); // wifi_set_event_handler_cb( wifi_callback ); shell_init(); //Démarage du client UDP user_set_station_config_udp(); MessageUDP.Status = E_ID; MessageSPi.Status = E_ID; system_os_task(all_recvTask,PRIO_SPI, all_recvTaskQueue, TASK_LENGHT); ///demo with a task to process the uart data system_os_task(uart_recvTask,PRIO_UART, uart_recvTaskQueue, TASK_LENGHT); // //demo with a task to process the spi }
/** * Set ESP8266 station DHCP hostname * @param aHostname max length:24 * @return ok */ bool ESP8266WiFiSTAClass::hostname(const char* aHostname) { /* vvvv RFC952 vvvv ASSUMPTIONS 1. A "name" (Net, Host, Gateway, or Domain name) is a text string up to 24 characters drawn from the alphabet (A-Z), digits (0-9), minus sign (-), and period (.). Note that periods are only allowed when they serve to delimit components of "domain style names". (See RFC-921, "Domain Name System Implementation Schedule", for background). No blank or space characters are permitted as part of a name. No distinction is made between upper and lower case. The first character must be an alpha character. The last character must not be a minus sign or period. A host which serves as a GATEWAY should have "-GATEWAY" or "-GW" as part of its name. Hosts which do not serve as Internet gateways should not use "-GATEWAY" and "-GW" as part of their names. A host which is a TAC should have "-TAC" as the last part of its host name, if it is a DoD host. Single character names or nicknames are not allowed. ^^^^ RFC952 ^^^^ - 24 chars max - only a..z A..Z 0..9 '-' - no '-' as last char */ size_t len = strlen(aHostname); if (len == 0 || len > 32) { // nonos-sdk limit is 32 // (dhcp hostname option minimum size is ~60) DEBUG_WIFI_GENERIC("WiFi.(set)hostname(): empty or large(>32) name\n"); return false; } // check RFC compliance bool compliant = (len <= 24); for (size_t i = 0; compliant && i < len; i++) if (!isalnum(aHostname[i]) && aHostname[i] != '-') compliant = false; if (aHostname[len - 1] == '-') compliant = false; if (!compliant) { DEBUG_WIFI_GENERIC("hostname '%s' is not compliant with RFC952\n", aHostname); } bool ret = wifi_station_set_hostname(aHostname); if (!ret) { DEBUG_WIFI_GENERIC("WiFi.hostname(%s): wifi_station_set_hostname() failed\n", aHostname); return false; } // now we should inform dhcp server for this change, using lwip_renew() // looping through all existing interface // harmless for AP, also compatible with ethernet adapters (to come) for (netif* intf = netif_list; intf; intf = intf->next) { // unconditionally update all known interfaces #if LWIP_VERSION_MAJOR == 1 intf->hostname = (char*)wifi_station_get_hostname(); #else intf->hostname = wifi_station_get_hostname(); #endif if (netif_dhcp_data(intf) != nullptr) { // renew already started DHCP leases err_t lwipret = dhcp_renew(intf); if (lwipret != ERR_OK) { DEBUG_WIFI_GENERIC("WiFi.hostname(%s): lwIP error %d on interface %c%c (index %d)\n", intf->hostname, (int)lwipret, intf->name[0], intf->name[1], intf->num); ret = false; } } } return ret && compliant; }
bool ESP8266WiFiClass::hostname(char* aHostname) { if(strlen(aHostname) > 32) { return false; } return wifi_station_set_hostname(aHostname); }
/****************************************************************************** * FunctionName : user_esp_platform_init * Description : device parame init based on espressif platform * Parameters : none * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR user_esp_platform_init(void) { os_sprintf(iot_version,"%s%d.%d.%dt%d(%s)",VERSION_TYPE,IOT_VERSION_MAJOR,\ IOT_VERSION_MINOR,IOT_VERSION_REVISION,device_type,UPGRADE_FALG); os_printf("IOT VERSION = %s\n",iot_version); system_param_load(ESP_PARAM_START_SEC, 0, &esp_param, sizeof(esp_param)); struct rst_info *rtc_info = system_get_rst_info(); os_printf("reset reason: %x\n", rtc_info->reason); if (rtc_info->reason == REASON_WDT_RST || rtc_info->reason == REASON_EXCEPTION_RST || rtc_info->reason == REASON_SOFT_WDT_RST) { if (rtc_info->reason == REASON_EXCEPTION_RST) { os_printf("Fatal exception (%d):\n", rtc_info->exccause); } os_printf("epc1=0x%08x, epc2=0x%08x, epc3=0x%08x, excvaddr=0x%08x, depc=0x%08x\n", rtc_info->epc1, rtc_info->epc2, rtc_info->epc3, rtc_info->excvaddr, rtc_info->depc); } /***add by tzx for saving ip_info to avoid dhcp_client start****/ struct dhcp_client_info dhcp_info; struct ip_info sta_info; system_rtc_mem_read(64,&dhcp_info,sizeof(struct dhcp_client_info)); if(dhcp_info.flag == 0x01 ) { if (true == wifi_station_dhcpc_status()) { wifi_station_dhcpc_stop(); } sta_info.ip = dhcp_info.ip_addr; sta_info.gw = dhcp_info.gw; sta_info.netmask = dhcp_info.netmask; if ( true != wifi_set_ip_info(STATION_IF,&sta_info)) { os_printf("set default ip wrong\n"); } } os_memset(&dhcp_info,0,sizeof(struct dhcp_client_info)); system_rtc_mem_write(64,&dhcp_info,sizeof(struct rst_info)); #if AP_CACHE wifi_station_ap_number_set(AP_CACHE_NUMBER); #endif #if 0 { char sofap_mac[6] = {0x16, 0x34, 0x56, 0x78, 0x90, 0xab}; char sta_mac[6] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab}; struct ip_info info; wifi_set_macaddr(SOFTAP_IF, sofap_mac); wifi_set_macaddr(STATION_IF, sta_mac); IP4_ADDR(&info.ip, 192, 168, 3, 200); IP4_ADDR(&info.gw, 192, 168, 3, 1); IP4_ADDR(&info.netmask, 255, 255, 255, 0); wifi_set_ip_info(STATION_IF, &info); IP4_ADDR(&info.ip, 10, 10, 10, 1); IP4_ADDR(&info.gw, 10, 10, 10, 1); IP4_ADDR(&info.netmask, 255, 255, 255, 0); wifi_set_ip_info(SOFTAP_IF, &info); } #endif if (esp_param.activeflag != 1) { #ifdef SOFTAP_ENCRYPT struct softap_config config; char password[33]; char macaddr[6]; wifi_softap_get_config(&config); wifi_get_macaddr(SOFTAP_IF, macaddr); os_memset(config.password, 0, sizeof(config.password)); os_sprintf(password, MACSTR "_%s", MAC2STR(macaddr), PASSWORD); os_memcpy(config.password, password, os_strlen(password)); config.authmode = AUTH_WPA_WPA2_PSK; wifi_softap_set_config(&config); #endif wifi_station_set_hostname( HOST_NAME ); wifi_set_opmode(STATION_MODE); } #if SENSOR_DEVICE user_sensor_init(esp_param.activeflag); #endif #if 0 if (wifi_get_opmode() != SOFTAP_MODE) { os_timer_disarm(&client_timer); os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, 1); os_timer_arm(&client_timer, 100, 0); } // 2015-12-27 single_key[0] = key_init_single( 12, PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12, NULL, GPIO_INTER); keys.key_num = 1; keys.single_key = single_key; key_init(&keys); #endif }
void wifi_init() { wifi_set_opmode(NULL_MODE); //Next time start up in NULL mode wifi_set_opmode_current(NULL_MODE); //Initialize station config parameters struct station_config stconf; memset(&stconf, 0, sizeof(stconf)); strncpy((char *)stconf.ssid, szWifiSsid, sizeof(stconf.ssid)); strncpy((char *)stconf.password, szWifiPassword, sizeof(stconf.password)); stconf.ssid[sizeof(stconf.ssid)-1]='\0'; stconf.password[sizeof(stconf.password)-1]='\0'; //Initialize AP config parameters struct softap_config apconf; memset(&apconf, 0, sizeof(apconf)); // SSID strncpy((char *)apconf.ssid, szWifiSsid, sizeof(apconf.ssid)); apconf.ssid[sizeof(apconf.ssid)-1]='\0'; // Password & encryption strncpy((char *)apconf.password, szWifiPassword, sizeof(apconf.password)); apconf.password[sizeof(apconf.password)-1]='\0'; if (strlen(szWifiPassword) >= 8) { apconf.authmode = AUTH_WPA_WPA2_PSK; } else { // if password <8 characters, don't use password. apconf.authmode = AUTH_WEP; memset(apconf.password, 0, sizeof(apconf.password)); } apconf.max_connection = 255; apconf.beacon_interval = 100; wifi_set_opmode_current(nWifiMode); wifi_softap_set_config_current(&apconf); wifi_station_set_config_current(&stconf); struct ip_info ipinfo; memset(&ipinfo, 0, sizeof(ipinfo)); ipinfo.ip.addr = aIP; ipinfo.netmask.addr = aNetmask; ipinfo.gw.addr = aGateway; if (nWifiMode == STATION_MODE) { wifi_station_dhcpc_stop(); wifi_station_set_hostname(szHostname); if (bUseDhcp) { wifi_station_dhcpc_start(); } else { wifi_set_ip_info(STATION_IF, &ipinfo); } } else { // DHCP Server should be stopped to change IP information wifi_softap_dhcps_stop(); wifi_set_ip_info(SOFTAP_IF, &ipinfo); uint32_t aFirstIp = (aIP & aNetmask) | 1; uint32_t aLastIp = (aIP & aNetmask) | (-1 & ~aNetmask); struct dhcps_lease leases; memset(&leases, 0, sizeof(leases)); // Determine whether the range before or after our IP is bigger if (aIP - aFirstIp > aLastIp - aIP) { leases.start_ip.addr = aFirstIp; leases.end_ip.addr = aIP - 1; } else { leases.start_ip.addr = aIP + 1; leases.end_ip.addr = aLastIp; } wifi_softap_set_dhcps_lease(&leases); uint8_t offer_router = 0; wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &offer_router); wifi_softap_dhcps_start(); } }
void ICACHE_FLASH_ATTR settings_apply( userSettings_t *settings ) { //Description if (!((settings->magic==0x42)&&(settings->version==SETTINGS_VERSION))) { os_printf("<ERROR: CAN NOT APPLY CORRUPT SETTINGS!>\n\r"); return; } if (settings->connection_successful) { ap_stop(); } else { ap_start(); } //Device information wifi_station_set_hostname(settings->name); //Network struct station_config stationConf; wifi_station_get_config( &stationConf ); stationConf.bssid_set = 0; os_memcpy( &stationConf.ssid, settings->ssid, 32 ); os_memcpy( &stationConf.password, settings->password, 64 ); wifi_station_set_config_current( &stationConf ); os_printf("########################## Wifi config set to SSID '%s' and PASSWORD '%s'.\n\r", settings->ssid, settings->password); if (settings->enable_dhcp) { wifi_station_dhcpc_start(); } else { wifi_station_dhcpc_stop(); struct ip_info dhcpc_ip_info; IP4_ADDR(&dhcpc_ip_info.ip, settings->static_ip_1, settings->static_ip_2, settings->static_ip_3, settings->static_ip_4); IP4_ADDR(&dhcpc_ip_info.gw, settings->static_gateway_1, settings->static_gateway_2, settings->static_gateway_3, settings->static_gateway_4); IP4_ADDR(&dhcpc_ip_info.netmask, settings->static_netmask_1, settings->static_netmask_2, settings->static_netmask_3, settings->static_netmask_4); wifi_set_ip_info(STATION_IF, &dhcpc_ip_info); } wifi_station_disconnect(); //MDNS if (settings->enable_mdns) { /*wifi_set_broadcast_if(STATIONAP_MODE); struct mdns_info *info = (struct mdns_info *) os_zalloc(sizeof(struct mdns_info)); info->host_name = settings->mdns_hostname; info->ipAddr = station_ipconfig.ip.addr; //ESP8266 station IP info->server_name = "FirmwaRe MDNS"; info->server_port = 80; info->txt_data[0] = “version = now”; info->txt_data[1] = “user1 = data1”; info->txt_data[2] = “user2 = data2”; espconn_mdns_init(info);*/ } //NTP if (settings->enable_ntp) { //settings->ntpserver //settings->timezone //settings->enable_summertime } //Security /* settings->password; */ //Digital outputs #if OUTPUT1>-1 if (settings->bootstate&1){ board_setOutput(OUTPUT1, true); } else { board_setOutput(OUTPUT1, false); } #endif #if OUTPUT2>-1 if (settings->bootstate&2) { board_setOutput(OUTPUT2, true); } else { board_setOutput(OUTPUT2, false); } #endif #if OUTPUT3>-1 if (settings->bootstate&4) { board_setOutput(OUTPUT3, true); } else { board_setOutput(OUTPUT3, false); } #endif #if OUTPUT4>-1 if (settings->bootstate&8) { board_setOutput(OUTPUT4, true); } else { board_setOutput(OUTPUT4, false); } #endif #if OUTPUT5>-1 if (settings->bootstate&16) { board_setOutput(OUTPUT5, true); } else { board_setOutput(OUTPUT5, false); } #endif #if OUTPUT6>-1 if (settings->bootstate&32) { board_setOutput(OUTPUT6, true); } else { board_setOutput(OUTPUT6, false); } #endif #if OUTPUT7>-1 if (settings->bootstate&64) { board_setOutput(OUTPUT7, true); } else { board_setOutput(OUTPUT7, false); } #endif #if OUTPUT8>-1 if (settings->bootstate&128) { board_setOutput(OUTPUT8, true); } else { board_setOutput(OUTPUT8, false); } #endif //ESPLight setPWM(settings->bootstate_R, settings->bootstate_G, settings->bootstate_B); //settings->ledstrip_type; //settings->ledstrip_length; //PKA /* settings->pka_wb settings->pka_wb_time */ }