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@20:88b9edbdf125, 2018-09-01 (annotated)
- Committer:
- marcel1691
- Date:
- Sat Sep 01 15:37:06 2018 +0000
- Revision:
- 20:88b9edbdf125
- Parent:
- 18:53d83febbe0a
- Child:
- 21:4693a8b2a665
Update auf mbed 5.x
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" |
marcel1691 | 20:88b9edbdf125 | 3 | #include "easy-connect.h" |
marcel1691 | 20:88b9edbdf125 | 4 | #include "MQTTNetwork.h" |
marcel1691 | 20:88b9edbdf125 | 5 | #include "MQTTmbed.h" |
icraggs | 2:638c854c0695 | 6 | #include "MQTTClient.h" |
icraggs | 2:638c854c0695 | 7 | |
marcel1691 | 20:88b9edbdf125 | 8 | #define logMessage printf |
marcel1691 | 20:88b9edbdf125 | 9 | |
stefan1691 | 18:53d83febbe0a | 10 | // Topic's |
stefan1691 | 18:53d83febbe0a | 11 | char* topicLight = "mbed/k64f/iotkit/light"; |
stefan1691 | 18:53d83febbe0a | 12 | char* topicPoti = "mbed/k64f/iotkit/poti"; |
stefan1691 | 17:719bb7709c80 | 13 | // MQTT Brocker |
marcel1691 | 20:88b9edbdf125 | 14 | char* hostname = "192.168.178.43"; |
marcel1691 | 20:88b9edbdf125 | 15 | int port = 31883; |
stefan1691 | 17:719bb7709c80 | 16 | // MQTT Message |
stefan1691 | 17:719bb7709c80 | 17 | MQTT::Message message; |
stefan1691 | 17:719bb7709c80 | 18 | // I/O Buffer |
stefan1691 | 17:719bb7709c80 | 19 | char buf[100]; |
stefan1691 | 17:719bb7709c80 | 20 | // UI |
marcel1691 | 20:88b9edbdf125 | 21 | DigitalOut led1( D10 ); |
stefan1691 | 18:53d83febbe0a | 22 | // Poti |
stefan1691 | 18:53d83febbe0a | 23 | AnalogIn poti( A0 ); |
stefan1691 | 17:719bb7709c80 | 24 | // Lichtsensor |
stefan1691 | 17:719bb7709c80 | 25 | AnalogIn light( A1 ); |
icraggs | 2:638c854c0695 | 26 | |
stefan1691 | 17:719bb7709c80 | 27 | /** Hilfsfunktion zum Publizieren auf MQTT Broker */ |
marcel1691 | 20:88b9edbdf125 | 28 | void publish( MQTTNetwork &mqttNetwork, MQTT::Client<MQTTNetwork, Countdown> &client ) |
stefan1691 | 17:719bb7709c80 | 29 | { |
marcel1691 | 20:88b9edbdf125 | 30 | logMessage("Connecting to %s:%d\r\n", hostname, port); |
marcel1691 | 20:88b9edbdf125 | 31 | int rc = mqttNetwork.connect(hostname, port); |
marcel1691 | 20:88b9edbdf125 | 32 | if (rc != 0) |
marcel1691 | 20:88b9edbdf125 | 33 | logMessage("rc from TCP connect is %d\r\n", rc); |
stefan1691 | 17:719bb7709c80 | 34 | |
stefan1691 | 17:719bb7709c80 | 35 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
icraggs | 6:e4c690c45021 | 36 | data.MQTTVersion = 3; |
icraggs | 8:a3e3113054a1 | 37 | data.clientID.cstring = "mbed-sample"; |
icraggs | 12:086a9314e8a5 | 38 | data.username.cstring = "testuser"; |
icraggs | 12:086a9314e8a5 | 39 | data.password.cstring = "testpassword"; |
marcel1691 | 20:88b9edbdf125 | 40 | if ((rc = client.connect(data)) != 0) |
marcel1691 | 20:88b9edbdf125 | 41 | logMessage("rc from MQTT connect is %d\r\n", rc); |
icraggs | 2:638c854c0695 | 42 | |
marcel1691 | 20:88b9edbdf125 | 43 | MQTT::Message message; |
marcel1691 | 20:88b9edbdf125 | 44 | |
stefan1691 | 17:719bb7709c80 | 45 | // Message als JSON aufbereiten und senden |
marcel1691 | 20:88b9edbdf125 | 46 | /*sprintf( buf, "%f", light.read() ); |
stefan1691 | 17:719bb7709c80 | 47 | printf( "Publish: %s\n", buf ); |
icraggs | 2:638c854c0695 | 48 | message.qos = MQTT::QOS0; |
icraggs | 2:638c854c0695 | 49 | message.retained = false; |
icraggs | 2:638c854c0695 | 50 | message.dup = false; |
stefan1691 | 17:719bb7709c80 | 51 | message.payload = (void*) buf; |
icraggs | 2:638c854c0695 | 52 | message.payloadlen = strlen(buf)+1; |
marcel1691 | 20:88b9edbdf125 | 53 | client.publish( topicLight, message);*/ |
stefan1691 | 18:53d83febbe0a | 54 | |
stefan1691 | 18:53d83febbe0a | 55 | // Message als JSON aufbereiten und senden |
stefan1691 | 18:53d83febbe0a | 56 | sprintf( buf, "%f", poti.read() ); |
stefan1691 | 18:53d83febbe0a | 57 | printf( "Publish: %s\n", buf ); |
stefan1691 | 18:53d83febbe0a | 58 | message.qos = MQTT::QOS0; |
stefan1691 | 18:53d83febbe0a | 59 | message.retained = false; |
stefan1691 | 18:53d83febbe0a | 60 | message.dup = false; |
stefan1691 | 18:53d83febbe0a | 61 | message.payload = (void*) buf; |
stefan1691 | 18:53d83febbe0a | 62 | message.payloadlen = strlen(buf)+1; |
stefan1691 | 18:53d83febbe0a | 63 | client.publish( topicPoti, message); |
icraggs | 2:638c854c0695 | 64 | |
stefan1691 | 17:719bb7709c80 | 65 | // Verbindung beenden, ansonsten ist nach 4x Schluss |
marcel1691 | 20:88b9edbdf125 | 66 | if ((rc = client.disconnect()) != 0) |
marcel1691 | 20:88b9edbdf125 | 67 | logMessage("rc from disconnect was %d\r\n", rc); |
marcel1691 | 20:88b9edbdf125 | 68 | |
marcel1691 | 20:88b9edbdf125 | 69 | mqttNetwork.disconnect(); |
icraggs | 0:0cae29831d01 | 70 | } |
stefan1691 | 17:719bb7709c80 | 71 | |
stefan1691 | 17:719bb7709c80 | 72 | /** Hauptprogramm */ |
stefan1691 | 17:719bb7709c80 | 73 | int main() |
stefan1691 | 17:719bb7709c80 | 74 | { |
marcel1691 | 20:88b9edbdf125 | 75 | logMessage("MQTTPublish\n"); |
marcel1691 | 20:88b9edbdf125 | 76 | |
marcel1691 | 20:88b9edbdf125 | 77 | NetworkInterface* network = easy_connect(true); |
marcel1691 | 20:88b9edbdf125 | 78 | if (!network) |
marcel1691 | 20:88b9edbdf125 | 79 | return -1; |
marcel1691 | 20:88b9edbdf125 | 80 | |
marcel1691 | 20:88b9edbdf125 | 81 | // TCP/IP und MQTT initialisieren (muss in main erfolgen) |
marcel1691 | 20:88b9edbdf125 | 82 | MQTTNetwork mqttNetwork(network); |
marcel1691 | 20:88b9edbdf125 | 83 | MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); |
stefan1691 | 17:719bb7709c80 | 84 | |
stefan1691 | 17:719bb7709c80 | 85 | while ( 1 ) |
stefan1691 | 17:719bb7709c80 | 86 | { |
stefan1691 | 17:719bb7709c80 | 87 | led1 = 1; |
marcel1691 | 20:88b9edbdf125 | 88 | publish( mqttNetwork, client ); |
stefan1691 | 17:719bb7709c80 | 89 | led1 = 0; |
marcel1691 | 20:88b9edbdf125 | 90 | wait ( 5.0 ); |
stefan1691 | 17:719bb7709c80 | 91 | } |
stefan1691 | 17:719bb7709c80 | 92 | } |