void ICACHE_FLASH_ATTR user_init(void) { uart_init(BIT_RATE_115200, BIT_RATE_115200); os_printf("SDK version:%s\n", system_get_sdk_version()); i2c_init(); #ifdef ESP8266OLED oled_init(); #endif key_and_gpio_init(); #ifdef ESP8266DOOR door_switch_init(); #endif wifi_set_event_handler_cb(wifi_handle_event_cb); mqttstart(); #ifdef ESP8266ENVINFO task_send_environment_info_init(); #endif #ifdef ESP8266CARDREAD pn532_init(); pn532_cb_event_init(); #endif struct station_config s_staconf; wifi_station_get_config_default(&s_staconf); if (os_strlen(s_staconf.ssid) == 0) { wifi_set_opmode(STATION_MODE); smartconfig_set_type(SC_TYPE_ESPTOUCH); smartconfig_start(smartconfig_done); } }
/** * Start Wifi connection * if passphrase is set the most secure supported mode will be automatically selected * @param ssid const char* Pointer to the SSID string. * @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal). * @param bssid uint8_t[6] Optional. BSSID / MAC of AP * @param channel Optional. Channel of AP * @param connect Optional. call connect * @return */ wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid, bool connect) { if(!WiFi.enableSTA(true)) { // enable STA failed return WL_CONNECT_FAILED; } if(!ssid || *ssid == 0x00 || strlen(ssid) > 32) { // fail SSID too long or missing! return WL_CONNECT_FAILED; } int passphraseLen = passphrase == nullptr ? 0 : strlen(passphrase); if(passphraseLen > 64) { // fail passphrase too long! return WL_CONNECT_FAILED; } struct station_config conf; conf.threshold.authmode = (passphraseLen == 0) ? AUTH_OPEN : (_useInsecureWEP ? AUTH_WEP : AUTH_WPA_PSK); if(strlen(ssid) == 32) memcpy(reinterpret_cast<char*>(conf.ssid), ssid, 32); //copied in without null term else strcpy(reinterpret_cast<char*>(conf.ssid), ssid); if(passphrase) { if (passphraseLen == 64) // it's not a passphrase, is the PSK, which is copied into conf.password without null term memcpy(reinterpret_cast<char*>(conf.password), passphrase, 64); else strcpy(reinterpret_cast<char*>(conf.password), passphrase); } else { *conf.password = 0; } conf.threshold.rssi = -127; conf.open_and_wep_mode_disable = !(_useInsecureWEP || *conf.password == 0); if(bssid) { conf.bssid_set = 1; memcpy((void *) &conf.bssid[0], (void *) bssid, 6); } else { conf.bssid_set = 0; } struct station_config conf_compare; if(WiFi._persistent){ wifi_station_get_config_default(&conf_compare); } else { wifi_station_get_config(&conf_compare); } if(sta_config_equal(conf_compare, conf)) { DEBUGV("sta config unchanged"); } else { ETS_UART_INTR_DISABLE(); if(WiFi._persistent) { wifi_station_set_config(&conf); } else { wifi_station_set_config_current(&conf); } ETS_UART_INTR_ENABLE(); } ETS_UART_INTR_DISABLE(); if(connect) { wifi_station_connect(); } ETS_UART_INTR_ENABLE(); if(channel > 0 && channel <= 13) { wifi_set_channel(channel); } if(!_useStaticIp) { wifi_station_dhcpc_start(); } return status(); }
irom void stats_generate(uint16_t size, char *dst) { const struct rst_info *rst_info; struct station_config sc_default, sc_current; uint32_t system_time; system_time = system_get_time(); rst_info = system_get_rst_info(); wifi_station_get_config_default(&sc_default); wifi_station_get_config(&sc_current); snprintf(dst, size, "> firmware version date: %s\n" "> system id: %u\n" "> spi flash id: %u\n" "> cpu frequency: %u\n" "> flash map: %s\n" "> reset cause: %s\n" ">\n" "> heap free: %u bytes\n" "> system clock: %u.%06u s\n" "> uptime: %u %02d:%02d:%02d\n" "> real time: %u %02d:%02d:%02d\n" "> size of config: %u\n" ">\n" "> int uart rx: %u\n" "> int uart tx: %u\n" "> timer_fast fired: %u\n" "> timer_slow fired: %u\n" "> timer_second fired: %u\n" "> background task: %u\n" ">\n" "> default ssid: %s, passwd: %s\n" "> current ssid: %s, passwd: %s\n" "> phy mode: %s\n" "> sleep mode: %s\n" "> channel: %u\n" "> signal strength: %d dB\n", __DATE__ " " __TIME__, system_get_chip_id(), spi_flash_get_id(), system_get_cpu_freq(), flash_map[system_get_flash_size_map()], reset_map[rst_info->reason], system_get_free_heap_size(), system_time / 1000000, system_time % 1000000, ut_days, ut_hours, ut_mins, ut_secs, rt_days, rt_hours, rt_mins, rt_secs, sizeof(config_t), stat_uart_rx_interrupts, stat_uart_tx_interrupts, stat_timer_fast, stat_timer_slow, stat_timer_second, stat_background_task, sc_default.ssid, sc_default.password, sc_current.ssid, sc_current.password, phy[wifi_get_phy_mode()], slp[wifi_get_sleep_type()], wifi_get_channel(), wifi_station_get_rssi()); }
/** * Start Wifi connection * if passphrase is set the most secure supported mode will be automatically selected * @param ssid const char* Pointer to the SSID string. * @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal). * @param bssid uint8_t[6] Optional. BSSID / MAC of AP * @param channel Optional. Channel of AP * @param connect Optional. call connect * @return */ wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid, bool connect) { if(!WiFi.enableSTA(true)) { // enable STA failed return WL_CONNECT_FAILED; } if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) { // fail SSID too long or missing! return WL_CONNECT_FAILED; } if(passphrase && strlen(passphrase) > 64) { // fail passphrase too long! return WL_CONNECT_FAILED; } struct station_config conf; strcpy(reinterpret_cast<char*>(conf.ssid), ssid); if(passphrase) { if (strlen(passphrase) == 64) // it's not a passphrase, is the PSK, which is copied into conf.password without null term memcpy(reinterpret_cast<char*>(conf.password), passphrase, 64); else strcpy(reinterpret_cast<char*>(conf.password), passphrase); } else { *conf.password = 0; } conf.threshold.rssi = -127; // TODO(#909): set authmode to AUTH_WPA_PSK if passphrase is provided conf.threshold.authmode = AUTH_OPEN; if(bssid) { conf.bssid_set = 1; memcpy((void *) &conf.bssid[0], (void *) bssid, 6); } else { conf.bssid_set = 0; } struct station_config conf_compare; if(WiFi._persistent){ wifi_station_get_config_default(&conf_compare); } else { wifi_station_get_config(&conf_compare); } if(sta_config_equal(conf_compare, conf)) { DEBUGV("sta config unchanged"); } else { ETS_UART_INTR_DISABLE(); if(WiFi._persistent) { wifi_station_set_config(&conf); } else { wifi_station_set_config_current(&conf); } ETS_UART_INTR_ENABLE(); } ETS_UART_INTR_DISABLE(); if(connect) { wifi_station_connect(); } ETS_UART_INTR_ENABLE(); if(channel > 0 && channel <= 13) { wifi_set_channel(channel); } if(!_useStaticIp) { wifi_station_dhcpc_start(); } return status(); }
// initialize the custom stuff that goes beyond esp-link void user_init() { // Initialize the GPIO subsystem. gpio_init(); /* ====================================== */ /* UART */ /* ====================================== */ // Initialize UART0 and UART1 /* NOTE: UART1 and I2S share same GPIO. Cannot use simultaneously. */ uart_init( BIT_RATE_115200, BIT_RATE_115200 ); // uart0_sendStr( "\nUART0 - USED TO PROGRAM THE MODULE\n" ); os_printf("\n===================\nUART1 - DEBUG OUPUT\n===================\n"); /* NOTE: PWM CANNOT BE USED SIMULTANEOUSLY WITH HW TIMER */ #if 0 /* ====================================== */ /* PWM */ /* ====================================== */ uint32 pwm_period = 1000; uint32 pwm_duty[PWM_CHANNEL] = {0}; uint32 io_info[][3] = { {PWM_0_OUT_IO_MUX,PWM_0_OUT_IO_FUNC,PWM_0_OUT_IO_NUM}, {PWM_1_OUT_IO_MUX,PWM_1_OUT_IO_FUNC,PWM_1_OUT_IO_NUM}, }; /* PIN FUNCTION INIT FOR PWM OUTPUT */ pwm_init(pwm_period, pwm_duty, PWM_CHANNEL, io_info); /* set pwm_duty cycle */ pwm_set_duty (14185, 0); pwm_set_duty (22222, 1); // todo: explain why 22222 is the highest possible value /* start PWM */ pwm_start(); // NOTE: PWM causes spikes in other GPIOs #endif /* ====================================== */ /* GPIO INTERRPUT */ /* ====================================== */ /* Set GPIO12 in GPIO mode */ PIN_FUNC_SELECT( PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12 ); /* Set GPIO12 as input */ GPIO_DIS_OUTPUT( GPIO_ID_PIN(12) ); /* Disable all GPIO interrupts */ ETS_GPIO_INTR_DISABLE(); /* Set a GPIO callback function */ ETS_GPIO_INTR_ATTACH( gpioCallback, NULL ); /* Configure the type of edge */ gpio_pin_intr_state_set( GPIO_ID_PIN(12), GPIO_PIN_INTR_ANYEDGE ); ETS_GPIO_INTR_ENABLE(); /* ====================================== */ /* SOFTWARE TIMER */ /* ====================================== */ // Set GPIO0 to output mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); //Set GPIO0 low gpio_output_set(0, RELAY_PIN, RELAY_PIN, 0); /* disarm timer */ os_timer_disarm((ETSTimer*)&some_timer); /* set callback */ os_timer_setfn((ETSTimer*)&some_timer, (os_timer_func_t *) softwareTimerCallback, NULL); /* arm the timer -> os_timer_arm(<pointer>, <period in ms>, <fire periodically>) */ os_timer_arm((ETSTimer*)&some_timer, 10, 1); /* ====================================== */ /* OS TASK */ /* ====================================== */ /* setup OS task */ // system_os_task(user_procTask, user_procTaskPrio, user_procTaskQueue, user_procTaskQueueLen); /* send a message to OS task (fire task) */ // system_os_post(user_procTaskPrio, 0, 0 ); /* ====================================== */ /* HARDWARE TIMER */ /* ====================================== */ /* The hardware timer is used to indicate when a complete IR message frame should have * arrived in order to process the received data and calculate the IR command. * * It is configured in "one-shot" mode. It is started when the beginning of an * IR message frame is detected and stopped after the complete message frame has been read. * This means that the duration of the HW timer should be longer than the duration of * the longest message frame. In the NEC IR tranmission protocol all message frames have * a duration of approximately 67.5ms. */ /* load the HW TIMER */ uint32 ticks = usToTicks(70000); // 70ms RTC_REG_WRITE(FRC1_LOAD_ADDRESS, ticks); /* register callback function */ ETS_FRC_TIMER1_INTR_ATTACH( hwTimerCallback, NULL ); /* enable interrupts */ TM1_EDGE_INT_ENABLE(); ETS_FRC1_INTR_ENABLE(); /* don't start timer yet */ /* the timer is started inside the GPIO INT callback */ /* ====================================== */ /* UDP SERVER */ /* ====================================== */ /* usage: echo <data> | nc -wl -u <ip address> <port> * example: echo "foo" | nc -w1 -u 192.168.1.187 7777 */ /* allocate space for server */ pUdpServer = (struct espconn *) os_zalloc(sizeof(struct espconn)); /* clear allocated memory */ ets_memset(pUdpServer, 0, sizeof(struct espconn)); /* create the server */ espconn_create(pUdpServer); /* set the type of server */ pUdpServer->type = ESPCONN_UDP; /* allocate memory for UDP settings */ pUdpServer->proto.udp = (esp_udp *) os_zalloc(sizeof(esp_udp)); /* set the port that the server will be listening to */ pUdpServer->proto.udp->local_port = 7777; /* register the callback */ espconn_regist_recvcb(pUdpServer, udpServerRxCb); /* start listening */ if (espconn_create(pUdpServer)) { while (1) { os_printf("Error creating a UDP server\n"); } } /* ====================================== */ /* WIFI */ /* ====================================== */ wifi_set_opmode(STATION_MODE); wifi_station_get_config_default(&stconf); // os_strncpy((char*) stconf.ssid, "TP-LINK_2.4GHz_FC2E51", 32); // os_strncpy((char*) stconf.password, "tonytony", 64); os_strncpy((char*) stconf.ssid, "WLAN-PUB", 32); os_strncpy((char*) stconf.password, "", 64); // os_strncpy((char*) stconf.ssid, "MAD air", 32); // os_strncpy((char*) stconf.password, "glioninlog", 64); stconf.bssid_set = 0; wifi_station_set_config(&stconf); // /* ====================================== */ // /* WS2812 LED STRIP */ // /* ====================================== */ // // /* NOTE: UART1 and I2S share same GPIO. Cannot use simultaneously. */ // ws2812_init(); // // /* G R B */ // uint8_t ledout[] = { // 0xff, 0x00, 0x00, //4th //// 0xff, 0x00, 0x00, //3rd //// 0x00, 0xff, 0x00, //2nd //// 0x00, 0x00, 0xff, //1st // }; // //#if 0 // os_printf("\r\nB R G: %x %x %x\r\n", ledout[0], ledout[1],ledout[2]); //#endif // // ws2812_push( ledout, sizeof( ledout ) ); /* ====================================== */ /* TCP CONNECTION */ /* ====================================== */ /* allocate space for server */ pTcpConn = (struct espconn *) os_zalloc(sizeof(struct espconn)); /* clear allocated memory */ ets_memset(pTcpConn, 0, sizeof(struct espconn)); /* set the type of connection */ pTcpConn->type = ESPCONN_TCP; /* set state to NONE */ pTcpConn->state = ESPCONN_NONE; /* allocate memory for TCP settings */ pTcpConn->proto.tcp = (esp_tcp *) os_zalloc(sizeof(esp_tcp)); /* set the port that the connection will be listening to */ pTcpConn->proto.tcp->local_port = espconn_port(); /* set the remote port and IP address */ pTcpConn->proto.tcp->remote_port = 80; os_memcpy(pTcpConn->proto.tcp->remote_ip, server_ip_address, sizeof(server_ip_address)); /* register callbacks */ espconn_regist_connectcb(pTcpConn, tcpConnCb); espconn_regist_reconcb(pTcpConn, tcpReconnCb); /* disarm timer */ os_timer_disarm((ETSTimer*)&wifi_setup_timer); /* set callback */ os_timer_setfn((ETSTimer*)&wifi_setup_timer, (os_timer_func_t *) wifiConnectTimerCb, NULL); /* arm the timer -> os_timer_arm(<pointer>, <period in ms>, <fire periodically>) */ os_timer_arm((ETSTimer*)&wifi_setup_timer, 5000, 1); return; }
/* Init the wireless * * Call both Soft-AP and Station default config * Change values according to Makefile hard-coded variables * Anyway set wifi opmode to STA+AP, it will change to STA if CHANGE_TO_STA is set to yes in Makefile * Call a timer to check the STA connection */ void ICACHE_FLASH_ATTR wifiInit() { // Check the wifi opmode int x = wifi_get_opmode() & 0x3; // If STA is enabled switch to STA+AP to allow for recovery, it will then switch to STA-only // once it gets an IP address if (x == 1) wifi_set_opmode(3); // Call both STATION and SOFTAP default config wifi_station_get_config_default(&stconf); wifi_softap_get_config_default(&apconf); DBG("Wifi init, mode=%s\n",wifiMode[x]); // Change STATION parameters, if defined in the Makefile #if defined(STA_SSID) && defined(STA_PASS) // Set parameters if (os_strlen((char*)stconf.ssid) == 0 && os_strlen((char*)stconf.password) == 0) { os_strncpy((char*)stconf.ssid, VERS_STR(STA_SSID), 32); os_strncpy((char*)stconf.password, VERS_STR(STA_PASS), 64); wifi_set_opmode(3); DBG("Wifi pre-config trying to connect to AP %s pw %s\n",(char*)stconf.ssid, (char*)stconf.password); // wifi_set_phy_mode(2); // limit to 802.11b/g 'cause n is flaky stconf.bssid_set = 0; wifi_station_set_config(&stconf); } #endif // Change SOFT_AP settings, if defined in Makefile #if defined(AP_SSID) // Check if ssid and pass are alphanumeric values int ssidlen = os_strlen(VERS_STR(AP_SSID)); if(checkString(VERS_STR(AP_SSID)) && ssidlen > 7 && ssidlen < 32){ // Clean memory and set the value of SSID os_memset(apconf.ssid, 0, 32); os_memcpy(apconf.ssid, VERS_STR(AP_SSID), os_strlen(VERS_STR(AP_SSID))); // Specify the length of ssid apconf.ssid_len= ssidlen; #if defined(AP_PASS) // If pass is at least 8 and less than 64 int passlen = os_strlen(VERS_STR(AP_PASS)); if( checkString(VERS_STR(AP_PASS)) && passlen > 7 && passlen < 64 ){ // Clean memory and set the value of PASS os_memset(apconf.password, 0, 64); os_memcpy(apconf.password, VERS_STR(AP_PASS), passlen); // Can't choose auth mode without a valid ssid and password #ifdef AP_AUTH_MODE // If set, use specified auth mode if(AP_AUTH_MODE >= 0 && AP_AUTH_MODE <=4) apconf.authmode = AP_AUTH_MODE; #else // If not, use WPA2 apconf.authmode = AUTH_WPA_WPA2_PSK; #endif }else if ( passlen == 0){ // If ssid is ok and no pass, set auth open apconf.authmode = AUTH_OPEN; // Remove stored password os_memset(apconf.password, 0, 64); } #endif }// end of ssid and pass check #ifdef AP_SSID_HIDDEN // If set, use specified ssid hidden parameter if(AP_SSID_HIDDEN == 0 || AP_SSID_HIDDEN ==1) apconf.ssid_hidden = AP_SSID_HIDDEN; #endif #ifdef AP_MAX_CONN // If set, use specified max conn number if(AP_MAX_CONN > 0 && AP_MAX_CONN <5) apconf.max_connection = AP_MAX_CONN; #endif #ifdef AP_BEACON_INTERVAL // If set use specified beacon interval if(AP_BEACON_INTERVAL >= 100 && AP_BEACON_INTERVAL <= 60000) apconf.beacon_interval = AP_BEACON_INTERVAL; #endif // Check softap config bool softap_set_conf = wifi_softap_set_config(&apconf); // Debug info DBG("Wifi Soft-AP parameters change: %s\n",softap_set_conf? "success":"fail"); #endif // if defined(AP_SSID) configWifiIP(); // The default sleep mode should be modem_sleep, but we set it here explicitly for good // measure. We can't use light_sleep because that powers off everthing and we would loose // all connections. wifi_set_sleep_type(MODEM_SLEEP_T); wifi_set_event_handler_cb(wifiHandleEventCb); // check on the wifi in a few seconds to see whether we need to switch mode os_timer_disarm(&resetTimer); os_timer_setfn(&resetTimer, resetTimerCb, NULL); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); }