IoTKitV3 / Mbed OS MQTTPublish

Dependencies:   QEI MFRC522 HTS221 IoTKit BMP180 MQTT

Fork of MQTTPublish by smd.iotkit2.ch

Committer:
stefan1691
Date:
Tue May 26 19:24:44 2020 +0000
Revision:
37:815a3954eef6
Parent:
36:85831ebc80f0
Integration Nucleo F303RE Board inkl. BMP180 Sensor

Who changed what in which revision?

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