sleep issues when waking up?

If doesn't fit into any other category ....
Post Reply
Zim
Posts: 289
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 265 times
Been thanked: 130 times

sleep issues when waking up?

Post by Zim »

Hello
I am trying to have this script go into deep sleep for a time and then wake up, restart and rerun script and send the msg$.
It doesn't seem to completely start from scratch. I've tried esp8266 and esp32 modules. Each time I press reset it sends msg$ but waking up from sleep on its own doesn't send. What am I missing?
Thanks
Zim

Code: [Local Link Removed for Guests]

'test sensor v1
cls
msg$ = "100"
MAC1$ = "52:02:91:E0:4D:33"  '  AP mac of the receiver
WIFI.CONNECT "Propane_Rx", "whatever"
pause 100
espnow.begin 
pause 100
espnow.add_peer MAC1$
pause 100


EspNow.Write(msg$)  

wlog "sleeping"

sleep 20
end
BeanieBots
Posts: 349
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 185 times
Been thanked: 112 times

Re: sleep issues when waking up?

Post by BeanieBots »

Can you give a few more details. Not clear what the issue is.
I have several devices which sleep, send data via espnow then go back to sleep all working just fine.
I do NOT wait for a wifi connection. Just use AP mode but you MUST specify the channel number.
Also set 'fast boot' in the config.

Hope to publish some examples soon but still fighting with some TFTs at the moment.
Zim
Posts: 289
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 265 times
Been thanked: 130 times

Re: sleep issues when waking up?

Post by Zim »

Thanks BeanieBots! I'll try that...
BeanieBots
Posts: 349
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 185 times
Been thanked: 112 times

Re: sleep issues when waking up?

Post by BeanieBots »

This is a work in progress, so don't take things too litterally, but it does work.
All the ADC stuff is prevent the battery from getting too low and causing damage.

Code: [Local Link Removed for Guests]

'********************************************************************************
'*                                                                              *
'*  Remote BME280 Transmitter                                                   *
'*  Config MUST be set to Fast Boot                                             *
'*  Sept 2021                                                                   *
'*  Using ESP-12f                                                               *
'*  V1.1.0 27/09/2022 Changed I2C from 2,0 to 4,5                               *
'*  Added IO(12) as linkable configuration.                                     *
'*  Moved BME280 Vcc from 3v3 to IO(14)                                         *
'*  Added select/case/sleep based on voltage.                                   *
'*  Added 2nd sensor 05/10/2022                                                 *
'*  Added Vstart for new battery detection 26/05/2023                           *
'*                                                                              *
'********************************************************************************

Started = millis
Vstart=ADC/242  '240 is nominal.
'OPTION.CPUFREQ 80 'saves a little power but takes 20mS longer.
WIFI.APMODE "Give the AP a name", "and a password", 11 'Required for ESPNOW on channel 11
pin.mode 14,output'BME280 Vcc
pin(14) = 1 'Turn on the BME280
pin.mode 2,output 'Onboard LED
pin(2) = 0  'Turn on onboard LED
pin.mode 12,input 'Linkable IO
ESP_Tx$="":N=0:ESP_Rx$=""
i2c.setup 4,5
word.setparam ESP_Tx$,"Name","Sens2"
Gosub GetValues
'Do while ( wifi.channel <> 11)
'Pause 10
'Loop
ESPNOW.BEGIN
ESPNOW.add_peer("2c:f4:32:a5:5f:0e")'sonoff 1
'##########################################################################
Do  
  gosub SendData

  If (pin(12) = 0) AND (V <= Vstart) then   
    Sleep S  
    else
    Vstart = V
    Pause 2000
    Gosub GetValues
    Pin(2) = 1'LED Off
    pause 1200
    Pin(2) = 0'LED On
  EndIf
  
Loop
'###########################################################################
SendData:
  ms = millis - started
  word.setparam ESP_Tx$,"ms",STR$(ms,"%2.2f")
  ESPNOW.WRITE(ESP_Tx$)
  if espnow.error$ <> "" then
    pause 50
    ESPNOW.WRITE(ESP_Tx$) 'Retry
  endif 
