element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Industrial Automation
  • Technologies
  • More
Industrial Automation
Blog Connect to AVNET iotconnect.io with Python IoTConnectSDK and BLE - part 8: Get BLE Image from Camera and Send to Cloud
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Industrial Automation to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 2 Nov 2021 1:41 PM Date Created
  • Views 1053 views
  • Likes 2 likes
  • Comments 1 comment
  • iiot
  • onsemi
  • avnet
  • smartedge
  • gateway
  • iotconnect.io
Related
Recommended

Connect to AVNET iotconnect.io with Python IoTConnectSDK and BLE - part 8: Get BLE Image from Camera and Send to Cloud

Jan Cumps
Jan Cumps
2 Nov 2021

AVNET's iotconnect.io cloud platform is an online service that you can use to send data to, and then show it on a dashboard. In this blog series I'm learning the Python SDK and integrating BLE devices.

In this post, two topics: how is the image retrieved from the OnSemi Smart Shot camera, and how is the image sent to the cloud.

image

image source: RSL10 Smart Shot Color Camera user's manual

 

Getting the Image from the Camera via BLE

 

When the cloud application needs an image off the remote camera, it sends a command to the IIOTConnect SmartEdge Gateway.

image

image: when the cloud user interface requests an image from the camera, it generates a command to the camera (child) via the device (gateway)

 

This triggers a Python function on the gateway.

def SmartShotCaptureCmd(msg):

 

This happens via a callback mechanism that listens for cloud commands:

# SDK call backs
def callbackMessageMyPythonSensorValue(msg):
    global sdk, my_command_dict, d2cMsg
    myprint(msg)
    return
    if msg != None and len(list(msg.items())) != 0:
        cmdType = msg["cmdType"]
        data = msg["data"]
        # For non OTA(commands etc)
        if cmdType != "0x02" and data != None:
            #
            # Put code here to do command
            #
            myprint("Message: " + str(msg))
            if (msg['data']['command'] == ' SmartShotCaptureCmd'):
                SmartShotCaptureCmd(data)
            elif # ...

 

After looking up the correct child in the list of all BLE devices in reach of the gateway, a connection is made.

        RSL10DataLock.acquire()
        smartshot = btle.Peripheral(data.upper())

 

