Welcome to Robot Rebels, an online robot maker community.

Register today to post your projects, ask questions, share knowledge and meet like-minded people from around the world.

The Community

Author Topic: RC PDU  (Read 583 times)


  • Hot glue gunslinger
  • Member
  • *
  • Posts: 151
  • This is a good day!
« on: March 22, 2017, 04:39:26 PM »
A small remote controlled (wifi) power distribution unit.

Board takes 12V DC as input. Has two step-down regulators, 5V and 3.3V

2017-03-22 21.25.27.jpg
*2017-03-22 21.25.27.jpg (93.35 kB . 800x474 - viewed 90 times)

Board holds an ESP8266
2017-03-22 21.25.55.jpg
*2017-03-22 21.25.55.jpg (108 kB . 800x520 - viewed 82 times)

and the backside is ugly
2017-03-22 21.26.20.jpg
*2017-03-22 21.26.20.jpg (129.75 kB . 800x530 - viewed 76 times)
And the schematic
PDU-ESP8266-RPi-server (1).png
*PDU-ESP8266-RPi-server (1).png (42.21 kB . 800x623 - viewed 71 times)
added a reset switch, and since the board was made so compact, it has no room for ins for in-system-programming, so I soldered extra long pins on the ESP-12E module. Now I can hook my USB interface to top top side of the ESP module.
2017-03-24 22.11.52.jpg
*2017-03-24 22.11.52.jpg (83.8 kB . 775x768 - viewed 63 times)
Software is written in python. I find it very pleasing to use micropython for microcontrollers.

When booted it act as a webserver, with an additional timer. wait two days and it will boot the RPi, or fire a web request to get it going on demand:
Code: [Select]
mogul@linuxine:~$ curl razfs2b-pwr
{"timer": 171841, "stage": "sleep", "RPi": 1}

turn on
Code: [Select]
mogul@linuxine:~$ curl razfs2b-pwr/on

then use status again to wast it step through stages to reach on
Code: [Select]
mogul@linuxine:~$ curl razfs2b-pwr
{"timer": 0, "stage": "on", "RPi": 1}

Not perfectly pleased with the structure of the code, but not too bad either. I have split the code into several modules for easy reuse. First a module to initialize a wifi station:
Code: [Select]
import network
import time

wlan = network.WLAN(network.STA_IF)

def start():
    import wifi_cfg

    except OSError as e:
        print("WARN: set dhcp_hostname:", str(e))

    wlan.connect(wifi_cfg.ssid, wifi_cfg.password)

def wait_for_join(timeout=5):
    retry = timeout
    while retry and not wlan.isconnected():
        retry =- 1
    return wlan.isconnected()

def get_info():
    return wlan.ifconfig()

then a module to act as a web-server:
Code: [Select]
import usocket
import uselect

class phttpd:
    def __init__(self,  port):
        addr = usocket.getaddrinfo("", port)[0][-1]
        self._sock = usocket.socket()
        self._poll = uselect.poll()
        self._poll.register(self._sock, uselect.POLLIN)
        self._methods = {"GET":{}}

    def register(self, method, uri, cb):
        self._methods[method][uri] = cb

    def _load_request(self, stream):
        l = [x.strip() for x in stream.readline().decode("utf-8").split(' ')]

        # eat and discard remaining headers
        while stream.readline().strip():

        return (l[:2]) # http method and uri

    def poll(self):
        if self._poll.poll(0):
            (c_stream, c_addr) = self._sock.accept()
            (m, u) = self._load_request(c_stream)
            print("%s: %s %s" % (c_addr[0], m, u))

                cb = self._methods[m][u]
            except KeyError:
                c_stream.write("HTTP/1.0 404 Not Found\n\n")
                c_stream.write("HTTP/1.0 200 ok\n\n%s\n" % cb())

and finally the sequencer:
Code: [Select]
import gc
import time
import machine
from phttpd import phttpd
import ujson

class timer:
    def __init__(self):
        self.t = 0
    def set(self, sec):
        self.t = time.time() + sec
    def get(self):
        return max(self.t - time.time(), 0)
    def timeout(self):
        return self.t and time.time() > self.t

class state:
    def __init__(self, s):
        self.s = s
    def set(self,s):
        print("[%d] state %s => %s" % (int(time.time()), self.s, s))
        self.s = s
    def get(self):
        return self.s
    def test(self, s):
        return s == self.s

