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: EthernetInterface MQTT mbed-rtos mbed
Fork of HelloMQTT by
main.cpp@17:719bb7709c80, 2015-03-16 (annotated)
- Committer:
- stefan1691
- Date:
- Mon Mar 16 10:26:15 2015 +0000
- Revision:
- 17:719bb7709c80
- Parent:
- 16:28d062c5522b
- Child:
- 18:53d83febbe0a
MQTT Publish von Sensordaten als JSON
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stefan1691 | 17:719bb7709c80 | 1 | /** MQTT Publish von Sensordaten als JSON */ |
stefan1691 | 17:719bb7709c80 | 2 | #include "mbed.h" |
icraggs | 8:a3e3113054a1 | 3 | #include "MQTTEthernet.h" |
icraggs | 2:638c854c0695 | 4 | #include "MQTTClient.h" |
icraggs | 2:638c854c0695 | 5 | |
stefan1691 | 17:719bb7709c80 | 6 | // Topic |
stefan1691 | 17:719bb7709c80 | 7 | char* topic = "mbed/k64f/iotkit"; |
stefan1691 | 17:719bb7709c80 | 8 | // MQTT Brocker |
stefan1691 | 17:719bb7709c80 | 9 | char* hostname = "iot.eclipse.org"; |
stefan1691 | 17:719bb7709c80 | 10 | int port = 1883; |
stefan1691 | 17:719bb7709c80 | 11 | // MQTT Message |
stefan1691 | 17:719bb7709c80 | 12 | MQTT::Message message; |
stefan1691 | 17:719bb7709c80 | 13 | // I/O Buffer |
stefan1691 | 17:719bb7709c80 | 14 | char buf[100]; |
stefan1691 | 17:719bb7709c80 | 15 | // UI |
stefan1691 | 17:719bb7709c80 | 16 | DigitalOut led1( LED1 ); |
stefan1691 | 17:719bb7709c80 | 17 | // Lichtsensor |
stefan1691 | 17:719bb7709c80 | 18 | AnalogIn light( A1 ); |
icraggs | 2:638c854c0695 | 19 | |
stefan1691 | 17:719bb7709c80 | 20 | /** Hilfsfunktion zum Publizieren auf MQTT Broker */ |
stefan1691 | 17:719bb7709c80 | 21 | void publish( MQTTEthernet &ipstack, MQTT::Client<MQTTEthernet, Countdown> &client ) |
stefan1691 | 17:719bb7709c80 | 22 | { |
stefan1691 | 17:719bb7709c80 | 23 | printf("Connecting to %s:%d\n", hostname, port); |
icraggs | 6:e4c690c45021 | 24 | int rc = ipstack.connect(hostname, port); |
stefan1691 | 17:719bb7709c80 | 25 | if ( rc != 0 ) |
stefan1691 | 17:719bb7709c80 | 26 | printf("rc from TCP connect is %d\n", rc); |
stefan1691 | 17:719bb7709c80 | 27 | |
stefan1691 | 17:719bb7709c80 | 28 | // mit MQTT Broker verbinden |
stefan1691 | 17:719bb7709c80 | 29 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
icraggs | 6:e4c690c45021 | 30 | data.MQTTVersion = 3; |
icraggs | 8:a3e3113054a1 | 31 | data.clientID.cstring = "mbed-sample"; |
icraggs | 12:086a9314e8a5 | 32 | data.username.cstring = "testuser"; |
icraggs | 12:086a9314e8a5 | 33 | data.password.cstring = "testpassword"; |
stefan1691 | 17:719bb7709c80 | 34 | if ( (rc = client.connect(data)) != 0 ) |
stefan1691 | 17:719bb7709c80 | 35 | printf("rc from MQTT connect is %d\n", rc); |
icraggs | 2:638c854c0695 | 36 | |
stefan1691 | 17:719bb7709c80 | 37 | // Message als JSON aufbereiten und senden |
stefan1691 | 17:719bb7709c80 | 38 | sprintf( buf, "{ \"light:\": %f }", light.read() ); |
stefan1691 | 17:719bb7709c80 | 39 | printf( "Publish: %s\n", buf ); |
icraggs | 2:638c854c0695 | 40 | message.qos = MQTT::QOS0; |
icraggs | 2:638c854c0695 | 41 | message.retained = false; |
icraggs | 2:638c854c0695 | 42 | message.dup = false; |
stefan1691 | 17:719bb7709c80 | 43 | message.payload = (void*) buf; |
icraggs | 2:638c854c0695 | 44 | message.payloadlen = strlen(buf)+1; |
stefan1691 | 17:719bb7709c80 | 45 | client.publish(topic, message); |
icraggs | 2:638c854c0695 | 46 | |
stefan1691 | 17:719bb7709c80 | 47 | // Verbindung beenden, ansonsten ist nach 4x Schluss |
stefan1691 | 17:719bb7709c80 | 48 | client.disconnect(); |
icraggs | 2:638c854c0695 | 49 | ipstack.disconnect(); |
icraggs | 0:0cae29831d01 | 50 | } |
stefan1691 | 17:719bb7709c80 | 51 | |
stefan1691 | 17:719bb7709c80 | 52 | /** Hauptprogramm */ |
stefan1691 | 17:719bb7709c80 | 53 | int main() |
stefan1691 | 17:719bb7709c80 | 54 | { |
stefan1691 | 17:719bb7709c80 | 55 | // Ethernet und MQTT initialisieren (muss in main erfolgen) |
stefan1691 | 17:719bb7709c80 | 56 | MQTTEthernet ipstack = MQTTEthernet(); |
stefan1691 | 17:719bb7709c80 | 57 | MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); |
stefan1691 | 17:719bb7709c80 | 58 | |
stefan1691 | 17:719bb7709c80 | 59 | while ( 1 ) |
stefan1691 | 17:719bb7709c80 | 60 | { |
stefan1691 | 17:719bb7709c80 | 61 | led1 = 1; |
stefan1691 | 17:719bb7709c80 | 62 | publish( ipstack, client ); |
stefan1691 | 17:719bb7709c80 | 63 | led1 = 0; |
stefan1691 | 17:719bb7709c80 | 64 | wait ( 2.0 ); |
stefan1691 | 17:719bb7709c80 | 65 | } |
stefan1691 | 17:719bb7709c80 | 66 | } |