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 3144 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 241 times)

Board holds an ESP8266
2017-03-22 21.25.55.jpg
*2017-03-22 21.25.55.jpg (108 kB . 800x520 - viewed 249 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 242 times)
And the schematic
PDU-ESP8266-RPi-server (1).png
*PDU-ESP8266-RPi-server (1).png (42.21 kB . 800x623 - viewed 244 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 219 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

Servo Calibrator by BaldwinK
[Today at 10:20:42 AM]

MKS Gen L 1.0 by jinx
[Today at 05:09:27 AM]

ESP32 "Life is Life" 64x32 flexible P4 RGB LED by jinx
[May 18, 2019, 11:19:44 AM]

Warco HV-4" Rotary Table by jinx
[May 08, 2019, 03:12:58 PM]

Warco WM14 milling machine by jinx
[April 28, 2019, 09:41:40 AM]

MKS GEN L Servo Issue by Reywas
[April 22, 2019, 09:10:08 PM]

Meowth Abandoned by Nemesis
[April 11, 2019, 11:08:00 AM]

Bench Power: Help a rebel out by jinx
[April 03, 2019, 04:20:10 AM]

Just a short intro... by MEgg
[March 18, 2019, 05:49:28 PM]

Yes I am alive! by jinx
[March 10, 2019, 04:38:53 AM]

CZ-1 2.0: problems, calibration and printing, part2 by MEgg
[February 26, 2019, 03:35:15 PM]

Hi, Mushroom glue here. by nschreiber0813
[February 25, 2019, 02:09:13 PM]

Arduino IK by BaldwinK
[February 25, 2019, 05:54:44 AM]

Raspberry Pi / Python eBook - free for the next 11 hours! by MEgg
[February 24, 2019, 09:49:43 AM]

Fun Fact by Impala
[February 13, 2019, 11:05:01 PM]

* Recent Posts

Servo Calibrator by BaldwinK
[Today at 10:20:42 AM]

Re: MKS Gen L 1.0 by jinx
[Today at 05:09:27 AM]

Re: MKS Gen L 1.0 by Bappi
[Today at 03:34:54 AM]

Re: MKS Gen L 1.0 by jinx
[Today at 02:28:17 AM]

Re: MKS Gen L 1.0 by Bappi
[May 20, 2019, 07:11:31 PM]

Re: MKS Gen L 1.0 by jinx
[May 20, 2019, 03:16:02 PM]

Re: MKS Gen L 1.0 by Bappi
[May 20, 2019, 02:53:00 PM]

Re: MKS Gen L 1.0 by jinx
[May 20, 2019, 03:31:49 AM]

Re: MKS Gen L 1.0 by Bappi
[May 19, 2019, 07:16:21 PM]

Re: MKS Gen L 1.0 by Bappi
[May 19, 2019, 04:04:47 PM]

Re: MKS Gen L 1.0 by jinx
[May 19, 2019, 08:50:41 AM]

Re: MKS Gen L 1.0 by Bappi
[May 18, 2019, 04:38:59 PM]

Re: MKS Gen L 1.0 by Bappi
[May 18, 2019, 04:25:37 PM]

Re: MKS Gen L 1.0 by jinx
[May 18, 2019, 04:10:22 PM]

Re: MKS Gen L 1.0 by Bappi
[May 18, 2019, 02:01:41 PM]