Transfer data from Python Script to the Cloud using Node Red

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

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)
        tcp_sock.connect((address, port))
        data = tcp_sock.recv(2048)
    except socket.error:        
        print(time.asctime( time.localtime(time.time()) ), "Socket closed.", file=sys.stderr)
    return data

def run():
    threading.Timer(5.0, run).start()

    data = send_hs_command("", 9999, b'{"emeter":{"get_realtime":{}}}')

    if not data:
        print(time.asctime(time.localtime(time.time()) ), "No data returned on power request.", file=sys.stderr)

    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)



Have you thought about trying one of the node-red tplink nodes? Go to the flows tab and search for uplink. I don't know if one will work or get the info you need, but that is the first thing I would try.

Hello, thank you! I will try that as my plan B. Right now I have a Python script that already runs in the Raspberry Pi, so I just want to get the values to Node-Red.

What is you actual end goal with the data?

I want to get the data to Node-RED so that I can later send it to a database in the Microsoft Azure Cloud.

I have now an inject node, then an exec node and a debug node. In the exec node I wrote the name of the file in the command line so "". The deployment works fine but I get this error when I inject:
4 Apr 08:58:16 - [info] [exec:Python Script] error:Error: Command failed: 1554361096644
/bin/sh: 1: not found


please don’t have the same issue in more than one thread it just wastes the time of the folk here who help out others.

If you like to stay with python script (I do this myself for various tasks) you should add support for MQTT and send the data to NR via MQTT. Once you have the data in NR and processed it, there are several nodes available for pushing it further to azure

You can send the information from python using MQTT or using an HTTP client on your python script and send a post or get call to Node Red

Hi @xoani ... based on your other post it looks like you figured out the solution to this question. You should put the solution in here so that this topic can be closed off (my guess is it was related to the not found).

The problem is that I wrote in the command line, and I should have written python