0

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.

  • In Azure we find route in [messageroute]https://i.imgur.com/Bq7zcNM.png where we send to custom [EndPoint]https://i.imgur.com/s6mpep5.png Refer [MSDoc]https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-d2c – Sampath Mar 15 '23 at 10:48
  • i need to prior first step first i.e. generating random test data and send it to cloud periodically,for that i need modified python program sample. – Prathamesh Parit Mar 15 '23 at 12:41
  • Could tell what data do want to send ,do want to send it to Iot Hub device , do you want send as message to device ( or) send it as desired properties to device twin's . – Sampath Mar 15 '23 at 13:34
  • data format like "boogie_(X=1 to 12)=(random no between 0-150)" e.g. "boogie_1=47".and send it t IOT hub device as messege – Prathamesh Parit Mar 15 '23 at 16:35
  • don't know about device twin but that massage likely to json format and able to parse it – Prathamesh Parit Mar 15 '23 at 16:38
  • Hello, @Prathamesh Parit I was able to send data from the python to the IoT Hub device without Mqtt(physical device not connected). for physical devices Refer to this [link](https://pypi.org/project/paho-mqtt/). – Sampath Mar 15 '23 at 18:51
  • can you tell me how you establish connection between azure with your python code (simulator).and send data to iot hub device ,if possible please share source code – Prathamesh Parit Mar 16 '23 at 05:10
  • Ok, I will provide it as Sample(or) as a Reference for Connecting to Azure IoT Hub using python with any esp8266 – Sampath Mar 16 '23 at 05:12

2 Answers2

0

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

Sampath
  • 810
  • 2
  • 2
  • 13
  • ok i will try it – Prathamesh Parit Mar 18 '23 at 02:29
  • appreciate your help @Sampath but it give some errors in IDE, I use SDK code for connecting to azure iot hub device and send telemetry massege to the device from my local system in python [link](https://learn.microsoft.com/en-us/azure/iot-develop/quickstart-send-telemetry-iot-hub?toc=%2Fazure%2Fiot-hub%2Ftoc.json&bc=%2Fazure%2Fiot-hub%2Fbreadcrumb%2Ftoc.json&pivots=programming-language-python) , furtherless i have to modify code for type of test data i wanted to send perodically ,once again thanks – Prathamesh Parit Mar 18 '23 at 08:13
  • i can monitor telemetry data which is send by my local system to azure iot hub device but how do i route it now – Prathamesh Parit Mar 18 '23 at 12:03
  • for Creating and Deleting routes do have a chance to Refer to this [link](https://learn.microsoft.com/en-us/azure/iot-hub/how-to-routing-portal?tabs=eventhubs) – Sampath Mar 18 '23 at 15:30
  • i need quick suggestion, At which service i should route the data, I Have free Rs.15800 credits azure account.My main objective is:- **1.send that data to ESP8266 or interpret(Process) that data and control esp8266 pins**.**2.Store that data ,anyalyze it and optional one send it to app service.** – Prathamesh Parit Mar 19 '23 at 07:17
  • for first one which service I should use , in my opinion Service bus is good for first objective ,but for that i need to establish connection between service bus and esp8266. and for easy one i establish connection between esp 8266 and a device but i think azure does not provide device to other device routing masseging service.So instead of that i have to use (event hub or service bus or service queue or azure storage) what should i use via considering above objective. – Prathamesh Parit Mar 19 '23 at 07:18
  • Azure IoT Hub provides several options for routing device-to-cloud messages. You can route messages to built-in endpoints, such as Event Hubs, or custom endpoints, such as Service Bus Queues, Service Bus Topics, Azure Storage containers, and Cosmos DB. For your `first objective`, you can route the data to a Service Bus Queue or Service Bus Topic. Service Bus Queues and Topics are messaging services that provide a way to send and receive messages between devices and services. You can use the Service Bus Queue or Topic to receive the telemetry data from the IoT Hub and then send it to ESP8266. – Sampath Mar 20 '23 at 14:23
  • For your second objective, you can route the data to an Azure Storage container or Cosmos DB. Azure Storage is a scalable and highly available data storage solution, while Cosmos DB is a fully managed, globally distributed, multi-model database service. Both of these services can be used to store and analyze telemetry data. – Sampath Mar 20 '23 at 14:24
  • You can also route the data to an Event Hub, which is a highly scalable and highly available data streaming platform. Event Hubs can be used to fan out data to multiple subscribers, including Azure Stream Analytics, Power BI, and Azure Functions. You can choose the service that best fits your needs based on the features and capabilities that you require this [link]https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/iot-hub/iot-hub-devguide-messages-d2c.md [micrsoft]https://learn.microsoft.com/en-us/azure/iot-hub/quickstart-bicep-route-messages?tabs=CLI – Sampath Mar 20 '23 at 14:27
  • I need suggestion ,I route data service bus topic but from that I will try to send data to azure function which will process data and control esp8266, But I don't know how to send data to azure function , Process it and Connect esp8266 to azure function service to control esp8266? I think routing is easy but processing data and establish connection between azure function and esp8266 is not going to easy, So please guide me. – Prathamesh Parit Mar 26 '23 at 15:20
  • i did't find any documentation regarding azure function and esp8266 connection. Is there any other service which i can use to easily process data and control esp8266 by it – Prathamesh Parit Mar 27 '23 at 06:04
0

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!

  • thanks for your aproach although i am going to use other aproach i.e. route data to service bus topic and from that topic i will send it to azure storage and esp8266 – Prathamesh Parit Mar 26 '23 at 15:03
  • but it is crucial to send entire data to esp8266 ,i will process it in azure cloud itself and control esp8266 via it, for that i will consider your aproach or mostly i am going to azure function – Prathamesh Parit Mar 26 '23 at 15:07
  • @PrathameshParit If you decide to use a service bus topic, you can trigger an Azure function from it using the steps in the following [link](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-trigger?tabs=in-process%2Cextensionv5&pivots=programming-language-csharp) Please do not forget to upvote or accept the answer if it useful so that it would benefit other community members to find the solution – LeelaRajesh_Sayana Mar 29 '23 at 14:36