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