Rover - Updates regarding hono interactions of rover
(1) - stdout pipe is redirected to hide the popen function output from console.
(2) - Now rover sends telemetry data from its sensors [about 15-20 messages per second]
(3) - Improvements regarding library modularization
Signed-off-by: Mustafa Ozcelikors <mozcelikors@gmail.com>
diff --git a/rover/src/RaspberryTest.cpp b/rover/src/RaspberryTest.cpp
index a0a18ec..b77e55f 100644
--- a/rover/src/RaspberryTest.cpp
+++ b/rover/src/RaspberryTest.cpp
@@ -130,8 +130,8 @@
int main()
{
- //To test the hono interaction library
- //registerDeviceToHonoInstance("idial.institute",8080,"DEFAULT_TENANT", 4771);
+ //Register the device to cloud
+ registerDeviceToHonoInstance("idial.institute",8080,"DEFAULT_TENANT", 4771);
//sendEventDataToHonoInstance("idial.institute",8080,"DEFAULT_TENANT", 4771,"Bearing",0.5);
RefreshThreadList();
diff --git a/rover/src/hono_interaction/hono_interaction.cpp b/rover/src/hono_interaction/hono_interaction.cpp
index a78f566..17e9225 100644
--- a/rover/src/hono_interaction/hono_interaction.cpp
+++ b/rover/src/hono_interaction/hono_interaction.cpp
@@ -73,7 +73,7 @@
status = 0;
break;
case 409:
- fprintf(stderr, ("409 Conflict\n"));
+ fprintf(stderr, ("409 Conflict. A device with this ID is registered already. \n"));
status = 0;
break;
case 503:
@@ -130,8 +130,13 @@
strcat(buffer, "/registration/");
strcat(buffer, tenant_name);
+ //To redirect pipe to prevent stdout showing all outputs generated by curl
+ strcat(buffer, " 2>curl_log"); //2>&1 would redirect to stderr, we choose to redirect to a file to be able to parse returned code
+
+#ifdef DEBUG_DEVICE_REGISTRATION
//Print the command that is created
printf("Command=%s\n",buffer);
+#endif
//Execute the command
fp = popen(buffer,"r");
@@ -146,11 +151,13 @@
num_buffer[3] = 0;
code = atoi (num_buffer);
+#ifdef DEBUG_DEVICE_REGISTRATION
//Print the code
printf("Code=%d\n",code);
//Print the response
printf("Response=%s\n",buffer);
+#endif
//Handle the code and return status: 1-succeeded 0-failed
status = handleCode(code);
@@ -204,8 +211,13 @@
strcat(buffer, num_buffer);
num_buffer[0] = 0; //Clear array
+ //To redirect pipe to prevent stdout showing all outputs generated by curl
+ strcat(buffer, " 2>curl_log"); //2>&1 would redirect to stderr, we choose to redirect to a file to be able to parse returned code
+
+#ifdef DEBUG_HTTP_RESPONSE
//Print the command that is created
printf("Command=%s\n",buffer);
+#endif
//Execute the command
fp = popen(buffer,"r");
@@ -220,11 +232,13 @@
num_buffer[3] = 0;
code = atoi (num_buffer);
+#ifdef DEBUG_HTTP_RESPONSE
//Print the code
printf("Code=%d\n",code);
//Print the response
printf("Response=%s\n",buffer);
+#endif
//Handle the code and return status: 1-succeeded 0-failed
status = handleCode(code);
@@ -278,8 +292,13 @@
strcat(buffer, num_buffer);
num_buffer[0] = 0; //Clear array
+ //To redirect pipe to prevent stdout showing all outputs generated by curl
+ strcat(buffer, " 2>curl_log"); //2>&1 would redirect to stderr, we choose to redirect to a file to be able to parse returned code
+
+#ifdef DEBUG_HTTP_RESPONSE
//Print the command that is created
printf("Command=%s\n",buffer);
+#endif
//Execute the command
fp = popen(buffer,"r");
@@ -294,11 +313,13 @@
num_buffer[3] = 0;
code = atoi (num_buffer);
+#ifdef DEBUG_HTTP_RESPONSE
//Print the code
printf("Code=%d\n",code);
//Print the response
printf("Response=%s\n",buffer);
+#endif
//Handle the code and return status: 1-succeeded 0-failed
status = handleCode(code);
diff --git a/rover/src/hono_interaction/hono_interaction.h b/rover/src/hono_interaction/hono_interaction.h
index 572b533..f6326f3 100644
--- a/rover/src/hono_interaction/hono_interaction.h
+++ b/rover/src/hono_interaction/hono_interaction.h
@@ -18,12 +18,16 @@
#define HONO_INTERACTION_HONO_INTERACTION_H_
+//Defines
+
+//#define DEBUG_HTTP_RESPONSE 1
+#define DEBUG_DEVICE_REGISTRATION 1
+
+
//Interfaces
int registerDeviceToHonoInstance (char * host_name, int port, char * tenant_name, int device_id);
-
int sendTelemetryDataToHonoInstance (char * host_name, int port, char * tenant_name, int device_id, char * field, double value);
-
int sendEventDataToHonoInstance (char * host_name, int port, char * tenant_name, int device_id, char * field, double value);
diff --git a/rover/src/tasks/compass_sensor_task.cpp b/rover/src/tasks/compass_sensor_task.cpp
index 280448a..0d1e932 100644
--- a/rover/src/tasks/compass_sensor_task.cpp
+++ b/rover/src/tasks/compass_sensor_task.cpp
@@ -20,6 +20,7 @@
*
*/
#include "compass_sensor_task.h"
+#include "../hono_interaction/hono_interaction.h"
#define HMC588L_ADDRESS 0x1E
#define CALIBRATION_DURATION 10000 //compass calibration has a duration of 5 seconds
@@ -154,6 +155,7 @@
if (i2c_hmc588l_fd >= 0) {
pthread_mutex_lock(&compass_lock);
bearing_shared = getBearingFromSensor();
+ sendTelemetryDataToHonoInstance("idial.institute",8080,"DEFAULT_TENANT", 4771,"Bearing", bearing_shared);
pthread_mutex_unlock(&compass_lock);
//printf("Bearing=%f\n", bearing_shared);
}
diff --git a/rover/src/tasks/infrared_distance_task.cpp b/rover/src/tasks/infrared_distance_task.cpp
index 68f43f2..e4b62ac 100644
--- a/rover/src/tasks/infrared_distance_task.cpp
+++ b/rover/src/tasks/infrared_distance_task.cpp
@@ -22,6 +22,7 @@
*/
#include "infrared_distance_task.h"
+#include "../hono_interaction/hono_interaction.h"
void setupInfraredSensors()
{
@@ -64,6 +65,8 @@
//setupInfraredSensors();
int chan;
+ char str_buf[32];
+
while (1)
{
infrared_distance_task_tmr.recordStartTime();
@@ -75,6 +78,9 @@
for (chan = 0; chan <= 3; chan ++)
{
infrared_shared[chan] = getDistanceFromInfraredSensor(chan);
+ sprintf(str_buf, "IR_%d", chan);
+ sendTelemetryDataToHonoInstance("idial.institute",8080,"DEFAULT_TENANT", 4771, str_buf, infrared_shared[chan]);
+ str_buf[0] = 0;
}
pthread_mutex_unlock(&infrared_lock);
//Task content ends here -------------------------------------------------
diff --git a/rover/src/tasks/temperature_task.cpp b/rover/src/tasks/temperature_task.cpp
index 8f11b4e..ae97699 100644
--- a/rover/src/tasks/temperature_task.cpp
+++ b/rover/src/tasks/temperature_task.cpp
@@ -36,6 +36,7 @@
*/
#include "temperature_task.h"
+#include "../hono_interaction/hono_interaction.h"
static int i2c_th02_fd = 0;
@@ -154,10 +155,13 @@
//Setting argument in pthread - whenever you R/W access to temperature_shared, you have to do the same.
pthread_mutex_lock(&temperature_lock);
temperature_shared = getTemperatureFromSensor();
+ sendTelemetryDataToHonoInstance("idial.institute",8080,"DEFAULT_TENANT", 4771,"temperature", temperature_shared);
pthread_mutex_unlock(&temperature_lock);
pthread_mutex_lock(&humidity_lock);
humidity_shared = getHumidityFromSensor();
+ sendTelemetryDataToHonoInstance("idial.institute",8080,"DEFAULT_TENANT", 4771,"humidity", humidity_shared);
+
pthread_mutex_unlock(&humidity_lock);
//printf("Temperaturex: %f\n", getTemperatureFromSensor());
diff --git a/rover/src/tasks/ultrasonic_sensor_grove_task.cpp b/rover/src/tasks/ultrasonic_sensor_grove_task.cpp
index e52eaa9..21afdfc 100644
--- a/rover/src/tasks/ultrasonic_sensor_grove_task.cpp
+++ b/rover/src/tasks/ultrasonic_sensor_grove_task.cpp
@@ -24,6 +24,7 @@
*/
#include "ultrasonic_sensor_grove_task.h"
+#include "../hono_interaction/hono_interaction.h"
void setup_GrooveUltrasonicRanger() {
//wiringPiSetup(); //Since this can only be used once in a program, we do it in main and comment this.
@@ -80,6 +81,7 @@
pthread_mutex_unlock(&distance_grove_lock);*/
pthread_mutex_lock(&distance_sr04_back_lock);
distance_sr04_back_shared = getCM_GrooveUltrasonicRanger();
+ sendTelemetryDataToHonoInstance("idial.institute",8080,"DEFAULT_TENANT", 4771,"US_Rear", distance_sr04_back_shared);
pthread_mutex_unlock(&distance_sr04_back_lock);
//printf("Distance: %dcm\n", getCM_GrooveUltrasonicRanger());
//Task content ends here -------------------------------------------------
diff --git a/rover/src/tasks/ultrasonic_sensor_sr04_back_task.cpp b/rover/src/tasks/ultrasonic_sensor_sr04_back_task.cpp
index 2aa8132..c35ade4 100644
--- a/rover/src/tasks/ultrasonic_sensor_sr04_back_task.cpp
+++ b/rover/src/tasks/ultrasonic_sensor_sr04_back_task.cpp
@@ -43,7 +43,7 @@
#include "ultrasonic_sensor_sr04_back_task.h"
-
+#include "../hono_interaction/hono_interaction.h"
void setup_HCSR04UltrasonicBack() {
//wiringPiSetup();
@@ -105,6 +105,8 @@
pthread_mutex_lock(&distance_sr04_back_lock);
distance_sr04_back_shared = getCM_HCSR04UltrasonicBack();
pthread_mutex_unlock(&distance_sr04_back_lock);
+ sendTelemetryDataToHonoInstance("idial.institute",8080,"DEFAULT_TENANT", 4771,"US_Rear", distance_sr04_back_shared);
+
//printf("Distance: %dcm\n", getCM_GrooveUltrasonicRanger());
//Task content ends here -------------------------------------------------
diff --git a/rover/src/tasks/ultrasonic_sensor_sr04_front_task.cpp b/rover/src/tasks/ultrasonic_sensor_sr04_front_task.cpp
index c0c7428..4a1247b 100644
--- a/rover/src/tasks/ultrasonic_sensor_sr04_front_task.cpp
+++ b/rover/src/tasks/ultrasonic_sensor_sr04_front_task.cpp
@@ -43,7 +43,7 @@
#include "ultrasonic_sensor_sr04_front_task.h"
-
+#include "../hono_interaction/hono_interaction.h"
void setup_HCSR04UltrasonicFront() {
//wiringPiSetup();
@@ -103,6 +103,7 @@
//Task content starts here -----------------------------------------------
pthread_mutex_lock(&distance_sr04_front_lock);
distance_sr04_front_shared = getCM_HCSR04UltrasonicFront();
+ sendTelemetryDataToHonoInstance("idial.institute",8080,"DEFAULT_TENANT", 4771,"US_Front", distance_sr04_front_shared);
pthread_mutex_unlock(&distance_sr04_front_lock);
//printf("Distance: %dcm\n", getCM_GrooveUltrasonicRanger());
//Task content ends here -------------------------------------------------