An image is retrieved and written to the gateway's file system

            #Start capture
            myprint("Start Capture " + datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.000Z"))
            imagefp = 0
            smartshot.setMTU(247)
            smartshot.writeCharacteristic(ChControlUuid, struct.pack('<b', 0x01))
            # Wait for capture complete notification
            smartshot.waitForNotifications(5)
            # Start image transfer
            PhotoFrameLength = 14
            smartshot.writeCharacteristic(ChControlUuid, struct.pack('<b', 0x04))
            smartshot.waitForNotifications(2)
            while (imagelength > 0) and ( count < 5000):
                count = count + 1
                imagelength = imagelength - PhotoFrameLength
                smartshot.waitForNotifications(1)

...

        if ( count < 5000):
            if (imagefp != 0):
                myprint("ImageClosed")
                imagefp.close()
            Index = 0
            NameIndex = 0
            count = 0
            for item in SmartShotIndex:
                if (item[0] == str(data)):
                    Index = item[1]
                    SmartShotIndex[count] = [data, Index + 1]
                count = count + 1    
            for item in AddressList:
                if (item['addr'] == str(data)):
                    NameIndex = item['name']
            photoname = str(uniqueId) + str(NameIndex) + "Capture" + str(Index) + ".jpg"
            myprint(photoname)
            os.system("mv capture.jpg " + photoname)
            SmartShotVersion = -1
            for item in AddressList:
                if item['addr'] == data:
                    SmartShotVersion = item['applicationversion']
                
            data1 = {
                'SmartShotIndex':Index,
                'SmartShotPhoto':photoname,
                'SmartShotVersion':SmartShotVersion,
                'SmartShotStatus':"Online"
            }
            if (type == 1):
                packet = {
                    "uniqueId": "ONSemi" + data.replace(':', '')[6:].upper() + "SmartShotBlackWhite"+ uniqueId[:6],
                    "time": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.000Z"),
                    "data": data1
                }
                OnBoard.SendDataNow(packet)
                result = SendPhotoToCloud(photoname,"ONSemi" + data.replace(':', '')[6:].upper() + "SmartShotBlackWhite"+ uniqueId[:6])
            else:
                packet = {
                    "uniqueId": "ONSemi" + data.replace(':', '')[6:].upper() + "SmartShotColor"+ uniqueId[:6],
                    "time": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.000Z"),
                    "data": data1
                }              
                OnBoard.SendDataNow(packet)
                result = SendPhotoToCloud(photoname,"ONSemi" + data.replace(':', '')[6:].upper() + "SmartShotColor"+ uniqueId[:6])
            myprint("PhotoSent " + datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.000Z"))
            os.system('rm ' + photoname)
            result = 0

 

Sending the Captured Photo to the Cloud

 

One of the last lines of code in the previous section was the command to upload the image to the cloud:

result = SendPhotoToCloud(photoname,"ONSemi" + data.replace(':', '')[6:].upper() + "SmartShotColor"+ uniqueId[:6])

 

This function uses a custom web service on iotconnect.io to load that data.

http_photo_upload = https://onsemirslapi.iotconnect.io/api/device/addGatewayImage

 

When the image is sent, a number of attributes are added to the payload, like the gateway and child identifiers.

Then the image is loaded:

    files = {'image': (name, open(name, 'rb'))}

    response = requests.request("POST", url, data=payload, headers=headers, files=files)
    response = response.json()
    if ((response != None ) and (response['status'] == 200)):
        return 0
    else:
        return 1

 

image

image: gui after I requested an image

image

image: data arriving

image

image: the command sent from cloud to gateway

.

 

The Python SDK with On Semiconductor RSL10 BLE article seriesIndustry
part 1: overview and goal
part 2: WiFi Provisioning
part 3: Adding a Module (RSL10)
part 4: Talk BLE to the On Semi RSL10 Sensor Kit
part 5: A Cloud User Experience Example
part 6: Register as a Gateway Device
part 7: Register a Gateway and Client Devices
part 8: Get BLE Image from Camera and Send to Cloud
The NODE-Red SDK article seriesIndustry
part 1: overview and goal
register a Thing and connect to IoTConnect.io cloud
part 2: create an account and log on to the portal
part 3: set up the thing and its interface in the cloud
part 4: set up Node-RED and first exchange
interact with IoTConnect.io cloud
part 5: online dashboard
part 6: rules and alerts
part 7: messages and commands from the cloud
safer connections with certificates
part 8a: safer connect with Self Signed Certificates
part 8b: safer connect with CA certificatesY
commercial and industrial scale: outsource certificate generation and programming to subcontractors and suppliers
part 9a: Outsource Certificate Signing in IIoT Supply ChainY
part 9b: IIoT supply chain and Certificates - Create Ca Root certificate, Load to IoTConnect Cloud and ValidateY
part 9c: IIoT supply chain and Certificates - Create an Intermediate CA Certificate for your SubcontractorY
part 9d: IIoT supply chain and Certificates - Subcontractor Generates a Thing Certificate for Your DeviceY
part 9e: IIoT supply chain and Certificates - Test!Y
commercial and industrial scale: Trusted Platform Module (TPM) Authentication
part 10: Trusted Platform Module (TPM) SecurityY
Infineon SLx9670 Trusted Platform Module (TPM) for IoT SecurityY
The Automate Device Provisioning and Cloud Configuration article seriesIndustry
Automatic Provisioning with REST APIY
  • Sign in to reply
  • DAB
    DAB over 3 years ago

    Good post Jan.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube