Skip to content

Instantly share code, notes, and snippets.

@1rabbit
Last active February 18, 2018 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1rabbit/c52e9951e62050ac7ef5d9850bd72d2c to your computer and use it in GitHub Desktop.
Save 1rabbit/c52e9951e62050ac7ef5d9850bd72d2c to your computer and use it in GitHub Desktop.
Attiny85 - RPI/ARM power management for battery operated devices
#!/bin/bash
gpio mode 28 out
gpio write 28 1
i="0"
while [ $i -lt 3 ]
do
sleep 12
stayup=`curl -k https://designdesk.org/wakeup`
if [ "$stayup" = "1" ]; then
echo 0 > /sys/class/leds/led0/brightness
sleep 1
echo 255 > /sys/class/leds/led0/brightness
sleep 1
echo 0 > /sys/class/leds/led0/brightness
sleep 1
echo 255 > /sys/class/leds/led0/brightness
ipAddr=$( ip a s tun0 | awk '/inet.*brd/ {print $2}' )
curl http://sms.api:1880/sms?message=Turtle%20UP%20%3A%20$ipAddr
exit
elif [ "$stayup" = "0" ]; then
/sbin/shutdown -h now
exit
else
i=$[$i+1]
fi
done
/sbin/shutdown -h now
@reboot . ~/.profile; sleep 5 && /bin/bash /root/autoturtle.sh
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
int MOSFET_PIN = 0;
int RPI_PIN = 2;
// Incremented by watchdog interrupt
volatile uint8_t wdt_count;
void setup() {
pinMode(RPI_PIN, INPUT);
pinMode(MOSFET_PIN, OUTPUT);
digitalWrite(MOSFET_PIN, HIGH);
ADCSRA &= ~_BV(ADEN); // switch ADC OFF
ACSR |= _BV(ACD); // switch Analog Compartaror OFF
// Configure attiny85 sleep mode
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
// Reset watchdog interrupt counter
wdt_count = 255; //max value
}
void loop() {
digitalWrite(MOSFET_PIN, HIGH);
// KEEP POWER UP
wdt_count = 0;
watchdog_start_interrupt(9); // prescale of 9 ~= 9sec
while(wdt_count < 1){ // Wait 1 watchdog interupts (~9secs)
sleep_mode(); // Make CPU sleep until next WDT interrupt
}
watchdog_stop();
bool keepRpiUp = digitalRead(RPI_PIN);
// TIMEOUT TILL POWER OFF
if (keepRpiUp == 0) {
wdt_count = 0;
watchdog_start_interrupt(9); // prescale of 9 ~= 9sec
while(wdt_count < 4){ // Wait 4 watchdog interupts (~36secs)
sleep_mode(); // Make CPU sleep until next WDT interrupt
}
watchdog_stop();
}
keepRpiUp = digitalRead(RPI_PIN);
// POWER OFF FOR 30 MINUTES
if (keepRpiUp == 0) { digitalWrite(MOSFET_PIN, LOW);
wdt_count = 0;
watchdog_start_interrupt(9); // prescale of 9 ~= 9sec
while(wdt_count < 200){ // Wait 200 watchdog interupts (~30mn)
sleep_mode(); // Make CPU sleep until next WDT interrupt
}
watchdog_stop();
}
}
/* Turn off WDT */
void watchdog_stop() {
WDTCR |= _BV(WDCE) | _BV(WDE);
WDTCR = 0x00;
}
/* Turn onn WDT (with interupt) */
void watchdog_start_interrupt(uint8_t wd_prescaler) {
if(wd_prescaler > 9) wd_prescaler = 9;
byte _prescaler = wd_prescaler & 0x7;
if (wd_prescaler > 7 ) _prescaler |= _BV(WDP3);
// ^ fourth bit of the prescaler is somewhere else in the register...
// set new watchdog timer prescaler value
WDTCR = _prescaler;
// start timed sequence
WDTCR |= _BV(WDIE) | _BV(WDCE) | _BV(WDE);
}
// Watchdog Interrupt Service / is executed when watchdog timed out
ISR(WDT_vect) {
wdt_count++;
WDTCR |= _BV(WDIE); // Watchdog goes to interrupt not reset
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment