1

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()
  • What in Azure? Azure is a huge platform, what service are you trying to use? Also a suggestion to use Node-RED is probably meant to totally replace the Python, not to combine the 2. – hardillb Apr 3 '19 at 13:49
  • I am not sure about that yet. Why do I have to replace the python? I have read that I can run the python script from Node-RED and get the outputs there. Am I wrong? – xoani Apr 4 '19 at 7:14
  • First, if you can't answer which Azure service you want to use then we can't help. Second, while there is a Node-RED Python function node that will just add unnecessary complication (you would have to rewrite the whole script anyway to make it work). You might as well just reimplement it all in native Node-RED, or find a npm node that already does this and wrap it as a Node-RED node. – hardillb Apr 4 '19 at 7:18
  • or an existing Node-RED node e.g. npmjs.com/package/node-red-contrib-tplink-iot – hardillb Apr 4 '19 at 7:25
  • Hi, thanks for your advice. Right now the python script already works on Node-RED. The ouput is in the message.payload. I am trying to store the message.payload in Blob Storage in Azure by using this node flows.nodered.org/node/node-red-contrib-azure-blob-storage . But I keep getting this error 4 Apr 11:47:22 - [info] [Save Blob:Azure Save Blob Storage] Error: ENOENT: no such file or directory, stat '556 Wh Do you know what this means? If I can't make it work like this I will try your approach of course. I just don't want to start from the beginning again without trying first. – xoani Apr 4 '19 at 9:50