Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
qos0pub_register.c
00001 /******************************************************************************* 00002 * Copyright (c) 2014 IBM Corp. 00003 * 00004 * All rights reserved. This program and the accompanying materials 00005 * are made available under the terms of the Eclipse Public License v1.0 00006 * and Eclipse Distribution License v1.0 which accompany this distribution. 00007 * 00008 * The Eclipse Public License is available at 00009 * http://www.eclipse.org/legal/epl-v10.html 00010 * and the Eclipse Distribution License is available at 00011 * http://www.eclipse.org/org/documents/edl-v10.php. 00012 * 00013 * Contributors: 00014 * Ian Craggs - initial API and implementation and/or initial documentation 00015 * Sergio R. Caprile - clarifications and/or documentation extension 00016 * 00017 * Description: 00018 * Normal topic name used to show registration process 00019 *******************************************************************************/ 00020 00021 #include <stdio.h> 00022 #include <string.h> 00023 #include <stdlib.h> 00024 #include <unistd.h> 00025 00026 #include "MQTTSNPacket.h" 00027 #include "transport.h" 00028 00029 00030 int main(int argc, char** argv) 00031 { 00032 int rc = 0; 00033 int mysock; 00034 unsigned char buf[500]; 00035 int buflen = sizeof(buf); 00036 MQTTSN_topicid topic; 00037 MQTTSNString topicstr; 00038 int len = 0; 00039 int retained = 0; 00040 char *topicname = "iot-2/evt/status/fmt/json"; 00041 char *host = "127.0.0.1"; 00042 int port = 20000; 00043 MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer; 00044 unsigned short topicid; 00045 00046 mysock = transport_open(); 00047 if(mysock < 0) 00048 return mysock; 00049 00050 if (argc > 1) 00051 host = argv[1]; 00052 00053 if (argc > 2) 00054 port = atoi(argv[2]); 00055 00056 printf("Sending to hostname %s port %d\n", host, port); 00057 00058 options.clientID.cstring = "d:quickstart:udptest:9002f7f1ad23"; 00059 len = MQTTSNSerialize_connect(buf, buflen, &options); 00060 rc = transport_sendPacketBuffer(host, port, buf, len); 00061 00062 /* wait for connack */ 00063 if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_CONNACK) 00064 { 00065 int connack_rc = -1; 00066 00067 if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0) 00068 { 00069 printf("Unable to connect, return code %d\n", connack_rc); 00070 goto exit; 00071 } 00072 else 00073 printf("connected rc %d\n", connack_rc); 00074 } 00075 else 00076 { 00077 printf("could not connect to gateway\n"); 00078 goto exit; 00079 } 00080 00081 /* register topic name */ 00082 printf("Registering\n"); 00083 int packetid = 1; 00084 topicstr.cstring = topicname; 00085 topicstr.lenstring.len = strlen(topicname); 00086 len = MQTTSNSerialize_register(buf, buflen, 0, packetid, &topicstr); 00087 rc = transport_sendPacketBuffer(host, port, buf, len); 00088 00089 if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_REGACK) /* wait for regack */ 00090 { 00091 unsigned short submsgid; 00092 unsigned char returncode; 00093 00094 rc = MQTTSNDeserialize_regack(&topicid, &submsgid, &returncode, buf, buflen); 00095 if (returncode != 0) 00096 { 00097 printf("return code %d\n", returncode); 00098 goto exit; 00099 } 00100 else 00101 printf("regack topic id %d\n", topicid); 00102 } 00103 else 00104 goto exit; 00105 00106 while (1) 00107 { 00108 if (1) 00109 { 00110 /* publish with obtained id */ 00111 printf("Publishing\n"); 00112 topic.type = MQTTSN_TOPIC_TYPE_NORMAL; 00113 topic.data.id = topicid; 00114 static const char* joypos[] = {"SN-LEFT", "SN-RIGHT", "SN-CENTRE", "SN-UP", "SN-DOWN"}; 00115 00116 unsigned char payload[250]; 00117 int payloadlen = sprintf((char*)payload, 00118 "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}", 00119 (rand() % 10) * 2.0, (rand() % 10) * 2.0, (rand() % 10) * 2.0, (rand() % 10) + 18.0, joypos[rand() % 5], (rand() % 10) * 30.0, (rand() % 10) * 30.0); 00120 len = MQTTSNSerialize_publish(buf, buflen, 0, 0, retained, 0, topic, payload, payloadlen); 00121 rc = transport_sendPacketBuffer(host, port, buf, len); 00122 00123 printf("rc %d from send packet for publish length %d\n", rc, len); 00124 } 00125 sleep(1); // Publish a message every second 00126 } 00127 exit: 00128 transport_close(); 00129 00130 return 0; 00131 }
Generated on Wed Jul 13 2022 10:46:03 by
1.7.2