Beispiel #1
0
static void
print_stuff() {
	printf("tmr now = %ss\n", time_now_f());

	printf("SDK version: %s\n", system_get_sdk_version());
	printf("APP version: %s built: %s %s\n", BUILD_DATE, __DATE__, __TIME__);

	printf("CPU freq was %d\n", system_get_cpu_freq());
	printf("CPU freq = %d\n", set_cpu_freq(160));
	printf("tmr now = %ss\n", time_now_f());

	printf("tout    = %sV\n", ffp(3, read_tout(0)*123/10));
	printf("vdd     = %sV\n", ffp(3, read_vdd()));

	print_mac(STATION_IF);
	print_mac(SOFTAP_IF);

	print_ip_info(SOFTAP_IF);
	print_ip_info(STATION_IF);

	printf("tmr now = %ss\n", time_now_f());
	printf("sleeping 2s\n");
	system_deep_sleep(2*1000000);
	vTaskDelete(NULL);
}
Beispiel #2
0
// Lua: dsleep( us, option )
static int node_deepsleep( lua_State* L )
{
  s32 us, option;
  //us = luaL_checkinteger( L, 1 );
  // Set deleep option, skip if nil
  if ( lua_isnumber(L, 2) )
  {
    option = lua_tointeger(L, 2);
    if ( option < 0 || option > 4)
      return luaL_error( L, "wrong arg range" );
    else
      deep_sleep_set_option( option );
  }
  // Set deleep time, skip if nil
  if ( lua_isnumber(L, 1) )
  {
    us = lua_tointeger(L, 1);
    // if ( us <= 0 )
    if ( us < 0 )
      return luaL_error( L, "wrong arg range" );
    else
      system_deep_sleep( us );
  }
  return 0;
}
Beispiel #3
0
static void
blink_led(void)
{
	int	i;
	uint16	pins;

	pin_config(12, GPIO_DIR_OUT);
	pin_config(13, GPIO_DIR_OUT);
	pin_config(15, GPIO_DIR_OUT);
	pins = BIT12|BIT13|BIT15;

	for (i = 1; i < 8; ++i) {			// 7 colours
		uint16	mask = ((i&4)<<(12-2)) | ((i&2)<<(13-1)) | ((i&1)<<(15-0));
		printf(" %d", i);
		gpio_output_set(mask, (~mask)&pins, 0, 0);
		delay_us(250000);
		gpio_output_set(0,    mask, 0, 0);	// all off
		delay_us(250000);
	}
	printf("\n");

	printf("tmr now = %ss\n", time_now_f());
	printf("sleeping 2s\n");
	system_deep_sleep(2*1000000);
	vTaskDelete(NULL);
}
Beispiel #4
0
static  int do_deepsleep(int argc, const char* argv[])
{
	char *tmp = argv[1];
	unsigned long n = skip_atoul(&tmp); 
	console_printf("Deep sleep mode for %ul microseconds\n", n);
	system_deep_sleep(n);
}
static enum v7_err dsleep(struct v7 *v7, v7_val_t *res) {
  enum v7_err rcode = V7_OK;
  v7_val_t time_v = v7_arg(v7, 0);
  double time = v7_to_number(time_v);
  v7_val_t flags_v = v7_arg(v7, 1);
  uint8 flags = v7_to_number(flags_v);

  if (!v7_is_number(time_v) || time < 0) {
    *res = v7_mk_boolean(false);
    goto clean;
  }
  if (v7_is_number(flags_v)) {
    if (!system_deep_sleep_set_option(flags)) {
      *res = v7_mk_boolean(false);
      goto clean;
    }
  }

  system_deep_sleep((uint32_t) time);

