IoTKitV3 / Mbed OS MQTTPublish

Dependencies:   QEI MFRC522 HTS221 IoTKit BMP180 MQTT

Fork of MQTTPublish by smd.iotkit2.ch

Committer:
marcel1691
Date:
Thu Apr 25 18:57:23 2019 +0000
Revision:
33:06cbea3c9d4c
Parent:
32:020ebbef6706
Child:
34:d5ec41049341
RFID Reader und Encoder entfernt beim DISCO_L475VG_IOT01A, gibt Probleme

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