Return
'###########################################################################
GetValues:
  V=ADC/242
    
    Select Case V          'Adjust volages to suit specific battery. 
    
      Case 0 to 3.50
        S = 3600           '1 Hour
        
      Case 3.50 to 3.60
        S = 2400           '20 minutes
        
      Case 3.60 to 3.65
        S = 1200            '10 minutes
        
      Case 3.65 to 3.70
        S = 900            '5 minutes
        
      Case 3.70 to 3.75
        S = 600            '3 minutes
        
      Case 3.75 to 3.80
        S = 300            '2 minutes
        
      Case 3.80 to 3.85
        S = 240             '1 minute
        
      Case 3.85 to 3.90
        S = 180             '30 seconds
        
      Case 3.90 to 4.00
        S = 60             '20 seconds
        
      Case 4.00 to 4.18
        S = 30             '10 seconds
        
      Case 4.18 to 4.2 
        S = 10              '5 seconds
        
      Case > 4.2
        S = 5
        
      Case else
        S = 15
        
    End Select

  Ret=BME280.setup(&h76)
  T=bme280.temp
  H=bme280.hum
  H=(H*1.0275)+0.03  'Humidity calibration
  P=bme280.qfe
  word.setparam ESP_Tx$,"T1",STR$(T,"%2.2f")
  word.setparam ESP_Tx$,"H1",STR$(H,"%2.2f")
  word.setparam ESP_Tx$,"P1",STR$(P,"%2.2f") 
  
  Ret=BME280.setup(&h77)
  T=bme280.temp
  H=bme280.hum
  H=(H*1.0055)+1.45  'Humidity calibration
  P=bme280.qfe
  word.setparam ESP_Tx$,"T2",STR$(T,"%2.2f")
  word.setparam ESP_Tx$,"H2",STR$(H,"%2.2f")
  word.setparam ESP_Tx$,"P2",STR$(P,"%2.2f")
  word.setparam ESP_Tx$,"V",STR$(V,"%2.2f")
  word.setparam ESP_Tx$,"S",STR$(S,"%2.2f") 

Return
'###########################################################################
P.S. Don't forget to link GPIO 16 to RST.
Not easy on a ESP-01
You do not have the required permissions to view the files attached to this post.
Zim
Posts: 289
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 265 times
Been thanked: 130 times

Re: sleep issues when waking up?

Post by Zim »

Thanks BeanieBots!

I learnt good stuff from your code!

Zim
lyizb
Posts: 101
Joined: Fri Feb 12, 2021 8:23 pm
Has thanked: 43 times
Been thanked: 25 times

Re: sleep issues when waking up?

Post by lyizb »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Fri Dec 08, 2023 10:43 pmAll the ADC stuff is prevent the battery from getting too low and causing damage.
I see some version of this has been running since Sept 2021--how long does it last on what sort of battery?

I was just about to set up similar with 18650, 4 AA NiMH, and 4 AA Alkaline, just to see how long they would last. ESP32 S2Mini would be the processors. Glad I found your code.
BeanieBots
Posts: 349
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 185 times
Been thanked: 112 times

Re: sleep issues when waking up?

Post by BeanieBots »

That particular one runs from 3*AAA NiMh 900mAh batteries.
The real capacity is probably down to about 600mAhr as they are very old now. Naturally, it depends a lot on how low you let them drain.
There is no regulator (to reduce quiescent current) just a series 1N4001 diode to drop a bit of voltage.
The potential divider to measure battery voltage is 3M3/1M0 draining under 1uA.
The BME280 pulls around 10uA when idle which is significant compared to a sleeping 8266 so I put its Vcc on an IO pin.
With that setup and a nominal sleep of 3 minutes, I get over six weeks of data.

I also have a 18650 version but that is charged via a small solar panel scavenged from a solar garden light.
The sleep time is reduced when the battery gets close to 4.0v to prevent over charging.
It's not been running long so little data but on a sunny day, sleep is reduced to as low 5 seconds and typically runs at between 1 & 2 minutes overnight.

I'm working on a version which will use LifePo4 with a nominal 3.2v and max 3.6v rather than Lithium ion which is nominal 3.7v and max 4.2v.
LifePo4 can also be discharged down to 1.5v without causing damage.
That will enable direct connection and thus be more efficient.
The ESP will keep running down 1.8v.
Hope that helps.

At some point I'll post it all as a project, including the receiver and TFT display. Trouble is, it keeps evolving!
lyizb
Posts: 101
Joined: Fri Feb 12, 2021 8:23 pm
Has thanked: 43 times
Been thanked: 25 times

Re: sleep issues when waking up?

Post by lyizb »

Thanks. Very suggestive. For complete stand-alone, ideally I'd be looking for 7 months of reading a temperature every 10 minutes (or up to every hour), and sending only a change of a degree C or more. That time period is because I spend 6+ months in Nova Scotia, and 6- months in Florida, and could recharge batteries after being away.

But with an 18650 UPS (charged either with mains or solar) there would be no need for such a long runtime, so it might just be idle curiosity to see how long it could run. (Your idea regarding a link to a GPIO pin to govern sleep or no sleep is noted.)

I hear what you say about how the goalposts keep moving.
BeanieBots
Posts: 349
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 185 times
Been thanked: 112 times

Re: sleep issues when waking up?

Post by BeanieBots »

Your goal should be possible.
I only have ~600mAhr. An 18650 could be up to 3500mAhr. So nearly 6X duration. A 21700 or two 18650 would give > 5Ahr.
I read every 3 mins. Increase that to 10 mins = ~3 times longer duration.
Combine both = 3*6*6 = >100 weeks.
In reality it will be less due to self discharge and lower if very cold (lithium does not like cold) but I think you could achieve >6 months without too much effort.
Also, don't forget to use 'non-connection' transmission such as ESPnow for short on times.
My code example stays awake for only 1.4 seconds. (though I built a 2nd one and sometimes it takes 2.6S. Not sure why the difference or why sometimes. It's NOT due to retries.)
Post Reply