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@32:020ebbef6706, 2019-04-24 (annotated)
- Committer:
- marcel1691
- Date:
- Wed Apr 24 07:21:26 2019 +0000
- Revision:
- 32:020ebbef6706
- Parent:
- 31:747be5f4f781
- Child:
- 33:06cbea3c9d4c
Multi Board
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 | 32:020ebbef6706 | 4 | #include "network-helper.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" |
marcel1691 | 26:cf9cf40c63ea | 9 | #include "Motor.h" |
marcel1691 | 28:aa3b3fa5b5a7 | 10 | #include "QEI.h" |
marcel1691 | 28:aa3b3fa5b5a7 | 11 | #include "MFRC522.h" |
marcel1691 | 28:aa3b3fa5b5a7 | 12 | |
marcel1691 | 28:aa3b3fa5b5a7 | 13 | // NFC/RFID Reader (SPI) |
marcel1691 | 30:2829089e2ef3 | 14 | MFRC522 rfidReader( MBED_CONF_IOTKIT_RFID_MOSI, MBED_CONF_IOTKIT_RFID_MISO, MBED_CONF_IOTKIT_RFID_SCLK, MBED_CONF_IOTKIT_RFID_SS, MBED_CONF_IOTKIT_RFID_RST ); |
icraggs | 2:638c854c0695 | 15 | |
marcel1691 | 28:aa3b3fa5b5a7 | 16 | // Sensoren wo Daten fuer Topics produzieren |
marcel1691 | 30:2829089e2ef3 | 17 | static DevI2C devI2c( MBED_CONF_IOTKIT_I2C_SDA, MBED_CONF_IOTKIT_I2C_SCL ); |
marcel1691 | 21:4693a8b2a665 | 18 | static HTS221Sensor hum_temp(&devI2c); |
marcel1691 | 30:2829089e2ef3 | 19 | AnalogIn hallSensor( MBED_CONF_IOTKIT_HALL_SENSOR ); |
marcel1691 | 30:2829089e2ef3 | 20 | DigitalIn button( MBED_CONF_IOTKIT_BUTTON1 ); |
marcel1691 | 28:aa3b3fa5b5a7 | 21 | //Use X2 encoding by default. |
marcel1691 | 32:020ebbef6706 | 22 | #ifdef TARGET_K64F |
marcel1691 | 30:2829089e2ef3 | 23 | QEI wheel (MBED_CONF_IOTKIT_BUTTON2, MBED_CONF_IOTKIT_BUTTON3, NC, 624); |
marcel1691 | 32:020ebbef6706 | 24 | #endif |
marcel1691 | 20:88b9edbdf125 | 25 | |
stefan1691 | 18:53d83febbe0a | 26 | // Topic's |
marcel1691 | 21:4693a8b2a665 | 27 | char* topicTEMP = "iotkit/sensor"; |
marcel1691 | 21:4693a8b2a665 | 28 | char* topicALERT = "iotkit/alert"; |
marcel1691 | 28:aa3b3fa5b5a7 | 29 | char* topicBUTTON = "iotkit/button"; |
marcel1691 | 28:aa3b3fa5b5a7 | 30 | char* topicENCODER = "iotkit/encoder"; |
marcel1691 | 28:aa3b3fa5b5a7 | 31 | char* topicRFID = "iotkit/rfid"; |
stefan1691 | 17:719bb7709c80 | 32 | // MQTT Brocker |
marcel1691 | 28:aa3b3fa5b5a7 | 33 | char* hostname = "broker.hivemq.com"; |
marcel1691 | 27:43f3a9552db2 | 34 | int port = 1883; |
stefan1691 | 17:719bb7709c80 | 35 | // MQTT Message |
stefan1691 | 17:719bb7709c80 | 36 | MQTT::Message message; |
stefan1691 | 17:719bb7709c80 | 37 | // I/O Buffer |
stefan1691 | 17:719bb7709c80 | 38 | char buf[100]; |
marcel1691 | 24:d2f0599798a1 | 39 | |
marcel1691 | 24:d2f0599798a1 | 40 | // Klassifikation |
marcel1691 | 24:d2f0599798a1 | 41 | char cls[3][10] = { "low", "middle", "high" }; |
marcel1691 | 24:d2f0599798a1 | 42 | int type = 0; |
marcel1691 | 24:d2f0599798a1 | 43 | |
stefan1691 | 17:719bb7709c80 | 44 | // UI |
marcel1691 | 30:2829089e2ef3 | 45 | OLEDDisplay oled( MBED_CONF_IOTKIT_OLED_RST, MBED_CONF_IOTKIT_OLED_SDA, MBED_CONF_IOTKIT_OLED_SCL ); |
marcel1691 | 30:2829089e2ef3 | 46 | DigitalOut led1( MBED_CONF_IOTKIT_LED1 ); |
marcel1691 | 30:2829089e2ef3 | 47 | DigitalOut alert( MBED_CONF_IOTKIT_LED3 ); |
icraggs | 2:638c854c0695 | 48 | |
marcel1691 | 26:cf9cf40c63ea | 49 | // Aktore(n) |
marcel1691 | 31:747be5f4f781 | 50 | Motor m1( MBED_CONF_IOTKIT_MOTOR1_PWM, MBED_CONF_IOTKIT_MOTOR1_FWD, MBED_CONF_IOTKIT_MOTOR1_REV ); // PWM, Vorwaerts, Rueckwarts |
marcel1691 | 31:747be5f4f781 | 51 | PwmOut speaker( MBED_CONF_IOTKIT_BUZZER ); |
marcel1691 | 26:cf9cf40c63ea | 52 | |
stefan1691 | 17:719bb7709c80 | 53 | /** Hilfsfunktion zum Publizieren auf MQTT Broker */ |
marcel1691 | 21:4693a8b2a665 | 54 | void publish( MQTTNetwork &mqttNetwork, MQTT::Client<MQTTNetwork, Countdown> &client, char* topic ) |
stefan1691 | 17:719bb7709c80 | 55 | { |
marcel1691 | 21:4693a8b2a665 | 56 | led1 = 1; |
marcel1691 | 21:4693a8b2a665 | 57 | printf("Connecting to %s:%d\r\n", hostname, port); |
marcel1691 | 21:4693a8b2a665 | 58 | |
marcel1691 | 20:88b9edbdf125 | 59 | int rc = mqttNetwork.connect(hostname, port); |
marcel1691 | 20:88b9edbdf125 | 60 | if (rc != 0) |
marcel1691 | 21:4693a8b2a665 | 61 | printf("rc from TCP connect is %d\r\n", rc); |
stefan1691 | 17:719bb7709c80 | 62 | |
stefan1691 | 17:719bb7709c80 | 63 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
icraggs | 6:e4c690c45021 | 64 | data.MQTTVersion = 3; |
icraggs | 8:a3e3113054a1 | 65 | data.clientID.cstring = "mbed-sample"; |
icraggs | 12:086a9314e8a5 | 66 | data.username.cstring = "testuser"; |
icraggs | 12:086a9314e8a5 | 67 | data.password.cstring = "testpassword"; |
marcel1691 | 20:88b9edbdf125 | 68 | if ((rc = client.connect(data)) != 0) |
marcel1691 | 21:4693a8b2a665 | 69 | printf("rc from MQTT connect is %d\r\n", rc); |
icraggs | 2:638c854c0695 | 70 | |
marcel1691 | 20:88b9edbdf125 | 71 | MQTT::Message message; |
marcel1691 | 20:88b9edbdf125 | 72 | |
marcel1691 | 21:4693a8b2a665 | 73 | oled.cursor( 2, 0 ); |
marcel1691 | 21:4693a8b2a665 | 74 | oled.printf( "Topi: %s\n", topic ); |
marcel1691 | 21:4693a8b2a665 | 75 | oled.cursor( 3, 0 ); |
marcel1691 | 21:4693a8b2a665 | 76 | oled.printf( "Push: %s\n", buf ); |
icraggs | 2:638c854c0695 | 77 | message.qos = MQTT::QOS0; |
icraggs | 2:638c854c0695 | 78 | message.retained = false; |
icraggs | 2:638c854c0695 | 79 | message.dup = false; |
stefan1691 | 17:719bb7709c80 | 80 | message.payload = (void*) buf; |
icraggs | 2:638c854c0695 | 81 | message.payloadlen = strlen(buf)+1; |
marcel1691 | 21:4693a8b2a665 | 82 | client.publish( topic, message); |
icraggs | 2:638c854c0695 | 83 | |
stefan1691 | 17:719bb7709c80 | 84 | // Verbindung beenden, ansonsten ist nach 4x Schluss |
marcel1691 | 20:88b9edbdf125 | 85 | if ((rc = client.disconnect()) != 0) |
marcel1691 | 21:4693a8b2a665 | 86 | printf("rc from disconnect was %d\r\n", rc); |
marcel1691 | 20:88b9edbdf125 | 87 | |
marcel1691 | 20:88b9edbdf125 | 88 | mqttNetwork.disconnect(); |
marcel1691 | 21:4693a8b2a665 | 89 | led1 = 0; |
icraggs | 0:0cae29831d01 | 90 | } |
stefan1691 | 17:719bb7709c80 | 91 | |
stefan1691 | 17:719bb7709c80 | 92 | /** Hauptprogramm */ |
stefan1691 | 17:719bb7709c80 | 93 | int main() |
stefan1691 | 17:719bb7709c80 | 94 | { |
marcel1691 | 21:4693a8b2a665 | 95 | uint8_t id; |
marcel1691 | 21:4693a8b2a665 | 96 | float temp, hum; |
marcel1691 | 28:aa3b3fa5b5a7 | 97 | int encoder; |
marcel1691 | 22:b671b01d00f9 | 98 | alert = 0; |
marcel1691 | 21:4693a8b2a665 | 99 | |
marcel1691 | 21:4693a8b2a665 | 100 | oled.clear(); |
marcel1691 | 21:4693a8b2a665 | 101 | oled.printf( "MQTTPublish\r\n" ); |
marcel1691 | 21:4693a8b2a665 | 102 | oled.printf( "host: %s:%s\r\n", hostname, port ); |
marcel1691 | 20:88b9edbdf125 | 103 | |
marcel1691 | 27:43f3a9552db2 | 104 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
marcel1691 | 27:43f3a9552db2 | 105 | oled.printf( "SSID: %s\r\n", MBED_CONF_APP_WIFI_SSID ); |
marcel1691 | 32:020ebbef6706 | 106 | // Connect to the network with the default networking interface |
marcel1691 | 32:020ebbef6706 | 107 | // if you use WiFi: see mbed_app.json for the credentials |
marcel1691 | 32:020ebbef6706 | 108 | NetworkInterface* wifi = connect_to_default_network_interface(); |
marcel1691 | 32:020ebbef6706 | 109 | if ( !wifi ) |
marcel1691 | 32:020ebbef6706 | 110 | { |
marcel1691 | 32:020ebbef6706 | 111 | printf("Cannot connect to the network, see serial output\n"); |
marcel1691 | 32:020ebbef6706 | 112 | return 1; |
marcel1691 | 27:43f3a9552db2 | 113 | } |
marcel1691 | 20:88b9edbdf125 | 114 | |
marcel1691 | 20:88b9edbdf125 | 115 | // TCP/IP und MQTT initialisieren (muss in main erfolgen) |
marcel1691 | 32:020ebbef6706 | 116 | MQTTNetwork mqttNetwork( wifi ); |
marcel1691 | 20:88b9edbdf125 | 117 | MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); |
marcel1691 | 21:4693a8b2a665 | 118 | |
marcel1691 | 21:4693a8b2a665 | 119 | /* Init all sensors with default params */ |
marcel1691 | 21:4693a8b2a665 | 120 | hum_temp.init(NULL); |
marcel1691 | 29:99e8c0ea9ac1 | 121 | hum_temp.enable(); |
marcel1691 | 29:99e8c0ea9ac1 | 122 | |
marcel1691 | 29:99e8c0ea9ac1 | 123 | // RFID Reader initialisieren |
marcel1691 | 29:99e8c0ea9ac1 | 124 | rfidReader.PCD_Init(); |
stefan1691 | 17:719bb7709c80 | 125 | |
stefan1691 | 17:719bb7709c80 | 126 | while ( 1 ) |
stefan1691 | 17:719bb7709c80 | 127 | { |
marcel1691 | 21:4693a8b2a665 | 128 | // Temperator und Luftfeuchtigkeit |
marcel1691 | 21:4693a8b2a665 | 129 | hum_temp.read_id(&id); |
marcel1691 | 21:4693a8b2a665 | 130 | hum_temp.get_temperature(&temp); |
marcel1691 | 21:4693a8b2a665 | 131 | hum_temp.get_humidity(&hum); |
marcel1691 | 24:d2f0599798a1 | 132 | if ( type == 0 ) |
marcel1691 | 26:cf9cf40c63ea | 133 | { |
marcel1691 | 24:d2f0599798a1 | 134 | temp -= 5.0f; |
marcel1691 | 26:cf9cf40c63ea | 135 | m1.speed( 0.0f ); |
marcel1691 | 26:cf9cf40c63ea | 136 | } |
marcel1691 | 24:d2f0599798a1 | 137 | else if ( type == 2 ) |
marcel1691 | 26:cf9cf40c63ea | 138 | { |
marcel1691 | 24:d2f0599798a1 | 139 | temp += 5.0f; |
marcel1691 | 26:cf9cf40c63ea | 140 | m1.speed( 1.0f ); |
marcel1691 | 26:cf9cf40c63ea | 141 | } |
marcel1691 | 26:cf9cf40c63ea | 142 | else |
marcel1691 | 26:cf9cf40c63ea | 143 | { |
marcel1691 | 26:cf9cf40c63ea | 144 | m1.speed( 0.75f ); |
marcel1691 | 26:cf9cf40c63ea | 145 | } |
marcel1691 | 24:d2f0599798a1 | 146 | sprintf( buf, "0x%X,%2.2f,%2.1f,%s", id, temp, hum, cls[type] ); |
marcel1691 | 24:d2f0599798a1 | 147 | type++; |
marcel1691 | 24:d2f0599798a1 | 148 | if ( type > 2 ) |
marcel1691 | 24:d2f0599798a1 | 149 | type = 0; |
marcel1691 | 21:4693a8b2a665 | 150 | publish( mqttNetwork, client, topicTEMP ); |
marcel1691 | 21:4693a8b2a665 | 151 | |
marcel1691 | 21:4693a8b2a665 | 152 | // alert Tuer offen |
marcel1691 | 23:4d8f3061890e | 153 | printf( "Hall %4.4f, alert %d\n", hallSensor.read(), alert.read() ); |
marcel1691 | 21:4693a8b2a665 | 154 | if ( hallSensor.read() > 0.6f ) |
marcel1691 | 21:4693a8b2a665 | 155 | { |
marcel1691 | 22:b671b01d00f9 | 156 | // nur einmal Melden!, bis Reset |
marcel1691 | 23:4d8f3061890e | 157 | if ( alert.read() == 0 ) |
marcel1691 | 22:b671b01d00f9 | 158 | { |
marcel1691 | 24:d2f0599798a1 | 159 | sprintf( buf, "alert: hall" ); |
marcel1691 | 22:b671b01d00f9 | 160 | message.payload = (void*) buf; |
marcel1691 | 22:b671b01d00f9 | 161 | message.payloadlen = strlen(buf)+1; |
marcel1691 | 22:b671b01d00f9 | 162 | publish( mqttNetwork, client, topicALERT ); |
marcel1691 | 22:b671b01d00f9 | 163 | alert = 1; |
marcel1691 | 22:b671b01d00f9 | 164 | } |
marcel1691 | 26:cf9cf40c63ea | 165 | speaker.period( 1.0 / 3969.0 ); // 3969 = Tonfrequenz in Hz |
marcel1691 | 26:cf9cf40c63ea | 166 | speaker = 0.5f; |
marcel1691 | 26:cf9cf40c63ea | 167 | wait( 0.5f ); |
marcel1691 | 26:cf9cf40c63ea | 168 | speaker.period( 1.0 / 2800.0 ); |
marcel1691 | 27:43f3a9552db2 | 169 | wait( 0.5f ); |
marcel1691 | 21:4693a8b2a665 | 170 | } |
marcel1691 | 22:b671b01d00f9 | 171 | else |
marcel1691 | 26:cf9cf40c63ea | 172 | { |
marcel1691 | 22:b671b01d00f9 | 173 | alert = 0; |
marcel1691 | 26:cf9cf40c63ea | 174 | speaker = 0.0f; |
marcel1691 | 26:cf9cf40c63ea | 175 | } |
marcel1691 | 28:aa3b3fa5b5a7 | 176 | |
marcel1691 | 28:aa3b3fa5b5a7 | 177 | // Button (nur wenn gedrueckt) |
marcel1691 | 28:aa3b3fa5b5a7 | 178 | if ( button == 0 ) |
marcel1691 | 28:aa3b3fa5b5a7 | 179 | { |
marcel1691 | 28:aa3b3fa5b5a7 | 180 | sprintf( buf, "ON" ); |
marcel1691 | 28:aa3b3fa5b5a7 | 181 | publish( mqttNetwork, client, topicBUTTON ); |
marcel1691 | 28:aa3b3fa5b5a7 | 182 | } |
marcel1691 | 28:aa3b3fa5b5a7 | 183 | |
marcel1691 | 28:aa3b3fa5b5a7 | 184 | // Encoder |
marcel1691 | 32:020ebbef6706 | 185 | #ifdef TARGET_K64F |
marcel1691 | 28:aa3b3fa5b5a7 | 186 | encoder = wheel.getPulses(); |
marcel1691 | 28:aa3b3fa5b5a7 | 187 | sprintf( buf, "%d", encoder ); |
marcel1691 | 28:aa3b3fa5b5a7 | 188 | publish( mqttNetwork, client, topicENCODER ); |
marcel1691 | 32:020ebbef6706 | 189 | #endif |
marcel1691 | 28:aa3b3fa5b5a7 | 190 | |
marcel1691 | 28:aa3b3fa5b5a7 | 191 | // RFID Reader |
marcel1691 | 28:aa3b3fa5b5a7 | 192 | if ( rfidReader.PICC_IsNewCardPresent()) |
marcel1691 | 28:aa3b3fa5b5a7 | 193 | if ( rfidReader.PICC_ReadCardSerial()) |
marcel1691 | 28:aa3b3fa5b5a7 | 194 | { |
marcel1691 | 28:aa3b3fa5b5a7 | 195 | // Print Card UID (2-stellig mit Vornullen, Hexadecimal) |
marcel1691 | 28:aa3b3fa5b5a7 | 196 | printf("Card UID: "); |
marcel1691 | 28:aa3b3fa5b5a7 | 197 | for ( int i = 0; i < rfidReader.uid.size; i++ ) |
marcel1691 | 28:aa3b3fa5b5a7 | 198 | printf("%02X:", rfidReader.uid.uidByte[i]); |
marcel1691 | 28:aa3b3fa5b5a7 | 199 | printf("\n"); |
marcel1691 | 28:aa3b3fa5b5a7 | 200 | |
marcel1691 | 28:aa3b3fa5b5a7 | 201 | // Print Card type |
marcel1691 | 28:aa3b3fa5b5a7 | 202 | int piccType = rfidReader.PICC_GetType(rfidReader.uid.sak); |
marcel1691 | 28:aa3b3fa5b5a7 | 203 | printf("PICC Type: %s \n", rfidReader.PICC_GetTypeName(piccType) ); |
marcel1691 | 28:aa3b3fa5b5a7 | 204 | |
marcel1691 | 28:aa3b3fa5b5a7 | 205 | sprintf( buf, "%02X:%02X:%02X:%02X:", rfidReader.uid.uidByte[0], rfidReader.uid.uidByte[1], rfidReader.uid.uidByte[2], rfidReader.uid.uidByte[3] ); |
marcel1691 | 28:aa3b3fa5b5a7 | 206 | publish( mqttNetwork, client, topicRFID ); |
marcel1691 | 28:aa3b3fa5b5a7 | 207 | |
marcel1691 | 28:aa3b3fa5b5a7 | 208 | } |
marcel1691 | 28:aa3b3fa5b5a7 | 209 | |
marcel1691 | 21:4693a8b2a665 | 210 | wait ( 2.0f ); |
stefan1691 | 17:719bb7709c80 | 211 | } |
stefan1691 | 17:719bb7709c80 | 212 | } |