Paho MQTT Client example with W7500

Dependencies:   DHT MQTTforLecture WIZnetInterface mbed-src

Fork of w7500-paho-mqtt by Bohyun Bang

Code

WIZwiki W7500 with paho mqtt client.

Import programw7500-paho-mqtt

Paho MQTT Client example with W7500

You have to change

You have to use your ethernet(network) information.

MQTTEthernet.h

    uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x00, 0x00}; // your mac address
    const char * ip_addr = "???.???.???.???"; // your ip
    const char * gw_addr = "???.???.???.???"; // your gateway
    const char * snmask = "???.???.???.???"; // your subnetmask

And you have to use your mqtt broker server information. Broker server ip address and port number listed bleow are available now. But I will close the mqtt broker server on July 15,2015.

main.cpp

    char* hostname = "104.199.146.45";
    int port = 1883;

Platform

WIZwiki W7500 [ https://developer.mbed.org/platforms/WIZwiki-W7500/ ] /media/uploads/bangbh/wizwiki-w7500.jpg

Component

YWRobot Easy module shield [ where to buy ] (This is not mbed component) /media/uploads/bangbh/ywrobot_esay-module-shield-v1.jpg

Test software

PC

MQTT-FX - A JavaFX based MQTT Client [ Home page ] /media/uploads/bangbh/mqtt-fx.png

Mobile(iPhone)

ZMQTT-UTILITY - MQTT Test utility|You can download from App store(iPhone). /media/uploads/bangbh/iphone-zmqtt-utility.png

Committer:
bangbh
Date:
Mon Sep 21 23:39:16 2015 +0000
Revision:
15:d4604c47da41
Parent:
14:2b30a541da82
for lecture

Who changed what in which revision?

UserRevisionLine numberNew contents of line
justinkim8:f837e0d255e8 1#include "mbed.h"
bangbh9:21574e058141 2#include "DHT.h"
bangbh9:21574e058141 3#include "MQTTEthernet.h"
bangbh9:21574e058141 4#include "MQTTClient.h"
justinkim8:f837e0d255e8 5
justinkim8:f837e0d255e8 6#define ECHO_SERVER_PORT 7
justinkim8:f837e0d255e8 7
bangbh9:21574e058141 8#include "mbed.h"
bangbh9:21574e058141 9
bangbh9:21574e058141 10int arrivedcount = 0;
bangbh9:21574e058141 11
bangbh9:21574e058141 12void messageArrived(MQTT::MessageData& md)
justinkim8:f837e0d255e8 13{
bangbh9:21574e058141 14 MQTT::Message &message = md.message;
bangbh9:21574e058141 15 printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
bangbh9:21574e058141 16 printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
bangbh9:21574e058141 17 ++arrivedcount;
bangbh9:21574e058141 18}
bangbh9:21574e058141 19
bangbh9:21574e058141 20void baud(int baudrate) {
bangbh9:21574e058141 21 Serial s(USBTX, USBRX);
bangbh9:21574e058141 22 s.baud(baudrate);
bangbh9:21574e058141 23}
bangbh9:21574e058141 24
bangbh9:21574e058141 25int main (void)
bangbh9:21574e058141 26{
bangbh9:21574e058141 27 DHT sensor(D4, DHT11);
bangbh9:21574e058141 28 AnalogIn ain(A1);
bangbh9:21574e058141 29 baud(115200);
justinkim8:f837e0d255e8 30 printf("Wait a second...\r\n");
bangbh9:21574e058141 31 MQTTEthernet ipstack = MQTTEthernet();
justinkim8:f837e0d255e8 32
bangbh9:21574e058141 33 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
justinkim8:f837e0d255e8 34
bangbh13:36456500fb7e 35 char* hostname = "222.98.173.244";
bangbh9:21574e058141 36 int port = 1883;
bangbh9:21574e058141 37
bangbh9:21574e058141 38 int rc = ipstack.connect(hostname, port);
bangbh9:21574e058141 39 if (rc != 0)
bangbh9:21574e058141 40 printf("rc from TCP connect is %d\n", rc);
bangbh9:21574e058141 41
bangbh15:d4604c47da41 42 char MQTTClientID[30];
bangbh9:21574e058141 43
bangbh9:21574e058141 44 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
bangbh9:21574e058141 45 data.MQTTVersion = 3;
bangbh15:d4604c47da41 46 sprintf(MQTTClientID,"WIZwiki-W7500-client-%d",rand()%1000);
bangbh15:d4604c47da41 47 data.clientID.cstring = MQTTClientID;
bangbh9:21574e058141 48 data.username.cstring = "testuser";
bangbh9:21574e058141 49 data.password.cstring = "testpassword";
bangbh9:21574e058141 50
bangbh9:21574e058141 51 if ((rc = client.connect(data)) != 0)
bangbh9:21574e058141 52 printf("rc from MQTT connect is %d\n", rc);
bangbh9:21574e058141 53
bangbh9:21574e058141 54 MQTT::Message message;
bangbh9:21574e058141 55 char buf[100];
bangbh9:21574e058141 56 int error = 0;
bangbh9:21574e058141 57 float hum = 0.0f, temp = 0.0f;
bangbh13:36456500fb7e 58 char i = 0;
bangbh13:36456500fb7e 59 while (true)
justinkim8:f837e0d255e8 60 {
bangbh13:36456500fb7e 61 if(i > 100) i = 0;
bangbh9:21574e058141 62 error = sensor.readData();
bangbh9:21574e058141 63 if (0 == error) {
bangbh9:21574e058141 64 hum = sensor.ReadHumidity();
bangbh9:21574e058141 65 temp = sensor.ReadTemperature(CELCIUS);
justinkim8:f837e0d255e8 66 }
bangbh9:21574e058141 67 message.qos = MQTT::QOS0;
bangbh9:21574e058141 68 message.retained = false;
bangbh9:21574e058141 69 message.dup = false;
bangbh13:36456500fb7e 70
bangbh13:36456500fb7e 71 sprintf(buf, "%3.1f", hum);
bangbh9:21574e058141 72 message.payload = (void*)buf;
bangbh13:36456500fb7e 73 message.payloadlen = strlen(buf);
bangbh13:36456500fb7e 74 rc = client.publish("Wiznet/humidity", message);
bangbh13:36456500fb7e 75
bangbh9:21574e058141 76 sprintf(buf, "%3.1f", temp);
bangbh9:21574e058141 77 message.payload = (void*)buf;
bangbh13:36456500fb7e 78 message.payloadlen = strlen(buf);
bangbh13:36456500fb7e 79 rc = client.publish("Wiznet/temperature", message);
bangbh13:36456500fb7e 80
bangbh13:36456500fb7e 81 sprintf(buf, "%3.1f", (rand()%1000)/10.0);
bangbh9:21574e058141 82 message.payload = (void*)buf;
bangbh13:36456500fb7e 83 message.payloadlen = strlen(buf);
bangbh13:36456500fb7e 84 rc = client.publish("Wiznet/fluidgauge/A", message);
bangbh13:36456500fb7e 85
bangbh13:36456500fb7e 86 sprintf(buf, "%3.1f", (rand()%1000)/10.0);
bangbh13:36456500fb7e 87 message.payload = (void*)buf;
bangbh13:36456500fb7e 88 message.payloadlen = strlen(buf);
bangbh13:36456500fb7e 89 rc = client.publish("Wiznet/fluidgauge/B", message);
bangbh13:36456500fb7e 90
bangbh13:36456500fb7e 91 sprintf(buf, "%3.1f", (rand()%1000)/10.0);
bangbh13:36456500fb7e 92 message.payload = (void*)buf;
bangbh13:36456500fb7e 93 message.payloadlen = strlen(buf);
bangbh13:36456500fb7e 94 rc = client.publish("Wiznet/fluidgauge/C", message);
bangbh9:21574e058141 95 client.yield(1000);
bangbh13:36456500fb7e 96 //wait(100);
justinkim8:f837e0d255e8 97 }
justinkim8:f837e0d255e8 98}
justinkim8:f837e0d255e8 99