-
Notifications
You must be signed in to change notification settings - Fork 0
/
uPy_powerstrip_part1.html
323 lines (303 loc) · 20.1 KB
/
uPy_powerstrip_part1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://tdt.rocks/uPy_powerstrip_part1.html" />
<title> TDT — WiFi Python PowerStrip (PART1 - The Hardware)
</title>
<link type="application/atom+xml" rel="alternate" href="/feed/atom.xml" title="TDT Atom Feed">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat" type="text/css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="http://tdt.rocks/theme/css/pygments.css">
<link rel="stylesheet" href="http://tdt.rocks/theme/css/style.css">
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-71190709-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-71190709-1');
</script>
<meta name="author" content="James Nzomo">
<meta name="description" content="A powerstrip that runs Python 3 and features IEEE 802.11 b/g/n Wi-Fi. Controllable via LAN and the Internet!">
<meta name="tags" contents="Hardware, MicroPython, ESP8266, electronics, ">
</head>
<body>
<header class="header">
<div class="container">
<div class="header-inner">
<!-- <h3 class="header-text">Thoughts & Deeds on Tech</h3> -->
<ul class="header-menu list-inline text-right">
<li><a class="nodec" href="http://tdt.rocks/categories.html">Categories</a></li>
<li class="muted">|</li>
<li><a class="nodec social-link icon-github" href="https://github.com/mrmoje" target="_blank"></a></li>
<li><a class="nodec social-link icon-twitter" href="https://twitter.com/mrmoje" target="_blank"></a></li>
<li><a class="nodec social-link icon-rss" href="http://tdt.rocks/feed/atom.xml" target="_blank"></a></li>
<li><a class="nodec social-link icon-stackoverflow" href="https://stackoverflow.com/users/1002644/moje" target="_blank"></a></li>
</ul>
<h1 class="header-name">
<a id="sitename" class="nodec" href="http://tdt.rocks">TDT</a>
</h1>
</div>
</div>
</header> <!-- /.header --> <div class="container">
<div class="post full-post">
<h1 class="post-title">
<a href="/uPy_powerstrip_part1.html" title="Permalink to WiFi Python PowerStrip (PART1 - The Hardware)">WiFi Python PowerStrip (PART1 - The Hardware)</a>
</h1>
<hr>
<ul class="list-inline">
<li class="post-date">
<a class="text-muted" href="/uPy_powerstrip_part1.html" title="2019-09-10T03:25:00+03:00">September 10, 2019</a>
</li>
<li class="muted">·</li>
<li class="post-category">
<a href="http://tdt.rocks/category/hardware.html">Hardware</a>
</li>
<li class="muted">·</li>
<li>
<a href="/tag/Hardware.html">Hardware</a>, <a href="/tag/MicroPython.html">MicroPython</a>, <a href="/tag/ESP8266.html">ESP8266</a>, <a href="/tag/electronics.html">electronics</a> </li>
<li class="muted">·</li>
<li>
<address class="post-author">
By <a href="http://tdt.rocks/author/james-nzomo.html">James Nzomo</a>
</address>
</li>
</ul>
<div class="post-content">
<h1>INTRO</h1>
<p>The powerstrip pictured below (fig.1) runs Python 3 and features IEEE 802.11 b/g/n Wi-Fi.
With the right python script(s) loaded, you can toggle each port over LAN and
with the right networking setup you can do the same, over the Internet from anywhere in the world
<img class="emoji" alt="sunglasses" height="20" width="20" style="display: inline;"
src="https://github.githubassets.com/images/icons/emoji/unicode/1f60e.png">!</p>
<p><img alt="THE SETUP" src="img/uPy_powerstrip/uPy_powerstrip.jpg" title="THE SHIZNIT">
<strong><em>fig.1 The shiznit.</em></strong></p>
<h1>Prelude</h1>
<p>Sometime late 2016, our Head of Dept put out an <a href="https://en.wikipedia.org/wiki/Request_for_proposal">RFP</a> for a python related
presentation/workshop that we were tasked to come up with and having hobbied a bit with MicroPython on
<a href="https://en.wikipedia.org/wiki/System_on_a_chip">SoCs</a>, I thought I'd field something on the topic.
We thus came up with a lightshow of desklamps toggled by rudimentary prototype-esque hardware all
controlled by Python. During the presentation session, after taking them through MicroPython
internals, the participants had a bit of fun scripting arbitrary light patterns/sequences in python.
Informative and fun.</p>
<p>Few months later, we ran a remix of the session for our <a href="https://twitter.com/nairobitechweek/status/844512757219295233">MOB Programming Workshop</a>
at <a href="http://nairobitechweek.com/">Nairobi Tech Week 2017</a>, toggling connected desklamps by tweeted hashtags and
again 2 years later, for one of our <a href="https://www.meetup.com/Python-Nairobi/events/cqbkrqyzhbxb/">Python-Nairobi Meetups</a> with refined hardware
(Pictured in fig.1 above), which I'll discuss here.</p>
<p>If you're keen to checkout the schematics, <a href="https://easyeda.com/editor#id=cc7ad7dc7a6d46a1a5b45dc8515a1653">click here for the EASYEDA_PROJECT</a>.
Plenty of context therein for this article as I'll be discussing the main components that make it work below.</p>
<p>But first.....</p>
<h1>Some words of caution</h1>
<p><center>
<img alt="JITAHADHARI" src="img/uPy_powerstrip/tahadhari.jpg" title="HIGH VOLTAGE WARNINGS">
</center></p>
<p>This article describes the design and construction of the above described gizmo for
the sole purpose of demonstrating what's possible with off the shelf tech described herein.</p>
<p>The concepts and design behind the subject device are of hobby quality. They have not been
fully thought through and do not adhere to any safety standards.
The device is neither suitable nor intended for any use ("production" or otherwise)
apart from illustrating what's possible.</p>
<p>This project involves working with mains electricity. This can be fatal!
Replicate at your own risk! And if you do, make it under the supervision of an expert in the field
or electronics and or cardiopulmonary resuscitation (CPR).
<center>
(This line will be "<code>s/f/r/g</code>" with a link to a safer design, sans all hi voltage stuff.<br>Keep your eyes peeled)
</center></p>
<p>Now that that's out of the way, lets talk about the thing from the heart of it all the way up to the mains.</p>
<h1>The Heart of it - ESP12</h1>
<p>The <a href="https://www.esp8266.com/wiki/doku.php?id=esp8266-module-family#esp-12">ESP12</a> (fig.2 below) is really a ESP8266EX nicely soldered onto a 24x16mm breakout board with a
PCB trace WiFi antenna, oscillator, SPI flash memory and a few passives all tucked away under
a nice square FCC approved RFI shield. I got the AI-Thinker ones. <a href="https://wiki.ai-thinker.com/_media/esp8266/a014ps01.pdf">Checkout their datasheet</a></p>
<p><img alt="ESP12_PIC" src="img/uPy_powerstrip/esp12.jpg" title="ESP12">
<strong><em>fig.2 ESP12 in schematic (left). Example AI Thinker ESP-12 PCB right.</em></strong></p>
<p>At the core of it is the mentioned <a href="https://www.espressif.com/products/hardware/esp8266ex/overview/">ESP8266EX</a> (fig.3 below), a system-on-chip (SoC) ic that integrates (among other things):-</p>
<ul>
<li>a Tensilica L106 32-bit RISC processor clocked at 80MHz (capable of up to 160MHz if it left the factory defect free)</li>
<li>80kB of ram but you only get to work with max 50kB for your data.</li>
<li>a 2.4 GHz transceiver radio for Wi-Fi</li>
<li>and many other things:-<a href="https://www.espressif.com/sites/default/files/documentation/0a-esp8266ex_datasheet_en.pdf">checkout the datasheet</a>.</li>
</ul>
<p><img alt="ESP8266_PIC" src="img/uPy_powerstrip/esp8266_esp3.png" title="ESP8266 on an ESP-03 PCB">
<strong><em>fig.3 ESP8266EX (circled in green) on an ESP-03 PCB. USB male connector to the left for size comparison</em></strong></p>
<p>Meager as it sounds, this SoC is beefy enough to run its own <a href="https://micropython.org">MicroPython</a> port
that provides a python3 interface to all the features within including Wi-Fi and all usable
11 GPIO pins, 4 of which we will use to control each outlet on our powerstrip.
(The 4 being GPIOs 12, 13, 14 and 16, see schematic in fig.2 above)</p>
<p>Operation is simple:- pull any of those 4 pins high (from code) and the respective outlet
gets toggled on via a Solid State Relay (SSR from here on).
The outcome of reverse is implicit, pull a pin low, off goes the corresponding outlet.</p>
<h1>Controlling mains - The SSR</h1>
<p>To switch the live pin on each outlet, I decided to go with the first capable
(and affordable) SSR I could find on <a href="https://store.nerokas.co.ke/index.php?route=product/product&product_id=1886">Nerokas</a>:- the <a href="https://store.nerokas.co.ke/index.php?route=product/product&product_id=1886">G3MB-202P</a>,
one for each outlet.
NB:- It's now discontinued. Successor as at time of this writing is OMRON's G3MC series PCB SSR.
Other manufacturers offer good replacements.</p>
<p><img alt="G3MB-202P_PIC" src="img/uPy_powerstrip/G3MB-202P_plus_schematic.jpg" title="G3MB-202P and Typical ssr schematic">
<strong><em>fig.4 Left:- OMRON G3MB-202P SSR. Right:- schematic of typical SSR</em></strong></p>
<p>Coming in a nice compact 20x24.5x5.5mm package and capable of max 2 Amps thru the high voltage side,
It's more than good enough for the job since we are only going to switch 240V 3-Watt LED lamps with it.
<strong><em>(For the murrcans, mm here = millimeters. For moonlanding units, divide by 25.4. Also, Apollo used SI units for the critical stuff)</em></strong></p>
<p>Another nice thing is that it provides opto-isolation out of the box. Yaani the low and high voltage side
are electronically isolated by an <a href="https://en.wikipedia.org/wiki/Opto-isolator">optocoupler</a> and all it asks for is 5V to power
its internal IR LED.</p>
<p>Operation is simple, supply 5V to pins <code>3(+)</code> & <code>4(-)</code>, and the HV side, pins <code>1</code> and <code>2</code>, complete the AC circuit for you.
Very oversimplified SSR operation but I'd like to keep the article simple. If you want more detail on how
the thing operates, <a href="https://www.ia.omron.com/data_pdf/guide/18/ssr_tg_e_9_2.pdf">check this guide out</a>. Also, good thing to keep in mind
a few <a href="https://omronfs.omron.com/en_US/ecb/products/pdf/precautions_ssr.pdf">PRECAUTIONS</a> for the component. (useful should you decide to replicate)</p>
<p>There is one small prollem however, our ESP12 will only put out 3.3V from any of the said 4 GPIO pins
which is insufficient to switch on our SSR. When any of those GPIO pins go high at 3.3V, we need to convert
that to a 5V input for the SSR to fully power on (see the datasheet). Thus we require a logic level
converter of sorts!</p>
<h1>Logic level conv - ESP12 => BC547 => SSR</h1>
<p>To carry out the 3.3V to 5V conversion, I chose a NPN transistor as a "low side" switch and settled
on the BC547 because I had plenty at home not to mention that it's also quite a capable component for the job.</p>
<p><img alt="BC547_G3MB-202P_PIC" src="img/uPy_powerstrip/BC547_G3MB-202P.jpg" title="BC547_G3MB-202P_SCHEMATIC">
<strong><em>fig.4 Left:- BC547 (Q4) in schematic as low side switch. Right:- A bunch of BC547s under a magnifying glass.</em></strong></p>
<p>Just to show it's a good fit, the chosen SSR's IR LED draws 10mA at 5V, which <a href="https://www.onsemi.com/pub/Collateral/BC546-D.PDF">(looking at the datasheet)</a>
is well below the BC547's <code>I<sub>c</sub>(max)</code> of 0.1A and <code>V<sub>ceo</sub>(max)</code> of 45V.
Also, the ESP8266 can supply max 12mA of current, which is more than the 1mA the BC547 requires for
<code>I<sub>b</sub></code> in order to supply the SSR's LED with the 10mA it needs <code>(I<sub>b</sub> = I<sub>c</sub>/10 = 1mA)</code>.</p>
<p>For good measure, lets "cap" that base current at 2.5mA like so:- When <code>I<sub>c</sub> = 10mA</code>, <code>V<sub>be</sub>(sat) = .72V</code>
and with the ESP supplying 3.3V on each GPIO pin we drop 2.58V with a resistor whose value will
be <code>R = V/I = 2.58V/0.0025A = 1032Ω</code>.
To make things simple, 1KΩ resistor would suffice - Brown-Black-Red and hopefully the 5%-Gold will take
care of the 32Ω change (or not).</p>
<h1>Power supply</h1>
<p>The power strip gets its supply from 240V mains (in my side of the world), but how do we light up the 5V SSR and 3.3V ESP12?
We could populate the PCB with a transformer and some rectification extras but we're pressed for space on
the board and in our enclosure.</p>
<p>Enter the <a href="http://www.hlktech.net/product_detail.php?ProId=54">HLK-PM01</a> (fig.5 below), an ultra-compact power module that comes in at 34mmx20mmx15mm.
It takes in 90 to 264V and supplies 5V at max 0.6A current with output noise of "i didn't check"/"not worried about it" decibels.</p>
<p><img alt="HLK_PM01" src="img/uPy_powerstrip/HLK_PM01_PIC.jpg" title="HLK_PM01_PIC">
<strong><em>fig.5 ultra-compact 5V power supply module.</em></strong></p>
<p>This is more than enough to run all the things on the board and then some, but just for fun let's look at ballpark calculations:-</p>
<ul>
<li>4x OMRON SSR Opto IR LED coming in at total 10mA x4 = 40mA</li>
<li>1x ESP12 at about 80mA average operating voltage (spikes can be taken care of by a CAP yes???)</li>
<li>4x ESP12 pin current draw at total 12mA x4 = 48mA</li>
</ul>
<p>So far we have 168mA. Add a very very generous 200mA for misc stuff on the board and you have 368mA...well within what the
HLK module can supply.</p>
<p>Finally for our 3.3V ESP12, we step that HLK's 5V down with a linear voltage regulator - the L78L33 which does max 100mA.
With a good enough CAP to sort out the ESP's current spikes, we have our rudimentary powersupply sorting all our electronics.</p>
<h1>The Enclosure</h1>
<p>One of the goals of this project was to have everything neatly tucked away into an off the shelf powerstrip enclosure.
At a local supermarket, I stumbled upon a <a href="https://sollatek.co.ke/shop/domestic-power-control/voltsafe/multiguard-mg-4u/">Solatek MG-4U</a> - a 4 port power strip (fig. 5 below)
with enough room for a 72mmx52mm PCB with 25mm height for components (once you chuck the 3 MoV surge protector in it)</p>
<p><center>
<img alt="MG4-4U" src="img/uPy_powerstrip/MG-4U.jpg" title="MG-4U">
<strong><em>fig.6 mg4u powerstrip.</em></strong>
</center></p>
<p>The strip has a nice rigid coper live rail that runs the length of all 4 ports. If you subdivide into 4 there will
still be enough plastic standoff to hold each port section sturdy-well.
On each of these pieces, there should be enough real estate to neatly solder a 16 AWG wire which we will connect
to the respective SSR via screw terminal.</p>
<p>With the earth and neutral rails left intact, supply the board with AC power on he screw terminal X3, routing live thru X3.2
and tapping neutral at X3.1.</p>
<h1>Outro</h1>
<ul>
<li>Checkout schematic on easyeda here:- <a href="https://easyeda.com/editor#id=cc7ad7dc7a6d46a1a5b45dc8515a1653">EASYEDA_PROJECT</a></li>
<li>Stay tuned for part 2 [3, [4, [5]]] where we look at interesting use cases (including voice control)</li>
</ul>
<p>In the meantime, here is a low res vidya of the thing running a python script that does a round robin toggle for all 4 ports</p>
<p><center>
<br>
<video height="320" controls>
<source src="vid/uPy_powerstrip/round_robin.mp4" type="video/mp4">
You seem to be from 1995. Browser does not support the video tag.
</video></p>
<p></center></p>
<p>Here's the script that does the above business:-</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22</pre></div></td><td class="code"><div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">machine</span>
<span class="kn">import</span> <span class="nn">utime</span>
<span class="n">pins</span> <span class="o">=</span> <span class="p">[</span><span class="n">machine</span><span class="o">.</span><span class="n">Pin</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">machine</span><span class="o">.</span><span class="n">Pin</span><span class="o">.</span><span class="n">OUT</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">13</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">16</span><span class="p">]]</span>
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">pins</span><span class="p">:</span>
<span class="n">p</span><span class="o">.</span><span class="n">off</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">onseq</span><span class="p">():</span>
<span class="k">for</span> <span class="n">pin</span> <span class="ow">in</span> <span class="n">pins</span><span class="p">:</span>
<span class="n">pin</span><span class="o">.</span><span class="n">on</span><span class="p">()</span>
<span class="n">utime</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">offseq</span><span class="p">():</span>
<span class="k">for</span> <span class="n">pin</span> <span class="ow">in</span> <span class="n">pins</span><span class="p">:</span>
<span class="n">pin</span><span class="o">.</span><span class="n">off</span><span class="p">()</span>
<span class="n">utime</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="k">while</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">onseq</span><span class="p">()</span>
<span class="n">offseq</span><span class="p">()</span>
</code></pre></div>
</td></tr></table>
</div>
</div>
<hr class="separator">
<div>
<div id="disqus_thread">
<script>
var disqus_shortname = 'tdtrocks';
(function() {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] ||
document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>
Please enable JavaScript to view the
<a href="https://disqus.com/?ref_noscript=tdtrocks">
comments powered by Disqus.
</a>
</noscript>
<a href="https://disqus.com" class="dsq-brlink">
blog comments powered by <span class="logo-disqus">Disqus</span>
</a>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<p class="text-center">
©Twenny Nineteen - James Nzomo, <a href="" target="_blank"></a> unless otherwise noted.
</p>
<div class="text-center">
Generated by <a href="http://getpelican.com" target="_blank">Pelican</a>
with a <a href="http://github.com/mrmoje/pelican-alchemy" target="_blank">copy-pasta</a>
of <a href="https://nairobilug.or.ke" target="_blank" >NairobiLUG's</a>
<a href="http://github.com/nairobilug/pelican-alchemy" target="_blank">alchemy</a> theme, <br/>
which inturn is a copy-pasta of Porter's <a href="https://github.com/porterjamesj/crowsfoot" target="_blank">crowsfoot</a> theme. <br/>
--- end of <a href="//tdt.rocks/pages/glossary#google-juice">google juice</a> ---.
</div>
</div>
</footer> <!-- /.footer -->
<script src="http://tdt.rocks/theme/js/jquery.min.js"></script>
<script src="http://tdt.rocks/theme/js/bootstrap.min.js"></script>
</body> <!-- 42 -->
</html>