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@25:5e982d03e7d1, 2018-09-09 (annotated)
- Committer:
- marcel1691
- Date:
- Sun Sep 09 11:56:38 2018 +0000
- Revision:
- 25:5e982d03e7d1
- Parent:
- 24:d2f0599798a1
- Child:
- 26:cf9cf40c63ea
Abgestimmt mit lernkube
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 | 21:4693a8b2a665 | 3 | #include "HTS221Sensor.h" |
marcel1691 | 20:88b9edbdf125 | 4 | #include "easy-connect.h" |
marcel1691 | 20:88b9edbdf125 | 5 | #include "MQTTNetwork.h" |
marcel1691 | 20:88b9edbdf125 | 6 | #include "MQTTmbed.h" |
icraggs | 2:638c854c0695 | 7 | #include "MQTTClient.h" |
marcel1691 | 21:4693a8b2a665 | 8 | #include "OLEDDisplay.h" |
icraggs | 2:638c854c0695 | 9 | |
marcel1691 | 21:4693a8b2a665 | 10 | static DevI2C devI2c(PTE0,PTE1); |
marcel1691 | 21:4693a8b2a665 | 11 | static HTS221Sensor hum_temp(&devI2c); |
marcel1691 | 21:4693a8b2a665 | 12 | AnalogIn hallSensor( PTC0 ); |
marcel1691 | 20:88b9edbdf125 | 13 | |
stefan1691 | 18:53d83febbe0a | 14 | // Topic's |
marcel1691 | 21:4693a8b2a665 | 15 | char* topicTEMP = "iotkit/sensor"; |
marcel1691 | 21:4693a8b2a665 | 16 | char* topicALERT = "iotkit/alert"; |
stefan1691 | 17:719bb7709c80 | 17 | // MQTT Brocker |
marcel1691 | 25:5e982d03e7d1 | 18 | char* hostname = "192.168.3.10"; |
marcel1691 | 20:88b9edbdf125 | 19 | int port = 31883; |
stefan1691 | 17:719bb7709c80 | 20 | // MQTT Message |
stefan1691 | 17:719bb7709c80 | 21 | MQTT::Message message; |
stefan1691 | 17:719bb7709c80 | 22 | // I/O Buffer |
stefan1691 | 17:719bb7709c80 | 23 | char buf[100]; |
marcel1691 | 24:d2f0599798a1 | 24 | |
marcel1691 | 24:d2f0599798a1 | 25 | // Klassifikation |
marcel1691 | 24:d2f0599798a1 | 26 | char cls[3][10] = { "low", "middle", "high" }; |
marcel1691 | 24:d2f0599798a1 | 27 | int type = 0; |
marcel1691 | 24:d2f0599798a1 | 28 | |
stefan1691 | 17:719bb7709c80 | 29 | // UI |
marcel1691 | 21:4693a8b2a665 | 30 | OLEDDisplay oled( PTE26, PTE0, PTE1); |
marcel1691 | 20:88b9edbdf125 | 31 | DigitalOut led1( D10 ); |
marcel1691 | 22:b671b01d00f9 | 32 | DigitalOut alert( D13 ); |
icraggs | 2:638c854c0695 | 33 | |
stefan1691 | 17:719bb7709c80 | 34 | /** Hilfsfunktion zum Publizieren auf MQTT Broker */ |
marcel1691 | 21:4693a8b2a665 | 35 | void publish( MQTTNetwork &mqttNetwork, MQTT::Client<MQTTNetwork, Countdown> &client, char* topic ) |
stefan1691 | 17:719bb7709c80 | 36 | { |
marcel1691 | 21:4693a8b2a665 | 37 | led1 = 1; |
marcel1691 | 21:4693a8b2a665 | 38 | printf("Connecting to %s:%d\r\n", hostname, port); |
marcel1691 | 21:4693a8b2a665 | 39 | |
marcel1691 | 20:88b9edbdf125 | 40 | int rc = mqttNetwork.connect(hostname, port); |
marcel1691 | 20:88b9edbdf125 | 41 | if (rc != 0) |
marcel1691 | 21:4693a8b2a665 | 42 | printf("rc from TCP connect is %d\r\n", rc); |
stefan1691 | 17:719bb7709c80 | 43 | |
stefan1691 | 17:719bb7709c80 | 44 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
icraggs | 6:e4c690c45021 | 45 | data.MQTTVersion = 3; |
icraggs | 8:a3e3113054a1 | 46 | data.clientID.cstring = "mbed-sample"; |
icraggs | 12:086a9314e8a5 | 47 | data.username.cstring = "testuser"; |
icraggs | 12:086a9314e8a5 | 48 | data.password.cstring = "testpassword"; |
marcel1691 | 20:88b9edbdf125 | 49 | if ((rc = client.connect(data)) != 0) |
marcel1691 | 21:4693a8b2a665 | 50 | printf("rc from MQTT connect is %d\r\n", rc); |
icraggs | 2:638c854c0695 | 51 | |
marcel1691 | 20:88b9edbdf125 | 52 | MQTT::Message message; |
marcel1691 | 20:88b9edbdf125 | 53 | |
marcel1691 | 21:4693a8b2a665 | 54 | oled.cursor( 2, 0 ); |
marcel1691 | 21:4693a8b2a665 | 55 | oled.printf( "Topi: %s\n", topic ); |
marcel1691 | 21:4693a8b2a665 | 56 | oled.cursor( 3, 0 ); |
marcel1691 | 21:4693a8b2a665 | 57 | oled.printf( "Push: %s\n", buf ); |
icraggs | 2:638c854c0695 | 58 | message.qos = MQTT::QOS0; |
icraggs | 2:638c854c0695 | 59 | message.retained = false; |
icraggs | 2:638c854c0695 | 60 | message.dup = false; |
stefan1691 | 17:719bb7709c80 | 61 | message.payload = (void*) buf; |
icraggs | 2:638c854c0695 | 62 | message.payloadlen = strlen(buf)+1; |
marcel1691 | 21:4693a8b2a665 | 63 | client.publish( topic, 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 | 21:4693a8b2a665 | 67 | printf("rc from disconnect was %d\r\n", rc); |
marcel1691 | 20:88b9edbdf125 | 68 | |
marcel1691 | 20:88b9edbdf125 | 69 | mqttNetwork.disconnect(); |
marcel1691 | 21:4693a8b2a665 | 70 | led1 = 0; |
icraggs | 0:0cae29831d01 | 71 | } |
stefan1691 | 17:719bb7709c80 | 72 | |
stefan1691 | 17:719bb7709c80 | 73 | /** Hauptprogramm */ |
stefan1691 | 17:719bb7709c80 | 74 | int main() |
stefan1691 | 17:719bb7709c80 | 75 | { |
marcel1691 | 21:4693a8b2a665 | 76 | uint8_t id; |
marcel1691 | 21:4693a8b2a665 | 77 | float temp, hum; |
marcel1691 | 22:b671b01d00f9 | 78 | alert = 0; |
marcel1691 | 21:4693a8b2a665 | 79 | |
marcel1691 | 21:4693a8b2a665 | 80 | oled.clear(); |
marcel1691 | 21:4693a8b2a665 | 81 | oled.printf( "MQTTPublish\r\n" ); |
marcel1691 | 21:4693a8b2a665 | 82 | oled.printf( "host: %s:%s\r\n", hostname, port ); |
marcel1691 | 20:88b9edbdf125 | 83 | |
marcel1691 | 20:88b9edbdf125 | 84 | NetworkInterface* network = easy_connect(true); |
marcel1691 | 20:88b9edbdf125 | 85 | if (!network) |
marcel1691 | 20:88b9edbdf125 | 86 | return -1; |
marcel1691 | 20:88b9edbdf125 | 87 | |
marcel1691 | 20:88b9edbdf125 | 88 | // TCP/IP und MQTT initialisieren (muss in main erfolgen) |
marcel1691 | 20:88b9edbdf125 | 89 | MQTTNetwork mqttNetwork(network); |
marcel1691 | 20:88b9edbdf125 | 90 | MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); |
marcel1691 | 21:4693a8b2a665 | 91 | |
marcel1691 | 21:4693a8b2a665 | 92 | /* Init all sensors with default params */ |
marcel1691 | 21:4693a8b2a665 | 93 | hum_temp.init(NULL); |
marcel1691 | 21:4693a8b2a665 | 94 | hum_temp.enable(); |
stefan1691 | 17:719bb7709c80 | 95 | |
stefan1691 | 17:719bb7709c80 | 96 | while ( 1 ) |
stefan1691 | 17:719bb7709c80 | 97 | { |
marcel1691 | 21:4693a8b2a665 | 98 | // Temperator und Luftfeuchtigkeit |
marcel1691 | 21:4693a8b2a665 | 99 | hum_temp.read_id(&id); |
marcel1691 | 21:4693a8b2a665 | 100 | hum_temp.get_temperature(&temp); |
marcel1691 | 21:4693a8b2a665 | 101 | hum_temp.get_humidity(&hum); |
marcel1691 | 24:d2f0599798a1 | 102 | if ( type == 0 ) |
marcel1691 | 24:d2f0599798a1 | 103 | temp -= 5.0f; |
marcel1691 | 24:d2f0599798a1 | 104 | else if ( type == 2 ) |
marcel1691 | 24:d2f0599798a1 | 105 | temp += 5.0f; |
marcel1691 | 24:d2f0599798a1 | 106 | sprintf( buf, "0x%X,%2.2f,%2.1f,%s", id, temp, hum, cls[type] ); |
marcel1691 | 24:d2f0599798a1 | 107 | type++; |
marcel1691 | 24:d2f0599798a1 | 108 | if ( type > 2 ) |
marcel1691 | 24:d2f0599798a1 | 109 | type = 0; |
marcel1691 | 21:4693a8b2a665 | 110 | publish( mqttNetwork, client, topicTEMP ); |
marcel1691 | 21:4693a8b2a665 | 111 | |
marcel1691 | 21:4693a8b2a665 | 112 | // alert Tuer offen |
marcel1691 | 23:4d8f3061890e | 113 | printf( "Hall %4.4f, alert %d\n", hallSensor.read(), alert.read() ); |
marcel1691 | 21:4693a8b2a665 | 114 | if ( hallSensor.read() > 0.6f ) |
marcel1691 | 21:4693a8b2a665 | 115 | { |
marcel1691 | 22:b671b01d00f9 | 116 | // nur einmal Melden!, bis Reset |
marcel1691 | 23:4d8f3061890e | 117 | if ( alert.read() == 0 ) |
marcel1691 | 22:b671b01d00f9 | 118 | { |
marcel1691 | 24:d2f0599798a1 | 119 | sprintf( buf, "alert: hall" ); |
marcel1691 | 22:b671b01d00f9 | 120 | message.payload = (void*) buf; |
marcel1691 | 22:b671b01d00f9 | 121 | message.payloadlen = strlen(buf)+1; |
marcel1691 | 22:b671b01d00f9 | 122 | publish( mqttNetwork, client, topicALERT ); |
marcel1691 | 22:b671b01d00f9 | 123 | alert = 1; |
marcel1691 | 22:b671b01d00f9 | 124 | } |
marcel1691 | 21:4693a8b2a665 | 125 | } |
marcel1691 | 22:b671b01d00f9 | 126 | else |
marcel1691 | 22:b671b01d00f9 | 127 | alert = 0; |
marcel1691 | 21:4693a8b2a665 | 128 | wait ( 2.0f ); |
stefan1691 | 17:719bb7709c80 | 129 | } |
stefan1691 | 17:719bb7709c80 | 130 | } |