简体   繁体   中英

Azure Event Hub - How to receive data from sensor via https?

I have a sensor that tracks people coming in and out of the building. In every event when someone comes in or out, the sensor sends the data via HTTPS to any URL or IP address I specify. If I set up my laptop as a webserver that listens for incoming traffic I can receive the data.

What I am trying to do is to go serverless and use Azure Event Hubs. I set up an event hub and run the test by pushing data from my laptop using a python script and I can successfully receive the data. Here is the sample code I used to test; I'm using the connection string to authenticate.

import time
import os
import uuid
import datetime
import random
import json

from azure.eventhub import EventHubProducerClient, EventData

# This script simulates the production of events for 10 devices.
devices = []
for x in range(0, 10):
    devices.append(str(uuid.uuid4()))

# Create a producer client to produce and publish events to the event hub.
producer = EventHubProducerClient.from_connection_string(conn_str="My End Point String", eventhub_name="my eventhub name")
for y in range(0,20):    # For each device, produce 20 events. 
    event_data_batch = producer.create_batch() # Create a batch. You will add events to the batch later. 
    for dev in devices:
        # Create a dummy reading.
        reading = {'id': dev, 'timestamp': str(datetime.datetime.utcnow()), 'uv': random.random(), 'temperature': random.randint(70, 100), 'humidity': random.randint(70, 100)}
        s = json.dumps(reading) # Convert the reading into a JSON string.
        event_data_batch.add(EventData(s)) # Add event data to the batch.
    producer.send_batch(event_data_batch) # Send the batch of events to the event hub.

# Close the producer.    
producer.close()

On my sensor, I don't have an option for these two things to enter. I only have the option to enter URL/IP.

Any idea how I can set this up?

I suggest moving data this way: Sensor > Azure Functions HTTP trigger > Azure Event Hubs

In summary, your sensor will call an HTTP endpoint hosted on Azure Functions and function will check and transform sensor data before forwarding it to an eventhub.

Here is documentation on HTTP trigger - https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=csharp

Here is the documentation on Event Hubs output binding - https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-hubs-output?tabs=csharp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM