generating test data and sending it to the azure iot cloud using python

769 views Asked by At

I have to generate test data periodically like every 6 minute and send it to azure iot cloud using python, And use that data to manipulate or control my other device both physical(esp8266 or arduino) and software one. i actually have very little bit of experiance in programming and very new to azure cloud. i don't how to address above problem solution ,i tried using sdk but but unable to excute. please guide me.

I have open free azure cloud and create iot hub also create devices But as you see i only know basic programming i am unable to modify python sdk for azure iot to generate sample data and make connection with azure IOT hub.

step1:generate test data and send to the azure IOT cloud (or azure device maybe).

step2:route that data to the another device to anyalyze and monitoring like certain app.

step3:control esp8266 or arduino using that test data.

in above i succeed to establish connection between azure IOT device and esp8266(only connection though).but stuck in first two steps.

if anybody have another aprraoch please tell me.

thanks in advance.

2

There are 2 answers

11
Sampath On

I followed the below steps to send data as message from python (local System) to IoT Device (Cloud) and Receive data from IoT Hub Device (Cloud) a to Python (local System).

enter image description here

  • In python


from  azure.iot.device  import  IoTHubDeviceClient, Message

import  random

 

CONNECTION_STRING = "YourDeviceConnectionString"

MESSAGE_TEMPLATE = "boogie_{}={}"

 

# create an instance of the device client using the connection string

device_client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)

 

# connect the device client

device_client.connect()

 

# send 12 "boogie" messages with random numbers

for  i  in  range(1, 13):

value = random.randint(0, 150)

message = Message(MESSAGE_TEMPLATE.format(i, value))

device_client.send_message(message)

print("Sent message: " + MESSAGE_TEMPLATE.format(i, value))

 

# receive and print the messages

while  True:

message = device_client.receive_message()

if  message  is  None:

continue

print("Received message: {}".format(message.data))

 

# disconnect the device client

device_client.disconnect()

For a physical devices like esp8266 Refer to this link.

  • Replace the connection device connection string and run the code.

enter image description here

enter image description here

2
LeelaRajesh_Sayana On

It is unclear what approach you have for your step 2. We can route the data from Azure IoT Hub to Event Hub and create a .Net console application which receives and processes the event data.

We can then send the data from the .Net console application to ESP8266 through MQTT. There are many available options available to choose for the MQTT broker. I have outlined the detailed instructions in the article Control NodeMCU ESP8266 device through Azure IoT Hub as it is too complex to fit it in the SO thread. I have used HiveMQ as MQTT provider. You can explore other options as well.

Note that this is one of the approaches to address this scenario and this flow can be achieved through different means such as Azure functions in place of .Net console application.

Please find the below images denoting simulated data, .Net client application data and the MQTT subscribed data received by ESP8266 board respectively.

enter image description here

enter image description here

enter image description here

Hope this helps!