Example #1
0
//Main routine. Initialize stdout, the I/O, filesystem and the webserver and we're done.
void user_init(void) {
	// init gpio pins used to reset&reprogram attached microcontrollers
	gpio_init();
	// put MCU into reset in case it interferes with serial-programming of the esp8266
	//GPIO_OUTPUT_SET(MCU_RESET, 0);
	// init UART
	uart_init(BIT_RATE_115200, BIT_RATE_115200);
	// say hello (leave some time to cause break in TX after boot loader's msg
	os_delay_us(10000L);
# define VERS_STR_STR(V) #V
# define VERS_STR(V) VERS_STR_STR(V)
	os_printf("\n\nInitializing esp-link\n" VERS_STR(VERSION) "\n");
	//configWipe();
	if (configRestore()) os_printf("Flash config restored\n");
	else os_printf("*** Flash config restore failed, using defaults ***\n");
	// Status LEDs
	statusInit();
	serledInit();
	logInit();
	// Wifi
	wifiInit();
	// init the flash filesystem with the html stuff
	EspFsInitResult res = espFsInit(&_binary_espfs_img_start);
	os_printf("espFsInit(0x%08lx) returned %d\n", (uint32_t)&_binary_espfs_img_start, res);
	// mount the http handlers
	httpdInit(builtInUrls, 80);
	// init the wifi-serial transparent bridge (port 23)
	serbridgeInit(23);
	uart_add_recv_cb(&serbridgeUartCb);
#ifdef SHOW_HEAP_USE
	os_timer_disarm(&prHeapTimer);
	os_timer_setfn(&prHeapTimer, prHeapTimerCb, NULL);
	os_timer_arm(&prHeapTimer, 3000, 1);
#endif

	struct rst_info *rst_info = system_get_rst_info();
	os_printf("Reset cause: %d=%s\n", rst_info->reason, rst_codes[rst_info->reason]);
	os_printf("exccause=%d epc1=0x%x epc2=0x%x epc3=0x%x excvaddr=0x%x depc=0x%x\n",
			rst_info->exccause, rst_info->epc1, rst_info->epc2, rst_info->epc3,
			rst_info->excvaddr, rst_info->depc);

	os_printf("** esp-link ready\n");
}
Example #2
0
}
#endif

void user_rf_pre_init(void) {
}

// address of espfs binary blob
extern uint32_t _binary_espfs_img_start;

static char *rst_codes[] = {
	"normal", "wdt reset", "exception", "soft wdt", "restart", "deep sleep", "external",
};

# define VERS_STR_STR(V) #V
# define VERS_STR(V) VERS_STR_STR(V)
char *esp_link_version = VERS_STR(VERSION);

//Main routine. Initialize stdout, the I/O, filesystem and the webserver and we're done.
void user_init(void) {
	// get the flash config so we know how to init things
	//configWipe(); // uncomment to reset the config for testing purposes
	bool restoreOk = configRestore();
	// init gpio pin registers
	gpio_init();
	// init UART
	uart_init(flashConfig.baud_rate, 115200);
	logInit(); // must come after init of uart
	// say hello (leave some time to cause break in TX after boot loader's msg
	os_delay_us(10000L);
	os_printf("\n\n** %s\n", esp_link_version);
	os_printf("Flash config restore %s\n", restoreOk ? "ok" : "*FAILED*");
Example #3
0
They can also be auth-functions. An asterisk will match any url starting with
everything before the asterisks; "*" matches everything. The list will be
handled top-down, so make sure to put more specific rules above the more
general ones. Authorization things (like authBasic) act as a 'barrier' and
should be placed above the URLs they protect.
*/
HttpdBuiltInUrl builtInUrls[] = {
  { "/flash/next", cgiGetFirmwareNext, NULL },
  { "/flash/upload", cgiUploadFirmware, NULL },
  { "/flash/reboot", cgiRebootFirmware, NULL },
  { NULL, NULL, NULL }
};

# define VERS_STR_STR(V) #V
# define VERS_STR(V) VERS_STR_STR(V)
static const char* const esp_link_version = VERS_STR(VERSION);


void ICACHE_FLASH_ATTR user_rf_pre_init(void) {
  /* undo upgrade, if the first boot failes
   * with an watchdog reset, soft watchdog reset or an exception
   */
  cgiFlashCheckUpgradeHealthy();

  //default is enabled
  system_set_os_print(DEBUG_SDK);
}

// Main routine to initialize esp-link.
void ICACHE_FLASH_ATTR user_init(void) {
  // Init gpio pin registers
Example #4
0
/*  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);
}
Example #5
0
// Main routine to initialize esp-link.
void user_init(void) {
  // get the flash config so we know how to init things
//  configWipe(); // uncomment to reset the config for testing purposes
  bool restoreOk = configRestore();
  // init gpio pin registers
  gpio_init();
  gpio_output_set(0, 0, 0, (1<<15)); // some people tie it to GND, gotta ensure it's disabled
  // init UART
  uart_init(flashConfig.baud_rate, 115200);
  logInit(); // must come after init of uart
  // say hello (leave some time to cause break in TX after boot loader's msg
  os_delay_us(10000L);
  os_printf("\n\n** %s\n", esp_link_version);
  os_printf("Flash config restore %s\n", restoreOk ? "ok" : "*FAILED*");

#if defined(STA_SSID) && defined(STA_PASS)
  int x = wifi_get_opmode() & 0x3;
  if (x == 2) {
    // we only force the STA settings when a full flash of the module has been made, which
    // resets the wifi settings not to have anything configured
    struct station_config stconf;
    wifi_station_get_config(&stconf);

    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);
#ifdef CGIWIFI_DBG
      os_printf("Wifi pre-config trying to connect to AP %s pw %s\n",
          (char*)stconf.ssid, (char*)stconf.password);
#endif
      wifi_set_opmode(3); // sta+ap, will switch to sta-only 15 secs after connecting
      stconf.bssid_set = 0;
      wifi_station_set_config(&stconf);
    }
  }
#endif

  // Status LEDs
  statusInit();
  serledInit();
  // Wifi
  wifiInit();

  // init the flash filesystem with the html stuff
  espFsInit(&_binary_espfs_img_start);
  //EspFsInitResult res = espFsInit(&_binary_espfs_img_start);
  //os_printf("espFsInit %s\n", res?"ERR":"ok");
  // mount the http handlers
  httpdInit(builtInUrls, 80);
  // init the wifi-serial transparent bridge (port 23)
  serbridgeInit(23, 2323);
  uart_add_recv_cb(&serbridgeUartCb);
#ifdef SHOW_HEAP_USE
  os_timer_disarm(&prHeapTimer);
  os_timer_setfn(&prHeapTimer, prHeapTimerCb, NULL);
  os_timer_arm(&prHeapTimer, 10000, 1);
#endif

  struct rst_info *rst_info = system_get_rst_info();
  NOTICE("Reset cause: %d=%s", rst_info->reason, rst_codes[rst_info->reason]);
  NOTICE("exccause=%d epc1=0x%x epc2=0x%x epc3=0x%x excvaddr=0x%x depc=0x%x",
    rst_info->exccause, rst_info->epc1, rst_info->epc2, rst_info->epc3,
    rst_info->excvaddr, rst_info->depc);
  uint32_t fid = spi_flash_get_id();
  NOTICE("Flash map %s, manuf 0x%02lX chip 0x%04lX", flash_maps[system_get_flash_size_map()],
      fid & 0xff, (fid&0xff00)|((fid>>16)&0xff));
  NOTICE("** esp-link ready");

  cgiServicesSNTPInit();

#ifdef MQTT
  NOTICE("initializing MQTT");
  mqtt_client_init();
#endif

  NOTICE("initializing user application");
  app_init();

  NOTICE("waiting for work to do...");
}