case5

Dependencies:   mbed SeeedShieldBot BluetoothSerial

Committer:
roman360025
Date:
Wed Sep 15 15:08:26 2021 +0000
Revision:
0:469ef45c1fe2
case5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
roman360025 0:469ef45c1fe2 1 #include "mbed.h"
roman360025 0:469ef45c1fe2 2
roman360025 0:469ef45c1fe2 3 #define MQTT_MAX_PACKET_SIZE 400
roman360025 0:469ef45c1fe2 4 #define MQTT_MAX_PAYLOAD_SIZE 300
roman360025 0:469ef45c1fe2 5
roman360025 0:469ef45c1fe2 6 void ibm_cloud_demo(NetworkInterface *net)
roman360025 0:469ef45c1fe2 7 {
roman360025 0:469ef45c1fe2 8 TCPSocket socket;
roman360025 0:469ef45c1fe2 9 MQTTClient client(&socket);
roman360025 0:469ef45c1fe2 10
roman360025 0:469ef45c1fe2 11 SocketAddress a;
roman360025 0:469ef45c1fe2 12 char* hostname = "quickstart.messaging.internetofthings.ibmcloud.com";
roman360025 0:469ef45c1fe2 13 net->gethostbyname(hostname, &a);
roman360025 0:469ef45c1fe2 14 int port = 1883;
roman360025 0:469ef45c1fe2 15 a.set_port(port);
roman360025 0:469ef45c1fe2 16
roman360025 0:469ef45c1fe2 17 printf("Connecting to %s:%d\r\n", hostname, port);
roman360025 0:469ef45c1fe2 18
roman360025 0:469ef45c1fe2 19 socket.open(net);
roman360025 0:469ef45c1fe2 20 printf("Opened socket\n\r");
roman360025 0:469ef45c1fe2 21 int rc = socket.connect(a);
roman360025 0:469ef45c1fe2 22 if (rc != 0)
roman360025 0:469ef45c1fe2 23 printf("rc from TCP connect is %d\r\n", rc);
roman360025 0:469ef45c1fe2 24 printf("Connected socket\n\r");
roman360025 0:469ef45c1fe2 25
roman360025 0:469ef45c1fe2 26
roman360025 0:469ef45c1fe2 27 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
roman360025 0:469ef45c1fe2 28 data.MQTTVersion = 3;
roman360025 0:469ef45c1fe2 29 data.clientID.cstring = "d:quickstart:my_test:my_test_board";
roman360025 0:469ef45c1fe2 30 data.username.cstring = "testuser";
roman360025 0:469ef45c1fe2 31 data.password.cstring = "testpassword";
roman360025 0:469ef45c1fe2 32 if ((rc = client.connect(data)) != 0)
roman360025 0:469ef45c1fe2 33 printf("rc from MQTT connect is %d\r\n", rc);
roman360025 0:469ef45c1fe2 34
roman360025 0:469ef45c1fe2 35 MQTT::Message message;
roman360025 0:469ef45c1fe2 36
roman360025 0:469ef45c1fe2 37 char buf[MQTT_MAX_PAYLOAD_SIZE];
roman360025 0:469ef45c1fe2 38 float temp = 1;
roman360025 0:469ef45c1fe2 39 float press = 2;
roman360025 0:469ef45c1fe2 40 float hum = 3;
roman360025 0:469ef45c1fe2 41
roman360025 0:469ef45c1fe2 42 sprintf(buf,
roman360025 0:469ef45c1fe2 43 "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Temp\":%0.4f,\"Pressure\":%0.4f,\"Humidity\":%0.4f}}",
roman360025 0:469ef45c1fe2 44 temp, press, hum);
roman360025 0:469ef45c1fe2 45 message.qos = MQTT::QOS0;
roman360025 0:469ef45c1fe2 46 message.retained = false;
roman360025 0:469ef45c1fe2 47 message.dup = false;
roman360025 0:469ef45c1fe2 48 message.payload = (void*)buf;
roman360025 0:469ef45c1fe2 49 message.payloadlen = strlen(buf);
roman360025 0:469ef45c1fe2 50
roman360025 0:469ef45c1fe2 51 char* topic = "iot-2/evt/my_event/fmt/json";
roman360025 0:469ef45c1fe2 52
roman360025 0:469ef45c1fe2 53 if( (message.payloadlen + strlen(topic)+1) >= MQTT_MAX_PACKET_SIZE )
roman360025 0:469ef45c1fe2 54 printf("message too long!\r\n");
roman360025 0:469ef45c1fe2 55
roman360025 0:469ef45c1fe2 56 rc = client.publish(topic, message);
roman360025 0:469ef45c1fe2 57 return;
roman360025 0:469ef45c1fe2 58 }
roman360025 0:469ef45c1fe2 59
roman360025 0:469ef45c1fe2 60
roman360025 0:469ef45c1fe2 61 nt main()
roman360025 0:469ef45c1fe2 62 {
roman360025 0:469ef45c1fe2 63 ibm_cloud_demo(NetworkInterface *net);
roman360025 0:469ef45c1fe2 64 }