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