file con versione corretta
Revision 0:705a0a684de2, committed 2018-07-26
- Comitter:
- fabio_gatti
- Date:
- Thu Jul 26 23:58:14 2018 +0000
- Child:
- 1:ddef44f2db39
- Commit message:
- primo rilascio
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTS221.lib Thu Jul 26 23:58:14 2018 +0000 @@ -0,0 +1,1 @@ +http://os.mbed.com/teams/ST/code/HTS221/#312ee2694a77
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MQTT_library.lib Thu Jul 26 23:58:14 2018 +0000 @@ -0,0 +1,1 @@ +http://os.mbed.com/teams/mqtt/code/MQTT/#9cff7b6bbd01
--- /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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Thu Jul 26 23:58:14 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#569159b784f70feaa32ce226aaca896fb83452f7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json Thu Jul 26 23:58:14 2018 +0000
@@ -0,0 +1,64 @@
+{
+ "macros": [
+ "MBED_CONF_APP_MAIN_STACK_SIZE=6000",
+ "MBED_CONF_APP_THREAD_STACK_SIZE=6000",
+ "DEVICE_STDIO_MESSAGES=1"
+ ],
+ "config": {
+ "wifi-shield": {
+ "help": "Options are internal, WIFI_IDW0XX1",
+ "value": "internal"
+ },
+ "wifi-tx": {
+ "help": "TX pin for serial connection to external device",
+ "value": "D1"
+ },
+ "wifi-rx": {
+ "help": "RX pin for serial connection to external device",
+ "value": "D0"
+ },
+ "wifi-spi_miso": {
+ "help": "SPI-MISO connection to external device",
+ "value": "PC_11"
+ },
+ "wifi-spi_mosi": {
+ "help": "SPI-MOSI connection to external device",
+ "value": "PC_12"
+ },
+ "wifi-spi_sclk": {
+ "help": "SPI-CLOCK connection to external device",
+ "value": "PC_10"
+ },
+ "wifi-spi_nss": {
+ "help": "SPI chip select of external device",
+ "value": "PE_0"
+ },
+ "wifi-reset": {
+ "help": "WIFI module reset pin",
+ "value": "PE_8"
+ },
+ "wifi-dataready": {
+ "help": "WIFI module data ready pin",
+ "value": "PE_1"
+ },
+ "wifi-wakeup": {
+ "help": "WIFI module wakeup pin",
+ "value": "PB_12"
+ }
+ },
+ "target_overrides": {
+ "*": {
+ "platform.stdio-baud-rate": 115200,
+ "platform.stdio-convert-newlines": true
+ },
+ "DISCO_L475VG_IOT1A": {
+ "wifi-spi_miso": "PC_11",
+ "wifi-spi_mosi": "PC_12",
+ "wifi-spi_sclk": "PC_10",
+ "wifi-spi_nss": "PE_0",
+ "wifi-reset": "PE_8",
+ "wifi-dataready": "PE_1",
+ "wifi-wakeup": "PB_12"
+ }
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wifi-ism43362.lib Thu Jul 26 23:58:14 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/wifi-ism43362/#a6e22ee7a226fbb4f5546b6fc39620d4ecb7ec7e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wifi-x-nucleo-idw01m1.lib Thu Jul 26 23:58:14 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/wifi-x-nucleo-idw01m1/#5871f7011d7ff2c50e3faf992ebab88e9f69dc95