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 312 times)


  • Hot glue gunslinger
  • Member
  • *
  • Posts: 141
  • 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 68 times)

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

Great selection by jinx
[August 18, 2017, 06:12:17 AM]

all metal hotend by jinx
[August 18, 2017, 06:08:36 AM]

stars by exploremore
[August 18, 2017, 01:15:44 AM]

PCB mill by ossipee
[August 17, 2017, 06:09:13 PM]

AGM by ossipee
[August 15, 2017, 06:03:19 PM]

Backlash by jinx
[August 14, 2017, 10:49:52 AM]

HF x2 mill un boxing by Impala
[August 13, 2017, 12:49:54 AM]

Re-writing robotics by PerceptualRobots
[August 11, 2017, 03:53:10 PM]

3D Printed Full size Humanoid by DangerousThing
[August 07, 2017, 02:09:23 AM]

Fun Fact by Impala
[August 06, 2017, 01:04:13 AM]

big vice by jinx
[August 04, 2017, 03:32:25 AM]

Cyclopis radiator by jinx
[August 03, 2017, 10:10:03 AM]

CAD learning by tinhead
[August 02, 2017, 03:03:50 AM]

lathe cutting off by jinx
[August 01, 2017, 11:05:54 AM]

HAD "Show us your Roboto" contest by Gareth
[July 31, 2017, 09:54:36 AM]

* Recent Posts

Re: Great selection by jinx
[August 18, 2017, 06:12:17 AM]

Re: all metal hotend by jinx
[August 18, 2017, 06:08:36 AM]

Re: stars by exploremore
[August 18, 2017, 01:15:44 AM]

Great selection by exploremore
[August 18, 2017, 01:04:16 AM]

PCB mill by ossipee
[August 17, 2017, 06:09:13 PM]

Re: all metal hotend by Bajdi
[August 17, 2017, 07:41:20 AM]

Re: AGM by ossipee
[August 15, 2017, 06:03:19 PM]

Re: AGM by danphobic_
[August 15, 2017, 05:03:46 PM]

Re: AGM by danphobic_
[August 15, 2017, 04:59:28 PM]

Re: AGM by jinx
[August 15, 2017, 03:12:15 AM]

Re: AGM by ossipee
[August 14, 2017, 07:19:51 PM]

Re: AGM by 1 what
[August 14, 2017, 06:59:36 PM]

Backlash by jinx
[August 14, 2017, 10:49:52 AM]

Re: AGM by jinx
[August 14, 2017, 05:30:58 AM]

Re: AGM by ossipee
[August 13, 2017, 07:10:01 PM]