Fabio Gatti
/
MultiProtocol_Cloud_copy
das
main.cpp
- Committer:
- fabio_gatti
- Date:
- 2018-08-01
- Revision:
- 2:983b7f5dde1e
- Parent:
- 1:ddef44f2db39
- Child:
- 3:647e37ef0eec
File content as of revision 2:983b7f5dde1e:
// 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 "farnell_iot_lab" #define WIFI_NETWORK_NAME "rucola" // Change it with your WiFi password name //#define WIFI_NETWORK_PASSWORD "smartlab" #define WIFI_NETWORK_PASSWORD "Rosmarino_10" #define WIFI_SECURITY NSAPI_SECURITY_WPA_WPA2 #define COMM_PROTO 0 // scegliere il protocollo di trasporto dati // COMM_PROTO = 0 -> accesso MQTT (default); // COMM_PROTO = 1 -> access HTTP; // COMM_PROTO = 2 -> accesso COAP; #ifndef COMM_PROTO #define COMM_PROTO 0 #endif #if COMM_PROTO==0 #define MQTT_HOST "demo.thingsboard.io" #define MQTT_PORT 1883 #define MQTT_TOPIC "v1/devices/me/telemetry" #define DEVICE_ACCESS_TOKEN "nNcsYA9xXL8OsEmHd6Cq" #endif #if COMM_PROTO==0 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; }; #endif 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()); #if COMM_PROTO==0 printf("Collegamento MQTT server: " MQTT_HOST "\n"); MQTTNetwork network(&wifi); MQTT::Client<MQTTNetwork, Countdown> client(network); char assess_token[] = DEVICE_ACCESS_TOKEN; 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 to MQTT server!\n"); #endif // 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 COMM_PROTO==0 if (client.publish(MQTT_TOPIC, payload, n) < 0) { printf("failed to publish MQTT message"); } #endif wait_ms(5000); } //client.disconnect(); //wifi.disconnect(); //printf("\ndone\n"); //return 0; }