Commit Iniziale, progetto funzionante caricato su box ETcontroller in
cantina
This commit is contained in:
115
MQTT/MQTTPacket/samples/publish-subscribe.txt
Normal file
115
MQTT/MQTTPacket/samples/publish-subscribe.txt
Normal file
@@ -0,0 +1,115 @@
|
||||
#include "MQTTPacket.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "EthernetInterface.h"
|
||||
|
||||
|
||||
TCPSocketConnection mysock;
|
||||
|
||||
int getdata(char* buf, int count)
|
||||
{
|
||||
return mysock.receive(buf, (size_t)count);
|
||||
}
|
||||
|
||||
int toStop = 0;
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
|
||||
int rc = 0;
|
||||
char buf[200];
|
||||
int buflen = sizeof(buf);
|
||||
int msgid = 1;
|
||||
MQTTString topicString = MQTTString_initializer;
|
||||
int req_qos = 0;
|
||||
char* payload = "mypayload";
|
||||
int payloadlen = strlen(payload);
|
||||
int len = 0;
|
||||
EthernetInterface eth;
|
||||
|
||||
eth.init(); //Use DHCP
|
||||
eth.connect();
|
||||
|
||||
rc = mysock.connect("m2m.eclipse.org", 1883);
|
||||
|
||||
data.clientID.cstring = "SendReceive mbed MQTT ";
|
||||
data.keepAliveInterval = 20;
|
||||
data.cleansession = 1;
|
||||
|
||||
mysock.set_blocking(true, 1000); /* 1 second Timeout */
|
||||
|
||||
len = MQTTSerialize_connect(buf, buflen, &data);
|
||||
rc = mysock.send(buf, len);
|
||||
|
||||
/* wait for connack */
|
||||
if (MQTTPacket_read(buf, buflen, getdata) == CONNACK)
|
||||
{
|
||||
int connack_rc;
|
||||
|
||||
if (MQTTDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
|
||||
{
|
||||
printf("Unable to connect, return code %d\n", connack_rc);
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
else
|
||||
goto exit;
|
||||
|
||||
/* subscribe */
|
||||
topicString.cstring = "substopic";
|
||||
len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos);
|
||||
|
||||
rc = mysock.send(buf, len);
|
||||
if (MQTTPacket_read(buf, buflen, getdata) == SUBACK) /* wait for suback */
|
||||
{
|
||||
int submsgid;
|
||||
int subcount;
|
||||
int granted_qos;
|
||||
|
||||
rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen);
|
||||
if (granted_qos != 0)
|
||||
{
|
||||
printf("granted qos != 0, %d\n", granted_qos);
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
else
|
||||
goto exit;
|
||||
|
||||
topicString.cstring = "pubtopic";
|
||||
while (!toStop)
|
||||
{
|
||||
if (MQTTPacket_read(buf, buflen, getdata) == PUBLISH)
|
||||
{
|
||||
int dup;
|
||||
int qos;
|
||||
int retained;
|
||||
int msgid;
|
||||
int payloadlen_in;
|
||||
char* payload_in;
|
||||
int rc;
|
||||
MQTTString receivedTopic;
|
||||
|
||||
rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic,
|
||||
&payload_in, &payloadlen_in, buf, buflen);
|
||||
printf("message arrived %.*s\n", payloadlen_in, payload_in);
|
||||
}
|
||||
|
||||
printf("publishing reading\n");
|
||||
len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, payload, payloadlen);
|
||||
rc = mysock.send(buf, len);
|
||||
}
|
||||
|
||||
printf("disconnecting\n");
|
||||
len = MQTTSerialize_disconnect(buf, buflen);
|
||||
rc = mysock.send(buf, len);
|
||||
|
||||
exit:
|
||||
eth.disconnect();
|
||||
|
||||
return 0;
|
||||
}
|
||||
89
MQTT/MQTTPacket/samples/simple-publish.txt
Normal file
89
MQTT/MQTTPacket/samples/simple-publish.txt
Normal file
@@ -0,0 +1,89 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 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:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "mbed.h"
|
||||
#include "EthernetInterface.h"
|
||||
#include "C12832_lcd.h"
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
|
||||
DigitalOut myled(LED2);
|
||||
C12832_LCD lcd;
|
||||
|
||||
int publish()
|
||||
{
|
||||
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
|
||||
int rc = 0;
|
||||
char buf[200];
|
||||
int buflen = sizeof(buf);
|
||||
TCPSocketConnection mysock;
|
||||
MQTTString topicString = MQTTString_initializer;
|
||||
char* payload = "I'm alive!";
|
||||
int payloadlen = strlen(payload);
|
||||
int len = 0;
|
||||
|
||||
mysock.connect("m2m.eclipse.org", 1883);
|
||||
|
||||
data.clientID.cstring = "mbed test client - Ian Craggs";
|
||||
data.keepAliveInterval = 20;
|
||||
data.cleansession = 1;
|
||||
data.MQTTVersion = 3;
|
||||
|
||||
len = MQTTSerialize_connect(buf, buflen, &data);
|
||||
|
||||
topicString.cstring = "mbed NXP LPC1768";
|
||||
len += MQTTSerialize_publish(buf + len, buflen - len, 0, 0, 0, 0, topicString, payload, payloadlen);
|
||||
|
||||
len += MQTTSerialize_disconnect(buf + len, buflen - len);
|
||||
|
||||
rc = 0;
|
||||
while (rc < len)
|
||||
{
|
||||
int rc1 = mysock.send(buf, len);
|
||||
if (rc1 == -1)
|
||||
{
|
||||
lcd.printf("Send failed\n");
|
||||
break;
|
||||
}
|
||||
else
|
||||
rc += rc1;
|
||||
}
|
||||
if (rc == len)
|
||||
lcd.printf("Send succeeded\n");
|
||||
wait(0.2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
EthernetInterface eth;
|
||||
eth.init(); //Use DHCP
|
||||
eth.connect();
|
||||
lcd.printf("IP Address is %s\n", eth.getIPAddress());
|
||||
|
||||
while(1)
|
||||
{
|
||||
myled = 1;
|
||||
publish();
|
||||
wait(0.2);
|
||||
myled = 0;
|
||||
publish();
|
||||
wait(0.2);
|
||||
}
|
||||
|
||||
eth.disconnect();
|
||||
}
|
||||
Reference in New Issue
Block a user