# Transfer data from Python Script to the Cloud using Node Red

**URL:** <https://discourse.nodered.org/t/transfer-data-from-python-script-to-the-cloud-using-node-red/9734>\
**Category:** General\
**Created:** [3 April 2019 14:04 UTC](https://discourse.nodered.org/t/transfer-data-from-python-script-to-the-cloud-using-node-red/9734 "2019-04-03T14:04:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![xoani](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xoani/32/7844_2.png) [@xoani](https://discourse.nodered.org/u/xoani)\
**Post date:** [3 April 2019 14:04 UTC](https://discourse.nodered.org/t/transfer-data-from-python-script-to-the-cloud-using-node-red/9734/1 "2019-04-03T14:04:05Z")

</div>

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](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:

```auto
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()

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [3 April 2019 19:45 UTC](https://discourse.nodered.org/t/transfer-data-from-python-script-to-the-cloud-using-node-red/9734/2 "2019-04-03T19:45:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![xoani](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xoani/32/7844_2.png) [@xoani](https://discourse.nodered.org/u/xoani)\
**Post date:** [4 April 2019 07:05 UTC](https://discourse.nodered.org/t/transfer-data-from-python-script-to-the-cloud-using-node-red/9734/3 "2019-04-04T07:05:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [4 April 2019 07:07 UTC](https://discourse.nodered.org/t/transfer-data-from-python-script-to-the-cloud-using-node-red/9734/4 "2019-04-04T07:07:53Z")

</div>

What is you actual end goal with the data?

---

<div class="post-metadata">

**Author:** ![xoani](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xoani/32/7844_2.png) [@xoani](https://discourse.nodered.org/u/xoani)\
**Post date:** [4 April 2019 07:10 UTC](https://discourse.nodered.org/t/transfer-data-from-python-script-to-the-cloud-using-node-red/9734/5 "2019-04-04T07:10:56Z")

</div>

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 "hello.py". 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: hello.py 1554361096644  
/bin/sh: 1: hello.py: not found

...

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [4 April 2019 07:22 UTC](https://discourse.nodered.org/t/transfer-data-from-python-script-to-the-cloud-using-node-red/9734/6 "2019-04-04T07:22:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [4 April 2019 11:03 UTC](https://discourse.nodered.org/t/transfer-data-from-python-script-to-the-cloud-using-node-red/9734/7 "2019-04-04T11:03:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenioCarvalho](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/keniocarvalho/32/5620_2.png) [@KenioCarvalho](https://discourse.nodered.org/u/KenioCarvalho)\
**Post date:** [4 April 2019 12:34 UTC](https://discourse.nodered.org/t/transfer-data-from-python-script-to-the-cloud-using-node-red/9734/8 "2019-04-04T12:34:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dustinw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dustinw/32/3426_2.png) [@dustinw](https://discourse.nodered.org/u/dustinw)\
**Post date:** [4 April 2019 14:30 UTC](https://discourse.nodered.org/t/transfer-data-from-python-script-to-the-cloud-using-node-red/9734/9 "2019-04-04T14:30:30Z")

</div>

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 `hello.py: not found`).

---

<div class="post-metadata">

**Author:** ![xoani](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xoani/32/7844_2.png) [@xoani](https://discourse.nodered.org/u/xoani)\
**Post date:** [5 April 2019 06:54 UTC](https://discourse.nodered.org/t/transfer-data-from-python-script-to-the-cloud-using-node-red/9734/10 "2019-04-05T06:54:16Z")

</div>

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