![](/media/cache/group/fe14.jpg.50x50_q85.jpg)
file con versione corretta
main.cpp@6:7cc6f2ffde1b, 2018-08-21 (annotated)
- Committer:
- fabio_gatti
- Date:
- Tue Aug 21 09:05:56 2018 +0000
- Revision:
- 6:7cc6f2ffde1b
- Parent:
- 5:8eb996b86a37
ripristina la versione della libreria wifi-ism
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fabio_gatti | 0:705a0a684de2 | 1 | // main.cpp |
fabio_gatti | 3:647e37ef0eec | 2 | #include <string> |
fabio_gatti | 0:705a0a684de2 | 3 | |
fabio_gatti | 0:705a0a684de2 | 4 | #include "mbed.h" |
fabio_gatti | 0:705a0a684de2 | 5 | #include "ISM43362Interface.h" |
fabio_gatti | 0:705a0a684de2 | 6 | #include "TCPSocket.h" |
fabio_gatti | 3:647e37ef0eec | 7 | #include "HTS221Sensor.h" |
fabio_gatti | 3:647e37ef0eec | 8 | |
fabio_gatti | 3:647e37ef0eec | 9 | #include "thingsboard_account.h" |
fabio_gatti | 3:647e37ef0eec | 10 | |
fabio_gatti | 3:647e37ef0eec | 11 | |
fabio_gatti | 3:647e37ef0eec | 12 | // H file per MQTT |
fabio_gatti | 0:705a0a684de2 | 13 | #include "MQTTmbed.h" |
fabio_gatti | 0:705a0a684de2 | 14 | #include "MQTTClient.h" |
fabio_gatti | 3:647e37ef0eec | 15 | |
fabio_gatti | 3:647e37ef0eec | 16 | // H file per COAP |
fabio_gatti | 3:647e37ef0eec | 17 | #include "sn_nsdl.h" |
fabio_gatti | 3:647e37ef0eec | 18 | #include "sn_coap_protocol.h" |
fabio_gatti | 3:647e37ef0eec | 19 | #include "sn_coap_header.h" |
fabio_gatti | 0:705a0a684de2 | 20 | |
fabio_gatti | 0:705a0a684de2 | 21 | // Definitions --------------------------------------------------------- |
fabio_gatti | 0:705a0a684de2 | 22 | |
fabio_gatti | 0:705a0a684de2 | 23 | // Change it with your WiFi network name |
fabio_gatti | 2:983b7f5dde1e | 24 | //#define WIFI_NETWORK_NAME "farnell_iot_lab" |
fabio_gatti | 0:705a0a684de2 | 25 | #define WIFI_NETWORK_NAME "rucola" |
fabio_gatti | 0:705a0a684de2 | 26 | // Change it with your WiFi password name |
fabio_gatti | 2:983b7f5dde1e | 27 | //#define WIFI_NETWORK_PASSWORD "smartlab" |
fabio_gatti | 0:705a0a684de2 | 28 | #define WIFI_NETWORK_PASSWORD "Rosmarino_10" |
fabio_gatti | 3:647e37ef0eec | 29 | |
fabio_gatti | 0:705a0a684de2 | 30 | #define WIFI_SECURITY NSAPI_SECURITY_WPA_WPA2 |
fabio_gatti | 0:705a0a684de2 | 31 | |
fabio_gatti | 5:8eb996b86a37 | 32 | #define COMM_PROTO 2 |
fabio_gatti | 2:983b7f5dde1e | 33 | |
fabio_gatti | 2:983b7f5dde1e | 34 | // scegliere il protocollo di trasporto dati |
fabio_gatti | 2:983b7f5dde1e | 35 | // COMM_PROTO = 0 -> accesso MQTT (default); |
fabio_gatti | 2:983b7f5dde1e | 36 | // COMM_PROTO = 1 -> access HTTP; |
fabio_gatti | 2:983b7f5dde1e | 37 | // COMM_PROTO = 2 -> accesso COAP; |
fabio_gatti | 2:983b7f5dde1e | 38 | #ifndef COMM_PROTO |
fabio_gatti | 2:983b7f5dde1e | 39 | #define COMM_PROTO 0 |
fabio_gatti | 2:983b7f5dde1e | 40 | #endif |
fabio_gatti | 2:983b7f5dde1e | 41 | |
fabio_gatti | 3:647e37ef0eec | 42 | // scegliere il token al device |
fabio_gatti | 4:591b0f33f8b0 | 43 | #define DEVICE_ID 1 |
fabio_gatti | 4:591b0f33f8b0 | 44 | #define DEVICE_ACCESS_TOKEN TEAM_1_DEVICE_ACCESS_TOKEN |
fabio_gatti | 3:647e37ef0eec | 45 | |
fabio_gatti | 3:647e37ef0eec | 46 | // ciclo di frequenza lettura |
fabio_gatti | 3:647e37ef0eec | 47 | #define SENSOR_READING_PERIOD 5000 //in ms |
fabio_gatti | 3:647e37ef0eec | 48 | |
fabio_gatti | 3:647e37ef0eec | 49 | #if COMM_PROTO==0 //MQTT protocol |
fabio_gatti | 3:647e37ef0eec | 50 | |
fabio_gatti | 3:647e37ef0eec | 51 | #define MQTT_HOST "demo.thingsboard.io" |
fabio_gatti | 3:647e37ef0eec | 52 | #define MQTT_PORT 1883 |
fabio_gatti | 3:647e37ef0eec | 53 | #define MQTT_TOPIC "v1/devices/me/telemetry" |
fabio_gatti | 2:983b7f5dde1e | 54 | |
fabio_gatti | 3:647e37ef0eec | 55 | #elif COMM_PROTO==1 //HTTP protocol |
fabio_gatti | 3:647e37ef0eec | 56 | #define HTTP_HOST "demo.thingsboard.io" |
fabio_gatti | 3:647e37ef0eec | 57 | #define HTTP_PORT 80 |
fabio_gatti | 3:647e37ef0eec | 58 | #define HTTP_STR_1 "/api/v1/" |
fabio_gatti | 3:647e37ef0eec | 59 | #define HTTP_STR_2 "/telemetry" |
fabio_gatti | 1:ddef44f2db39 | 60 | |
fabio_gatti | 3:647e37ef0eec | 61 | #elif COMM_PROTO==2 //COAP protocol |
fabio_gatti | 3:647e37ef0eec | 62 | #define COAP_HOST "demo.thingsboard.io" |
fabio_gatti | 3:647e37ef0eec | 63 | #define COAP_PORT 5683 |
fabio_gatti | 3:647e37ef0eec | 64 | #define COAP_STR_1 "/api/v1/" |
fabio_gatti | 3:647e37ef0eec | 65 | #define COAP_STR_2 "/telemetry" |
fabio_gatti | 3:647e37ef0eec | 66 | |
fabio_gatti | 3:647e37ef0eec | 67 | #define SHOW_COAP_RESPONSE false |
fabio_gatti | 2:983b7f5dde1e | 68 | |
fabio_gatti | 3:647e37ef0eec | 69 | #endif //end protocol |
fabio_gatti | 2:983b7f5dde1e | 70 | |
fabio_gatti | 3:647e37ef0eec | 71 | // strutture comuni |
fabio_gatti | 3:647e37ef0eec | 72 | |
fabio_gatti | 3:647e37ef0eec | 73 | #if COMM_PROTO == 0 |
fabio_gatti | 0:705a0a684de2 | 74 | class MQTTNetwork |
fabio_gatti | 0:705a0a684de2 | 75 | { |
fabio_gatti | 0:705a0a684de2 | 76 | public: |
fabio_gatti | 0:705a0a684de2 | 77 | MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) { |
fabio_gatti | 0:705a0a684de2 | 78 | socket = new TCPSocket(); |
fabio_gatti | 0:705a0a684de2 | 79 | } |
fabio_gatti | 0:705a0a684de2 | 80 | |
fabio_gatti | 0:705a0a684de2 | 81 | ~MQTTNetwork() { |
fabio_gatti | 0:705a0a684de2 | 82 | delete socket; |
fabio_gatti | 0:705a0a684de2 | 83 | } |
fabio_gatti | 0:705a0a684de2 | 84 | |
fabio_gatti | 0:705a0a684de2 | 85 | int read(unsigned char* buffer, int len, int timeout) { |
fabio_gatti | 0:705a0a684de2 | 86 | return socket->recv(buffer, len); |
fabio_gatti | 0:705a0a684de2 | 87 | } |
fabio_gatti | 0:705a0a684de2 | 88 | |
fabio_gatti | 0:705a0a684de2 | 89 | int write(unsigned char* buffer, int len, int timeout) { |
fabio_gatti | 0:705a0a684de2 | 90 | return socket->send(buffer, len); |
fabio_gatti | 0:705a0a684de2 | 91 | } |
fabio_gatti | 0:705a0a684de2 | 92 | |
fabio_gatti | 0:705a0a684de2 | 93 | int connect(const char* hostname, int port) { |
fabio_gatti | 0:705a0a684de2 | 94 | socket->open(network); |
fabio_gatti | 0:705a0a684de2 | 95 | return socket->connect(hostname, port); |
fabio_gatti | 0:705a0a684de2 | 96 | } |
fabio_gatti | 0:705a0a684de2 | 97 | |
fabio_gatti | 0:705a0a684de2 | 98 | int disconnect() { |
fabio_gatti | 0:705a0a684de2 | 99 | return socket->close(); |
fabio_gatti | 0:705a0a684de2 | 100 | } |
fabio_gatti | 0:705a0a684de2 | 101 | |
fabio_gatti | 0:705a0a684de2 | 102 | private: |
fabio_gatti | 0:705a0a684de2 | 103 | NetworkInterface* network; |
fabio_gatti | 0:705a0a684de2 | 104 | TCPSocket* socket; |
fabio_gatti | 0:705a0a684de2 | 105 | }; |
fabio_gatti | 3:647e37ef0eec | 106 | |
fabio_gatti | 3:647e37ef0eec | 107 | #elif COMM_PROTO==2 //COAP protocol |
fabio_gatti | 3:647e37ef0eec | 108 | UDPSocket socket; // Socket to talk CoAP over |
fabio_gatti | 3:647e37ef0eec | 109 | Thread recvfromThread; // Thread to receive messages over CoAP |
fabio_gatti | 3:647e37ef0eec | 110 | struct coap_s* coapHandle; |
fabio_gatti | 3:647e37ef0eec | 111 | coap_version_e coapVersion = COAP_VERSION_1; |
fabio_gatti | 3:647e37ef0eec | 112 | |
fabio_gatti | 3:647e37ef0eec | 113 | // CoAP HAL |
fabio_gatti | 3:647e37ef0eec | 114 | void* coap_malloc(uint16_t size) { |
fabio_gatti | 3:647e37ef0eec | 115 | return malloc(size); |
fabio_gatti | 3:647e37ef0eec | 116 | } |
fabio_gatti | 3:647e37ef0eec | 117 | |
fabio_gatti | 3:647e37ef0eec | 118 | void coap_free(void* addr) { |
fabio_gatti | 3:647e37ef0eec | 119 | free(addr); |
fabio_gatti | 3:647e37ef0eec | 120 | } |
fabio_gatti | 3:647e37ef0eec | 121 | |
fabio_gatti | 3:647e37ef0eec | 122 | // tx_cb and rx_cb are not used in this program |
fabio_gatti | 3:647e37ef0eec | 123 | uint8_t coap_tx_cb(uint8_t *a, uint16_t b, sn_nsdl_addr_s *c, void *d) { |
fabio_gatti | 3:647e37ef0eec | 124 | printf("coap tx cb\n"); |
fabio_gatti | 3:647e37ef0eec | 125 | return 0; |
fabio_gatti | 3:647e37ef0eec | 126 | } |
fabio_gatti | 3:647e37ef0eec | 127 | |
fabio_gatti | 3:647e37ef0eec | 128 | int8_t coap_rx_cb(sn_coap_hdr_s *a, sn_nsdl_addr_s *b, void *c) { |
fabio_gatti | 3:647e37ef0eec | 129 | printf("coap rx cb\n"); |
fabio_gatti | 3:647e37ef0eec | 130 | return 0; |
fabio_gatti | 3:647e37ef0eec | 131 | } |
fabio_gatti | 3:647e37ef0eec | 132 | |
fabio_gatti | 3:647e37ef0eec | 133 | |
fabio_gatti | 2:983b7f5dde1e | 134 | #endif |
fabio_gatti | 0:705a0a684de2 | 135 | |
fabio_gatti | 3:647e37ef0eec | 136 | Serial pc(USBTX, USBRX); //use these pins for serial coms. |
fabio_gatti | 0:705a0a684de2 | 137 | int main() |
fabio_gatti | 0:705a0a684de2 | 138 | { |
fabio_gatti | 3:647e37ef0eec | 139 | |
fabio_gatti | 0:705a0a684de2 | 140 | int count = 0; |
fabio_gatti | 3:647e37ef0eec | 141 | pc.baud(115200); |
fabio_gatti | 3:647e37ef0eec | 142 | |
fabio_gatti | 3:647e37ef0eec | 143 | printf(" --- START SESSION ---\n"); |
fabio_gatti | 0:705a0a684de2 | 144 | ISM43362Interface wifi(MBED_CONF_APP_WIFI_SPI_MOSI, |
fabio_gatti | 0:705a0a684de2 | 145 | MBED_CONF_APP_WIFI_SPI_MISO, |
fabio_gatti | 0:705a0a684de2 | 146 | MBED_CONF_APP_WIFI_SPI_SCLK, |
fabio_gatti | 0:705a0a684de2 | 147 | MBED_CONF_APP_WIFI_SPI_NSS, |
fabio_gatti | 0:705a0a684de2 | 148 | MBED_CONF_APP_WIFI_RESET, |
fabio_gatti | 0:705a0a684de2 | 149 | MBED_CONF_APP_WIFI_DATAREADY, |
fabio_gatti | 0:705a0a684de2 | 150 | MBED_CONF_APP_WIFI_WAKEUP, false); |
fabio_gatti | 0:705a0a684de2 | 151 | |
fabio_gatti | 0:705a0a684de2 | 152 | // Scanning WiFi networks ------------------------------------------ |
fabio_gatti | 0:705a0a684de2 | 153 | |
fabio_gatti | 0:705a0a684de2 | 154 | WiFiAccessPoint *ap; |
fabio_gatti | 0:705a0a684de2 | 155 | |
fabio_gatti | 0:705a0a684de2 | 156 | count = wifi.scan(NULL, 0); |
fabio_gatti | 0:705a0a684de2 | 157 | printf("%d networks available.\n", count); |
fabio_gatti | 0:705a0a684de2 | 158 | |
fabio_gatti | 0:705a0a684de2 | 159 | /* Limit number of network arbitrary to 15 */ |
fabio_gatti | 0:705a0a684de2 | 160 | count = count < 15 ? count : 15; |
fabio_gatti | 0:705a0a684de2 | 161 | |
fabio_gatti | 0:705a0a684de2 | 162 | ap = new WiFiAccessPoint[count]; |
fabio_gatti | 0:705a0a684de2 | 163 | count = wifi.scan(ap, count); |
fabio_gatti | 0:705a0a684de2 | 164 | for (int i = 0; i < count; i++) { |
fabio_gatti | 0:705a0a684de2 | 165 | printf("Network: %s RSSI: %hhd\n", ap[i].get_ssid(), ap[i].get_rssi()); |
fabio_gatti | 0:705a0a684de2 | 166 | } |
fabio_gatti | 0:705a0a684de2 | 167 | |
fabio_gatti | 0:705a0a684de2 | 168 | delete[] ap; |
fabio_gatti | 0:705a0a684de2 | 169 | |
fabio_gatti | 0:705a0a684de2 | 170 | // Connecting to WiFi network -------------------------------------- |
fabio_gatti | 0:705a0a684de2 | 171 | |
fabio_gatti | 0:705a0a684de2 | 172 | printf("\nConnecting to %s...\n", WIFI_NETWORK_NAME); |
fabio_gatti | 0:705a0a684de2 | 173 | int ret = wifi.connect(WIFI_NETWORK_NAME, WIFI_NETWORK_PASSWORD, WIFI_SECURITY); |
fabio_gatti | 0:705a0a684de2 | 174 | if (ret != 0) { |
fabio_gatti | 0:705a0a684de2 | 175 | printf("\nConnection error\n"); |
fabio_gatti | 0:705a0a684de2 | 176 | return -1; |
fabio_gatti | 0:705a0a684de2 | 177 | } |
fabio_gatti | 0:705a0a684de2 | 178 | |
fabio_gatti | 0:705a0a684de2 | 179 | printf("Success\n\n"); |
fabio_gatti | 0:705a0a684de2 | 180 | printf("MAC: %s\n", wifi.get_mac_address()); |
fabio_gatti | 0:705a0a684de2 | 181 | printf("IP: %s\n", wifi.get_ip_address()); |
fabio_gatti | 0:705a0a684de2 | 182 | printf("Netmask: %s\n", wifi.get_netmask()); |
fabio_gatti | 0:705a0a684de2 | 183 | printf("Gateway: %s\n", wifi.get_gateway()); |
fabio_gatti | 0:705a0a684de2 | 184 | printf("RSSI: %d\n\n", wifi.get_rssi()); |
fabio_gatti | 0:705a0a684de2 | 185 | |
fabio_gatti | 3:647e37ef0eec | 186 | #if COMM_PROTO == 0 //MQTT |
fabio_gatti | 2:983b7f5dde1e | 187 | printf("Collegamento MQTT server: " MQTT_HOST "\n"); |
fabio_gatti | 0:705a0a684de2 | 188 | |
fabio_gatti | 2:983b7f5dde1e | 189 | MQTTNetwork network(&wifi); |
fabio_gatti | 2:983b7f5dde1e | 190 | MQTT::Client<MQTTNetwork, Countdown> client(network); |
fabio_gatti | 0:705a0a684de2 | 191 | |
fabio_gatti | 2:983b7f5dde1e | 192 | char assess_token[] = DEVICE_ACCESS_TOKEN; |
fabio_gatti | 2:983b7f5dde1e | 193 | |
fabio_gatti | 2:983b7f5dde1e | 194 | MQTTPacket_connectData conn_data = MQTTPacket_connectData_initializer; |
fabio_gatti | 2:983b7f5dde1e | 195 | conn_data.username.cstring = assess_token; |
fabio_gatti | 0:705a0a684de2 | 196 | |
fabio_gatti | 2:983b7f5dde1e | 197 | if (network.connect(MQTT_HOST, MQTT_PORT) < 0) { |
fabio_gatti | 2:983b7f5dde1e | 198 | printf("failed to connect to " MQTT_HOST "\n"); |
fabio_gatti | 2:983b7f5dde1e | 199 | return -1; |
fabio_gatti | 2:983b7f5dde1e | 200 | } |
fabio_gatti | 0:705a0a684de2 | 201 | |
fabio_gatti | 2:983b7f5dde1e | 202 | if (client.connect(conn_data) < 0) { |
fabio_gatti | 2:983b7f5dde1e | 203 | printf("failed to send MQTT connect message\n"); |
fabio_gatti | 2:983b7f5dde1e | 204 | return -1; |
fabio_gatti | 2:983b7f5dde1e | 205 | } |
fabio_gatti | 0:705a0a684de2 | 206 | |
fabio_gatti | 2:983b7f5dde1e | 207 | printf("successfully connect to MQTT server!\n"); |
fabio_gatti | 3:647e37ef0eec | 208 | |
fabio_gatti | 3:647e37ef0eec | 209 | #elif COMM_PROTO == 1 //HTTP |
fabio_gatti | 3:647e37ef0eec | 210 | |
fabio_gatti | 3:647e37ef0eec | 211 | printf("Collegamento HTTP server: " HTTP_HOST "\n"); |
fabio_gatti | 3:647e37ef0eec | 212 | TCPSocket socket; |
fabio_gatti | 3:647e37ef0eec | 213 | nsapi_error_t response; |
fabio_gatti | 3:647e37ef0eec | 214 | |
fabio_gatti | 3:647e37ef0eec | 215 | // Open a socket on the network interface, and create a TCP connection |
fabio_gatti | 3:647e37ef0eec | 216 | socket.open(&wifi); |
fabio_gatti | 3:647e37ef0eec | 217 | response = socket.connect(HTTP_HOST, HTTP_PORT); |
fabio_gatti | 3:647e37ef0eec | 218 | if(0 != response) { |
fabio_gatti | 3:647e37ef0eec | 219 | printf("Error connecting: %d\n", response); |
fabio_gatti | 3:647e37ef0eec | 220 | socket.close(); |
fabio_gatti | 3:647e37ef0eec | 221 | return -1; |
fabio_gatti | 3:647e37ef0eec | 222 | } |
fabio_gatti | 3:647e37ef0eec | 223 | socket.close(); |
fabio_gatti | 3:647e37ef0eec | 224 | #elif COMM_PROTO == 2 // COAP |
fabio_gatti | 3:647e37ef0eec | 225 | |
fabio_gatti | 3:647e37ef0eec | 226 | //inserire un test di invio dati al server coap |
fabio_gatti | 3:647e37ef0eec | 227 | |
fabio_gatti | 3:647e37ef0eec | 228 | |
fabio_gatti | 2:983b7f5dde1e | 229 | #endif |
fabio_gatti | 0:705a0a684de2 | 230 | |
fabio_gatti | 0:705a0a684de2 | 231 | // Initialize sensors -------------------------------------------------- |
fabio_gatti | 0:705a0a684de2 | 232 | |
fabio_gatti | 0:705a0a684de2 | 233 | uint8_t id; |
fabio_gatti | 3:647e37ef0eec | 234 | DevI2C i2c_2(PB_11, PB_10); |
fabio_gatti | 3:647e37ef0eec | 235 | HTS221Sensor hum_temp(&i2c_2); |
fabio_gatti | 3:647e37ef0eec | 236 | |
fabio_gatti | 3:647e37ef0eec | 237 | hum_temp.init(NULL); |
fabio_gatti | 3:647e37ef0eec | 238 | hum_temp.enable(); |
fabio_gatti | 3:647e37ef0eec | 239 | hum_temp.read_id(&id); |
fabio_gatti | 3:647e37ef0eec | 240 | printf("HTS221 humidity & temperature sensor = 0x%X\r\n", id); |
fabio_gatti | 0:705a0a684de2 | 241 | |
fabio_gatti | 3:647e37ef0eec | 242 | // Variabili di appoggio ----------------------------------------------- |
fabio_gatti | 3:647e37ef0eec | 243 | #if COMM_PROTO == 1 |
fabio_gatti | 3:647e37ef0eec | 244 | uint8_t http_request[1024]; |
fabio_gatti | 3:647e37ef0eec | 245 | char request_body[256]; |
fabio_gatti | 3:647e37ef0eec | 246 | static uint8_t http_resp[512]; |
fabio_gatti | 3:647e37ef0eec | 247 | uint16_t reqLen; |
fabio_gatti | 3:647e37ef0eec | 248 | uint16_t respLen; |
fabio_gatti | 0:705a0a684de2 | 249 | |
fabio_gatti | 3:647e37ef0eec | 250 | #elif COMM_PROTO == 2 // COAP |
fabio_gatti | 3:647e37ef0eec | 251 | char coap_body[256]; |
fabio_gatti | 3:647e37ef0eec | 252 | char coap_uri_path[256]; |
fabio_gatti | 3:647e37ef0eec | 253 | uint16_t coap_message_id; |
fabio_gatti | 3:647e37ef0eec | 254 | coap_message_id=0; |
fabio_gatti | 3:647e37ef0eec | 255 | |
fabio_gatti | 0:705a0a684de2 | 256 | |
fabio_gatti | 3:647e37ef0eec | 257 | #endif |
fabio_gatti | 3:647e37ef0eec | 258 | |
fabio_gatti | 3:647e37ef0eec | 259 | |
fabio_gatti | 3:647e37ef0eec | 260 | |
fabio_gatti | 3:647e37ef0eec | 261 | |
fabio_gatti | 3:647e37ef0eec | 262 | // ciclo di lettura sensori e caricamento su cloud |
fabio_gatti | 0:705a0a684de2 | 263 | for (;;) { |
fabio_gatti | 0:705a0a684de2 | 264 | float temp, humid; |
fabio_gatti | 0:705a0a684de2 | 265 | |
fabio_gatti | 0:705a0a684de2 | 266 | hum_temp.get_temperature(&temp); |
fabio_gatti | 0:705a0a684de2 | 267 | hum_temp.get_humidity(&humid); |
fabio_gatti | 3:647e37ef0eec | 268 | |
fabio_gatti | 3:647e37ef0eec | 269 | printf("ID: %d HTS221: [temp] %.2f C, [hum] %.2f%%\r\n", DEVICE_ID,temp, humid); |
fabio_gatti | 0:705a0a684de2 | 270 | |
fabio_gatti | 3:647e37ef0eec | 271 | #if COMM_PROTO == 0 |
fabio_gatti | 0:705a0a684de2 | 272 | char msg[256]; |
fabio_gatti | 0:705a0a684de2 | 273 | int n = snprintf(msg, sizeof(msg), |
fabio_gatti | 3:647e37ef0eec | 274 | "{\"ID\":%d,\"temperature\":%f, \"humidity\":%f}", |
fabio_gatti | 3:647e37ef0eec | 275 | DEVICE_ID,temp, humid); |
fabio_gatti | 0:705a0a684de2 | 276 | |
fabio_gatti | 0:705a0a684de2 | 277 | void *payload = reinterpret_cast<void*>(msg); |
fabio_gatti | 0:705a0a684de2 | 278 | size_t payload_len = n; |
fabio_gatti | 3:647e37ef0eec | 279 | printf("Message payload lenght: %d\r\n",payload_len); |
fabio_gatti | 0:705a0a684de2 | 280 | printf("publish to: %s %d %s\r\n", MQTT_HOST, MQTT_PORT, MQTT_TOPIC); |
fabio_gatti | 0:705a0a684de2 | 281 | if (client.publish(MQTT_TOPIC, payload, n) < 0) { |
fabio_gatti | 0:705a0a684de2 | 282 | printf("failed to publish MQTT message"); |
fabio_gatti | 0:705a0a684de2 | 283 | } |
fabio_gatti | 3:647e37ef0eec | 284 | |
fabio_gatti | 3:647e37ef0eec | 285 | #elif COMM_PROTO == 1 |
fabio_gatti | 3:647e37ef0eec | 286 | // ciclo di scrittura su socket |
fabio_gatti | 3:647e37ef0eec | 287 | // - open |
fabio_gatti | 3:647e37ef0eec | 288 | // - connect |
fabio_gatti | 3:647e37ef0eec | 289 | // - send |
fabio_gatti | 3:647e37ef0eec | 290 | // - close |
fabio_gatti | 3:647e37ef0eec | 291 | |
fabio_gatti | 3:647e37ef0eec | 292 | socket.open(&wifi); |
fabio_gatti | 3:647e37ef0eec | 293 | response = socket.connect(HTTP_HOST, HTTP_PORT); |
fabio_gatti | 3:647e37ef0eec | 294 | if(0 != response) { |
fabio_gatti | 3:647e37ef0eec | 295 | printf("Error connecting: %d\n", response); |
fabio_gatti | 3:647e37ef0eec | 296 | socket.close(); |
fabio_gatti | 3:647e37ef0eec | 297 | return -1; |
fabio_gatti | 3:647e37ef0eec | 298 | } |
fabio_gatti | 3:647e37ef0eec | 299 | |
fabio_gatti | 3:647e37ef0eec | 300 | |
fabio_gatti | 3:647e37ef0eec | 301 | // body of the request |
fabio_gatti | 3:647e37ef0eec | 302 | |
fabio_gatti | 3:647e37ef0eec | 303 | sprintf(request_body, "{\"ID\":%d,\"temperature\": %f,\"humidity\": %f}\r\n",DEVICE_ID, temp, humid); |
fabio_gatti | 3:647e37ef0eec | 304 | |
fabio_gatti | 3:647e37ef0eec | 305 | // build header of the request |
fabio_gatti | 3:647e37ef0eec | 306 | sprintf((char *)http_request, "POST %s%s%s HTTP/1.1\r\nHost: %s \r\n", HTTP_STR_1,DEVICE_ACCESS_TOKEN,HTTP_STR_2, HTTP_HOST); |
fabio_gatti | 3:647e37ef0eec | 307 | strcat((char *)http_request, "Accept: */*\r\n"); |
fabio_gatti | 3:647e37ef0eec | 308 | strcat((char *)http_request, "User-agent: ST-475-IOT\r\n"); |
fabio_gatti | 3:647e37ef0eec | 309 | strcat((char *)http_request, "Connection: Close\r\n"); |
fabio_gatti | 3:647e37ef0eec | 310 | char buffer[64]; |
fabio_gatti | 3:647e37ef0eec | 311 | strcat((char *)http_request, "Content-Type: application/json\r\n"); |
fabio_gatti | 3:647e37ef0eec | 312 | sprintf(buffer, "Content-Length: %d \r\n\r\n", strlen(request_body)); |
fabio_gatti | 3:647e37ef0eec | 313 | strcat((char *)http_request, buffer); |
fabio_gatti | 3:647e37ef0eec | 314 | |
fabio_gatti | 3:647e37ef0eec | 315 | // append body to the header of the request |
fabio_gatti | 3:647e37ef0eec | 316 | strcat((char *)http_request, request_body); |
fabio_gatti | 3:647e37ef0eec | 317 | reqLen = strlen((char *)http_request); |
fabio_gatti | 3:647e37ef0eec | 318 | printf((char *)http_request); |
fabio_gatti | 3:647e37ef0eec | 319 | |
fabio_gatti | 3:647e37ef0eec | 320 | // Send a simple http request |
fabio_gatti | 3:647e37ef0eec | 321 | |
fabio_gatti | 3:647e37ef0eec | 322 | nsapi_size_t size = strlen((char *)http_request); |
fabio_gatti | 3:647e37ef0eec | 323 | response = 0; |
fabio_gatti | 3:647e37ef0eec | 324 | |
fabio_gatti | 3:647e37ef0eec | 325 | while(size) |
fabio_gatti | 3:647e37ef0eec | 326 | { |
fabio_gatti | 3:647e37ef0eec | 327 | response = socket.send(((char *)http_request)+response, size); |
fabio_gatti | 3:647e37ef0eec | 328 | |
fabio_gatti | 3:647e37ef0eec | 329 | if (response < 0) { |
fabio_gatti | 3:647e37ef0eec | 330 | printf("Error sending data: %d\n", response); |
fabio_gatti | 3:647e37ef0eec | 331 | socket.close(); |
fabio_gatti | 3:647e37ef0eec | 332 | return -1; |
fabio_gatti | 3:647e37ef0eec | 333 | } else { |
fabio_gatti | 3:647e37ef0eec | 334 | size -= response; |
fabio_gatti | 3:647e37ef0eec | 335 | // Check if entire message was sent or not |
fabio_gatti | 3:647e37ef0eec | 336 | printf("sent %d [%.*s]\n", response, strstr((char *)http_request, "\r\n")-(char *)http_request, (char *)http_request); |
fabio_gatti | 3:647e37ef0eec | 337 | } |
fabio_gatti | 3:647e37ef0eec | 338 | } |
fabio_gatti | 3:647e37ef0eec | 339 | // pulizia risorse della socket |
fabio_gatti | 3:647e37ef0eec | 340 | socket.close(); |
fabio_gatti | 3:647e37ef0eec | 341 | |
fabio_gatti | 3:647e37ef0eec | 342 | #elif COMM_PROTO == 2 //COAP |
fabio_gatti | 3:647e37ef0eec | 343 | |
fabio_gatti | 3:647e37ef0eec | 344 | |
fabio_gatti | 3:647e37ef0eec | 345 | // Open a socket on the network interface |
fabio_gatti | 3:647e37ef0eec | 346 | socket.open(&wifi); |
fabio_gatti | 3:647e37ef0eec | 347 | |
fabio_gatti | 3:647e37ef0eec | 348 | // Initialize the CoAP protocol handle, pointing to local implementations on malloc/free/tx/rx functions |
fabio_gatti | 3:647e37ef0eec | 349 | coapHandle = sn_coap_protocol_init(&coap_malloc, &coap_free, &coap_tx_cb, &coap_rx_cb); |
fabio_gatti | 3:647e37ef0eec | 350 | |
fabio_gatti | 3:647e37ef0eec | 351 | |
fabio_gatti | 3:647e37ef0eec | 352 | // Path to the resource we want to retrieve |
fabio_gatti | 3:647e37ef0eec | 353 | sprintf(coap_uri_path, "%s%s%s", COAP_STR_1,DEVICE_ACCESS_TOKEN,COAP_STR_2); |
fabio_gatti | 3:647e37ef0eec | 354 | sprintf(coap_body, "{\"ID\":%d,\"temperature\": %f,\"humidity\": %f}\r\n", DEVICE_ID,temp, humid); |
fabio_gatti | 3:647e37ef0eec | 355 | |
fabio_gatti | 3:647e37ef0eec | 356 | printf ("URI PATH: %s\n",coap_uri_path); |
fabio_gatti | 3:647e37ef0eec | 357 | printf ("BODY: %s\n",coap_body); |
fabio_gatti | 3:647e37ef0eec | 358 | printf ("id: %d\n",coap_message_id); |
fabio_gatti | 3:647e37ef0eec | 359 | |
fabio_gatti | 3:647e37ef0eec | 360 | // See ns_coap_header.h |
fabio_gatti | 3:647e37ef0eec | 361 | sn_coap_hdr_s *coap_res_ptr = (sn_coap_hdr_s*)calloc(sizeof(sn_coap_hdr_s), 1); |
fabio_gatti | 3:647e37ef0eec | 362 | coap_res_ptr->uri_path_ptr = (uint8_t*)coap_uri_path; // Path |
fabio_gatti | 3:647e37ef0eec | 363 | coap_res_ptr->uri_path_len = strlen(coap_uri_path); |
fabio_gatti | 3:647e37ef0eec | 364 | coap_res_ptr->msg_type = COAP_MSG_TYPE_NON_CONFIRMABLE; |
fabio_gatti | 3:647e37ef0eec | 365 | coap_res_ptr->msg_code = COAP_MSG_CODE_REQUEST_POST; // CoAP method |
fabio_gatti | 3:647e37ef0eec | 366 | coap_res_ptr->content_format = COAP_CT_JSON; // CoAP content type |
fabio_gatti | 3:647e37ef0eec | 367 | coap_res_ptr->payload_len = strlen(coap_body); // Body length |
fabio_gatti | 3:647e37ef0eec | 368 | coap_res_ptr->payload_ptr = (uint8_t*)coap_body; // Body pointer |
fabio_gatti | 3:647e37ef0eec | 369 | coap_res_ptr->options_list_ptr = 0; // Optional: options list |
fabio_gatti | 3:647e37ef0eec | 370 | coap_res_ptr->msg_id = coap_message_id; //msg ID, don't forget to increase it |
fabio_gatti | 3:647e37ef0eec | 371 | coap_message_id++; |
fabio_gatti | 3:647e37ef0eec | 372 | |
fabio_gatti | 3:647e37ef0eec | 373 | // Calculate the CoAP message size, allocate the memory and build the message |
fabio_gatti | 3:647e37ef0eec | 374 | uint16_t message_len = sn_coap_builder_calc_needed_packet_data_size(coap_res_ptr); |
fabio_gatti | 3:647e37ef0eec | 375 | printf("Calculated message length: %d bytes\n", message_len); |
fabio_gatti | 3:647e37ef0eec | 376 | |
fabio_gatti | 3:647e37ef0eec | 377 | uint8_t* message_ptr = (uint8_t*)malloc(message_len); |
fabio_gatti | 3:647e37ef0eec | 378 | sn_coap_builder(message_ptr, coap_res_ptr); |
fabio_gatti | 3:647e37ef0eec | 379 | |
fabio_gatti | 3:647e37ef0eec | 380 | // Uncomment to see the raw buffer that will be sent... |
fabio_gatti | 3:647e37ef0eec | 381 | printf("Message is: "); |
fabio_gatti | 3:647e37ef0eec | 382 | for (size_t ix = 0; ix < message_len; ix++) { |
fabio_gatti | 3:647e37ef0eec | 383 | printf("%02x ", message_ptr[ix]); |
fabio_gatti | 3:647e37ef0eec | 384 | } |
fabio_gatti | 3:647e37ef0eec | 385 | printf("\n"); |
fabio_gatti | 3:647e37ef0eec | 386 | |
fabio_gatti | 3:647e37ef0eec | 387 | int scount = socket.sendto(COAP_HOST, COAP_PORT, message_ptr, message_len); |
fabio_gatti | 3:647e37ef0eec | 388 | printf("Sent %d bytes to coap://%s:%d\n", scount,COAP_HOST, COAP_PORT); |
fabio_gatti | 3:647e37ef0eec | 389 | |
fabio_gatti | 3:647e37ef0eec | 390 | // routine di ricezione |
fabio_gatti | 3:647e37ef0eec | 391 | #if SHOW_COAP_RESPONSE == true |
fabio_gatti | 3:647e37ef0eec | 392 | SocketAddress addr; |
fabio_gatti | 3:647e37ef0eec | 393 | uint8_t* recv_buffer = (uint8_t*)malloc(1280); // Suggested is to keep packet size under 1280 bytes |
fabio_gatti | 3:647e37ef0eec | 394 | |
fabio_gatti | 3:647e37ef0eec | 395 | if ((ret = socket.recvfrom(&addr, recv_buffer, 1280)) >= 0) { |
fabio_gatti | 3:647e37ef0eec | 396 | // to see where the message came from, inspect addr.get_addr() and addr.get_port() |
fabio_gatti | 3:647e37ef0eec | 397 | |
fabio_gatti | 3:647e37ef0eec | 398 | printf("Received packets from (%s,%d)\n", addr.get_ip_address(),addr.get_port()); |
fabio_gatti | 3:647e37ef0eec | 399 | |
fabio_gatti | 3:647e37ef0eec | 400 | sn_coap_hdr_s* parsed = sn_coap_parser(coapHandle, ret, recv_buffer, &coapVersion); |
fabio_gatti | 3:647e37ef0eec | 401 | |
fabio_gatti | 3:647e37ef0eec | 402 | // We know the payload is going to be a string |
fabio_gatti | 3:647e37ef0eec | 403 | std::string payload((const char*)parsed->payload_ptr, parsed->payload_len); |
fabio_gatti | 3:647e37ef0eec | 404 | |
fabio_gatti | 3:647e37ef0eec | 405 | printf("\tmsg_id: %d\n", parsed->msg_id); |
fabio_gatti | 3:647e37ef0eec | 406 | printf("\tmsg_code: %d\n", parsed->msg_code); |
fabio_gatti | 3:647e37ef0eec | 407 | printf("\tcontent_format: %d\n", parsed->content_format); |
fabio_gatti | 3:647e37ef0eec | 408 | printf("\tpayload_len: %d\n", parsed->payload_len); |
fabio_gatti | 3:647e37ef0eec | 409 | printf("\tpayload: %s\n", payload.c_str()); |
fabio_gatti | 3:647e37ef0eec | 410 | printf("\toptions_list_ptr: %p\n", parsed->options_list_ptr); |
fabio_gatti | 3:647e37ef0eec | 411 | } |
fabio_gatti | 3:647e37ef0eec | 412 | |
fabio_gatti | 3:647e37ef0eec | 413 | free(recv_buffer); |
fabio_gatti | 3:647e37ef0eec | 414 | |
fabio_gatti | 3:647e37ef0eec | 415 | |
fabio_gatti | 3:647e37ef0eec | 416 | #endif //end SHOW_COAP_RESPONSE |
fabio_gatti | 3:647e37ef0eec | 417 | socket.close(); |
fabio_gatti | 3:647e37ef0eec | 418 | sn_coap_protocol_destroy(coapHandle); |
fabio_gatti | 3:647e37ef0eec | 419 | free(coap_res_ptr); |
fabio_gatti | 3:647e37ef0eec | 420 | free(message_ptr); |
fabio_gatti | 3:647e37ef0eec | 421 | |
fabio_gatti | 3:647e37ef0eec | 422 | |
fabio_gatti | 3:647e37ef0eec | 423 | #endif //end protocol selection |
fabio_gatti | 3:647e37ef0eec | 424 | wait_ms(SENSOR_READING_PERIOD); |
fabio_gatti | 0:705a0a684de2 | 425 | } |
fabio_gatti | 0:705a0a684de2 | 426 | |
fabio_gatti | 3:647e37ef0eec | 427 | //le disconnect non vengono raggiunte perche' rimaniamo all'interno del ciclo |
fabio_gatti | 2:983b7f5dde1e | 428 | //client.disconnect(); |
fabio_gatti | 2:983b7f5dde1e | 429 | //wifi.disconnect(); |
fabio_gatti | 0:705a0a684de2 | 430 | |
fabio_gatti | 2:983b7f5dde1e | 431 | //printf("\ndone\n"); |
fabio_gatti | 2:983b7f5dde1e | 432 | //return 0; |
fabio_gatti | 0:705a0a684de2 | 433 | |
fabio_gatti | 0:705a0a684de2 | 434 | } |