I created a room temperature control automation system project based on people's presence using Azure Custom Vision and Raspberry Pi. I have successfully sent DHT11 temperature sensor data, camera sensors for the number of people in the image to Azure IoT Hub. The next step is how to get data from Azure IoT Hub into Azure Functions and add data to the local database. After that, you can also perform on/off actions on the device being monitored.
My expectation is that after the data can be received by Azure IoT Hub with proof of data in the telemetry message, the data can also be received by Azure Functions, the data can be added to the local database, and with this data Azure Functions can perform actions in the form of on/off to the device monitored.
I have created an event hub, event in azure-iot-hub and i've also created a function from local vscode so that the values from the tool can be entered into the my local database. My problem was when doing the create an Azure Functions project step and in point number 4. Run the following command to create a Functions app in this folder. Link reference: https://github.com/microsoft/IoT-For-Beginners/tree/main/2-farm/lessons/5-migrate-application-to-the-cloud#create-a-serverless-application
My program has an error that can't run as shown in the following image. error azure function still error like this. error azure function 2
With Event Hub trigger, we can obtain data from the Azure IoT Hub. This function triggers a response to an event sent to an Event Hub event stream.
import logging
from azure.functions import EventHubEvent
from typing import List
def main(events: List[EventHubEvent]):
for event in events:
logging.info('Python EventHub trigger processed an event: %s',
event.get_body().decode('utf-8'))
Create an Azure Event Hubs trigger for Azure Functions using Visual Studio Code with the steps below:
local.settings.json
in the AzureWebJobsStorage Value.Code with local MySQL:
from typing import List
import json
import logging
import pymysql
from azure.functions import EventHubEvent
# MySQL connection details
MYSQL_HOST = '127.0.0.1'
MYSQL_USER = 'root'
MYSQL_PASSWORD = 'Password'
MYSQL_DB = 'quickstartdb'
def create_table_if_not_exists(cursor):
# Define your table schema here
table_schema = """
CREATE TABLE IF NOT EXISTS event_data2 (
id INT AUTO_INCREMENT PRIMARY KEY,
event_data JSON
)
"""
cursor.execute(table_schema)
def insert_event_data(cursor, event_data):
# Insert JSON event data into the table
insert_query = "INSERT INTO event_data2 (event_data) VALUES (%s)"
cursor.execute(insert_query, (json.dumps(event_data),))
def main(events: List[EventHubEvent]):
# Create a MySQL connection
connection = pymysql.connect(
host=MYSQL_HOST,
user=MYSQL_USER,
password=MYSQL_PASSWORD,
db=MYSQL_DB,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
try:
with connection.cursor() as cursor:
# Create table if not exists
create_table_if_not_exists(cursor)
# Process each event
for event in events:
event_data = json.loads(event.get_body().decode('utf-8'))
# Log the event data
logging.info('Python EventHub trigger processed an event: %s', event_data)
# Insert JSON event data into MySQL
insert_event_data(cursor, event_data)
# Commit the changes
connection.commit()
finally:
# Close the MySQL connection
connection.close()
Function Logs:
MySQL:
Another method: