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 786 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 111 times)

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

SoloLearn by Bajdi
[March 16, 2018, 10:15:14 AM]

Tiny function generator by ossipee
[March 14, 2018, 05:03:05 PM]

MKS GEN L Servo Issue by jinx
[March 06, 2018, 03:58:34 AM]

ROS Breakthrough by maelh
[March 05, 2018, 08:15:40 PM]

Post your last purchase. by Impala
[March 05, 2018, 02:47:58 PM]

MKS Gen L 1.0 by jinx
[March 04, 2018, 08:41:28 AM]

Robots & Lasers by ZeroMax
[February 23, 2018, 07:58:33 PM]

Hello from HITBOT - a young robot arm team by jinx
[February 22, 2018, 02:26:52 AM]

Assemble by Santiago
[February 06, 2018, 02:40:47 AM]

Ebot SL by erco
[January 30, 2018, 07:51:34 PM]

Simple One-Servo Walker by erco
[January 28, 2018, 09:00:22 PM]

Bob's gone Wood by erco
[January 28, 2018, 08:57:31 PM]

Welcome to the model section by jinx
[January 20, 2018, 10:11:40 AM]

syncthing - true p2p directory sync by mogul
[January 18, 2018, 03:21:49 PM]

Youtube by Impala
[January 18, 2018, 01:36:26 PM]

* Recent Posts

Re: SoloLearn by Bajdi
[March 16, 2018, 10:15:14 AM]

Tiny function generator by ossipee
[March 14, 2018, 05:03:05 PM]

SoloLearn by jinx
[March 09, 2018, 06:52:23 AM]

Re: MKS GEN L Servo Issue by jinx
[March 06, 2018, 03:58:34 AM]

Re: ROS Breakthrough by maelh
[March 05, 2018, 08:15:40 PM]

Re: MKS GEN L Servo Issue by Smashley
[March 05, 2018, 05:59:36 PM]

Re: MKS GEN L Servo Issue by huwmcgregor
[March 05, 2018, 05:14:39 PM]

Re: Post your last purchase. by Impala
[March 05, 2018, 02:47:58 PM]

Re: Post your last purchase. by ossipee
[March 05, 2018, 10:52:56 AM]

Re: Post your last purchase. by Impala
[March 04, 2018, 10:27:22 PM]

Re: Post your last purchase. by ZeroMax
[March 04, 2018, 09:00:27 PM]

Re: Post your last purchase. by jinx
[March 04, 2018, 08:48:25 AM]

Re: MKS Gen L 1.0 by jinx
[March 04, 2018, 08:41:28 AM]

Re: Post your last purchase. by Impala
[March 04, 2018, 08:23:48 AM]

Re: MKS Gen L 1.0 by cristi.istrate
[March 04, 2018, 07:48:59 AM]