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@22:b671b01d00f9, 2018-09-02 (annotated)
- Committer:
- marcel1691
- Date:
- Sun Sep 02 16:20:11 2018 +0000
- Revision:
- 22:b671b01d00f9
- Parent:
- 21:4693a8b2a665
- Child:
- 23:4d8f3061890e
Hall Sensor nur einmal melden
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 | 21:4693a8b2a665 | 18 | char* hostname = "192.168.178.60"; |
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]; |
stefan1691 | 17:719bb7709c80 | 24 | // UI |
marcel1691 | 21:4693a8b2a665 | 25 | OLEDDisplay oled( PTE26, PTE0, PTE1); |
marcel1691 | 20:88b9edbdf125 | 26 | DigitalOut led1( D10 ); |
marcel1691 | 22:b671b01d00f9 | 27 | DigitalOut alert( D13 ); |
icraggs | 2:638c854c0695 | 28 | |
stefan1691 | 17:719bb7709c80 | 29 | /** Hilfsfunktion zum Publizieren auf MQTT Broker */ |
marcel1691 | 21:4693a8b2a665 | 30 | void publish( MQTTNetwork &mqttNetwork, MQTT::Client<MQTTNetwork, Countdown> &client, char* topic ) |
stefan1691 | 17:719bb7709c80 | 31 | { |
marcel1691 | 21:4693a8b2a665 | 32 | led1 = 1; |
marcel1691 | 21:4693a8b2a665 | 33 | printf("Connecting to %s:%d\r\n", hostname, port); |
marcel1691 | 21:4693a8b2a665 | 34 | |
marcel1691 | 20:88b9edbdf125 | 35 | int rc = mqttNetwork.connect(hostname, port); |
marcel1691 | 20:88b9edbdf125 | 36 | if (rc != 0) |
marcel1691 | 21:4693a8b2a665 | 37 | printf("rc from TCP connect is %d\r\n", rc); |
stefan1691 | 17:719bb7709c80 | 38 | |
stefan1691 | 17:719bb7709c80 | 39 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
icraggs | 6:e4c690c45021 | 40 | data.MQTTVersion = 3; |
icraggs | 8:a3e3113054a1 | 41 | data.clientID.cstring = "mbed-sample"; |
icraggs | 12:086a9314e8a5 | 42 | data.username.cstring = "testuser"; |
icraggs | 12:086a9314e8a5 | 43 | data.password.cstring = "testpassword"; |
marcel1691 | 20:88b9edbdf125 | 44 | if ((rc = client.connect(data)) != 0) |
marcel1691 | 21:4693a8b2a665 | 45 | printf("rc from MQTT connect is %d\r\n", rc); |
icraggs | 2:638c854c0695 | 46 | |
marcel1691 | 20:88b9edbdf125 | 47 | MQTT::Message message; |
marcel1691 | 20:88b9edbdf125 | 48 | |
marcel1691 | 21:4693a8b2a665 | 49 | oled.cursor( 2, 0 ); |
marcel1691 | 21:4693a8b2a665 | 50 | oled.printf( "Topi: %s\n", topic ); |
marcel1691 | 21:4693a8b2a665 | 51 | oled.cursor( 3, 0 ); |
marcel1691 | 21:4693a8b2a665 | 52 | oled.printf( "Push: %s\n", buf ); |
icraggs | 2:638c854c0695 | 53 | message.qos = MQTT::QOS0; |
icraggs | 2:638c854c0695 | 54 | message.retained = false; |
icraggs | 2:638c854c0695 | 55 | message.dup = false; |
stefan1691 | 17:719bb7709c80 | 56 | message.payload = (void*) buf; |
icraggs | 2:638c854c0695 | 57 | message.payloadlen = strlen(buf)+1; |
marcel1691 | 21:4693a8b2a665 | 58 | client.publish( topic, message); |
icraggs | 2:638c854c0695 | 59 | |
stefan1691 | 17:719bb7709c80 | 60 | // Verbindung beenden, ansonsten ist nach 4x Schluss |
marcel1691 | 20:88b9edbdf125 | 61 | if ((rc = client.disconnect()) != 0) |
marcel1691 | 21:4693a8b2a665 | 62 | printf("rc from disconnect was %d\r\n", rc); |
marcel1691 | 20:88b9edbdf125 | 63 | |
marcel1691 | 20:88b9edbdf125 | 64 | mqttNetwork.disconnect(); |
marcel1691 | 21:4693a8b2a665 | 65 | led1 = 0; |
icraggs | 0:0cae29831d01 | 66 | } |
stefan1691 | 17:719bb7709c80 | 67 | |
stefan1691 | 17:719bb7709c80 | 68 | /** Hauptprogramm */ |
stefan1691 | 17:719bb7709c80 | 69 | int main() |
stefan1691 | 17:719bb7709c80 | 70 | { |
marcel1691 | 21:4693a8b2a665 | 71 | uint8_t id; |
marcel1691 | 21:4693a8b2a665 | 72 | float temp, hum; |
marcel1691 | 22:b671b01d00f9 | 73 | alert = 0; |
marcel1691 | 21:4693a8b2a665 | 74 | |
marcel1691 | 21:4693a8b2a665 | 75 | oled.clear(); |
marcel1691 | 21:4693a8b2a665 | 76 | oled.printf( "MQTTPublish\r\n" ); |
marcel1691 | 21:4693a8b2a665 | 77 | oled.printf( "host: %s:%s\r\n", hostname, port ); |
marcel1691 | 20:88b9edbdf125 | 78 | |
marcel1691 | 20:88b9edbdf125 | 79 | NetworkInterface* network = easy_connect(true); |
marcel1691 | 20:88b9edbdf125 | 80 | if (!network) |
marcel1691 | 20:88b9edbdf125 | 81 | return -1; |
marcel1691 | 20:88b9edbdf125 | 82 | |
marcel1691 | 20:88b9edbdf125 | 83 | // TCP/IP und MQTT initialisieren (muss in main erfolgen) |
marcel1691 | 20:88b9edbdf125 | 84 | MQTTNetwork mqttNetwork(network); |
marcel1691 | 20:88b9edbdf125 | 85 | MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); |
marcel1691 | 21:4693a8b2a665 | 86 | |
marcel1691 | 21:4693a8b2a665 | 87 | /* Init all sensors with default params */ |
marcel1691 | 21:4693a8b2a665 | 88 | hum_temp.init(NULL); |
marcel1691 | 21:4693a8b2a665 | 89 | hum_temp.enable(); |
stefan1691 | 17:719bb7709c80 | 90 | |
stefan1691 | 17:719bb7709c80 | 91 | while ( 1 ) |
stefan1691 | 17:719bb7709c80 | 92 | { |
marcel1691 | 21:4693a8b2a665 | 93 | // Temperator und Luftfeuchtigkeit |
marcel1691 | 21:4693a8b2a665 | 94 | hum_temp.read_id(&id); |
marcel1691 | 21:4693a8b2a665 | 95 | hum_temp.get_temperature(&temp); |
marcel1691 | 21:4693a8b2a665 | 96 | hum_temp.get_humidity(&hum); |
marcel1691 | 21:4693a8b2a665 | 97 | sprintf( buf, "0x%X,%2.2f,%2.1f", id, temp, hum ); |
marcel1691 | 21:4693a8b2a665 | 98 | publish( mqttNetwork, client, topicTEMP ); |
marcel1691 | 21:4693a8b2a665 | 99 | |
marcel1691 | 21:4693a8b2a665 | 100 | // alert Tuer offen |
marcel1691 | 21:4693a8b2a665 | 101 | if ( hallSensor.read() > 0.6f ) |
marcel1691 | 21:4693a8b2a665 | 102 | { |
marcel1691 | 22:b671b01d00f9 | 103 | // nur einmal Melden!, bis Reset |
marcel1691 | 22:b671b01d00f9 | 104 | if ( alert == 0 ) |
marcel1691 | 22:b671b01d00f9 | 105 | { |
marcel1691 | 22:b671b01d00f9 | 106 | sprintf( buf, "door open" ); |
marcel1691 | 22:b671b01d00f9 | 107 | message.payload = (void*) buf; |
marcel1691 | 22:b671b01d00f9 | 108 | message.payloadlen = strlen(buf)+1; |
marcel1691 | 22:b671b01d00f9 | 109 | publish( mqttNetwork, client, topicALERT ); |
marcel1691 | 22:b671b01d00f9 | 110 | alert = 1; |
marcel1691 | 22:b671b01d00f9 | 111 | } |
marcel1691 | 21:4693a8b2a665 | 112 | } |
marcel1691 | 22:b671b01d00f9 | 113 | else |
marcel1691 | 22:b671b01d00f9 | 114 | alert = 0; |
marcel1691 | 21:4693a8b2a665 | 115 | wait ( 2.0f ); |
stefan1691 | 17:719bb7709c80 | 116 | } |
stefan1691 | 17:719bb7709c80 | 117 | } |