# Monotring industrial Robots via Azure Iot ###### tags: `ESP8266` `Socket` `Rasspberry Pi` `Azure IOT HUB` `Blob Storage` `Azure Function APP` `Azure Tabel Storage` `Power BI` > In all Factories we have machines or robots, these are the core of the factories, the more follow up for info from them , the better the performance. > That's why in this platform prototype , I'm Going to collect data from these robot ( Temperature and Vibration) Using Simulation code into Adafruit Feather HUZZAH with ESP8266 this code send the Robot ID , Related Related Temperature and related Vibration( we can add as much as we can of Robots and we can change the type of info to add beside Temp and Vib like up time , errors, etc... ). > These Info gonna send over TCP/IP Socket to a Raspberry Pi , or any PC running Python code attached into this tutorial. That will play the role of called it IoT Gateway, and that will save a cost and development time of this platform. > Once Info sent to Raspberry Pi , for IoT Gateway , The Gateway will collect the info according to Algorithm that we like to program , in real time ,and send the result of this Alogarthem based on Periodical messages , that we can set also, to Azure IoT Hub , that we reffer to it into Python code and Related "CONNECTION_STRING". > Onec the Message reach the IoT Hub , we creat a Listner for all these messages into out IoT Gateway so we validate the info has been sent.Actually we create Thread that take the messages ( Telementary) from cloud and print it into Console. > Once the Message is into Azure IoT Hub , we route them to : > 1- Blob Storage Using Message routing , > 2- Table Storage using Azure Function APP > 3- then download and process the data into remote computer. ![](https://i.imgur.com/RMb4tzq.png) > We are going to show how to do all this into this Tutorial. > Btw. Python , Javascript and C++ codes Made by recommended libraries from Microsoft and just adapted the Logic / Algorithm by us to be suitable for our platform. - Table of Content [ToC] ## :memo: Platform Requirements? ### Step 1: The Hardware - [x] Adafruit Feather HUZZAH with ESP8266, more info [Here](https://www.adafruit.com/product/2821) , and [here](https://randomnerdtutorials.com/how-to-install-esp8266-board-arduino-ide/) we find out how to connect to Arduino IDE ![](https://i.imgur.com/1BbftZ7.jpg) - [X] Raspberry Pi 4 , 4GB , that we can set up [here](https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up). ![](https://i.imgur.com/YBMqjPx.jpg) - [X] Microsoft Azure account , free or paid with min 50 USD.Here come [more info.](https://azure.microsoft.com/en-us/free/) ![](https://i.imgur.com/58i1u9T.png) - [ ] Optional Microsoft Power BI account , free or paid. Here Come [more info ](https://powerbi.microsoft.com/en-us/landing/signin/) ![](https://i.imgur.com/DYEVvS9.jpg) :rocket: ### Step 2: Socket / Robot Code in ESP8266 ```cpp= /* In all Factories we have machines or robots, these are the cor of the factories, the more follow up for info from them , the better the performance. That’s why in this platform prototype , I’m Going to collect data from these robot ( Temperature and Vibration) Using Simulation code into Adafruit Feather HUZZAH with ESP8266 this code send the Robot ID , Related Temp and Related Temperature and related Vibration( we can add as much as we can of Robots and we can change the type of info to add beside Temp and Vib like up time , errors, etc… ). These Info gonna send over TCP/IP Socket to a Raspberry Pi , or any PC running Python code attached into this tutorial. That will play the role of called it IoT Gateway, and that will save a cost and development time of this platform. Once Info sent to Raspberry Pi , for IoT Gateway , The Gateway will collect the info according to Algorithm that we like to program , in real time ,and send the result of this Alogarthem based on Periodical messages , that we can set also, to Azure IoT Hub , that we reffer to it into Python code and Related “CONNECTION_STRING”. Onec the Message reach the IoT Hub , we creat a Listner for all these messages into our IoT Gateway so we validate the info has been sent.Actually we create Thread that take the messages ( Telementary) from cloud and print it into Console. Once the Message is into Azure IoT Hub , we route them to : 1- Blob Storage 2- Table Storage using Azure Function APP 3- Power BI. We are going to show how to do all this into this Tutorial. Btw. Python , Javascript and C++ codes Made by recommended libraries from Microsoft and just adapted the Logic / Algorithm by us to be suitable for our platform. */ ///Autor : Ammar Alnahhas , ammar@dsna.se /// Date : 2021-02-06 #include <ESP8266WiFi.h> #include <Adafruit_GFX.h> //https://github.com/adafruit/Adafruit-GFX-Library #include <Adafruit_SSD1306.h> //https://github.com/adafruit/Adafruit_SSD1306 #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) // Reset pin # (or -1 if sharing Arduino reset pin) in ESPwe set LED_BUILTIN #define OLED_RESET LED_BUILTIN // Update these with values suitable for your network. const char* ssid = "*********"; // Your Home Network const char* password = "*********"; // Your Home Network Password // Our socket Server const char * Node_3_IP ="192.168.5.199"; //"192.168.5.108"; // Your Socket Listner and replier IP const int socket_port = 8888; // Our Example you can choose yours // Inti. Soket Server WiFiServer server(socket_port); //Init Clinet for Socket WiFiClient S_client;// = server.available(); // init. Adafruit SSD1306 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //My_Function bool setup_wifi(void); bool setup_Socket(void); void My_Socket_Writer(char cmd); void My_loop_Flash_text(const char *Text , int posX, int posY, int Font_Size); void My_loop_Flash_string(String Text , int posX, int posY, int Font_Size); void My_Socket(); void setup() { Serial.begin(115200); //set up OLED with I2c on addr0x3C if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32 Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } // Printing My Logo My_loop_Flash_text("DSNA" , 45,15,2); display.clearDisplay(); My_loop_Flash_text("Connecting to Wifi" , 0,10,1); display.clearDisplay(); setup_wifi(); } void loop() { // Here we Simulate the Temp nad Vibration and send them over soket to // IoT Gateway that send them to Azure IoT int temp= rand()%99; int Vib=rand()%350; String TMSG ="Temp/"; TMSG+=+"1/"; TMSG+=String(temp); // Send Temp My_Socket_Writer(TMSG); delay(5000); String VMSG ="Vib/"; VMSG+="1/"; VMSG+=String(Vib); // Send vibration My_Socket_Writer(VMSG); delay(5000); } bool setup_wifi() { delay(10); // We start by connecting to a WiFi network WiFi.mode(WIFI_STA); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); // Print on OLED My_loop_Flash_text("Connected to Wifi" , 0,10,1); String IP_addr = WiFi.localIP().toString(); My_loop_Flash_string( IP_addr, 16,10,1); display.clearDisplay(); return true; } //////////////////////////////////////////////////////// bool setup_Socket(){ // Set Up Socket Server server.begin(); Serial.print("Socket Server is connected to "); Serial.print(WiFi.localIP()); Serial.print(" on port "); Serial.println(socket_port); // Print on Oled My_loop_Flash_text("Socket Srv is READY" , 0,10,1); display.clearDisplay(); //My_loop_Flash_text("Telnet ip + 8888" , 0,10,1); //display.clearDisplay(); return true; } ///////////////////////////////////////////////////// void My_Socket_Writer(String cmd){// To Write Data to Socket // Once Socket Connected if(!S_client.connect(Node_3_IP,socket_port)) { Serial.println("Failed to Connect to 8888 socket ");// If Server not started // Write to oled My_loop_Flash_text("Failed Con.8888" , 0,10,1); display.clearDisplay(); } else{ Serial.println("Write->Read Socket Connected"); // Send the Message (Temp/ID/Temp Value) // Or (vib/ID/value) S_client.print(cmd); // Write to oled My_loop_Flash_string(cmd , 0,10,1); display.clearDisplay(); // DON'T Stop the Client(DOn't Disconnect the Socket) //until you get the Reply. //S_client.stop(); Serial.println("cmd sent"); } } ///////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// ////////////////////Oled Screen Function //////////////////////// /////////////////////////////////////////////////////////////////// void My_loop_Flash_text(const char *Text , int posX, int posY, int Font_Size) { display.clearDisplay(); display.setTextSize(Font_Size); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(posX, posY); // Start at top-left corner display.cp437(true); // Use full 256 char 'Code Page 437' font display.write(Text); display.display(); delay(2000); display.invertDisplay(true); delay(1000); display.invertDisplay(false); delay(1000); } void My_loop_Flash_string(String Text , int posX, int posY, int Font_Size) { display.clearDisplay(); display.setTextSize(Font_Size); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(posX, posY); // Start at top-left corner display.cp437(true); // Use full 256 char 'Code Page 437' font // Length (with one extra character for the null terminator) int str_len = Text.length() + 1; // Prepare the character array (the buffer) char char_array[str_len]; // Copy it over Text.toCharArray(char_array, str_len); display.write(char_array); display.display(); delay(2000); display.invertDisplay(true); delay(1000); display.invertDisplay(false); delay(1000); } void My_loop_Flash_Number(double My_Number , int posX, int posY, int Font_Size) { display.clearDisplay(); display.setTextSize(Font_Size); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(posX, posY); // Start at top-left corner display.cp437(true); // Use full 256 char 'Code Page 437' font display.println(My_Number); display.display(); delay(2000); display.invertDisplay(true); delay(1000); display.invertDisplay(false); delay(1000); } void My_loop_Flash_Number(int My_Number , int posX, int posY, int Font_Size) { display.clearDisplay(); display.setTextSize(Font_Size); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(posX, posY); // Start at top-left corner display.cp437(true); // Use full 256 char 'Code Page 437' font display.println(My_Number); display.display(); delay(2000); display.invertDisplay(true); delay(1000); display.invertDisplay(false); delay(1000); } void My_loop_Flash_Number_con (int My_Number , int posX, int posY, int Font_Size) { //display.clearDisplay(); display.setTextSize(Font_Size); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(posX, posY); // Start at top-left corner display.cp437(true); // Use full 256 char 'Code Page 437' font display.println(My_Number); display.display(); delay(2000); display.invertDisplay(true); delay(1000); display.invertDisplay(false); delay(1000); } void My_text(const char *Text , int posX, int posY, int Font_Size) { display.clearDisplay(); display.setTextSize(Font_Size); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(posX, posY); // Start at top-left corner display.cp437(true); // Use full 256 char 'Code Page 437' font display.write(Text); display.display(); delay(2000); } ``` ### Step 3: IoT Gateway RPI Python code: ``` Python= """ """ In all Factories we have machines or robots, these are the cor of the factories, the more follow up for info from them , the better the performance. That’s why in this platform prototype , I’m Going to collect data from these robot ( Temperature and Vibration) Using Simulation code into Adafruit Feather HUZZAH with ESP8266 this code send the Robot ID , Related Temp and Related Temperature and related Vibration( we can add as much as we can of Robots and we can change the type of info to add beside Temp and Vib like up time , errors, etc… ). These Info gonna send over TCP/IP Socket to a Raspberry Pi , or any PC running Python code attached into this tutorial. That will play the role of called it IoT Gateway, and that will save a cost and development time of this platform. Once Info sent to Raspberry Pi , for IoT Gateway , The Gateway will collect the info according to Algorithm that we like to program , in real time ,and send the result of this Logarithm based on Periodical messages , that we can set also, to Azure IoT Hub , that we refer to it into Python code and Related “CONNECTION_STRING”. Onec the Message reach the IoT Hub , we creat a Listener for all these messages into our IoT Gateway so we validate the info has been sent.Actually we create Thread that take the messages ( Telemetry) from cloud and print it into Console. Once the Message is into Azure IoT Hub , we route them to : 1- Blob Storage 2- Table Storage using Azure Function APP 3- Power BI. We are going to show how to do all this into this Tutorial. Btw. Python , Javascript and C++ codes Made by recommended libraries from Microsoft and just adapted the Logic / Algorithm by us to be suitable for our platform. ///Author : Ammar Alnahhas , ammar@dsna.se /// Date : 2021-02-06 ### Updated on 2020-02-07 to receive messages from Azure IoT Hub to my device multi-threading https://hackmd.io/@DSNA22/Monotring_industrial_Robots_via_Azure_Iot """ import socket import time import threading from azure.iot.device import IoTHubDeviceClient, Message from datetime import datetime # Using the Python Device SDK for IoT Hub: # https://github.com/Azure/azure-iot-sdk-python # The sample connects to a device-specific MQTT endpoint on your IoT Hub. # The device connection string to authenticate the device with your IoT hub. # Using the Azure CLI: # az iot hub device-identity show-connection-string --hub-name {YourIoTHubName} --device-id MyNodeDevice --output table CONNECTION_STRING = "HostName=IndustrialHub.azure-devices.net;DeviceId=DSNAGateway;SharedAccessKey=xk57jJ0kIo8RLjN7SBLGHuF3r8nv3yhinWr1q78p9x4=" # Define the JSON message to send to IoT Hub. ROBOT_NAME = "ROBOT#1" TEMPERATURE = 20.0 VIBRATION = 60 MSG_TXT = '{{"ROBOT_NAME" : {robot_name},"temperature": {temperature},"Vibration": {vibration}}}' RECEIVED_MESSAGES = 0 def iothub_client_init(): # Create an IoT Hub client client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING) return client # Editing Micrsoft code to take info as Parameter def iothub_client_telemetry_sample_run(client, rob_name, temp, vib): # Build the message robot_name = rob_name # temperature temperature = temp # vibration vibration = vib # Build the message msg_txt_formatted = MSG_TXT.format(robot_name=robot_name, temperature=temperature, vibration=vibration) message = Message(msg_txt_formatted) message.custom_properties["robot_name"] = robot_name message.custom_properties["temperature"] = temperature message.custom_properties["vibration"] = vibration # Add a custom application property to the message. # An IoT hub can filter on these properties without access to the message body. if temperature > 40: # over 40C message.custom_properties["temperatureAlert"] = "true" else: message.custom_properties["temperatureAlert"] = "false" # Vibration if vibration > 150: # Over 150Hz message.custom_properties["vibrationAlert"] = "true" else: message.custom_properties["vibrationAlert"] = "false" # Send the message. print(f"Sending Message {message}") # print("Sending message: {}".format(message)) client.send_message(message) print("Message successfully sent") def print_time_date(): dateTimeObj = datetime.now() print( f"[:::] {dateTimeObj.year}-{dateTimeObj.month}-{dateTimeObj.day}|{dateTimeObj.hour}:" f"{dateTimeObj.minute}:{dateTimeObj.second} ") def message_received_handler(message): print("the data in the message received was ") print(message.data) print("custom properties are") print(message.custom_properties) print("content Type: {0}".format(message.content_type)) print("") def message_listener(client): # set the mesage received handler on the client while True: client.on_message_received = message_received_handler time.sleep(1000) # receive 4096 bytes each time in the socket BUFFER_SIZE = 4096 if __name__ == '__main__': # Gateway receive from all IP addresses on port 8888 SERVER_HOST = "0.0.0.0" SERVER_PORT = 8888 # create the server socket # TCP socket s = socket.socket() # bind the socket to our local address s.bind((SERVER_HOST, SERVER_PORT)) # enabling our server to accept connections # 5 here is the number of unaccepted connections that # the system will allow before refusing new connections s.listen(5) # Average Temp and Vibration parameter avg_temp = 0 avg_vib = 0 # Azure IoT print("Connect to Azure IoT Hub") client = iothub_client_init() for point in range(5): print('* '), time.sleep(2) # Set Timer t = time.time() # Create Listener Thread : message_listener_thread = threading.Thread(target=message_listener, args=(client,)) message_listener_thread.daemon = True message_listener_thread.start() while 1: # update Timer : if time.time() - t > 30: # Second print_time_date() # Send message to Azure IoT Hub iothub_client_telemetry_sample_run(client, "ROBOT#1", avg_temp, avg_vib) # reset the timer t = time.time() else: pass print(f"[*] Socket server on Port :{SERVER_PORT}") client_socket, address = s.accept() # receive using client socket, not server socket while 1: received = client_socket.recv(4096).decode() l_rec = [] l_rec = received.split("/") print(f" Received from Robot # {l_rec[1]} this info : {received}") # print(l_rec) if l_rec[0] == 'Temp': # Calculating Average Temp avg_temp = (avg_temp + int(l_rec[2])) / 2 print(f"[**]The Average Temperature is {avg_temp}") elif l_rec[0] == 'Vib': # Calculating Average Temp avg_vib = (avg_vib + int(l_rec[2])) / 2 print(f"[**]The Average Vibration is {avg_vib}") # Once Received close the socket and break the loop client_socket.close() break ``` Here come a film that demostrate what happend between robots and IoT Gateway {%youtube 7gEuEDMwVmQ %} and so looks the output in Rasspbery PI terminal: ![](https://i.imgur.com/fImPw4n.jpg) ### Step 4: Azure IoT : Once we [created our Azure Account](https://azure.microsoft.com/en-us/free/) you need to create your first [Resource group](https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/manage-resource-groups-portal) , mine looks like this : > ![](https://i.imgur.com/wNOjmFm.jpg) we can start creating Azure IoT Hub as : #### 1- Save Telementary into Azure blob storage via Azure IoT Hub : IoT Hub is a service provided by Microsoft Azure that can be used to connect, provision and manage millions of IoT devices; communicating large amounts of data per month. IoT Hub acts as a bridge between devices and their solutions in the cloud, allowing them to store, analyze and act on data in real time. It enables secure and reliable bi-directional communication (from Device to Cloud [D2C] and from Cloud to Device [C2D]) over widely used IoT communication protocols such as HTTP, MQTT and AMQPS. We can Find more info about IoT Hub Documentation by Microsoft Azure [here](https://docs.microsoft.com/en-in/azure/iot-hub/) Creating an IoT Hub You can create an IoT Hub by following the steps given below : 1. Sign in to your Microsoft Azure account. 2. search : IoT Hub and click it out! >![](https://i.imgur.com/VsKxMcH.jpg) follow the steps into this [guide from microsoft](https://docs.microsoft.com/en-in/azure/iot-hub/iot-hub-create-through-portal), then yout IoT Hub will looks something like mine : >![](https://i.imgur.com/66RjCSz.jpg) 4- Shared access policies : into the same guide you'll find info how to create share policy 5- Also How to register a new device in the IoT hub and thierwill generate the connection string that you need into IoT Gatweay Python code ( Line 46). so be sure to document it. ![](https://i.imgur.com/F4U2ch4.jpg) **Once IoT Hub and Iot Device developt and running we need to create under same resource group a storage account and blob storage as** : Creating an Azure Storage Account 1- Search : storage and click it out! ![](https://i.imgur.com/vXWjVrt.jpg) 2- Select Storage Accounts and [follow this guide to create it](https://docs.microsoft.com/en-us/azure/storage/common/storage-account-create?tabs=azure-portal)! then the storage account will looks like this : ![](https://i.imgur.com/7IoFLNm.jpg) **Once Storage Created we gonna attached it as End Point to Out IOT Hub** Storage Account as Endpoint to IoT Hub 1. Go to your IoT Hub and click on Endpoints under Messaging. 2. Click on Add to add an Endpoint. 3. An Add Endpoint pane will open. Enter a name for the endpoint and select the type of endpoint as **Azure Storage Container**. More options will appear after selecting the type of endpoint as Storage Container. Till now we have IoT Hub + and Device connected to it ( Out IoT Gateway) + Storage attached to thi IoT Hub. **Now We route the data to our storage from IoT Hub as** : Adding Route for Routing Data 1. Go to your IoT Hub and click on Routes under Messaging. 2. Click on Add to add a Route. 3. A new pan named Create a new route will open. Enter a name for the route. Select Device Telementary as the Data Source.Select the endpoint that was just created as the Endpoint.Select Enable Rule as On.Enter true as the Query String. ![](https://i.imgur.com/e7ICPP0.jpg) 3.1 Click add endpoint : 3.1.1 Set the name for your endpoint. 3.1.2 Pick or Create the related container into your storage account. ![](https://i.imgur.com/mDqfWH9.jpg) 3.1.3 **Change Encoding to JSON** 3.1.4 Click Create 4. Click on Save. Now that we have set the route for routing the IoT Hub messages to the Storage Account, we can connect the application that sends data to the IoT Hub and check the Storage Account for the messages. Connect the ESP and start sending data to your IoT Gateway the to Azure IoT Hub. Open your Storage Container from the Storage Account on Microsoft Azure. (Storage Account will be accessible from the dashboard if you have pinned it to the dashboard)You will see a folder with the name of your IoT Hub.You will find files inside the folder and can download it from there. And our JSON Files in blobs look like : ![](https://i.imgur.com/Y2x0EXC.jpg) And JSON File looks Like: ```JSON= { "EnqueuedTimeUtc": "2021-02-08T22:23:23.4430000Z", "Properties": { "robot_name": "ROBOT#1", "temperature": "33.0", "temperatureAlert": "false", "vibration": "138.0", "vibrationAlert": "false" }, "SystemProperties": { "connectionDeviceId": "DSNAGateway", "connectionAuthMethod": "{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}", "connectionDeviceGenerationId": "637484073522275531", "enqueuedTime": "2021-02-08T22:23:23.4430000Z" }, "Body": "eyJST0JPVF9OQU1FIiA6IFJPQk9UIzEsInRlbXBlcmF0dXJlIjogMzMuMCwiVmlicmF0aW9uIjogMTM4LjB9" } ``` **Reading the Blobs into remote computer** and the code to download the latest blob and print out the robot related info is here : ``` python = ''' In all Factories we have machines or robots, these are the cor of the factories, the more follow up for info from them , the better the performance. That’s why in this platform prototype , I’m Going to collect data from these robot ( Temperature and Vibration) Using Simulation code into Adafruit Feather HUZZAH with ESP8266 this code send the Robot ID , Related Temp and Related Temperature and related Vibration( we can add as much as we can of Robots and we can change the type of info to add beside Temp and Vib like up time , errors, etc… ). These Info gonna send over TCP/IP Socket to a Raspberry Pi , or any PC running Python code attached into this tutorial. That will play the role of called it IoT Gateway, and that will save a cost and development time of this platform. Once Info sent to Raspberry Pi , for IoT Gateway , The Gateway will collect the info according to Algorithm that we like to program , in real time ,and send the result of this Logarithm based on Periodical messages , that we can set also, to Azure IoT Hub , that we refer to it into Python code and Related “CONNECTION_STRING”. Onec the Message reach the IoT Hub , we creat a Listener for all these messages into our IoT Gateway so we validate the info has been sent.Actually we create Thread that take the messages ( Telemetry) from cloud and print it into Console. Once the Message is into Azure IoT Hub , we route them to : 1- Blob Storage 2- Table Storage using Azure Function APP 3- Power BI. We are going to show how to do all this into this Tutorial. Btw. Python , Javascript and C++ codes Made by recommended libraries from Microsoft and just adapted the Logic / Algorithm by us to be suitable for our platform. ///Author : Ammar Alnahhas , ammar@dsna.se /// Date : 2021-02-06 ### Updated on 2020-02-07 to receive messages from Azure IoT Hub to my device multi-threading https://hackmd.io/@DSNA22/Monotring_industrial_Robots_via_Azure_Iot ''' import os, json,time,datetime from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__ try: #print("Azure Blob Storage v" + __version__ + " - Python quickstart sample") # Retrieve the connection string for use with the application. The storage # connection string is stored in an environment variable on the machine # running the application called AZURE_STORAGE_CONNECTION_STRING. If the environment variable is # created after the application is launched in a console or with Visual Studio, # the shell or application needs to be closed and reloaded to take the # environment variable into account. #in CMD Please Run : #setx AZURE_STORAGE_CONNECTION_STRING "<yourconnectionstring>" #Replace <yourconnectionstring> with your actual connection string. #After you add the environment variable in Windows, you must start a new instance of the command window. #guid : https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING') # Get current directory to hold blob data local_path = os.getcwd() local_file_name="dsna.txt" # this will carry the file name of last blob got from blob storage last_read_blob=" " while True: ## STEP 1 : GET the LAST BOLB #for debugging #print("\nListing blobs...") # Create the BlobServiceClient object which will be used to create a container client blob_service_client = BlobServiceClient.from_connection_string(connect_str) # Create a unique name for the container container_name = "dsnablobcontainer" # Create the container container_client = blob_service_client.get_container_client(container_name) blob_list = container_client.list_blobs() #Get latest json file in container last_jason_blob=" " for blob in blob_list: #for debugging #print("\t" + blob.name) last_json_blob =blob.name # to not process the same file if freq. of reading # was faster than the blob creation. if last_jason_blob == last_read_blob : #print(f" Current JSON file is {last_json_blob}") last_read_blob = last_jason_blob ## STEP 2 : Download the LAST BOLB # Create a blob client using the local file name as the name for the blob blob_client = blob_service_client.get_blob_client(container=container_name, blob=last_json_blob) # Add 'DOWNLOAD' before the .jason extension so you can see both files in the data directory download_file_path = os.path.join(local_path, str.replace(local_file_name ,'.txt', 'DOWNLOAD.txt')) #for debugging #print(download_file_path) # Download the blob to a local Json file #cprint("\nDownloading blob to \n\t" + download_file_path) with open(download_file_path, "wb") as download_file: download_file.write(blob_client.download_blob().readall()) with open(download_file_path, "r") as jsonfile : for line in jsonfile: #get each line (telementray) from jason file as dictionary telementary=json.loads(line) ## STEP 3 : Show Data From JSON #get proparties dictionary into sensor_data dictionary sensor_data= telementary.get('Properties') print(f"@:{telementary.get('EnqueuedTimeUtc')} ") print(f"Robot name: {sensor_data.get('robot_name')} ") print(f"Robot temp: {sensor_data.get('temperature')} °C") print(f"Robot Vib: {sensor_data.get('vibration')} Hz ") print("______________________________________") else : pass # Chech the blob storage every minute for new blob time.sleep(60) except Exception as ex: print('Exception:') print(ex) ``` and the related output : ![](https://i.imgur.com/K1LAWM4.jpg) #### 2- Save Telementary into Azure Table storage via Azure IoT Hub & Function app : Starting From the Same IoT Hub we ganna create an new Azure storage account and an Azure Function App to store IoT Hub messages in Azure table storage. to achive that we need to : 1. We creat Storage account as we did before. 2. Creat an Endpint that will be attached to Function app as trigger. 3. Create and deploy an Azure Function App 4. Get out messages into our Table Storage. We creat a new storage account as created before. ![](https://i.imgur.com/detOQme.jpg) Let's create an end point and Prepare for IoT Hub connection to read Telementary IoT Hub exposes a built-in Event Hub-compatible endpoint to enable applications to read IoT Hub messages. Meanwhile, applications use consumer groups to read data from IoT Hub. Before creating an Azure Function App to read data from your IoT hub, you need to: create a route to the Event hub , as we use special route to our Blob storage so the Buuilt in Endpoints is not active unless we creat a rout to it. >>>Each IoT hub comes with built-in system endpoints to handle system and device messages. When you create new endpoints and routes, messages stop flowing to the built-in endpoint unless you create a separate route and direct them there. Learn more ![](https://i.imgur.com/yPl1hZo.jpg) so if we need the both routes working we we creat a route to built in endpoint as next pix: ![](https://i.imgur.com/YOZq4kU.jpg) Then we create the route as before but we chose event this time: **(((Dont Forget to Disable route in Blob example)))** ![](https://i.imgur.com/t6wDOzK.jpg) Now we back to out IoT Hub and choos Built-in Endpoints adn document Event Hub-compatible name we gonna use it in function process. **Now we create our Consumer group into endpoint** Create a consumer group for your IoT hub 1. Open your IoT hub. 2. Click Endpoints. 3. Click Events under Built-in endpoints. 1. In the Properties pane, enter a name under Consumer groups and make a note of the name. 1. Click Save. ![](https://i.imgur.com/q0Ive9u.jpg) Now Our Trigger is ready , Let's Creat the Function APP. at home page : 1- Search Function as : ![](https://i.imgur.com/F4EnQL6.jpg) 2- Fill in the info : App name: The name of the Function App. The name must be globally unique. Resource group: Use the same resource group that your IoT Hub uses. Storage Account: The storage account that you created. 3- Click Create. 4- Open the Function App once it is created. 5-Create a new function in the Function App. 5-1 : Click New Function. 5-2 : Select JavaScript for Language, and Data Processing for Scenario. 5-3 : Click the EventHubTrigger-JavaScript template. 5-4 : Enter the necessary information for the template. **Name your function**: The name of the function. **Event Hub name**: The Event Hub-compatible name you noted down. **Event Hub connection**: Click new to add the connection string of your IoT hub endpoint that you made up. 5-5 : Click Create. ![](https://i.imgur.com/a60Gf6l.jpg) Now the input is ready let's set up Table storage as **output to our function** as : 1- Go to Click Integration > New Output > Select Azure Table Storage > Select. 2- Enter the necessary information. **Table parameter name**: Use *outputTable* for the name, which will be used in the Azure Functions' code. **Table name**: Use *deviceData* for the name. **Storage account connection**: Click new and select or input your storage account. 3- Click add. ![](https://i.imgur.com/tCYfcNv.jpg) ![](https://i.imgur.com/yytJpsH.jpg) so now **we add the trigger and function code** 1. Click Triggers, 2. Click Azure Event Hub (myEventHubTrigger). 3. Under Event Hub consumer group, **enter the name of the consumer group** that you created, and then click Save. 4. Click Code+Tese. 5. add your javascript to index.js with the following 6. click Test/Run, if OK then Save it. ![](https://i.imgur.com/gUbyn5A.jpg) **Verify your message in your table storage** Run the sample application on your device to send messages to your IoT hub. Download and install Microsoft Azure Storage Explorer. Open Microsoft Azure Storage Explorer, click Add an Azure Account > Sign in, and then sign in to your Azure account. Click your Azure subscription > Storage Accounts > your storage account > Tables > deviceData. You should see messages sent from your device to your IoT hub logged in the deviceData table. ![](https://i.imgur.com/GXKeAMQ.jpg) Now back to Remoe computer , surf to local working folder , start Power shell and create the next python script in file, then run it! ```python= ''' In all Factories we have machines or robots, these are the cor of the factories, the more follow up for info from them , the better the performance. That’s why in this platform prototype , I’m Going to collect data from these robot ( Temperature and Vibration) Using Simulation code into Adafruit Feather HUZZAH with ESP8266 this code send the Robot ID , Related Temp and Related Temperature and related Vibration( we can add as much as we can of Robots and we can change the type of info to add beside Temp and Vib like up time , errors, etc… ). These Info gonna send over TCP/IP Socket to a Raspberry Pi , or any PC running Python code attached into this tutorial. That will play the role of called it IoT Gateway, and that will save a cost and development time of this platform. Once Info sent to Raspberry Pi , for IoT Gateway , The Gateway will collect the info according to Algorithm that we like to program , in real time ,and send the result of this Logarithm based on Periodical messages , that we can set also, to Azure IoT Hub , that we refer to it into Python code and Related “CONNECTION_STRING”. Onec the Message reach the IoT Hub , we creat a Listener for all these messages into our IoT Gateway so we validate the info has been sent.Actually we create Thread that take the messages ( Telemetry) from cloud and print it into Console. Once the Message is into Azure IoT Hub , we route them to : 1- Blob Storage 2- Table Storage using Azure Function APP 3- Power BI. We are going to show how to do all this into this Tutorial. Btw. Python , Javascript and C++ codes Made by recommended libraries from Microsoft and just adapted the Logic / Algorithm by us to be suitable for our platform. ///Author : Ammar Alnahhas , ammar@dsna.se /// Date : 2021-02-12 https://hackmd.io/@DSNA22/Monotring_industrial_Robots_via_Azure_Iot ''' #https://docs.microsoft.com/en-us/python/api/azure-data-tables/azure.data.tables.tableserviceclient ## STEP 1 : Instantiate a table service client import time from azure.data.tables import TableClient with TableClient.from_connection_string('*******',table_name="IoTDeviceData") as table: try: print(" Fetching data from Table Storage account -dsna2table- and Table : -IoTDeviceData-") # [START query_entities] ## STEP 2 : Query the entities in the table to local List entities = list(table.list_entities()) ## STEP 3 : fill local dict. with enties data myEnt={} for i, entity in enumerate(entities): myEnt=entity ## STEP 4 : Print out data to consol from dict. print(f"@ {myEnt.get('EnqueuedTimeUtc')}") print(f"Robot Name : {myEnt.get('robot_name')}") print(f"Robot temp: {myEnt.get('temperature')} °C") print(f"Robot Vib: {myEnt.get('vibration')} Hz ") print("______________________________________") # Reapeat Parsing data every 10 sec. time.sleep(10) except Exception as ex: print('Exception:') print(ex) ``` the output like : ![](https://i.imgur.com/r3lY64k.jpg) # DSNA22 OTHER Projects : * [My Morse Code.](https://hackmd.io/@DSNA22/MorseCode) * [Traffic Lights Controler with IR Remote.](https://hackmd.io/@DSNA22/Italian_Job) * [Math Trainig with Arduino , LCD,and 4X4 Keypad.](https://hackmd.io/@DSNA22/Math_Trainig_with_Arduino) * [Rock,Scissors or Paper game.](https://hackmd.io/@DSNA22/Rock_Scissors_Paper) * [Connecting Quad Seven segment and DHT11 via WiFi and API.](https://hackmd.io/@DSNA22/4_Seven_segment_and_DHT11_via_WiFi) * [Multi-protocol communication tutorial](https://hackmd.io/@DSNA22/Multi_protocol_communication_tutorial) * YouTube Channel : [ Or you can visit my YouTube channel Here.](https://www.youtube.com/c/EngAmmarALNahas/videos) * Or my Linkedin [here](https://www.linkedin.com/in/ammar-alnahhas-4b0b064b/). # Thanks : Thanks for [Haithem S](https://www.linkedin.com/in/haithem-s-020867138/) for all knowledge and experience that looks clearly in backscenes of this project!