  *res = v7_mk_boolean(true);
  goto clean;

clean:
  return rcode;
}
STATIC mp_obj_t machine_deepsleep(void) {
    // default to sleep forever
    uint32_t sleep_us = 0;

    // see if RTC.ALARM0 should wake the device
    if (pyb_rtc_alarm0_wake & MACHINE_WAKE_DEEPSLEEP) {
        uint64_t t = pyb_rtc_get_us_since_2000();
        if (pyb_rtc_alarm0_expiry <= t) {
            sleep_us = 1; // alarm already expired so wake immediately
        } else {
            uint64_t delta = pyb_rtc_alarm0_expiry - t;
            if (delta <= 0xffffffff) {
                // sleep for the desired time
                sleep_us = delta;
            } else {
                // overflow, just set to maximum sleep time
                sleep_us = 0xffffffff;
            }
        }
    }

    // prepare for RTC reset at wake up
    rtc_prepare_deepsleep(sleep_us);
    // put the device in a deep-sleep state
    system_deep_sleep_set_option(0); // default power down mode; TODO check this
    system_deep_sleep(sleep_us);

    for (;;) {
        // we must not return
        ets_loop_iter();
    }

    return mp_const_none;
}
void ICACHE_FLASH_ATTR
user_sensor_deep_sleep_enter(void)
{
  return;
    system_deep_sleep(SENSOR_DEEP_SLEEP_TIME > link_start_time \
    ? SENSOR_DEEP_SLEEP_TIME - link_start_time : 30000000);
}
Beispiel #8
0
STATIC mp_obj_t esp_deepsleep(mp_uint_t n_args, const mp_obj_t *args) {
    uint32_t sleep_us = n_args > 0 ? mp_obj_get_int(args[0]) : 0;
    // prepare for RTC reset at wake up
    rtc_prepare_deepsleep(sleep_us);
    system_deep_sleep_set_option(n_args > 1 ? mp_obj_get_int(args[1]) : 0);
    system_deep_sleep(sleep_us);
    return mp_const_none;
}
void ICACHE_FLASH_ATTR data_func() {
    // Read out the sensor data structure from RTC memory
    system_rtc_mem_read( SENSOR_DATA_MEM_ADDR, &sensor_data, sizeof(SENSOR_DATA_RTC_MEM) );
    
    // When the system powers on for the first time, the data in the rtc memory is random.
    struct esp_platform_saved_param esp_param_t;
    user_esp_platform_load_param(&esp_param_t);  // Stored in flash
    // Load user params to check if the device was successfully registered to the server
    // If it wasn't, it usually returns 255 (from the flash.)

    if(sensor_data.init_flg!=INIT_MAGIC || sensor_data.cnt>SENSOR_DATA_NUM ) {
        // This case runs when we first power on or when it time to flush the RTC memory of old data.
        if(esp_param_t.activeflag!=1) {   // If registered & activated
            user_esp_platform_init();     // Router is not configured. Setup softAP. Wait for config. 
            #ifdef SERVER_SSL_ENABLE
            user_webserver_init(SERVER_SSL_PORT);
            #else
            user_webserver_init(SERVER_PORT);
            #endif
        } else { // was connected! So we set init magic to exit the setup loop
            sensor_data.init_flg = INIT_MAGIC;
            sensor_data.cnt = 0;
            system_rtc_mem_write(SENSOR_DATA_MEM_ADDR, &sensor_data, sizeof(SENSOR_DATA_RTC_MEM));
            __SET__DEEP_SLEEP__WAKEUP_NO_RF__; 
            system_deep_sleep(100000); 
        }
    } else { // This is where the measurements are made
        uint16 vdd_val = 0;
        if(sensor_data.cnt<0 || sensor_data.cnt>=SENSOR_DATA_NUM) 
            sensor_data.cnt=0; // range check and resets counter if needed

        /* Reads power supply voltage, byte 107 of init_data.bin should be set to 0xFF.
        *  Replace with your own code.*/
        sensor_data.data[sensor_data.cnt++] = (uint16)(phy_get_vdd33());
        system_rtc_mem_write( SENSOR_DATA_MEM_ADDR, &sensor_data, sizeof(SENSOR_DATA_RTC_MEM) );

        // Setup next sleep cycle
        if(sensor_data.cnt==SENSOR_DATA_NUM-1) { __SET__DEEP_SLEEP__WAKEUP_NORMAL__; }
        else { __SET__DEEP_SLEEP__WAKEUP_NO_RF__; }

        // Uploads or go to sleep
        if(sensor_data.cnt == SENSOR_DATA_NUM) { user_esp_platform_init(); }
        else { system_deep_sleep(SENSOR_DEEP_SLEEP_TIME); }
    }
}
Beispiel #10
0
void ICACHE_FLASH_ATTR
at_setupCmdGslp(uint8_t id, char *pPara)
{
	uint32_t n;
	pPara++;
	
	n = atoi(pPara);
	at_backOk;
	system_deep_sleep(n*1000);
}
//callback when data is recieved
void ICACHE_FLASH_ATTR recieveCB(void *arg,char *pData,unsigned short len)
{
    struct espconn *pNewEspConn = (struct espconn *)arg;
    os_printf("Response from Plotly received.  Data length = %d\n\r", len);
    os_printf("Plotly IP address was: %d. %d. %d. %d \n\r", IP2STR(&(pNewEspConn->proto.tcp->remote_ip)));
    int i=0;
    for (i=0; i<len; i++)   // Read data by each character and print it on the debug screen
    {
        os_printf("%c", pData[i]);
    }
    os_printf("\n");
    system_deep_sleep(sleep_time_min*60*1000*1000); // go to sleep after recieving data and wake up periodically
}
Beispiel #12
0
static v7_val_t dsleep(struct v7 *v7) {
  v7_val_t time_v = v7_arg(v7, 0);
  uint32 time = v7_to_number(time_v);
  v7_val_t flags_v = v7_arg(v7, 1);
  uint8 flags = v7_to_number(flags_v);

  if (!v7_is_number(time_v) || time < 0) return v7_create_boolean(false);
  if (v7_is_number(flags_v)) {
    if (!system_deep_sleep_set_option(flags)) return v7_create_boolean(false);
  }

  system_deep_sleep(time);

  return v7_create_boolean(true);
}
Beispiel #13
0
void init() {
    Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
    Serial.systemDebugOutput(false); // Enable debug output to serial

    WifiStation.enable(false);
    WifiAccessPoint.enable(false);

    initDisplay();

    ReadTemp.Init(DS_PIN);
    ReadTemp.StartMeasure();

    displayTemps();

    system_deep_sleep(30 * 1000 * 1000);
}
void wifi_callback( System_Event_t *evt )
{
    os_printf( "%s: %d\n", __FUNCTION__, evt->event );
    
		char toto[7] = "hello\n\0";
		//uart0_tx_buffer(toto, 7);
    switch ( evt->event )
    {
        case EVENT_STAMODE_CONNECTED:
        {
		
		char toto[7] = "hello\n\0";
		//uart0_tx_buffer(toto, 7);
	   	os_printf("connect to ssid %s, channel %d\n",
			evt->event_info.connected.ssid,
			evt->event_info.connected.channel);
	    	break;
        }

        case EVENT_STAMODE_DISCONNECTED:
        {
	    os_printf("disconnect from ssid %s, reason %d\n",
	                evt->event_info.disconnected.ssid,
	                evt->event_info.disconnected.reason);
	    
	    deep_sleep_set_option( 0 );
	    system_deep_sleep( 60 * 1000 * 1000 );  // 60 seconds

	    break;
        }

        case EVENT_STAMODE_GOT_IP:
        {
            os_printf("ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR,
                        IP2STR(&evt->event_info.got_ip.ip),
                        IP2STR(&evt->event_info.got_ip.mask),
                        IP2STR(&evt->event_info.got_ip.gw));
            os_printf("\n");
            break;
        }
        
        default:
        {
            break;
        }
    }
}
Beispiel #15
0
/** for data xchange channel */
bool __plugin_pm_sleep(void *class_ptr, char *method_name, void *input_pack)
{
    uint8_t *arg_ptr = (uint8_t *)input_pack;
    uint32_t sec;

    memcpy(&sec, arg_ptr, sizeof(uint32_t)); arg_ptr += sizeof(uint32_t);

    Serial1.printf("deep sleep %lu sec\r\n", sec);
    writer_print(TYPE_STRING, "\"ok, deep sleep\"");
    response_msg_close(STREAM_DATA);
    delay(100);
    //turn off grove's power
    digitalWrite(SWITCH_GROVE_POWER, 0);

    system_deep_sleep(sec * 1000000);

    return true;
}
Beispiel #16
0
bool SystemClass::deepSleep(uint32 timeMilliseconds, DeepSleepOptions options /* = eDSO_RF_CAL_BY_INIT_DATA */)
{
	if (!system_deep_sleep_set_option((uint8)options)) return false;
	system_deep_sleep(timeMilliseconds * 1000);
	return true;
}
Beispiel #17
0
static void ICACHE_FLASH_ATTR
timer_fn(void *timer_data)
{
    uint32_t delay = MAX_LOOP_TIMER_MS;

    os_timer_disarm(&timer);

    PRINTF("state: %d\n\r", state);
    switch(state) { 
        case STATE_LOAD_PERSIST:
            if(persist_load()) {
            	PRINTF("persist data sensor value: %d\r\n", persist_data_ptr()->sensor_value);
            	PRINTF("persist data sensor poll count: %d\r\n", persist_data_ptr()->poll_counter);

                state = STATE_HF_READ;
                persist_data_ptr()->poll_counter += 1;

                if(persist_data_ptr()->poll_counter >= 10) {
                    // read low-frequency sensor (temp etc)
                    persist_data_ptr()->poll_counter = 0;
                }
                persist_dirty();
            } else {
                PRINTF("err loading persist\r\n");
                // todo: goto error state (blink led?)
                state = STATE_IDLE;
            }
            delay = MIN_LOOP_TIMER_MS;
            break;
        case STATE_INIT_PERSIST:
            persist_init();
            state = STATE_HF_READ;
            delay = MIN_LOOP_TIMER_MS;
            break;
        case STATE_HF_READ:
            state = STATE_LF_READ;
            if(GPIO_INPUT_GET(GPIO_SWITCH)) {
                if(!(persist_data_ptr()->sensor_value)) {
                    persist_data_ptr()->sensor_value = true;
                    persist_dirty();
                    state = STATE_HF_WIFI_CONNECT;
                }
            } else {
                if(persist_data_ptr()->sensor_value) {
                    persist_data_ptr()->sensor_value = false;
                    persist_dirty();
                    state = STATE_HF_WIFI_CONNECT;
                }
            }
            break;
        case STATE_HF_WIFI_CONNECT:
            if(wifi_station_get_connect_status()==STATION_GOT_IP) {
                state = STATE_HF_SEND;
            } else {
                wifi_connect();
                state = STATE_HF_WIFI_WAIT;
            }
            break;
        case STATE_HF_WIFI_WAIT:
            if(wifi_station_get_connect_status()==STATION_GOT_IP) {
                state = STATE_HF_SEND;
            }
            break;
        case STATE_HF_SEND:
            state = STATE_HF_SEND_WAIT;
            break;
        case STATE_HF_SEND_WAIT:
            state = STATE_LF_READ;
            break;
        case STATE_LF_READ:
            state = STATE_LF_WIFI_CONNECT;
            break;
        case STATE_LF_WIFI_CONNECT:
            if(wifi_station_get_connect_status()==STATION_GOT_IP) {
                state = STATE_LF_SEND;
            } else {
                wifi_connect();
                state = STATE_LF_WIFI_WAIT;
            }
            break;
        case STATE_LF_WIFI_WAIT:
            if(wifi_station_get_connect_status()==STATION_GOT_IP) {
                state = STATE_LF_SEND;
            }
            break;
        case STATE_LF_SEND:
            state = STATE_LF_SEND_WAIT;
            break;
        case STATE_LF_SEND_WAIT:
            state = STATE_IDLE;
            break;
        case STATE_IDLE:
            // go to deep sleep
            persist_save();
            system_deep_sleep(SLEEP_TIME_US);
            return;
            break;
        default:
            // uhoh
            PRINTF("invalid state, reset\n\r");
            state = STATE_IDLE;
            break;
    }
       
    os_timer_setfn(&timer, (os_timer_func_t *)timer_fn, NULL);
    os_timer_arm(&timer, delay, false); 
/*
        case STATE_IDLE:
            http_get("http://10.1.3.161/text", "", my_http_callback);
            state = STATE_HTTP_PENDING;
            timeout = 30;
            break;
        case STATE_HTTP_PENDING:
            timeout--;
            if(timeout==0) {
                PRINTF("timeout!\n\r");
                state=STATE_IDLE;
            } else {
                delay_ms(1000);
            }
            break;
        default:
            PRINTF("invalid state, reset\n\r");
            state=STATE_IDLE;
            break;
    }
    //system_os_post(user_procTaskPrio, 0, 0 ); */
}
Beispiel #18
0
void serialCallBack(Stream& stream, char arrivedChar, unsigned short availableCharsCount) {
	if (arrivedChar == '\n') {
		char str[availableCharsCount];
		for (int i = 0; i < availableCharsCount; i++) {
			str[i] = stream.read();
			if (str[i] == '\r' || str[i] == '\n') {
				str[i] = '\0';
			}
		}
		
		if (!strcmp(str, "connect")) {
			// connect to wifi
			WifiStation.config(WIFI_SSID, WIFI_PWD);
			WifiStation.enable(true);
		} else if (!strcmp(str, "ip")) {
			Serial.printf("ip: %s mac: %s\r\n", WifiStation.getIP().toString().c_str(), WifiStation.getMAC().c_str());
		} else if (!strcmp(str, "ota")) {
			OtaUpdate();
		} else if (!strcmp(str, "switch")) {
			Switch();
		} else if (!strcmp(str, "restart")) {
			System.restart();
		} else if (!strcmp(str, "ls")) {
			Vector<String> files = fileList();
			Serial.printf("filecount %d\r\n", files.count());
			for (unsigned int i = 0; i < files.count(); i++) {
				Serial.println(files[i]);
			}
		} else if (!strcmp(str, "cat")) {
			Vector<String> files = fileList();
			if (files.count() > 0) {
				Serial.printf("dumping file %s:\r\n", files[0].c_str());
				Serial.println(fileGetContent(files[0]));
			} else {
				Serial.println("Empty spiffs!");
			}
		} else if (!strcmp(str, "info")) {
			ShowInfo();
		} else if (!strcmp(str, "help")) {
			Serial.println();
			Serial.println("available commands:");
			Serial.println("  help - display this message");
			Serial.println("  ip - show current ip address");
			Serial.println("  connect - connect to wifi");
			Serial.println("  restart - restart the esp8266");
			Serial.println("  switch - switch to the other rom and reboot");
			Serial.println("  ota - perform ota update, switch rom and reboot");
			Serial.println("  info - show esp8266 info");
			Serial.println("  sl - Sleep for 5 seconds");
#ifndef DISABLE_SPIFFS
			Serial.println("  ls - list files in spiffs");
			Serial.println("  cat - show first file in spiffs");
#endif
			Serial.println();
		} else if (!strcmp(str, "sl")) {
			Serial.println("Going to sleep");
			delay(500);
			system_deep_sleep(5000000);
			delay(500);
			Serial.println("Wakeing up");
		} else {
			Serial.println("unknown command");
		}
	}
}
/******************************************************************************
 * FunctionName : gm_state_run
 * Description  : 
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR
gm_state_run(greemon_state_t gm_run_state){
	switch (gm_run_state) {

// --------------------------------------------------------------------------

		case (_STATE_STARTUP):
		/* Initialize UART, 
		 * Register Callback for lwip
		 * set wifi handler
		 * do not connect to wifi at this point
		 */
			gm_error_counter = 0;
			if ( !gm_startup() ) gm_state_set(_STATE_ERROR);
			INFO("Waiting for init callback");
		break;

// --------------------------------------------------------------------------

		case (_STATE_INIT):	
		/* Fired after LWIP callback,
		 * Register interrupt for reset
		 * Load Configuration
		 */
			INFO("System initialized. Greemon startup...");
			if ( !gm_init() ) gm_state_set(_STATE_ERROR);
		break;

// --------------------------------------------------------------------------

		case (_STATE_RESET):

		break;

// --------------------------------------------------------------------------

		case (_STATE_ERROR):
		/* 
		 * this state only happens if there was an error
		 */
			INFO("Wooooooops... there was an ERROR.");
			INFO("restart is the only exit")
		break;

// --------------------------------------------------------------------------

		case (_STATE_INITIALIZED):
			/* System is initialized,
			 * we can load the config before we startup our greemon system
			*/
			if ( !gm_load_config() ) gm_state_set(_STATE_ERROR);
		break;

// --------------------------------------------------------------------------

		case (_STATE_CONFIG_USER):
		/*	magic word has been found in the flash data,
		 *	we start with the user configuration
		 *	but first read sensor data
		 */
			INFO("user config has been loaded");
			if ( !gm_read_sensors() ) gm_state_set(_STATE_ERROR);
			gm_state_set(_STATE_SENSOR_DATA_SAVED);
		break;

// --------------------------------------------------------------------------

		case (_STATE_CONFIG_DEFAULT):
		/*	magic word has not been found in the flash data,
		 *	we start with the default configuration
		 */
			INFO("loaded standard configuration");
			INFO("webserver starting");
			if ( !gm_webserver_start() ) gm_state_set(_STATE_ERROR);
			gm_state_set(_STATE_WEBSERVER_RUNNING);
		break;

// --------------------------------------------------------------------------

		case (_STATE_CONFIG_RECIEVED):
		INFO("config recieved - please restart the controller");
		break;

// --------------------------------------------------------------------------

		case (_STATE_WEBSERVER_PARSING):
			
		break;

// --------------------------------------------------------------------------

		case (_STATE_WEBSERVER_RUNNING):
		/* Webserver is running as long as 
		 * we did not recieve a new configuration
		 */
			INFO("webserver running");
			INFO("waiting for HTTP requests");

// TEST FOR BH1750
//TODO: DELETEME
//		i2ctest();	


		break;

// --------------------------------------------------------------------------

		case (_STATE_SAVED_CONFIGURATION):
			INFO("configuration has been saved");
			wifi_station_disconnect();
			gm_webserver_stop();
			INFO("restarting");
			system_restart();
			while(1) os_delay_us(100);
		break;

// --------------------------------------------------------------------------

		case (_STATE_TIME_RECIEVED):
			INFO("start connecting with server");
			if ( !gm_server_connect() ) gm_state_set(_STATE_DEEP_SLEEP);
		break;

// --------------------------------------------------------------------------

		case (_STATE_SENSOR_DATA_SAVED):
			INFO("Sensor data saved");
			if ( !gm_connect_ap() ) gm_state_set(_STATE_ERROR);
			INFO("connecting.. waiting for wifi-handler");
		break;

