-
Notifications
You must be signed in to change notification settings - Fork 734
IBM IoT sample #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
IBM IoT sample #13
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ae4ae9e
IBM deviceSample's cpp & Cmake files
andrey4latyshev 384ee61
Create README.md
andrey4latyshev f370f2c
Create sample.json
andrey4latyshev 443b5a1
Info files updated
andrey4latyshev c34f9a2
Update sample.json
andrey4latyshev 8c27665
Copyright and GUID fixed
andrey4latyshev 8e61784
Update sample.json
andrey4latyshev 94006ab
Update README.md
JoeOster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
cmake_minimum_required (VERSION 3.0) | ||
|
||
project (ibm-device) | ||
|
||
set(CMAKE_CXX_STANDARD "11") | ||
|
||
set(IOT_SDK_FOLDER "$ENV{HOME}/iot-c") | ||
|
||
include_directories( | ||
"${IOT_SDK_FOLDER}/paho.mqtt.c/src" | ||
"${IOT_SDK_FOLDER}/paho.mqtt.c/build/output" | ||
"${IOT_SDK_FOLDER}/build" | ||
"${IOT_SDK_FOLDER}/src/wiotp/sdk" | ||
) | ||
|
||
file(GLOB LIB_SRC ${IOT_SDK_FOLDER}/src/wiotp/sdk/*.c ${IOT_SDK_FOLDER}/src/wiotp/sdk/*.h) | ||
|
||
add_library(iotp STATIC ${LIB_SRC}) | ||
|
||
set(device_sample_files cpp/deviceSample.c) | ||
|
||
add_executable(ibm-device ${device_sample_files}) | ||
|
||
target_link_libraries(ibm-device iotp paho-mqtt3as pthread ssl crypto) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Copyright Intel Corporation | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# `IBM Device` Sample | ||
|
||
`IBM Device` sample shows how-to develop a device code using Watson IoT Platform iot-c device client library, connect and interact with Watson IoT Platform Service. | ||
|
||
| Optimized for | Description | ||
|:--- |:--- | ||
| OS | Linux* Ubuntu* 16.04, Linux* Ubuntu* 18.04, | ||
| Software | Paho MQTT C library, OpenSSL development package | ||
| What you will learn | Use protocol MQTT to send events from a device | ||
|
||
## Purpose | ||
This is a simple sample you could use for a test of IBM device connection. This project shows how-to develop a device code using Watson IoT Platform iot-c device client library, connect and interact with Watson IoT Platform Service. | ||
|
||
## Key Implementation Details | ||
This sample includes the function/code snippets to perform the following actions: | ||
- Initiliaze client library | ||
- Configure device from configuration parameters specified in a configuration file | ||
- Set client logging | ||
- Enable error handling routines | ||
- Send device events to WIoTP service | ||
- Receive and process commands from WIoTP service | ||
|
||
##License | ||
Code sample uses Eclipse Public License - v1.0 | ||
|
||
##Building the `IBM Device` Sample | ||
|
||
### On a Linux* System | ||
|
||
The detailed instructions on how to install the custom provided all dependency libraries for Linux can be [found here](https://github.com/ibm-watson-iot/iot-c#build-instructions). | ||
|
||
Perform the following steps: | ||
1. Run in the terminal: | ||
``` | ||
cd $ENV{HOME} | ||
git clone https://github.com/ibm-watson-iot/iot-c.git | ||
cd iot-c | ||
make | ||
sudo make -C paho.mqtt.c install | ||
sudo make install | ||
``` | ||
> Note! By default the directory to install the 'iot-c' library is $ENV{HOME}. Otherwise you should enter valid path to this library as variable IOT_SDK_FOLDER in CMakeLists.txt file. | ||
|
||
2. Run the following lines from the sample folder 'ibm-device': | ||
``` | ||
mkdir build | ||
cd build | ||
cmake .. | ||
make all | ||
``` | ||
3. Run the program using: | ||
``` | ||
ibm-device deviceSample --config <path_to_downloaded_configuration_file> | ||
``` | ||
4. Clean the program using: | ||
``` | ||
make clean | ||
``` | ||
## Running the Sample | ||
|
||
Configure IoT device on [IBM Watson IoT Platform Page](https://ibm-watson-iot.github.io/iot-c/device/). | ||
|
||
### Application Parameters | ||
|
||
The samples uses path to configuration file as a parameter. | ||
Download the configuration file with all the credentials according to [instructions](https://ibm-watson-iot.github.io/iot-c/device/). | ||
|
||
### Example of Output | ||
|
||
TBD | ||
JoeOster marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
/******************************************************************************* | ||
JoeOster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Copyright (c) 2018 IBM Corp. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* Contributors: | ||
* Ranjan Dasgupta - Initial drop of deviceSample.c | ||
* | ||
*******************************************************************************/ | ||
|
||
/* | ||
* This sample shows how-to develop a device code using Watson IoT Platform | ||
* iot-c device client library, connect and interact with Watson IoT Platform Service. | ||
* | ||
* This sample includes the function/code snippets to perform the following actions: | ||
* - Initiliaze client library | ||
* - Configure device from configuration parameters specified in a configuration file | ||
* - Set client logging | ||
* - Enable error handling routines | ||
* - Send device events to WIoTP service | ||
* - Receive and process commands from WIoTP service | ||
* | ||
* SYNTAX: | ||
* deviceSample --config <config_file_path> | ||
* | ||
* Pre-requisite: | ||
* 1. This sample requires a device configuration file. | ||
* Refer to "Device Confioguration File" section of iot-c docs for details. | ||
* 2. Register type and id (specified in the configuration file) with IBM Watson IoT Platform service. | ||
* | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <signal.h> | ||
#include <memory.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
/* Include header file of IBM Watson IoT platform C Client for devices */ | ||
#include "iotp_device.h" | ||
|
||
char *configFilePath = NULL; | ||
volatile int interrupt = 0; | ||
char *progname = "deviceSample"; | ||
int useEnv = 0; | ||
int testCycle = 0; | ||
|
||
/* Usage text */ | ||
void usage(void) { | ||
fprintf(stderr, "Usage: %s --config config_file_path\n", progname); | ||
exit(1); | ||
} | ||
|
||
/* Signal handler - to support CTRL-C to quit */ | ||
void sigHandler(int signo) { | ||
signal(SIGINT, NULL); | ||
fprintf(stdout, "Received signal: %d\n", signo); | ||
interrupt = 1; | ||
} | ||
|
||
/* Get and process command line options */ | ||
void getopts(int argc, char** argv) | ||
{ | ||
int count = 1; | ||
|
||
while (count < argc) | ||
{ | ||
if (strcmp(argv[count], "--config") == 0) | ||
{ | ||
if (++count < argc) | ||
configFilePath = argv[count]; | ||
else | ||
usage(); | ||
} | ||
if (strcmp(argv[count], "--useEnv") == 0) { | ||
useEnv = 1; | ||
} | ||
if (strcmp(argv[count], "--testCycle") == 0) { | ||
if (++count < argc) | ||
testCycle = atoi(argv[count]); | ||
else | ||
usage(); | ||
} | ||
count++; | ||
} | ||
} | ||
|
||
/* | ||
* Device command callback function | ||
* Device developers can customize this function based on their use case | ||
* to handle device commands sent by WIoTP. | ||
* Set this callback function using API setCommandHandler(). | ||
*/ | ||
void deviceCommandCallback (char* type, char* id, char* commandName, char *format, void* payload, size_t payloadSize) | ||
{ | ||
fprintf(stdout, "Received device command:\n"); | ||
fprintf(stdout, "Type=%s ID=%s CommandName=%s Format=%s Len=%d\n", | ||
type?type:"", id?id:"", commandName?commandName:"", format?format:"", (int)payloadSize); | ||
fprintf(stdout, "Payload: %s\n", payload?(char *)payload:""); | ||
|
||
/* Device developers - add your custom code to process device commands */ | ||
} | ||
|
||
void logCallback (int level, char * message) | ||
{ | ||
if ( level > 0 ) | ||
fprintf(stdout, "%s\n", message? message:"NULL"); | ||
fflush(stdout); | ||
} | ||
|
||
void MQTTTraceCallback (int level, char * message) | ||
{ | ||
if ( level > 0 ) | ||
fprintf(stdout, "%s\n", message? message:"NULL"); | ||
fflush(stdout); | ||
} | ||
|
||
/* Main program */ | ||
int main(int argc, char *argv[]) | ||
{ | ||
int rc = 0; | ||
int cycle = 0; | ||
|
||
/* | ||
* DEV_NOTES: | ||
* Specifiy variable for WIoT client object | ||
*/ | ||
IoTPConfig *config = NULL; | ||
IoTPDevice *device = NULL; | ||
|
||
/* check for args */ | ||
if ( argc < 2 ) | ||
usage(); | ||
|
||
/* Set signal handlers */ | ||
signal(SIGINT, sigHandler); | ||
signal(SIGTERM, sigHandler); | ||
|
||
/* get argument options */ | ||
getopts(argc, argv); | ||
|
||
/* Set IoTP Client log handler */ | ||
rc = IoTPConfig_setLogHandler(IoTPLog_FileDescriptor, stdout); | ||
if ( rc != 0 ) { | ||
fprintf(stderr, "WARN: Failed to set IoTP Client log handler: rc=%d\n", rc); | ||
exit(1); | ||
} | ||
|
||
/* Create IoTPConfig object using configuration options defined in the configuration file. */ | ||
rc = IoTPConfig_create(&config, configFilePath); | ||
if ( rc != 0 ) { | ||
fprintf(stderr, "ERROR: Failed to initialize configuration: rc=%d\n", rc); | ||
exit(1); | ||
} | ||
|
||
/* read additional config from environment */ | ||
if ( useEnv == 1 ) { | ||
IoTPConfig_readEnvironment(config); | ||
} | ||
|
||
/* Create IoTPDevice object */ | ||
rc = IoTPDevice_create(&device, config); | ||
if ( rc != 0 ) { | ||
fprintf(stderr, "ERROR: Failed to configure IoTP device: rc=%d\n", rc); | ||
exit(1); | ||
} | ||
|
||
/* Set MQTT Trace handler */ | ||
rc = IoTPDevice_setMQTTLogHandler(device, &MQTTTraceCallback); | ||
if ( rc != 0 ) { | ||
fprintf(stderr, "WARN: Failed to set MQTT Trace handler: rc=%d\n", rc); | ||
} | ||
|
||
/* Invoke connection API IoTPDevice_connect() to connect to WIoTP. */ | ||
rc = IoTPDevice_connect(device); | ||
if ( rc != 0 ) { | ||
fprintf(stderr, "ERROR: Failed to connect to Watson IoT Platform: rc=%d\n", rc); | ||
fprintf(stderr, "ERROR: Returned error reason: %s\n", IOTPRC_toString(rc)); | ||
exit(1); | ||
} | ||
|
||
/* | ||
* Set device command callback using API IoTPDevice_setCommandsHandler(). | ||
* Refer to deviceCommandCallback() function DEV_NOTES for details on | ||
* how to process device commands received from WIoTP. | ||
*/ | ||
IoTPDevice_setCommandsHandler(device, deviceCommandCallback); | ||
|
||
/* | ||
* Invoke device command subscription API IoTPDevice_subscribeToCommands(). | ||
* The arguments for this API are commandName, format, QoS | ||
* If you want to subscribe to all commands of any format, set commandName and format to "+" | ||
*/ | ||
char *commandName = "+"; | ||
char *format = "+"; | ||
IoTPDevice_subscribeToCommands(device, commandName, format); | ||
|
||
|
||
/* Use IoTPDevice_sendEvent() API to send device events to Watson IoT Platform. */ | ||
|
||
/* Sample event - this sample device will send this event once in every 10 seconds. */ | ||
char *data = "{\"d\" : {\"SensorID\": \"Test\", \"Reading\": 7 }}"; | ||
|
||
while(!interrupt) | ||
{ | ||
fprintf(stdout, "Send status event\n"); | ||
rc = IoTPDevice_sendEvent(device,"status", data, "json", QoS0, NULL); | ||
fprintf(stdout, "RC from publishEvent(): %d\n", rc); | ||
|
||
if ( testCycle > 0 ) { | ||
cycle += 1; | ||
if ( cycle >= testCycle ) { | ||
break; | ||
} | ||
} | ||
sleep(10); | ||
} | ||
|
||
fprintf(stdout, "Publish event cycle is complete.\n"); | ||
|
||
/* Disconnect device */ | ||
rc = IoTPDevice_disconnect(device); | ||
if ( rc != IOTPRC_SUCCESS ) { | ||
fprintf(stderr, "ERROR: Failed to disconnect from Watson IoT Platform: rc=%d\n", rc); | ||
exit(1); | ||
} | ||
|
||
/* Destroy client */ | ||
IoTPDevice_destroy(device); | ||
|
||
/* Clear configuration */ | ||
IoTPConfig_clear(config); | ||
|
||
return 0; | ||
|
||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"guid": "CF755377-7B46-47D3-89A3-AB624692B2E8", | ||
"name": "IBM Device", | ||
"categories": ["Toolkit/Intel® oneAPI IoT Toolkit/IoT Connection Tools"], | ||
JoeOster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"description": "This project shows how-to develop a device code using Watson IoT Platform iot-c device client library, connect and interact with Watson IoT Platform Service.", | ||
"dependencies": ["iot-c|https://github.com/ibm-watson-iot/iot-c"], | ||
"languages": [{"cpp":{}}], | ||
"os": ["linux"], | ||
"ciTests": { | ||
"linux": [ | ||
{ "id": "ibm-device", | ||
"env": [], | ||
"steps": [ | ||
"mkdir build", | ||
"cd build", | ||
"cmake ..", | ||
"make" | ||
] | ||
} | ||
] | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these instructions applicable anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andrey4latyshev - can you take a look at this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, these instructions help to build prerequisites.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why ask them to git clone the sample from that repo when it is present in this repo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This step is needed to clone the dependency library IBM Watson IoT Platform from git.