MQTT ESP8266 basic test

Committer:
nenad
Date:
Thu Apr 30 23:31:41 2020 +0000
Revision:
0:75fa10a22545
Child:
1:276a09a95334
PMK2020 MQTT ethernet test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nenad 0:75fa10a22545 1 /* mbed Microcontroller Library
nenad 0:75fa10a22545 2 * Copyright (c) 2019 ARM Limited
nenad 0:75fa10a22545 3 * SPDX-License-Identifier: Apache-2.0
nenad 0:75fa10a22545 4 */
nenad 0:75fa10a22545 5
nenad 0:75fa10a22545 6 #include "mbed.h"
nenad 0:75fa10a22545 7 #include "EthernetInterface.h"
nenad 0:75fa10a22545 8 #include "platform/mbed_thread.h"
nenad 0:75fa10a22545 9 #include <MQTTClientMbedOs.h>
nenad 0:75fa10a22545 10 // Blinking rate in milliseconds
nenad 0:75fa10a22545 11 #define BLINKING_RATE_MS 500
nenad 0:75fa10a22545 12 // Initialise the digital pin LED1 as an output
nenad 0:75fa10a22545 13 DigitalOut led(LED1);
nenad 0:75fa10a22545 14 Serial pc(USBTX,USBRX);
nenad 0:75fa10a22545 15
nenad 0:75fa10a22545 16 InterruptIn button(USER_BUTTON);
nenad 0:75fa10a22545 17
nenad 0:75fa10a22545 18 int arrivedcount = 0;
nenad 0:75fa10a22545 19 TCPSocket socket;
nenad 0:75fa10a22545 20 MQTTClient client(&socket);
nenad 0:75fa10a22545 21 MQTT::Message message;
nenad 0:75fa10a22545 22
nenad 0:75fa10a22545 23 int button_pressed=0;
nenad 0:75fa10a22545 24
nenad 0:75fa10a22545 25 float version = 0.6;
nenad 0:75fa10a22545 26 char* topic = "mbed-sample-pub";
nenad 0:75fa10a22545 27 char* topic_sub = "mbed-sample-sub";
nenad 0:75fa10a22545 28
nenad 0:75fa10a22545 29
nenad 0:75fa10a22545 30 void messageArrived(MQTT::MessageData& md)
nenad 0:75fa10a22545 31 {
nenad 0:75fa10a22545 32 MQTT::Message &message = md.message;
nenad 0:75fa10a22545 33 pc.printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
nenad 0:75fa10a22545 34 pc.printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
nenad 0:75fa10a22545 35 ++arrivedcount;
nenad 0:75fa10a22545 36 }
nenad 0:75fa10a22545 37
nenad 0:75fa10a22545 38 void buttonFunction() {
nenad 0:75fa10a22545 39
nenad 0:75fa10a22545 40 button_pressed=1;
nenad 0:75fa10a22545 41
nenad 0:75fa10a22545 42 }
nenad 0:75fa10a22545 43
nenad 0:75fa10a22545 44
nenad 0:75fa10a22545 45
nenad 0:75fa10a22545 46
nenad 0:75fa10a22545 47 int main()
nenad 0:75fa10a22545 48 {
nenad 0:75fa10a22545 49 button.rise(&buttonFunction); // attach the address of the flip function to the rising edge
nenad 0:75fa10a22545 50
nenad 0:75fa10a22545 51 const char* hostname = "broker.mqttdashboard.com";
nenad 0:75fa10a22545 52 int port = 1883;
nenad 0:75fa10a22545 53 // Network interface
nenad 0:75fa10a22545 54 EthernetInterface net;
nenad 0:75fa10a22545 55
nenad 0:75fa10a22545 56 // Bring up the ethernet interface
nenad 0:75fa10a22545 57 printf("Ethernet socket example\n");
nenad 0:75fa10a22545 58 net.connect();
nenad 0:75fa10a22545 59
nenad 0:75fa10a22545 60 // Show the network address
nenad 0:75fa10a22545 61 const char *ip = net.get_ip_address();
nenad 0:75fa10a22545 62 pc.printf("IP address is: %s\n", ip ? ip : "No IP");
nenad 0:75fa10a22545 63
nenad 0:75fa10a22545 64
nenad 0:75fa10a22545 65 //client = MQTTClient::(&socket);
nenad 0:75fa10a22545 66 socket.open(&net);
nenad 0:75fa10a22545 67 socket.connect(hostname, port);
nenad 0:75fa10a22545 68
nenad 0:75fa10a22545 69 int rc=0;
nenad 0:75fa10a22545 70
nenad 0:75fa10a22545 71 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
nenad 0:75fa10a22545 72 data.MQTTVersion = 3;
nenad 0:75fa10a22545 73 data.clientID.cstring = "mbed-sample";
nenad 0:75fa10a22545 74 //data.username.cstring = "testuser";
nenad 0:75fa10a22545 75 //data.password.cstring = "testpassword";
nenad 0:75fa10a22545 76 if ((rc = client.connect(data)) != 0)
nenad 0:75fa10a22545 77 pc.printf("rc from MQTT connect is %d\r\n", rc);
nenad 0:75fa10a22545 78
nenad 0:75fa10a22545 79 if ((rc = client.subscribe(topic_sub, MQTT::QOS2, messageArrived)) != 0)
nenad 0:75fa10a22545 80 pc.printf("rc from MQTT subscribe is %d\r\n", rc);
nenad 0:75fa10a22545 81
nenad 0:75fa10a22545 82
nenad 0:75fa10a22545 83
nenad 0:75fa10a22545 84
nenad 0:75fa10a22545 85
nenad 0:75fa10a22545 86
nenad 0:75fa10a22545 87
nenad 0:75fa10a22545 88 while (true) {
nenad 0:75fa10a22545 89 led = !led;
nenad 0:75fa10a22545 90 thread_sleep_for(BLINKING_RATE_MS);
nenad 0:75fa10a22545 91 if (button_pressed==1) {
nenad 0:75fa10a22545 92 button_pressed=0;
nenad 0:75fa10a22545 93 pc.printf("sace publish\r\n");
nenad 0:75fa10a22545 94 // QoS 0
nenad 0:75fa10a22545 95 char buf[100];
nenad 0:75fa10a22545 96 sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version);
nenad 0:75fa10a22545 97 message.qos = MQTT::QOS0;
nenad 0:75fa10a22545 98 message.retained = false;
nenad 0:75fa10a22545 99 message.dup = false;
nenad 0:75fa10a22545 100 message.payload = (void*)buf;
nenad 0:75fa10a22545 101 message.payloadlen = strlen(buf)+1;
nenad 0:75fa10a22545 102 client.publish(topic, message);
nenad 0:75fa10a22545 103 //pc.printf("sace jild\r\n");
nenad 0:75fa10a22545 104 //client.yield(1000);
nenad 0:75fa10a22545 105 //pc.printf("izjildovo\r\n");
nenad 0:75fa10a22545 106 }
nenad 0:75fa10a22545 107 pc.printf("sace jild\r\n");
nenad 0:75fa10a22545 108 client.yield(1000);
nenad 0:75fa10a22545 109 pc.printf("izjildovo\r\n");
nenad 0:75fa10a22545 110
nenad 0:75fa10a22545 111 }
nenad 0:75fa10a22545 112 }