// --------------------------------------------------------------------------

		case (_STATE_WIFI_CONNECTED):
		/* Connected to AP and recieved an IP Adress
		 * Starting SNTP Client
		 */
			if ( !gm_sntp_start() ) gm_state_set(_STATE_ERROR);
			user_sntp_wait_valid_time();
			INFO("recieved time from server");
			// NEXT STATE ->_STATE_TIME_RECIEVED
			gm_state_set(_STATE_TIME_RECIEVED);
		break;

// --------------------------------------------------------------------------

		case (_STATE_CONNECTION_CLOSED):
		
		break;

// --------------------------------------------------------------------------

		case (_STATE_CONNECTION_OPEN):
			INFO("sending data..");
			if ( !gm_server_send_data() ) gm_state_set(_STATE_ERROR);
		break;

// --------------------------------------------------------------------------

		case (_STATE_CONNECTION_TRANSMITTED):
			INFO("transmitted data successfully")
			// A dataset has been transmitted. 
			// Remove from the flash happend automatically when we popped the data
			// Start 16 secs timer because ts only acceps data every 15 secs
			if ( 0 == global_cfg.storedData ) {
				INFO("all data has been sent");
				gm_state_set(_STATE_DEEP_SLEEP);
			} else {
				INFO("still data(%d) remaining, starting timer", global_cfg.storedData);
				os_timer_setfn(&timer_server_wait,timer_server_wait_cb,NULL);
				os_timer_arm(&timer_server_wait,GM_SERVER_TRANSMIT_INTERVAL,1);
			}
		break;

// --------------------------------------------------------------------------

		case (_STATE_DEEP_SLEEP):
			INFO("Finished work. Deep sleep, yay.");
			system_deep_sleep(60*1000*1000); // uint32_t time in us 
		break;

// --------------------------------------------------------------------------
	}
}
Beispiel #20
0
void EspClass::deepSleep(uint32_t time_us, WakeMode mode)
{
	system_deep_sleep_set_option(static_cast<int>(mode));
 	system_deep_sleep(time_us);
}
Beispiel #21
0
static void ICACHE_FLASH_ATTR deepSleepCb(void *arg) {
	system_deep_sleep_set_option(2);
	system_deep_sleep(100*1000);
}