Hi,
I am trying to send data i.e an integer value using the send function mentioned in IOTKIT.h.
I am not able to send the data. Please guide in this.
My code is as follows
/****************************************************************CODE**************************************************************/
//This example reads the temperature sensor on the Galileo's
//onboard ADC, AD7298, using the iio (Industrial I/O) subsystem and
//sends the observed data to the Intel IoTkit Cloud
#include <IoTkit.h> // include IoTkit.h to use the Intel IoT Kit
#include <Ethernet.h> // must be included to use IoTkit
#include <aJSON.h>
#include <SPI.h>
// create an object of the IoTkit class
IoTkit iotkit;
uint8_t *tag;
int last_byte;
void setup() {
Serial1.begin(9600);
Serial.begin(9600);
Serial1.flush();
// call begin on the IoTkit object before calling any other methods
iotkit.begin();
}
void loop() {
tag = readTag();
last_byte = (int) tag[9];
//Serial.print("Value is ");
//Serial.println(last_byte);
// call send to generate one observation.
// parm1 - the name of the measurement. It must have been previously registered.
// parm2 - the value to send as the measurement observation
// you can also generate your own JSON and send multiple keys and values
// in this format:
//
// {
// “n”: “temperature sensor”,
// “v”: “27.2"
// }
//
// you need to escape the quotations to pass it directly to iotkit.send:
// iotkit.send("{\"n\": \"temperature sensor\",\"v\":\"27.2\"}");
//
iotkit.send("b_temp", last_byte);
// you can also send a /full JSON string with your own variables:
//
// aJsonObject* root = aJson.createObject();
// if (root != NULL) {
// aJson.addItemToObject(root, "n", aJson.createItem(metric));
// aJson.addItemToObject(root, "v", aJson.createItem(value));
// iotkit.send(aJson.print(root)); // this sends your full json
// aJson.deleteItem(root);
// }
//
// If you are receiving incoming commands, listen for them with receive
// If you have your own custom json-parsing receive function, pass as argument
// such as iotkit.receive(callback);
// It must follow this prototype, but any name: void callback(char* json)
//
//iotkit.receive();
iotkit.receive(callback);
delay(1500);
}
// this is an example callback that parses a user-created JSON in the form
// {
// "component": "led",
// "command": "off"
// }
// and turns off LED at pin 13 hard-coded
//
void callback(char* json) {
Serial.println(json);
aJsonObject* parsed = aJson.parse(json);
if (&parsed == NULL) {
// invalid or empty JSON
Serial.println("recieved invalid JSON");
return;
}
aJsonObject* component = aJson.getObjectItem(parsed, "component");
aJsonObject* command = aJson.getObjectItem(parsed, "command");
if ((component != NULL)) {
if (strcmp(component->valuestring, "led") == 0) {
if ((command != NULL)) {
if (strcmp(command->valuestring, "off") == 0) {
pinMode(13, OUTPUT);
digitalWrite(13, false);
}
if (strcmp(command->valuestring, "on") == 0) {
pinMode(13, OUTPUT);
digitalWrite(13, true);
}
}
}
}
}
uint8_t* readTag()
{
char bytestoRead;
uint8_t TagData[10];
char header;
int var;
int counter = 0;
int clientresult;
//Serial.println("Inside readTag");
if(Serial1.available()>0)
{
//Serial.println("Inside readTag111");
if((header =Serial1.read()) == 36)
{
//bytestoRead = 0 ;
//TagData = "";
counter = 0;
for ( var = 0 ; var <10;var++)
{
bytestoRead = Serial1.read();
//Serial.print(bytestoRead);
if(bytestoRead>='0' && bytestoRead<= '9')
TagData[counter] = bytestoRead;
Serial.print(TagData[counter]);
counter ++;
}
//TagData[11] = '/n';
// Serial.print(TagData[counter]);
//clientresult = client.print(TagData+'\n');
//Serial.print(clientresult);
Serial1.flush();
}
}
return TagData;
}
I have register the b_temp with my device.
Pleas guide me