I am running a Python Script on a Raspberry Pi to get the data measured by a Smart Plug (voltage, current, energy, power). This data is being printed on the terminal of the Raspberry Pi, but what I need is to send it to the Microsoft Cloud.
I am a beginner in these topics so I need some help. How can I transfer this data "easily" into Microsoft Azure? Someone adviced me to use Node Red to call the values that are being printed in the terminal, and then transfer them to a databank in the Microsoft Azure Cloud. Is this doable? How can I start? I'm thankful for any tips cause I am very lost.
My Python Script is based on the original from BeardMonkey https://www.beardmonkey.eu/tplink/hs110/2017/11/21/collect-and-store-realtime-data-from-the-tp-link-hs110.html
This is the script that I am using:
import sys
import time
import socket
import json
import threading
from struct import *
def int_to_bytes(x):
return x.to_bytes((x.bit_length() + 7) // 8, 'big')
def int_from_bytes(xbytes):
return int.from_bytes(xbytes, 'big')
def encrypt(string):
key = 171
result = pack('>I', len(string))
for i in string:
a = key ^ i
key = a
result += int_to_bytes(a)
return result
def decrypt(string):
key = 171
result = b""
for i in string:
a = key ^ i
key = i
result += int_to_bytes(a)
return result
def send_hs_command(address, port, cmd):
data = b""
tcp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
tcp_sock.connect((address, port))
tcp_sock.send(encrypt(cmd))
data = tcp_sock.recv(2048)
except socket.error:
print(time.asctime( time.localtime(time.time()) ), "Socket closed.", file=sys.stderr)
finally:
tcp_sock.close()
return data
def run():
threading.Timer(5.0, run).start()
data = send_hs_command("192.168.40.101", 9999, b'{"emeter":{"get_realtime":{}}}')
if not data:
print(time.asctime(time.localtime(time.time()) ), "No data returned on power request.", file=sys.stderr)
return
decrypted_data = decrypt(data[4:]).decode()
#print("DECRYPTED DATA", '\n',decrypted_data,'\n' )
json_data = json.loads(decrypted_data)
#print("JSON DATA", '\n',json_data,'\n' )
emeter = json_data["emeter"]["get_realtime"]
#print("RAW METER", '\n',emeter,'\n' )
print("SMART PLUG DATA - ",time.asctime( time.localtime(time.time()) ),'\n', "Current: ", emeter["current_ma"],"mA",'\n'\
,"Voltage: ",emeter["voltage_mv"], "mV", '\n',"Power: ", emeter["power_mw"], "mW",'\n', "Total Energy: ",\
emeter["total_wh"], "Wh", '\n\n')
if not emeter:
print("No emeter data returned on power request.", file=sys.stderr)
return
run()