Home » Eclipse Projects » Kura » Publish data to the cloud
Publish data to the cloud [message #1764596] |
Wed, 31 May 2017 14:41 |
Javier Ortiz Messages: 8 Registered: May 2017 |
Junior Member |
|
|
Hello there,
I want to publish data to aws cloud, I've already configured it on the kura web UI. Now, I'm my bundle, I'm not sure what to do to publish data to a specific topic.
I've been looking at Bluetooth LE Example and Heater Demo, and I've seen the methods doPublishKeys and doPublish. I understand that those methods are the one used to publish data to a topic. My question is, something called KuraPayload is been used, and I don't know if that is compatible with AWS Json (I want, if possible, to send data in Json format to the cloud).
Will I be able to understand that KuraPayload when it arrives in my aws console?
Is there a way to just publish a message with pairs of "attribute": "valor"?
Also, when in the bundle the cloudservice is set, it's the one I want?, I mean in the kura web UI there are 2 Cloud Services: org.eclipse.kura.cloud.CloudService and org.eclipse.kura.cloud.CloudService-AWS, and from the web UI I did connect to the AWS service. When I call the respective method publishSomething() in my bundle, will it publish de data to the AWS cloud?
I hope I did express myself well.
Thanks for reading,
Javi.
[Updated on: Wed, 31 May 2017 14:52] Report message to a moderator
|
|
| | | |
Re: Publish data to the cloud [message #1764702 is a reply to message #1764614] |
Thu, 01 June 2017 13:41 |
Javier Ortiz Messages: 8 Registered: May 2017 |
Junior Member |
|
|
I did it and it worked!
I've been using the DataService instead of the CloudService to publish de data (I need to have control on the topic) and it has worked good so far. However, in my policy I don't want to allow subscriptions, and if in the policy that we created for the AWS cloud example I remove the action "iot:Subscribe" and try to connect and send data to the cloud, It does not work because the DataService is trying to subscribe to a topic (rpi/rpi/myapp/xmltopic) but it fails and disconnects. 'rpi' is what I put in the attributes topic.context.name and client-id. Here I put what appears in the log:
2017-06-01 13:27:18,023 [DataServiceImpl:ReconnectTask] INFO o.e.k.c.d.t.m.MqttDataTransport - # Connecting...
2017-06-01 13:27:18,025 [pool-9-thread-1] INFO o.e.k.c.s.r.LogStatusRunnable - Notification LED fast blinking
2017-06-01 13:27:18,061 [MQTT Con: rpi] INFO o.e.k.c.s.SSLSocketFactoryWrapper - SSL Endpoint Identification enabled.
2017-06-01 13:27:22,099 [DataServiceImpl:ReconnectTask] INFO o.e.k.c.d.t.m.MqttDataTransport - # Connected!
2017-06-01 13:27:22,100 [DataServiceImpl:ReconnectTask] INFO o.e.k.c.d.t.m.MqttDataTransport - # ------------------------------------------------------------
2017-06-01 13:27:22,101 [pool-9-thread-1] INFO o.e.k.c.s.r.LogStatusRunnable - Notification LED off
2017-06-01 13:27:22,102 [DataServiceImpl:ReconnectTask] INFO o.e.k.c.d.DataServiceImpl - Notified connected
2017-06-01 13:27:22,104 [DataServiceImpl:ReconnectTask] INFO o.e.k.c.d.DataServiceImpl - New session established. Unpublishing all in-flight messages. Disregarding the QoS level, this may cause duplicate messages.
2017-06-01 13:27:22,110 [DataServiceImpl:ReconnectTask] INFO o.e.k.c.c.CloudServiceImpl - Birth certificate republish is disabled in configuration
2017-06-01 13:27:22,112 [DataServiceImpl:ReconnectTask] INFO o.e.k.c.c.CloudServiceImpl - Default subscriptions are disabled in configuration
2017-06-01 13:27:22,113 [DataServiceImpl:ReconnectTask] INFO o.e.k.c.Cloudlet - Cloud Client Connection Restored
2017-06-01 13:27:22,114 [DataServiceImpl:ReconnectTask] INFO o.e.k.c.Cloudlet - Cloud Client Connection Restored
2017-06-01 13:27:22,115 [DataServiceImpl:ReconnectTask] INFO o.e.k.c.Cloudlet - Cloud Client Connection Restored
2017-06-01 13:27:22,117 [DataServiceImpl:ReconnectTask] INFO o.e.k.c.d.t.m.MqttDataTransport - Subscribing to topic: rpi/rpi/myapp/xmltopic with QoS: 0
2017-06-01 13:27:22,196 [DataServiceImpl:ReconnectTask] ERROR o.e.k.c.d.t.m.MqttDataTransport - Cannot subscribe to topic: rpi/rpi/myapp/xmltopic
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:154)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:89)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:112)
... 1 more
2017-06-01 13:27:22,197 [MQTT Rec: rpi] WARN o.e.k.c.d.t.m.MqttDataTransport - Connection Lost
I've been loking into it and try to guess where the bundle tries to subscribe to that topic, but I did not manage to find it.
Is there a way for my bundle to not to try to connect to that topic?
Thanks and regards,
Javi.
|
|
| |
Re: Publish data to the cloud [message #1764711 is a reply to message #1764706] |
Thu, 01 June 2017 14:40 |
Javier Ortiz Messages: 8 Registered: May 2017 |
Junior Member |
|
|
Hello Matteo,
Thanks for answering. My bundle is pretty much like the Bluetooth LE Example, but instead of implementing the CloudServiceListener interface, it does implement the DataServiceListener interface, and uses DataService instead of CloudService to connect and publish data to the cloud.
The bundle scans for BLE devices, but in my case I am interested on reading heart rate measurements from devices that offer the Heart Rate Service, so it does connect to them, and when it receives a heart rate measurement, it publish this data to the cloud, to a specific topic.
So what I do is the following:
1. Get the DataService
public void setDataService(DataService dataService) {
this.dataService = dataService;
}
2. Connect to the cloud
if (!dataService.isConnected())
{
logger.info("Conectando a la nube...");
try {
dataService.connect();
}
catch (KuraConnectException e)
{
logger.error("Error al conectar a la nube", e);
}
}
3. Publish data to the cloud when a heart rate measurement is received
protected static void publishData(String address, int pulso, int energia)
{
if (dataService.isConnected())
{
JSONObject payload = new JSONObject();
payload.put("address", address);
payload.put("pulso", pulso);
if (energia != -1) payload.put("energia", energia);
int qos = 1;
boolean retain = false;
try
{
dataService.publish(topic, payload.toJSONString().getBytes(), qos, retain, 4);
logger.info("Datos publicados!");
} catch (Exception e) {
logger.error("Error al publicar datos", e);
}
}
else logger.error("Error al publicar: Desconectado de la nube.");
}
My bundle does not try to subscribe to any topic in any moment, here are the methods for the DataServiceListener interface:
@Override
public void onDisconnecting()
{
}
@Override
public void onDisconnected()
{
}
@Override
public void onConnectionLost(Throwable cause)
{
}
@Override
public void onConnectionEstablished()
{
}
@Override
public void onMessageArrived(String topic, byte[] payload, int qos, boolean retained)
{
}
@Override
public void onMessageConfirmed(int messageId, String appTopic) {
}
@Override
public void onMessagePublished(int messageId, String appTopic) {
}
Is there something I am doing wrong?
The DataService seems to be doing things that I dont want to be done (like subscribing to that topic, publishing control messages, etc.). Is there anyway to avoid that behavior?
Thanks so much for your help. Regards,
Javi.
|
|
| | | | |
Goto Forum:
Current Time: Thu Sep 19 18:42:18 GMT 2024
Powered by FUDForum. Page generated in 0.05550 seconds
|