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 2234 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 199 times)

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

Web TIme API - by Gareth
[December 07, 2018, 04:34:37 PM]

API Directory by Gareth
[December 06, 2018, 03:34:16 PM]

The Original Prusa I3 Mk3 by tinhead
[November 30, 2018, 02:49:52 PM]

Email notifications by CraigCox
[November 28, 2018, 10:26:06 PM]

And another one. by CraigCox
[November 28, 2018, 10:17:54 PM]

MKS Gen L 1.0 by jinx
[November 28, 2018, 02:23:50 AM]

This kids a freaking genius by ossipee
[November 21, 2018, 04:55:40 PM]

Time of Flight >>> Rounded Corners !!! by Gareth
[November 15, 2018, 03:21:51 PM]

Double_ing up the "Time of Flight" sensors by Gareth
[November 13, 2018, 04:05:02 AM]

Point cloud anyone !!! maybe 3D scanner by maelh
[November 11, 2018, 05:38:33 AM]

removable magnetic print surface by Gareth
[November 09, 2018, 10:53:45 AM]

Robot Cop by Valeriex
[November 07, 2018, 05:48:20 AM]

it's lukeyes by Valeriex
[November 07, 2018, 05:47:16 AM]

TOF gets a local DEBUG screen by Gareth
[November 07, 2018, 03:47:29 AM]

BlueTooth elves choose "BlueTeeth" by Gareth
[November 07, 2018, 03:40:19 AM]

* Recent Posts

Web TIme API - by Gareth
[December 07, 2018, 04:34:37 PM]

API Directory by Gareth
[December 06, 2018, 03:34:16 PM]

Re: The Original Prusa I3 Mk3 by tinhead
[November 30, 2018, 02:49:52 PM]

Re: The Original Prusa I3 Mk3 by Bajdi
[November 30, 2018, 02:26:48 PM]

Re: The Original Prusa I3 Mk3 by tinhead
[November 30, 2018, 04:38:53 AM]

Re: Email notifications by CraigCox
[November 28, 2018, 10:26:06 PM]

Re: And another one. by CraigCox
[November 28, 2018, 10:17:54 PM]

Re: MKS Gen L 1.0 by jinx
[November 28, 2018, 02:23:50 AM]

Re: MKS Gen L 1.0 by Prokktor
[November 27, 2018, 08:35:38 AM]

Re: The Original Prusa I3 Mk3 by tinhead
[November 26, 2018, 02:42:26 PM]

Re: The Original Prusa I3 Mk3 by jinx
[November 26, 2018, 02:24:05 PM]

Re: The Original Prusa I3 Mk3 by Bajdi
[November 25, 2018, 07:54:21 AM]

Re: The Original Prusa I3 Mk3 by tinhead
[November 25, 2018, 06:11:22 AM]

Re: The Original Prusa I3 Mk3 by Bajdi
[November 25, 2018, 04:40:31 AM]

The Original Prusa I3 Mk3 by tinhead
[November 24, 2018, 12:02:51 PM]