class sequencer:
    def __init__(self, scheduled_interval):
        self.RPiSense = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP)
        self.hddPwr = machine.Pin(13, machine.Pin.OUT)
        self.RPiPwr = machine.Pin(14, machine.Pin.OUT)
        self.stage = state("off")
        self.timer = timer()
        self.scheduled_interval = scheduled_interval

    def get_state(self):
        return ujson.dumps({
          'stage': self.stage.get(),
          'RPi':   self.RPiSense.value(),
          "timer": self.timer.get()

    def on(self):
        if self.stage.test("sleep"):
            return "ok"
            return "not sleeping"

    def force_off(self):
        return "ok"

    def poll(self):
        if self.stage.test("off"): # all off, wait for timed wakeup

        if self.stage.test("sleep") and self.timer.timeout(): # sleep is over

        if self.stage.test("wakeup"): # power up hdd
            self.stage.set("hdd spin up")

        if self.stage.test("hdd spin up") and self.timer.timeout(): # power up RPi
            self.stage.set("RPi boot up")

        if self.stage.test("RPi boot up") and self.timer.timeout(): # RPi now booted

        if self.stage.test("on") and self.RPiSense.value() == 0: # RPi signaled poweroff
            self.stage.set("showdown initiated")

        if self.stage.test("showdown initiated") and self.RPiSense.value() == 1: # RPi canceled poweroff

        if self.stage.test("showdown initiated") and self.timer.timeout(): # power off

p = sequencer(3600*24*2)
w = phttpd(80)

def start():
    w.register('GET', '/', p.get_state)
    w.register('GET', '/on', p.on)
    w.register('GET', '/FORCE_OFF', p.force_off)

    while True:

To make it start on power up:
Code: [Select]
import wifi
import webrepl
import RPi_sequencer

print("our_ip", wifi.get_info()[0])


wifi and webrepl take two configuration files:
Code: [Select]
Code: [Select]

To make the RPi power it self off I have added a line to the kernel configuration, done on
Code: [Select]
# GIPIO=22, goes low when ready for power cut

and then connected RPi GIPIO=22 to ESP8266 GPIO-12.
« Last Edit: April 29, 2017, 10:28:49 AM by mogul »


* Search

* Recent Topics

syncthing - true p2p directory sync by mogul
[Today at 03:21:49 PM]

Youtube by Impala
[Today at 01:36:26 PM]

Bob's gone Wood by Protowrxs
[January 16, 2018, 10:41:57 PM]

Automatic Garage Door malfunction by Protowrxs
[January 16, 2018, 05:34:00 PM]

speech synthesizer chip by Protowrxs
[January 16, 2018, 05:22:22 PM]

Spider by Protowrxs
[January 16, 2018, 05:18:26 PM]

CZ-1 2.0: problems, calibration and printing, part2 by MEgg
[January 14, 2018, 06:35:28 PM]

3D printing: autoleveling, autoadjustment by MEgg
[January 09, 2018, 05:18:46 PM]

Simple One-Servo Walker by ZeroMax
[January 03, 2018, 10:04:07 PM]

Anycubic Kossel (pulley) by Deity
[December 31, 2017, 09:04:23 AM]

Merry Christmas by Impala
[December 24, 2017, 12:04:45 AM]

printed CNC engraver by jinx
[December 17, 2017, 04:45:43 AM]

metal chassis desktop bot by craighissett
[December 14, 2017, 06:55:16 AM]

LEGO by Impala
[December 13, 2017, 09:46:53 PM]

Your workshop space by viswesh
[December 07, 2017, 09:32:47 PM]

* Recent Posts

syncthing - true p2p directory sync by mogul
[Today at 03:21:49 PM]

Re: Youtube by Impala
[Today at 01:36:26 PM]

Re: Youtube by mogul
[Today at 04:36:47 AM]

Re: Youtube by jinx
[Today at 03:04:42 AM]

Youtube by Impala
[January 17, 2018, 10:23:59 AM]

Re: Bob's gone Wood by Protowrxs
[January 16, 2018, 10:41:57 PM]

Bob's gone Wood by ZeroMax
[January 16, 2018, 06:49:12 PM]

Re: Automatic Garage Door malfunction by Protowrxs
[January 16, 2018, 05:34:00 PM]

Re: speech synthesizer chip by Protowrxs
[January 16, 2018, 05:22:22 PM]

Re: Spider by Protowrxs
[January 16, 2018, 05:18:26 PM]

Spider by viswesh
[January 15, 2018, 01:14:19 PM]

Re: CZ-1 2.0: problems, calibration and printing, part2 by MEgg
[January 14, 2018, 06:35:28 PM]

Re: speech synthesizer chip by ZeroMax
[January 14, 2018, 01:49:46 PM]

Re: CZ-1 2.0: problems, calibration and printing, part2 by MEgg
[January 14, 2018, 01:09:24 PM]

Re: Automatic Garage Door malfunction by MEgg
[January 13, 2018, 07:28:48 AM]