Building IoT enabled power-strip with MicroPython and NodeMCU
This was on my TODO list for a long time. But, never managed to time to start working on it, I was also kind of scared of doing the AC wiring without adult supervision :).
Items used
- Power-strip
- USB power plug (any standard mobile phone charger)
- wires
- NodeMCU Amica
- Relay board
- MicroPython
- Mosquitto server on my home network
I ordered double relay boards (this one was marked for Arduino) from Amazon, and they were laying in the boxes in the Pune Hackerspace for a long time.
Yesterday, we had a Raspberry Pi workshop in the hackerspace as part of the Python Pune monthly meetup. Nikhil was present in the meetup, and I asked for help from him as he is a real hardware expert.
We took one of the existing power-strip from the hackerspace, and also a mobile phone charger. After taking out 2 of the power sockets we had enough space to plug-in the rest of the system inside of it. Of course, Nikhil did all the hard work of soldering the wires in the proper manner.
The relay board is connected to a NodeMCU Amica running MicroPython. It has a code like the following example:
import time
from machine import Pin
from umqtt.simple import MQTTClient
# Received messages from subscriptions will be delivered to this callback
def sub_cb(topic, msg):
led1 = Pin(14,Pin.OUT)
if msg == b"on_msg":
led1.low()
elif msg == b"off_msg":
led1.high()
def main(server="SERVER_IP"):
c = MQTTClient("umqtt_client", server)
c.set_callback(sub_cb)
c.connect()
c.subscribe(b"your_topic")
while True:
c.wait_msg()
c.disconnect()
if __name__ == "__main__":
try:
time.sleep(10)
main()
except:
pass
I will have to cover up the holes with something, and also push the code to a proper repository. Meanwhile this was the first usable thing I made with help from friends in the Hackerspace Pune. Come and join us to have more fun and build new things.
Btw, remember to have a password protected mosquitto server :)