initial release

Dependencies:   BSP_B-L475E-IOT01 MQTT

Fork of Multiprotocol by Farnell-Element14 Bologna IOT Team

Revision:
0:705a0a684de2
Child:
1:ddef44f2db39
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jul 26 23:58:14 2018 +0000
@@ -0,0 +1,168 @@
+// main.cpp
+
+#include "mbed.h"
+#include "ISM43362Interface.h"
+#include "TCPSocket.h"
+#include "MQTTmbed.h"
+#include "MQTTClient.h"
+#include "HTS221Sensor.h"
+
+// Definitions ---------------------------------------------------------
+
+// Change it with your WiFi network name
+#define WIFI_NETWORK_NAME       "rucola"
+// Change it with your WiFi password name
+#define WIFI_NETWORK_PASSWORD   "Rosmarino_10"
+#define WIFI_SECURITY           NSAPI_SECURITY_WPA_WPA2
+
+#define MQTT_HOST               "demo.thingsboard.io"
+#define MQTT_PORT               1883
+#define MQTT_TOPIC              "v1/devices/me/telemetry"
+class MQTTNetwork
+{
+public:
+    MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) {
+        socket = new TCPSocket();
+    }
+
+    ~MQTTNetwork() {
+        delete socket;
+    }
+
+    int read(unsigned char* buffer, int len, int timeout) {
+        return socket->recv(buffer, len);
+    }
+
+    int write(unsigned char* buffer, int len, int timeout) {
+        return socket->send(buffer, len);
+    }
+
+    int connect(const char* hostname, int port) {
+        socket->open(network);
+        return socket->connect(hostname, port);
+    }
+
+    int disconnect() {
+        return socket->close();
+    }
+
+private:
+    NetworkInterface* network;
+    TCPSocket* socket;
+};
+
+int main()
+{
+    int count = 0;
+
+    ISM43362Interface wifi(MBED_CONF_APP_WIFI_SPI_MOSI,
+            MBED_CONF_APP_WIFI_SPI_MISO,
+            MBED_CONF_APP_WIFI_SPI_SCLK,
+            MBED_CONF_APP_WIFI_SPI_NSS,
+            MBED_CONF_APP_WIFI_RESET,
+            MBED_CONF_APP_WIFI_DATAREADY,
+            MBED_CONF_APP_WIFI_WAKEUP, false);
+
+    // Scanning WiFi networks ------------------------------------------
+
+    WiFiAccessPoint *ap;
+
+    count = wifi.scan(NULL, 0);
+    printf("%d networks available.\n", count);
+
+    /* Limit number of network arbitrary to 15 */
+    count = count < 15 ? count : 15;
+
+    ap = new WiFiAccessPoint[count];
+    count = wifi.scan(ap, count);
+    for (int i = 0; i < count; i++) {
+        printf("Network: %s RSSI: %hhd\n", ap[i].get_ssid(), ap[i].get_rssi());
+    }
+
+    delete[] ap;
+
+    // Connecting to WiFi network --------------------------------------
+
+    printf("\nConnecting to %s...\n", WIFI_NETWORK_NAME);
+    int ret = wifi.connect(WIFI_NETWORK_NAME, WIFI_NETWORK_PASSWORD, WIFI_SECURITY);
+    if (ret != 0) {
+        printf("\nConnection error\n");
+        return -1;
+    }
+
+    printf("Success\n\n");
+    printf("MAC: %s\n", wifi.get_mac_address());
+    printf("IP: %s\n", wifi.get_ip_address());
+    printf("Netmask: %s\n", wifi.get_netmask());
+    printf("Gateway: %s\n", wifi.get_gateway());
+    printf("RSSI: %d\n\n", wifi.get_rssi());
+
+    printf("Collegamento MQTT server\n");
+
+MQTTNetwork network(&wifi);
+MQTT::Client<MQTTNetwork, Countdown> client(network);
+
+char assess_token[] = "nNcsYA9xXL8OsEmHd6Cq";
+
+MQTTPacket_connectData conn_data = MQTTPacket_connectData_initializer;
+conn_data.username.cstring = assess_token;
+
+if (network.connect(MQTT_HOST, MQTT_PORT) < 0) {
+    printf("failed to connect to " MQTT_HOST  "\n");
+    return -1;
+}
+
+if (client.connect(conn_data) < 0) {
+    printf("failed to send MQTT connect message\n");
+    return -1;
+}
+
+printf("successfully connect!\n");
+
+// Initialize sensors --------------------------------------------------
+
+uint8_t id;
+DevI2C i2c_2(PB_11, PB_10);
+HTS221Sensor hum_temp(&i2c_2);
+
+hum_temp.init(NULL);
+hum_temp.enable();
+hum_temp.read_id(&id);
+printf("HTS221  humidity & temperature sensor = 0x%X\r\n", id);
+
+// Get data from sensors -----------------------------------------------
+
+for (;;) {
+    float temp, humid;
+
+    hum_temp.get_temperature(&temp);
+    hum_temp.get_humidity(&humid);
+    printf("HTS221:  [temp] %.2f C, [hum]   %.2f%%\r\n", temp, humid);
+
+    char msg[256];
+    int n = snprintf(msg, sizeof(msg),
+        "{\"temperature\":%f, \"humidity\":%f, \"active\": false}",
+        temp, humid);
+
+    void *payload = reinterpret_cast<void*>(msg);
+    size_t payload_len = n;
+
+    printf("publish to: %s %d %s\r\n", MQTT_HOST, MQTT_PORT, MQTT_TOPIC);
+
+    if (client.publish(MQTT_TOPIC, payload, n) < 0) {
+        printf("failed to publish MQTT message");
+    }
+
+    wait_ms(5000);
+}
+
+
+
+
+client.disconnect();
+wifi.disconnect();
+
+printf("\ndone\n");
+return 0;
+
+}
\ No newline at end of file