Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: QEI MFRC522 HTS221 IoTKit BMP180 MQTT
Fork of MQTTPublish by
main.cpp
- Committer:
- stefan1691
- Date:
- 2015-03-16
- Revision:
- 17:719bb7709c80
- Parent:
- 16:28d062c5522b
- Child:
- 18:53d83febbe0a
File content as of revision 17:719bb7709c80:
/** MQTT Publish von Sensordaten als JSON */ #include "mbed.h" #include "MQTTEthernet.h" #include "MQTTClient.h" // Topic char* topic = "mbed/k64f/iotkit"; // MQTT Brocker char* hostname = "iot.eclipse.org"; int port = 1883; // MQTT Message MQTT::Message message; // I/O Buffer char buf[100]; // UI DigitalOut led1( LED1 ); // Lichtsensor AnalogIn light( A1 ); /** Hilfsfunktion zum Publizieren auf MQTT Broker */ void publish( MQTTEthernet &ipstack, MQTT::Client<MQTTEthernet, Countdown> &client ) { printf("Connecting to %s:%d\n", hostname, port); int rc = ipstack.connect(hostname, port); if ( rc != 0 ) printf("rc from TCP connect is %d\n", rc); // mit MQTT Broker verbinden 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\n", rc); // Message als JSON aufbereiten und senden sprintf( buf, "{ \"light:\": %f }", light.read() ); printf( "Publish: %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 client.disconnect(); ipstack.disconnect(); } /** Hauptprogramm */ int main() { // Ethernet und MQTT initialisieren (muss in main erfolgen) MQTTEthernet ipstack = MQTTEthernet(); MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); while ( 1 ) { led1 = 1; publish( ipstack, client ); led1 = 0; wait ( 2.0 ); } }