Bohyun Bang / w7500-paho-mqtt

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

main.cpp

Committer:
bangbh
Date:
21 months ago
Revision:
15:d4604c47da41
Parent:
14:2b30a541da82

File content as of revision 15:d4604c47da41:

#include "mbed.h"
#include "DHT.h"
#include "MQTTEthernet.h"
#include "MQTTClient.h"

#define ECHO_SERVER_PORT   7

#include "mbed.h"
 
int arrivedcount = 0;
 
void messageArrived(MQTT::MessageData& md)
{
    MQTT::Message &message = md.message;
    printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
    printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
    ++arrivedcount;
}

void baud(int baudrate) {
    Serial s(USBTX, USBRX);
    s.baud(baudrate);
}

int main (void)
{
    DHT sensor(D4, DHT11);
    AnalogIn ain(A1);
    baud(115200);
    printf("Wait a second...\r\n");
    MQTTEthernet ipstack = MQTTEthernet();
    
    MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
    
    char* hostname = "222.98.173.244";
    int port = 1883;
    
    int rc = ipstack.connect(hostname, port);
    if (rc != 0)
        printf("rc from TCP connect is %d\n", rc);
        
    char MQTTClientID[30];
    
    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
    data.MQTTVersion = 3;
    sprintf(MQTTClientID,"WIZwiki-W7500-client-%d",rand()%1000);
    data.clientID.cstring = MQTTClientID;
    data.username.cstring = "testuser";
    data.password.cstring = "testpassword";  

    if ((rc = client.connect(data)) != 0)
        printf("rc from MQTT connect is %d\n", rc);
        
    MQTT::Message message;
    char buf[100];
    int error = 0;
    float hum = 0.0f, temp = 0.0f;
    char i = 0;
    while (true)
    {
        if(i > 100) i = 0;
        error = sensor.readData();
        if (0 == error) {
            hum = sensor.ReadHumidity();
            temp = sensor.ReadTemperature(CELCIUS);
        }
        message.qos = MQTT::QOS0;
        message.retained = false;
        message.dup = false;
        
        sprintf(buf, "%3.1f", hum);
        message.payload = (void*)buf;
        message.payloadlen = strlen(buf);
        rc = client.publish("Wiznet/humidity", message);
        
        sprintf(buf, "%3.1f", temp);
        message.payload = (void*)buf;
        message.payloadlen = strlen(buf);
        rc = client.publish("Wiznet/temperature", message);
        
        sprintf(buf, "%3.1f", (rand()%1000)/10.0);
        message.payload = (void*)buf;
        message.payloadlen = strlen(buf);
        rc = client.publish("Wiznet/fluidgauge/A", message);
        
        sprintf(buf, "%3.1f", (rand()%1000)/10.0);
        message.payload = (void*)buf;
        message.payloadlen = strlen(buf);
        rc = client.publish("Wiznet/fluidgauge/B", message);
        
        sprintf(buf, "%3.1f", (rand()%1000)/10.0);
        message.payload = (void*)buf;
        message.payloadlen = strlen(buf);
        rc = client.publish("Wiznet/fluidgauge/C", message);
        client.yield(1000);
        //wait(100);
    }
}