IoTKitV3 / Mbed OS MQTTPublish

Dependencies:   QEI MFRC522 HTS221 IoTKit BMP180 MQTT

Fork of MQTTPublish by smd.iotkit2.ch

Committer:
marcel1691
Date:
Mon Sep 10 16:47:34 2018 +0000
Revision:
26:cf9cf40c63ea
Parent:
25:5e982d03e7d1
Child:
27:43f3a9552db2
Motor und Buzzer integriert

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 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"
marcel1691 26:cf9cf40c63ea 9 #include "Motor.h"
icraggs 2:638c854c0695 10
marcel1691 21:4693a8b2a665 11 static DevI2C devI2c(PTE0,PTE1);
marcel1691 21:4693a8b2a665 12 static HTS221Sensor hum_temp(&devI2c);
marcel1691 21:4693a8b2a665 13 AnalogIn hallSensor( PTC0 );
marcel1691 20:88b9edbdf125 14
stefan1691 18:53d83febbe0a 15 // Topic's
marcel1691 21:4693a8b2a665 16 char* topicTEMP = "iotkit/sensor";
marcel1691 21:4693a8b2a665 17 char* topicALERT = "iotkit/alert";
stefan1691 17:719bb7709c80 18 // MQTT Brocker
marcel1691 25:5e982d03e7d1 19 char* hostname = "192.168.3.10";
marcel1691 20:88b9edbdf125 20 int port = 31883;
stefan1691 17:719bb7709c80 21 // MQTT Message
stefan1691 17:719bb7709c80 22 MQTT::Message message;
stefan1691 17:719bb7709c80 23 // I/O Buffer
stefan1691 17:719bb7709c80 24 char buf[100];
marcel1691 24:d2f0599798a1 25
marcel1691 24:d2f0599798a1 26 // Klassifikation
marcel1691 24:d2f0599798a1 27 char cls[3][10] = { "low", "middle", "high" };
marcel1691 24:d2f0599798a1 28 int type = 0;
marcel1691 24:d2f0599798a1 29
stefan1691 17:719bb7709c80 30 // UI
marcel1691 21:4693a8b2a665 31 OLEDDisplay oled( PTE26, PTE0, PTE1);
marcel1691 20:88b9edbdf125 32 DigitalOut led1( D10 );
marcel1691 22:b671b01d00f9 33 DigitalOut alert( D13 );
icraggs 2:638c854c0695 34
marcel1691 26:cf9cf40c63ea 35 // Aktore(n)
marcel1691 26:cf9cf40c63ea 36 Motor m1(D3, D2, D4); // PWM, Vorwaerts, Rueckwarts
marcel1691 26:cf9cf40c63ea 37 PwmOut speaker( D12 );
marcel1691 26:cf9cf40c63ea 38
stefan1691 17:719bb7709c80 39 /** Hilfsfunktion zum Publizieren auf MQTT Broker */
marcel1691 21:4693a8b2a665 40 void publish( MQTTNetwork &mqttNetwork, MQTT::Client<MQTTNetwork, Countdown> &client, char* topic )
stefan1691 17:719bb7709c80 41 {
marcel1691 21:4693a8b2a665 42 led1 = 1;
marcel1691 21:4693a8b2a665 43 printf("Connecting to %s:%d\r\n", hostname, port);
marcel1691 21:4693a8b2a665 44
marcel1691 20:88b9edbdf125 45 int rc = mqttNetwork.connect(hostname, port);
marcel1691 20:88b9edbdf125 46 if (rc != 0)
marcel1691 21:4693a8b2a665 47 printf("rc from TCP connect is %d\r\n", rc);
stefan1691 17:719bb7709c80 48
stefan1691 17:719bb7709c80 49 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 6:e4c690c45021 50 data.MQTTVersion = 3;
icraggs 8:a3e3113054a1 51 data.clientID.cstring = "mbed-sample";
icraggs 12:086a9314e8a5 52 data.username.cstring = "testuser";
icraggs 12:086a9314e8a5 53 data.password.cstring = "testpassword";
marcel1691 20:88b9edbdf125 54 if ((rc = client.connect(data)) != 0)
marcel1691 21:4693a8b2a665 55 printf("rc from MQTT connect is %d\r\n", rc);
icraggs 2:638c854c0695 56
marcel1691 20:88b9edbdf125 57 MQTT::Message message;
marcel1691 20:88b9edbdf125 58
marcel1691 21:4693a8b2a665 59 oled.cursor( 2, 0 );
marcel1691 21:4693a8b2a665 60 oled.printf( "Topi: %s\n", topic );
marcel1691 21:4693a8b2a665 61 oled.cursor( 3, 0 );
marcel1691 21:4693a8b2a665 62 oled.printf( "Push: %s\n", buf );
icraggs 2:638c854c0695 63 message.qos = MQTT::QOS0;
icraggs 2:638c854c0695 64 message.retained = false;
icraggs 2:638c854c0695 65 message.dup = false;
stefan1691 17:719bb7709c80 66 message.payload = (void*) buf;
icraggs 2:638c854c0695 67 message.payloadlen = strlen(buf)+1;
marcel1691 21:4693a8b2a665 68 client.publish( topic, message);
icraggs 2:638c854c0695 69
stefan1691 17:719bb7709c80 70 // Verbindung beenden, ansonsten ist nach 4x Schluss
marcel1691 20:88b9edbdf125 71 if ((rc = client.disconnect()) != 0)
marcel1691 21:4693a8b2a665 72 printf("rc from disconnect was %d\r\n", rc);
marcel1691 20:88b9edbdf125 73
marcel1691 20:88b9edbdf125 74 mqttNetwork.disconnect();
marcel1691 21:4693a8b2a665 75 led1 = 0;
icraggs 0:0cae29831d01 76 }
stefan1691 17:719bb7709c80 77
stefan1691 17:719bb7709c80 78 /** Hauptprogramm */
stefan1691 17:719bb7709c80 79 int main()
stefan1691 17:719bb7709c80 80 {
marcel1691 21:4693a8b2a665 81 uint8_t id;
marcel1691 21:4693a8b2a665 82 float temp, hum;
marcel1691 22:b671b01d00f9 83 alert = 0;
marcel1691 21:4693a8b2a665 84
marcel1691 21:4693a8b2a665 85 oled.clear();
marcel1691 21:4693a8b2a665 86 oled.printf( "MQTTPublish\r\n" );
marcel1691 21:4693a8b2a665 87 oled.printf( "host: %s:%s\r\n", hostname, port );
marcel1691 20:88b9edbdf125 88
marcel1691 20:88b9edbdf125 89 NetworkInterface* network = easy_connect(true);
marcel1691 20:88b9edbdf125 90 if (!network)
marcel1691 20:88b9edbdf125 91 return -1;
marcel1691 20:88b9edbdf125 92
marcel1691 20:88b9edbdf125 93 // TCP/IP und MQTT initialisieren (muss in main erfolgen)
marcel1691 20:88b9edbdf125 94 MQTTNetwork mqttNetwork(network);
marcel1691 20:88b9edbdf125 95 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
marcel1691 21:4693a8b2a665 96
marcel1691 21:4693a8b2a665 97 /* Init all sensors with default params */
marcel1691 21:4693a8b2a665 98 hum_temp.init(NULL);
marcel1691 21:4693a8b2a665 99 hum_temp.enable();
stefan1691 17:719bb7709c80 100
stefan1691 17:719bb7709c80 101 while ( 1 )
stefan1691 17:719bb7709c80 102 {
marcel1691 21:4693a8b2a665 103 // Temperator und Luftfeuchtigkeit
marcel1691 21:4693a8b2a665 104 hum_temp.read_id(&id);
marcel1691 21:4693a8b2a665 105 hum_temp.get_temperature(&temp);
marcel1691 21:4693a8b2a665 106 hum_temp.get_humidity(&hum);
marcel1691 24:d2f0599798a1 107 if ( type == 0 )
marcel1691 26:cf9cf40c63ea 108 {
marcel1691 24:d2f0599798a1 109 temp -= 5.0f;
marcel1691 26:cf9cf40c63ea 110 m1.speed( 0.0f );
marcel1691 26:cf9cf40c63ea 111 }
marcel1691 24:d2f0599798a1 112 else if ( type == 2 )
marcel1691 26:cf9cf40c63ea 113 {
marcel1691 24:d2f0599798a1 114 temp += 5.0f;
marcel1691 26:cf9cf40c63ea 115 m1.speed( 1.0f );
marcel1691 26:cf9cf40c63ea 116 }
marcel1691 26:cf9cf40c63ea 117 else
marcel1691 26:cf9cf40c63ea 118 {
marcel1691 26:cf9cf40c63ea 119 m1.speed( 0.75f );
marcel1691 26:cf9cf40c63ea 120 }
marcel1691 24:d2f0599798a1 121 sprintf( buf, "0x%X,%2.2f,%2.1f,%s", id, temp, hum, cls[type] );
marcel1691 24:d2f0599798a1 122 type++;
marcel1691 24:d2f0599798a1 123 if ( type > 2 )
marcel1691 24:d2f0599798a1 124 type = 0;
marcel1691 21:4693a8b2a665 125 publish( mqttNetwork, client, topicTEMP );
marcel1691 21:4693a8b2a665 126
marcel1691 21:4693a8b2a665 127 // alert Tuer offen
marcel1691 23:4d8f3061890e 128 printf( "Hall %4.4f, alert %d\n", hallSensor.read(), alert.read() );
marcel1691 21:4693a8b2a665 129 if ( hallSensor.read() > 0.6f )
marcel1691 21:4693a8b2a665 130 {
marcel1691 22:b671b01d00f9 131 // nur einmal Melden!, bis Reset
marcel1691 23:4d8f3061890e 132 if ( alert.read() == 0 )
marcel1691 22:b671b01d00f9 133 {
marcel1691 24:d2f0599798a1 134 sprintf( buf, "alert: hall" );
marcel1691 22:b671b01d00f9 135 message.payload = (void*) buf;
marcel1691 22:b671b01d00f9 136 message.payloadlen = strlen(buf)+1;
marcel1691 22:b671b01d00f9 137 publish( mqttNetwork, client, topicALERT );
marcel1691 22:b671b01d00f9 138 alert = 1;
marcel1691 22:b671b01d00f9 139 }
marcel1691 26:cf9cf40c63ea 140 speaker.period( 1.0 / 3969.0 ); // 3969 = Tonfrequenz in Hz
marcel1691 26:cf9cf40c63ea 141 speaker = 0.5f;
marcel1691 26:cf9cf40c63ea 142 wait( 0.5f );
marcel1691 26:cf9cf40c63ea 143 speaker.period( 1.0 / 2800.0 );
marcel1691 26:cf9cf40c63ea 144 wait( 0.5f );
marcel1691 21:4693a8b2a665 145 }
marcel1691 22:b671b01d00f9 146 else
marcel1691 26:cf9cf40c63ea 147 {
marcel1691 22:b671b01d00f9 148 alert = 0;
marcel1691 26:cf9cf40c63ea 149 speaker = 0.0f;
marcel1691 26:cf9cf40c63ea 150 }
marcel1691 21:4693a8b2a665 151 wait ( 2.0f );
stefan1691 17:719bb7709c80 152 }
stefan1691 17:719bb7709c80 153 }