Commit d8e047a8 authored by ajil.k's avatar ajil.k

added

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="accountSettings">
<option name="activeRegion" value="us-east-1" />
<option name="recentlyUsedRegions">
<list>
<option value="us-east-1" />
</list>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8Inspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="E712" />
</list>
</option>
</inspection_tool>
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N801" />
</list>
</option>
</inspection_tool>
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="dict.mobile" />
<option value="dict.dob" />
<option value="dict.gender" />
<option value="dict.name" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (azure_task)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/azure_task.iml" filepath="$PROJECT_DIR$/.idea/azure_task.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
import random
import time
from datetime import datetime
import paho.mqtt.client as mqtt
# Set the connection string for your device
iot_hub_name = "ajil"
device_id = "iot_device1"
sas_token = "SharedAccessSignature sr=ajil.azure-devices.net%2Fdevices%2Fiot_device1&sig=uJM96e2IYKU0%2FQb" \
"%2Bq9sJAKCLGvJc04oFKoj%2BtLtXIbc%3D&se=1677580978"
# Define the message that send to IoT Hub.
TEMPERATURE = 20.0
HUMIDITY = 60
def mqtt_client_init():
# Create an MQTT client
client = mqtt.Client(client_id=device_id, protocol=mqtt.MQTTv311, clean_session=False)
return client
def iot_hub_client_telemetry_run():
try:
# Create MQTT client
# Create an instance of the MQTT client
client = mqtt_client_init()
client.tls_set_context(context=None)
# Set the username and password for the MQTT client
username = "{0}.azure-devices.net/{1}/api-version=2018-06-30".format(iot_hub_name, device_id)
client.username_pw_set(username=username, password=sas_token)
# Connect to the IoT Hub using MQTT
client.connect(iot_hub_name + ".azure-devices.net", port=8883)
while True:
# Build the message with simulated telemetry values.
temperature = TEMPERATURE + (random.random() * 15)
humidity = HUMIDITY + (random.random() * 20)
epoch = datetime.now().timestamp()
message = '{{"epoch": {0}, "temperature": {1}, "humidity": {2}}}'.format(epoch, temperature, humidity)
# Send the message to the IoT Hub using MQTT
print("Sending message: {}".format(message))
client.publish("devices/{0}/messages/events/".format(device_id), payload=message, qos=1, retain=False)
print("Message successfully sent")
time.sleep(10)
except KeyboardInterrupt:
print("IoTHubClient stopped")
if __name__ == '__main__':
print("IoT Hub Quickstart #1 - Simulated device")
print("Press Ctrl-C to exit")
iot_hub_client_telemetry_run()
import random
from datetime import datetime
from pymodbus.client.sync import ModbusTcpClient
from azure.iot.device import IoTHubDeviceClient, Message
# Modbus TCP/IP connection settings
modbus_host = "192.168.1.1"
modbus_port = 502
modbus_slave_id = 1
modbus_address = 0 # start address to read from
modbus_count = 10 # number of registers to read
# Define the JSON message to send to IoT Hub.
TEMPERATURE = 20.0
HUMIDITY = 60
# Azure IoT Hub connection string and message settings
connection_string = "HostName=ajil.azure-devices.net;DeviceId=iot_device1;SharedAccessKey" \
"=dnHYnjY8RpQHtUlmeJAhygoOVdFX5xBNjgM/qm5FZ80="
temperature = TEMPERATURE + (random.random() * 15)
humidity = HUMIDITY + (random.random() * 20)
epoch = datetime.now().timestamp()
message = '{{"epoch": {0}, "temperature": {1}, "humidity": {2}}}'.format(epoch, temperature, humidity)
message = Message("hello from Modbus device")
# Create Modbus TCP client and connect to device
client = ModbusTcpClient(modbus_host, port=modbus_port)
client.connect()
# Read data from Modbus device
result = client.read_holding_registers(modbus_address, modbus_count, unit=modbus_slave_id)
# Create Azure IoT Hub client and connect to device
device_client = IoTHubDeviceClient.create_from_connection_string(connection_string)
device_client.connect()
# Send data to Azure IoT Hub
device_client.send_message(message)
# Disconnect from Modbus device and Azure IoT Hub
client.close()
device_client.disconnect()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment