Giter Site home page Giter Site logo

azure-samples / iot-hub-c-raspberrypi-client-app Goto Github PK

View Code? Open in Web Editor NEW
36.0 25.0 34.0 37 KB

Application collects weather data from a BME280 sensor and sends it to IoT hub, also take actions according to cloud-to-device message, device management.

Home Page: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-raspberry-pi-kit-c-get-started

License: MIT License

CMake 2.41% C 92.87% Shell 4.72%

iot-hub-c-raspberrypi-client-app's Introduction

services platforms author
iot-hub
C
shizn

IoT Hub Raspberry Pi 3 Client application

Build Status

This repo contains the source code to help you get started with Azure IoT using the Microsoft IoT Pack for Raspberry Pi 3 Starter Kit. You will find the full tutorial on Docs.microsoft.com.

This repo contains an arduino application that runs on Raspberry Pi 3 with a BME280 temperature&humidity sensor, and then sends these data to your IoT hub. At the same time, this application receives Cloud-to-Device messages from your IoT hub, and takes actions according to the C2D command.

Set up your Pi

Enable SSH on your Pi

Follow this page to enable SSH on your Pi.

Enable SPI on your Pi

Follow this page to enable SPI on your Pi

Connect your sensor with your Pi

Connect with a physical BEM280 sensor and LED

You can follow the image to connect your BME280 and a LED with your Raspberry Pi 3.

BME280

Download and setup the client app

  1. Clone the client application to local:

    sudo apt-get install git-core
    
    git clone https://github.com/Azure-Samples/iot-hub-c-raspberrypi-client-app.git
  2. Run setup script:

    cd ./iot-hub-c-raspberrypi-client-app
    
    sudo chmod u+x setup.sh
    
    sudo ./setup.sh

    If you don't have a physical BME280, you can use '--simulated-data' as command line parameter to simulate temperature&humidity data.

    sudo ./setup.sh --simulated-data

Run your client application

Run the client application with root priviledge, and you also need provide your Azure IoT hub device connection string, note your connection should be quoted in the command.

sudo ./app '<your Azure IoT hub device connection string>'

Send Cloud-to-Device command

You can send a C2D message to your device. You can see the device prints out the message and blinks once when receiving the message.

Send Device Method command

You can send start or stop device method command to your Pi to start/stop sending message to your IoT hub.

iot-hub-c-raspberrypi-client-app's People

Contributors

alberto-vega avatar ftheile avatar in4margaret avatar microsoftopensource avatar msftgits avatar sldragon avatar wesmc7777 avatar yuwzho avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

iot-hub-c-raspberrypi-client-app's Issues

LogError and LogInfo in main.c

Hi
For some reason I can not run the app. The errors are: undefined reference to LogError' and undefined reference to LogInfo'. Where do I find these functions. Or Is there the posibility to replace them with printf.

I tried everything with Raspberry 2B und jessy

Best
MaHa

Error creating "app" file during install due to some json error

I had installed the iot-hub-c-raspberry-client-app two or three weeks ago and it worked although it gave me the following error (for the 2nd message only) but then it continued running: Error: Time:... File:/root/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:728 (result = MULTITREE_CHILD_NOT_FOUND)
I tried reinstalling a bunch of times during the last day or two and kept failing creating the "app" file due to some json error.
I realized that the latest branch of the azure-iot-sdk-c had a commit done yesterday (2018Mar05).
It then occurred to me to try the version lts_01_2018 of the azure-iot-sdk-c; I made 2 changes to the setup.sh file:
1- to install on /home/pi instead on /root
2- to use lts_01_2018
The install completed and ran, although with the same MULTITREE_CHILD_NOT_FOUND error.
These are the changes I did to the setup.sh file:

#On Raspbian Stretch precompiled sdk from ppa is no good. Lets compile it manually
#cd ~
cd /home/pi
mkdir Source
cd Source
#git clone --recursive https://github.com/azure/azure-iot-sdk-c.git
git clone -b lts_01_2018 --recursive https://github.com/azure/azure-iot-sdk-c.git

