Laptop Charging automation

  • A script that checks battery percentage and publishes “on or off” to a topic
  • Write a cron job to trigger the above script every 5 minutes
  • NodeMCU subscribes to the above topic and control plug using a relay module
  1. NodeMCU
  2. AC to DC convertor
  3. Relay Module
  4. Plug Point
  5. PVC box
  6. Indicator(optional)
  7. Wire
  8. Prototype board
  9. Wire connector
import random
import time
from paho.mqtt import client as mqtt_client
import certifi #For Ceritificate
import psutil # For Checking battery Percentage

## MQTT broker details here

broker = '<broker host here>'
port = <port here>
topic = <your topic here>
client_id = f'python-mqtt-{random.randint(0, 1000)}'
username = <username>
password = <password>
# .........................................................................
connected = False

max_threshold = 90 #maximum threshold where we need to off the supply
min_threshold = 30 ##minimum threshold where we need to off the supply

def on_connect(client, userdata, flags, rc):
if rc == 0:
global connected
connected = True
battery = psutil.sensors_battery()
message = "False"
if battery.power_plugged:
if battery.percent >= max_threshold:
message = "False"
message = "True"
if battery.percent <= min_threshold:
message = "True"
print("message is: ", message)
result = client.publish(topic, message)
print("Failed to connect, return code %d\n", rc)

def connect_mqtt():
client = mqtt_client.Client(client_id)
client.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(broker, port)
return client

def run():
client = connect_mqtt()
while not connected:
print("connecting ....")

if __name__ == '__main__':
import time
from umqttsimple import MQTTClient
import ubinascii
import machine
import network
import gc

gc.collect() # Collect Garbages

output_pin = machine.Pin(2, Pin.OUT) ## Setting pin 2(GPIO2 or D4) as output pin

ssid = <your ssid>
password = <your password>
mqtt_server = <mqtt host>

client_id = ubinascii.hexlify(machine.unique_id()) ## Generate Unique client id
station = network.WLAN(network.STA_IF) # Setting NodeMCU as Wifi Client
station.connect(ssid, password) # Trigger connection

while station.isconnected() == False:
## Wait until connected

def subscribe_callback(topic, msg):
"""Subscribe callback function when new message is send
This will be triggered
First parameter is the topic which message is recieved
second parameter is the message
if topic == b'socket':
if msg == b'True': # Set pin value to Low
output_pin.on() # Set pin value to High

def connect_and_subscribe():
global client_id, mqtt_server, topic_sub
#If you are using SSL then ssl=True and ssl_params must be passed else no need
client = MQTTClient(client_id, mqtt_server, user=<MQTT Username>, password=<MQTT password>,
ssl=True, ssl_params={"server_hostname": <MQTT Host>})
client.set_callback(subscribe_callback) #Setting callback function for client
client.connect() #Trigger connection
client.subscribe("socket") # Subscribe to a top socket
return client

def restart_and_reconnect():
# When nodemcu get stuck use this function to reset
print('Failed to connect to MQTT broker. Reconnecting...')
machine.reset() # Reset machine

client = connect_and_subscribe()
except OSError as e:

while True:
client.check_msg() #Check the message in infinite loop
except OSError as e:



Coding, Reading,Python

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store