Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
I'm trying to connect my Intel Galileo Gen 2 board to thingworkx. I'm following the tutorial Weather Application with Intel Galileo | ThingWorx . In the example, they have used the HTU21D sensor . As it wasn't available where I live, I'm performing the tutorial using a BMP180 sensor. I'm using it to sense temperature and pressure.
I have modified the code given for HTU21D , as follow for BMP180:
Note: For security reasons in have modified my API KEY and MAC address.
include <twApi.h>
#include <twLogger.h>
#include <twOSPort.h>
#include <SFE_BMP180.h>
#include <Ethernet.h>
#include <stdio.h>
#include <string.h>
#include <Wire.h>
SFE_BMP180 tp;
/* Mac address of the network adapter */
byte mac[] = {0x98, 0x4F, 0xEE, 0x01, 0xCB, 0xE9 };
/* Name of your thing */
char * thingName = "myTestTempPress";
/* IP/hostname of your TWX server */
char * serverName = "mitpune66.cloud.thingworx.com";
/* port */
int port = 443;
/* API key */
char * apiKey = "d9c81d35-5a28-49b0-9ddf-e9193eda3b66";
/* refresh rate */
int timeBetweenRefresh = 1000;
/* Hold all the properties */
struct {
double Temperature;
double Pressure;
}
properties;
void sendPropertyUpdate() {
/* Create the property list */
propertyList * proplist = twApi_CreatePropertyList("Temperature",twPrimitive_CreateFromNumber(properties.Temperature), 0);
if (!proplist) {
TW_LOG(TW_ERROR,"sendPropertyUpdate: Error allocating property list");
return;
}
twApi_AddPropertyToList(proplist,"Pressure",twPrimitive_CreateFromNumber(properties.Pressure), 0);
twApi_PushProperties(TW_THING, thingName, proplist, -1, FALSE);
twApi_DeletePropertyList(proplist);
}
void dataCollectionTask()
{
char status;
status = tp.startTemperature();
status = tp.getTemperature(properties.Temperature);
status = tp.startPressure(3);
status = tp.getPressure(properties.Pressure, properties.Temperature);
Serial.print("Time:");
Serial.print(millis());
Serial.print(" Temperature:");
Serial.print(properties.Temperature, 1);
Serial.print("C");
Serial.print(" Pressure:");
Serial.print(properties.Pressure, 1);
Serial.print("mb");
Serial.println();
/* Update the properties on the server */
sendPropertyUpdate();
}
/*****************
* Property Handler Callbacks
******************/
enum msgCodeEnum propertyHandler(const char * entityName, const char * propertyName, twInfoTable ** value, char isWrite, void * userdata) {
char * asterisk = "*";
if (!propertyName) propertyName = asterisk;
TW_LOG(TW_TRACE,"propertyHandler - Function called for Entity %s, Property %s", entityName, propertyName);
if (value) {
/* Property Reads */
if (strcmp(propertyName, "Temperature") == 0) *value = twInfoTable_CreateFromNumber(propertyName, properties.Temperature);
else if (strcmp(propertyName, "Pressure") == 0) *value = twInfoTable_CreateFromNumber(propertyName, properties.Pressure);
else return TWX_NOT_FOUND;
return TWX_SUCCESS;
}
else {
TW_LOG(TW_ERROR,"propertyHandler - NULL pointer for value");
return TWX_BAD_REQUEST;
}
}
void setup() {
int err=0;
/* Open serial connection */
Serial.begin(9600);
/* Wait for someone to launch the console */
delay(3000);
/* Setup the ethernet connection */
Serial.println("Attempting to start Ethernet2");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
else {
Serial.println("Sucess getting dhcp address");
}
/* start adapter */
system("ifup eth0");
/* wait for it to start */
delay(1000);
Serial.print("Galileo IP address: ");
Serial.println(Ethernet.localIP());
system("telnetd -l /bin/sh");
Serial.println("BMP180 Example!");
tp.begin();
Serial.print("Initialize TWX: ");
err = twApi_Initialize(serverName, port, TW_URI, apiKey, NULL, MESSAGE_CHUNK_SIZE, MESSAGE_CHUNK_SIZE, TRUE);
if (err) {
Serial.println("Error initializing the API");
}
/* Allow self signed certs */
twApi_SetSelfSignedOk();
/* Regsiter our properties */
twApi_RegisterProperty(TW_THING, thingName, "Temperature", TW_NUMBER, NULL, "ALWAYS", 0, propertyHandler,NULL);
twApi_RegisterProperty(TW_THING, thingName, "Pressure", TW_NUMBER, NULL, "ALWAYS", 0, propertyHandler,NULL);
/* Bind our thing */
twApi_BindThing(thingName);
/* Connect to server */
if (!twApi_Connect(CONNECT_TIMEOUT, CONNECT_RETRIES)) {
Serial.println("sucessefully connected to TWX!");
}
}
void loop() {
// put your main code here, to run repeatedly:
dataCollectionTask();
delay(timeBetweenRefresh);
Serial.print(".");
}
Please let me know where I'm going wrong and quick response to this issue will be really appreciated as I have to give a demo to my fellow classmates on the day after tomorrow on IOT and ThingWorx.