IoTKitV3 / Mbed OS MQTTPublish

Dependencies:   QEI MFRC522 HTS221 IoTKit BMP180 MQTT

Fork of MQTTPublish by smd.iotkit2.ch

main.cpp

Committer:
marcel1691
Date:
2018-09-01
Revision:
21:4693a8b2a665
Parent:
20:88b9edbdf125
Child:
22:b671b01d00f9

File content as of revision 21:4693a8b2a665:

/** MQTT Publish von Sensordaten */
#include "mbed.h"
#include "HTS221Sensor.h"
#include "easy-connect.h"
#include "MQTTNetwork.h"
#include "MQTTmbed.h"
#include "MQTTClient.h"
#include "OLEDDisplay.h"

static DevI2C devI2c(PTE0,PTE1);
static HTS221Sensor hum_temp(&devI2c);
AnalogIn hallSensor( PTC0 );

// Topic's
char* topicTEMP =  "iotkit/sensor";
char* topicALERT = "iotkit/alert";
// MQTT Brocker
char* hostname = "192.168.178.60";
int port = 31883;
// MQTT Message
MQTT::Message message;
// I/O Buffer
char buf[100];
// UI
OLEDDisplay oled( PTE26, PTE0, PTE1);
DigitalOut led1( D10 );

/** Hilfsfunktion zum Publizieren auf MQTT Broker */
void publish( MQTTNetwork &mqttNetwork, MQTT::Client<MQTTNetwork, Countdown> &client, char* topic )
{
    led1 = 1;
    printf("Connecting to %s:%d\r\n", hostname, port);
    
    int rc = mqttNetwork.connect(hostname, port);
    if (rc != 0)
        printf("rc from TCP connect is %d\r\n", rc);

    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    data.MQTTVersion = 3;
    data.clientID.cstring = "mbed-sample";
    data.username.cstring = "testuser";
    data.password.cstring = "testpassword";
    if ((rc = client.connect(data)) != 0)
        printf("rc from MQTT connect is %d\r\n", rc);

    MQTT::Message message;    
    
    oled.cursor( 2, 0 );
    oled.printf( "Topi: %s\n", topic );
    oled.cursor( 3, 0 );    
    oled.printf( "Push: %s\n", buf );
    message.qos = MQTT::QOS0;
    message.retained = false;
    message.dup = false;
    message.payload = (void*) buf;
    message.payloadlen = strlen(buf)+1;
    client.publish( topic, message);  
    
    // Verbindung beenden, ansonsten ist nach 4x Schluss
    if ((rc = client.disconnect()) != 0)
        printf("rc from disconnect was %d\r\n", rc);

    mqttNetwork.disconnect();
    led1 = 0;
}

/** Hauptprogramm */
int main()
{
    uint8_t id;
    float temp, hum;
    
    oled.clear();
    oled.printf( "MQTTPublish\r\n" );
    oled.printf( "host: %s:%s\r\n", hostname, port );

    NetworkInterface* network = easy_connect(true);
    if (!network) 
        return -1;

    // TCP/IP und MQTT initialisieren (muss in main erfolgen)
    MQTTNetwork mqttNetwork(network);
    MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
    
    /* Init all sensors with default params */
    hum_temp.init(NULL);
    hum_temp.enable();    

    while   ( 1 ) 
    {
        // Temperator und Luftfeuchtigkeit
        hum_temp.read_id(&id);
        hum_temp.get_temperature(&temp);
        hum_temp.get_humidity(&hum);    
        sprintf( buf, "0x%X,%2.2f,%2.1f", id, temp, hum );        
        publish( mqttNetwork, client, topicTEMP );
        
        // alert Tuer offen 
        if  ( hallSensor.read() > 0.6f )
        {
            sprintf( buf, "door open" );
            message.payload = (void*) buf;
            message.payloadlen = strlen(buf)+1;
            publish( mqttNetwork, client, topicALERT );          
        }
        wait    ( 2.0f );
    }
}