Regards,
handcyclist

Illegal Instruction

Hello,

After running the setup script with or without a sensor I get the following errors:

a. I don't think the app is compiling properly
b. When I try to run the app with my connection string after compiling, I get an Illegal Instruction message

Any thoughts?

Thanks in advance.

pi@rpi0w-sensor:/iot-hub-c-raspberrypi-client-app $ cmake . && make
-- Configuring done
CMake Error: Unable to open check cache file for write. /home/pi/iot-hub-c-raspberrypi-client-app/CMakeFiles/cmake.check_cache
pi@rpi0w-sensor:
/iot-hub-c-raspberrypi-client-app $ sudo cmake . && make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/iot-hub-c-raspberrypi-client-app
Built target app
pi@rpi0w-sensor:/iot-hub-c-raspberrypi-client-app $ sudo ./app ''
Illegal instruction
pi@rpi0w-sensor:
/iot-hub-c-raspberrypi-client-app $

iothubcerror

Getting it to compile Aug 2020

Not an issue.
But hopefully a helping hand

Got this running with PI zero and latest "Raspberry PI OS"

Here are my steps

  1. Use PI Imager
    Raspberry PI OS Full (32-bit)
    Use 8GB 10x MicroSD Card
  2. Overlay
    SSH.
    wpa_supplicant.conf
    instructions here: https://www.tomshardware.com/reviews/raspberry-pi-headless-setup-how-to,6028.html
  3. Connect via putty
    download: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
    address: raspberrypi.local
    uid: pi
    pwd: raspberry
  4. Enable VNC
    sudo raspi-config
    Interfacting Options
    P3 VNC
  5. Install CMAke
    sudo apt-get install cmake
  6. Install GIT
    sudo apt-get install git-core
  7. Fix libssl
    sudo apt-get purge -y libssl-dev
    sudo apt-get install libcurl4-openssl-dev
    sudo apt-get install libssl-dev
  8. Update PI
    sudo apt-get update
    sudo apt-get upgrade
  9. Reboot
    sudo reboot
  10. Connect VNC
    download: https://realvnc.com/en/connect/download/viewer/windows/
    address: raspberrypi.local
    uid: pi
    pwd: raspberry
  11. Run Welcome Wizzard
    • Set Country
    • change password
    • choose default network
    • update software
  12. Restart
  13. Connect via putty
    address: raspberrypi.local
    uid: pi
    pwd: new password
  14. Git the Azure IOT SOurce
    cd ~
    mkdir Source
    cd Source
    git clone --recursive https://github.com/azure/azure-iot-sdk-c.git
  15. Build the Azure IOT Source
    cd azure-iot-sdk-c/build_all/linux
    ./build.sh --no-make
    cd ../../cmake/iotsdk_linux
    make
    sudo make install
    '
  16. Install Sample App Source
    cd ~
    git clone https://github.com/Azure-Samples/iot-hub-c-raspberrypi-client-app.git
    cd ./iot-hub-c-raspberrypi-client-app
  17. Install wiringPI
    sudo apt-get install wiringpi
  18. Install parson
    git clone https://github.com/kgabis/parson.git
    cd ./parson
    mv parson.c parson.h ..
    cd ..
    rm -rf parson
  19. Set the sample "Simulate Data" as required
    sed -i 's/#define SIMULATED_DATA 0/#define SIMULATED_DATA 1/' config.h
  20. Make the sample
    cmake . && make
  21. Run the sample
    sudo ./app 'DEVICE CONNECTION STRING'

Build error

After following the solution to #22 (checked all libraries in /usr/local/lib, copied internal folder from /home/pi/Source/azure-iot-sdk-c/iothub_client/inc to /usr/local/include/azureiot, added parson.h & parson.c), I ran into more errors shown below:

Scanning dependencies of target app
[ 20%] Building C object CMakeFiles/app.dir/main.c.o
[ 40%] Building C object CMakeFiles/app.dir/wiring.c.o
[ 60%] Linking C executable app
//usr/local/lib/libiothub_client.a(iothub_client_ll_uploadtoblob.c.o): In function 'IoTHubClient_LL_UploadToBlob_step1and2':
iothub_client_ll_uploadtoblob.c:(.text+0x1018): undefined reference to 'json_parse_string'
iothub_client_ll_uploadtoblob.c:(.text+0x1084): undefined reference to 'json_value_get_object'
iothub_client_ll_uploadtoblob.c:(.text+0x10fc): undefined reference to 'json_object_get_string'
iothub_client_ll_uploadtoblob.c:(.text+0x11e0): undefined reference to 'json_object_get_string'
iothub_client_ll_uploadtoblob.c:(.text+0x1258): undefined reference to 'json_object_get_string'
iothub_client_ll_uploadtoblob.c:(.text+0x12d0): undefined reference to 'json_object_get_string'
iothub_client_ll_uploadtoblob.c:(.text+0x1348): undefined reference to 'json_object_get_string'
iothub_client_ll_uploadtoblob.c:(.text+0x1818): undefined reference to 'json_value_free'
collect2: error: ld returned 1 exit status
CMakeFiles/app.dir/build.make:172: recipe for target 'app' failed
make[2]: *** [app] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/app.dir/all' failed
make[1]: *** [CMakeFiles/app.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Any help would be appreciated. Thanks!

Error while running setup.sh

Hello all
I get below errors while running setup.sh, appreciate any help or suggestions on how to fix the error

CMakeFiles/app.dir/main.c.o:/home/pi/iot-hub-c-raspberrypi-client-app/main.c:334: more undefined references to `LogError' follow
collect2: error: ld returned 1 exit status
CMakeFiles/app.dir/build.make:172: recipe for target 'app' failed
make[2]: *** [app] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/app.dir/all' failed
make[1]: *** [CMakeFiles/app.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Errors when using 4G router

Running this on a RPi3, bullseye, works fine but what is causing those error message every time?

Any hint to fix this?

Suddenly it can go into a mode with just errors, then sending one or more messages successfully, then errors again

The Node.js example does not show this type of problem

Info: Message sent to Azure IoT Hub
Error: Time:Sun Apr  3 09:43:03 2022 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Info: Sending message: { "deviceId": "Raspberry Pi - C", "messageId": 9, "temperature": 25.869999, "humidity": 65.239998 }
Info: Message sent to Azure IoT Hub
Error: Time:Sun Apr  3 09:43:09 2022 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Info: Sending message: { "deviceId": "Raspberry Pi - C", "messageId": 10, "temperature": 28.189999, "humidity": 65.919998 }
Info: Message sent to Azure IoT Hub
Error: Time:Sun Apr  3 09:43:15 2022 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Info: Sending message: { "deviceId": "Raspberry Pi - C", "messageId": 11, "temperature": 24.430000, "humidity": 72.440002 }
Info: Message sent to Azure IoT Hub
Error: Time:Sun Apr  3 09:43:20 2022 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Info: Sending message: { "deviceId": "Raspberry Pi - C", "messageId": 12, "temperature": 22.309999, "humidity": 64.910004 }
Info: Message sent to Azure IoT Hub
Error: Time:Sun Apr  3 09:43:26 2022 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Info: Sending message: { "deviceId": "Raspberry Pi - C", "messageId": 13, "temperature": 29.220001, "humidity": 67.379997 }
Info: Message sent to Azure IoT Hub
Error: Time:Sun Apr  3 09:43:32 2022 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Info: Sending message: { "deviceId": "Raspberry Pi - C", "messageId": 14, "temperature": 20.750000, "humidity": 70.449997 }
Info: Message sent to Azure IoT Hub

Error: Time:Sun Apr  3 09:54:32 2022 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Error: Time:Sun Apr  3 09:54:33 2022 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Error: Time:Sun Apr  3 09:54:34 2022 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Error: Time:Sun Apr  3 09:54:34 2022 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Error: Time:Sun Apr  3 09:54:35 2022 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Error: Time:Sun Apr  3 09:54:36 2022 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Error: Time:Sun Apr  3 09:54:37 2022 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Error: Time:Sun Apr  3 09:54:37 2022 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)

wiringpi unavailable

The setup.sh script fails when it is fetching wiringpi.
Given that wiringpi will soon be deprecated, and the git repo currently inaccessible:

git.drogon.net is temporarily unavailable. Please look for alternatives for wiringPi, etc. -Gordon

)

.. will a fork of the library be made available here for future use?

Error when running the sample app - MULTITREE_CHILD_NOT_FOUND

I compiled and installed the azure-iot-sdk-c from the branch lts_08_2023, but when I compile the sample app, with SIMULATED_DATA 1, I get this error:

➜  0a43  build git:(master) ✗ sudo ./app "HostName=xxx.azure-devices.net;DeviceId=xxx;SharedAccessKey=xxx"
Info: Sending message: { "deviceId": "Raspberry Pi - C", "messageId": 1, "temperature": 26.200001, "humidity": 71.029999 }
Info: Message sent to Azure IoT Hub
Error: Time:Sat Jun  8 20:00:44 2024 File:/home/xxx/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Info: Sending message: { "deviceId": "Raspberry Pi - C", "messageId": 2, "temperature": 25.790001, "humidity": 71.809998 }
Info: Message sent to Azure IoT Hub
Error: Time:Sat Jun  8 20:00:46 2024 File:/home/xxx/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)
Error: Time:Sat Jun  8 20:00:52 2024 File:/home/xxx/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:671 (result = MULTITREE_CHILD_NOT_FOUND)

on the azure side, I see that I receive the first telemetry (the one with "temperature": 26.200001), but not subsequent ones. Same thing if I kill and restart the app, I receive the first one but not the ones after...

I guess the error is in the parsing of the response from Azure, leaving the first message un-acknowledged? Just a guess

Recipe for target app failed

This issue seems to be the same from #25 I saw that there is a workaround that I will try at some point. Hoping this gets resolved for future users trying the tutorial @SLdragon any insights on this? Below are the errors

-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/iot-hub-c-raspberrypi-client-app/iot-hub-c-raspberrypi-client-app
[ 20%] Linking C executable app
//usr/local/lib/libiothub_client.a(iothub_client_ll_uploadtoblob.c.o): In function IoTHubClient_LL_UploadToBlob_step1and2': iothub_client_ll_uploadtoblob.c:(.text+0x1018): undefined reference to json_parse_string'
iothub_client_ll_uploadtoblob.c:(.text+0x1084): undefined reference to json_value_get_object' iothub_client_ll_uploadtoblob.c:(.text+0x10fc): undefined reference to json_object_get_string'
iothub_client_ll_uploadtoblob.c:(.text+0x11e0): undefined reference to json_object_get_string' iothub_client_ll_uploadtoblob.c:(.text+0x1258): undefined reference to json_object_get_string'
iothub_client_ll_uploadtoblob.c:(.text+0x12d0): undefined reference to json_object_get_string' iothub_client_ll_uploadtoblob.c:(.text+0x1348): undefined reference to json_object_get_string'
iothub_client_ll_uploadtoblob.c:(.text+0x1814): undefined reference to `json_value_free'
collect2: error: ld returned 1 exit status
CMakeFiles/app.dir/build.make:172: recipe for target 'app' failed
make[2]: *** [app] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/app.dir/all' failed
make[1]: *** [CMakeFiles/app.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Installing error of libcurl4-openssl-dev on Raspbian buster

Hey

I am trying to use this on Raspbian buster, and having an installation error of libcurl4-openssl-dev as below after running the ./setup.sh command. I am not sure whether this already supports Raspbian buster.

The following packages have unmet dependencies:
 libcurl4-openssl-dev : Conflicts: libssl1.0-dev but 1.0.2q-2 is to be installed
E: Unable to correct problems, you have held broken packages.

But if I try to install libcurl4-openssl-dev by apt-get install command, I do not get the same error.
image

Error: ...(result = MULTITREE_CHILD_NOT_FOUND)

Hello:
Some code is not working as intended.
I get the following error

Info: Sending message: { "deviceId": "Raspberry Pi - C", "messageId": 1, "temperature": 24.889999, "humidity": 72.779999 }
Info: Message sent to Azure IoT Hub
Error: Time:Tue Mar 13 19:48:35 2018 File:/home/pi/Source/azure-iot-sdk-c/serializer/src/multitree.c Func:MultiTree_GetLeafValue Line:735 (result = MULTITREE_CHILD_NOT_FOUND)
Info: Sending message: { "deviceId": "Raspberry Pi - C", "messageId": 2, "temperature": 21.320000, "humidity": 77.250000 }
Info: Message sent to Azure IoT Hub
...
and then message 3, 4, etc without more errors

Code in main.c for the iot-hub-c-raspberrypi-client-app sample

I suspect this code line is wrong, or incomplete, or should not be there:
Line 374: IoTHubClient_LL_SetDeviceTwinCallback(iotHubClientHandle, twinCallback, NULL);

What does this code line do?
Line 375: IoTHubClient_LL_SetOption(iotHubClientHandle, "product_info", "HappyPath_RaspberryPi-C");

I suspect this function is tried to be called by line 374
Line 139: void twinCallback(
DEVICE_TWIN_UPDATE_STATE updateState,
const unsigned char *payLoad,
size_t size,
void *userContextCallback)

Missing parson.c parson.h source files.

The setup.sh script extracts the parson files one directory back outside the project file. So when you run cmake it will throw dependency errors, requiring you to manually move the 2 parson files into iot-hub-c-raspberrypi-client-app directory, in order to build correctly.

Working setup: RPi3 and Bullseye

I started using the sample with the script but it did not work, failed on multiple things, so after reading and googling numerously, the following steps finally worked for me:

For example in C

Install CMAke
sudo apt-get install cmake

Install GIT
sudo apt-get install git-core

Fix libssl
sudo apt-get purge -y libssl-dev
sudo apt-get install libcurl4-openssl-dev
sudo apt-get install libssl-dev

Update PI
sudo apt-get update
sudo apt-get upgrade

Reboot
sudo reboot

Git the Azure IOT SOurce
cd ~
mkdir Source
cd Source
git clone --recursive https://github.com/azure/azure-iot-sdk-c.git
Build the Azure IOT Source
cd azure-iot-sdk-c/build_all/linux
./build.sh --no-make
cd ../../cmake
make
sudo make install

Git the Azure C Shared Utility
cd ~
cd Source
git clone --recursive https://github.com/Azure/azure-c-shared-utility.git
cd azure-c-shared-utility
mkdir cmake
cd cmake
cmake ..
cmake --build .
sudo make install

Install Sample App Source
cd ~
git clone https://github.com/Azure-Samples/iot-hub-c-raspberrypi-client-app.git
cd ./iot-hub-c-raspberrypi-client-app

Install wiringPI
sudo apt-get install wiringpi

Install parson
git clone https://github.com/kgabis/parson.git
cd ./parson
mv parson.c parson.h ..
cd ..
rm -rf parson

Set the sample "Simulate Data" as required
sed -i 's/#define SIMULATED_DATA 0/#define SIMULATED_DATA 1/' config.h

Make the sample
cmake . && make

Run the sample
sudo ./app 'DEVICE CONNECTION STRING'

setup.sh

Hi
I think I found an error in setup.sh
Is there the possibility, that line 63 ./build should be bevor line 62
Best
maha

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.