Plant Monitoring CS

Dependencies:   HTS221

Committer:
titi9211
Date:
Thu Mar 26 10:01:31 2020 +0000
Revision:
62:4763f2aa486c
Parent:
61:700f3f204adc
TSL2561

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:857719181846 1 /* WiFi Example
adustm 57:c8c960b825f0 2 * Copyright (c) 2018 ARM Limited
mbed_official 0:857719181846 3 *
mbed_official 0:857719181846 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 0:857719181846 5 * you may not use this file except in compliance with the License.
mbed_official 0:857719181846 6 * You may obtain a copy of the License at
mbed_official 0:857719181846 7 *
mbed_official 0:857719181846 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 0:857719181846 9 *
mbed_official 0:857719181846 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 0:857719181846 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 0:857719181846 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 0:857719181846 13 * See the License for the specific language governing permissions and
mbed_official 0:857719181846 14 * limitations under the License.
mbed_official 0:857719181846 15 */
mbed_official 0:857719181846 16
mbed_official 0:857719181846 17 #include "mbed.h"
mbed_official 0:857719181846 18 #include "TCPSocket.h"
titi9211 59:169324c8e604 19 #include "HTS221Sensor.h"
titi9211 59:169324c8e604 20 #include "DHT22.h"
titi9211 62:4763f2aa486c 21 #include "TSL2561.h"
mbed_official 0:857719181846 22
mbed_official 44:63be19b7a3db 23 #define WIFI_IDW0XX1 2
titi9211 59:169324c8e604 24 #define UBIDOTS_TOKEN "BBFF-xp89MM8kmzzbp6eJ074XjDpqz7qryh"
titi9211 59:169324c8e604 25 #define UBIDOTS_DEVICE "stmwifi"
titi9211 59:169324c8e604 26
titi9211 59:169324c8e604 27
mbed_official 32:bca3f5f442b3 28
adustm 58:8d4bde75ebb9 29 #if (defined(TARGET_DISCO_L475VG_IOT01A) || defined(TARGET_DISCO_F413ZH))
adustm adustm@gmail.com 55:23a0d1e8270a 30 #include "ISM43362Interface.h"
adustm adustm@gmail.com 55:23a0d1e8270a 31 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);
adustm adustm@gmail.com 55:23a0d1e8270a 32
mbed_official 33:12f0df4d51d7 33 #else // External WiFi modules
mbed_official 21:b2f2f6a840b4 34
adustm 57:c8c960b825f0 35 #if MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
mbed_official 32:bca3f5f442b3 36 #include "SpwfSAInterface.h"
mbed_official 32:bca3f5f442b3 37 SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
mbed_official 44:63be19b7a3db 38 #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
mbed_official 10:5b5beb106156 39
mbed_official 33:12f0df4d51d7 40 #endif
mbed_official 0:857719181846 41
mbed_official 0:857719181846 42 const char *sec2str(nsapi_security_t sec)
mbed_official 0:857719181846 43 {
mbed_official 0:857719181846 44 switch (sec) {
mbed_official 0:857719181846 45 case NSAPI_SECURITY_NONE:
mbed_official 0:857719181846 46 return "None";
mbed_official 0:857719181846 47 case NSAPI_SECURITY_WEP:
mbed_official 0:857719181846 48 return "WEP";
mbed_official 0:857719181846 49 case NSAPI_SECURITY_WPA:
mbed_official 0:857719181846 50 return "WPA";
mbed_official 0:857719181846 51 case NSAPI_SECURITY_WPA2:
mbed_official 0:857719181846 52 return "WPA2";
mbed_official 0:857719181846 53 case NSAPI_SECURITY_WPA_WPA2:
mbed_official 0:857719181846 54 return "WPA/WPA2";
mbed_official 0:857719181846 55 case NSAPI_SECURITY_UNKNOWN:
mbed_official 0:857719181846 56 default:
mbed_official 0:857719181846 57 return "Unknown";
mbed_official 0:857719181846 58 }
mbed_official 0:857719181846 59 }
mbed_official 0:857719181846 60
mbed_official 37:3a31525e2971 61 int scan_demo(WiFiInterface *wifi)
mbed_official 0:857719181846 62 {
mbed_official 0:857719181846 63 WiFiAccessPoint *ap;
mbed_official 0:857719181846 64
mbed_official 35:052c1ba06ce7 65 printf("Scan:\n");
mbed_official 0:857719181846 66
mbed_official 0:857719181846 67 int count = wifi->scan(NULL,0);
adustm adustm@gmail.com 55:23a0d1e8270a 68 printf("%d networks available.\n", count);
mbed_official 0:857719181846 69
mbed_official 0:857719181846 70 /* Limit number of network arbitrary to 15 */
mbed_official 0:857719181846 71 count = count < 15 ? count : 15;
mbed_official 0:857719181846 72
mbed_official 0:857719181846 73 ap = new WiFiAccessPoint[count];
mbed_official 0:857719181846 74 count = wifi->scan(ap, count);
mbed_official 0:857719181846 75 for (int i = 0; i < count; i++)
mbed_official 0:857719181846 76 {
mbed_official 35:052c1ba06ce7 77 printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
mbed_official 0:857719181846 78 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
mbed_official 0:857719181846 79 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
mbed_official 0:857719181846 80 }
mbed_official 0:857719181846 81
mbed_official 0:857719181846 82 delete[] ap;
mbed_official 37:3a31525e2971 83 return count;
mbed_official 0:857719181846 84 }
mbed_official 0:857719181846 85
mbed_official 0:857719181846 86 void http_demo(NetworkInterface *net)
mbed_official 0:857719181846 87 {
mbed_official 0:857719181846 88 TCPSocket socket;
mbed_official 24:323569a565ec 89 nsapi_error_t response;
mbed_official 0:857719181846 90
mbed_official 0:857719181846 91 // Send a simple http request
titi9211 59:169324c8e604 92 char sbuffer[256];
titi9211 59:169324c8e604 93 char message[64];
titi9211 59:169324c8e604 94
titi9211 59:169324c8e604 95 /* Analog ressources */
titi9211 59:169324c8e604 96 AnalogIn adc_temp(ADC_TEMP); // Internal Temp Sensor to ADC Channel
titi9211 59:169324c8e604 97 AnalogIn adc_vbat(ADC_VBAT); // VBAT / 3 internal to ADC channel
titi9211 59:169324c8e604 98
titi9211 62:4763f2aa486c 99 // static DevI2C devI2c(PB_11,PB_10);
titi9211 62:4763f2aa486c 100 // static DevI2C devI2c(D14,D15);
titi9211 62:4763f2aa486c 101 //static HTS221Sensor sen_hum_temp(&devI2c);
titi9211 61:700f3f204adc 102 static DHT22 tempSensor(D2); // Vérifier que le capteur DHT22 est relié au PIN D2
titi9211 62:4763f2aa486c 103 static TSL2561 LUM(PB_11, PB_10); // Vérifier que le capteur TSL2591 est bien relié aux ports SDA et SCL
titi9211 61:700f3f204adc 104 static AnalogIn analog_value(A0); // Vérifier que le capteur d'humidité du sol est bien relié au port analogique A0
titi9211 59:169324c8e604 105
titi9211 59:169324c8e604 106 //sen_hum_temp.init(NULL);
titi9211 59:169324c8e604 107 //sen_hum_temp.enable();
titi9211 59:169324c8e604 108
titi9211 59:169324c8e604 109 /* Global variables */
titi9211 59:169324c8e604 110 float temp;
titi9211 59:169324c8e604 111 float t;
titi9211 59:169324c8e604 112 float hum;
titi9211 59:169324c8e604 113 float h;
titi9211 59:169324c8e604 114 float lux;
titi9211 59:169324c8e604 115 float moist_r;
titi9211 59:169324c8e604 116 float moist_v;
titi9211 59:169324c8e604 117
titi9211 59:169324c8e604 118 while(true){
titi9211 59:169324c8e604 119 // Open a socket on the network interface, and create a TCP connection
titi9211 59:169324c8e604 120 socket.open(net);
titi9211 59:169324c8e604 121 response = socket.connect("things.ubidots.com", 80);
titi9211 59:169324c8e604 122 if(0 != response) {
titi9211 59:169324c8e604 123 printf("Error connecting: %d\n", response);
mbed_official 24:323569a565ec 124 socket.close();
mbed_official 24:323569a565ec 125 return;
mbed_official 24:323569a565ec 126 }
titi9211 59:169324c8e604 127 printf("Connected to the Server\n");
titi9211 59:169324c8e604 128
titi9211 59:169324c8e604 129 /* Sensor acquisition */
titi9211 59:169324c8e604 130 // ADC sensors
titi9211 59:169324c8e604 131 printf("Temperature and humidity acquisition\n");
titi9211 59:169324c8e604 132 tempSensor.sample();
titi9211 59:169324c8e604 133 printf("\n");
titi9211 59:169324c8e604 134 t = (float) tempSensor.getTemperature();
titi9211 59:169324c8e604 135 temp = t/10;
titi9211 59:169324c8e604 136 h = (float) tempSensor.getHumidity();
titi9211 59:169324c8e604 137 hum = h/10;
titi9211 59:169324c8e604 138 printf("Luminosity acquisition\n");
titi9211 62:4763f2aa486c 139 lux = (float) LUM.lux();
titi9211 59:169324c8e604 140 printf("Soil moisture acquisition\n");
titi9211 59:169324c8e604 141 moist_r = analog_value.read();
titi9211 59:169324c8e604 142 printf("Soil moisture conversion\n");
titi9211 59:169324c8e604 143 moist_v = moist_r*100;
titi9211 59:169324c8e604 144
titi9211 59:169324c8e604 145 /* Construct content of HTTP command */
titi9211 61:700f3f204adc 146 sprintf(message, "{\"temperature\": %0.2f, \"humidity\": %0.2f, \"soilmoisture\": %f, \"luminosite\": %0.2f}", temp, hum, moist_v, lux); // Vérifier que les API labels sont bien renseignés
titi9211 59:169324c8e604 147 printf("Content Length = %d\r\n", (int)strlen(message));
titi9211 59:169324c8e604 148
titi9211 59:169324c8e604 149 /* Construct HTTP command to send */
titi9211 59:169324c8e604 150 sprintf(sbuffer, "POST /api/v1.6/devices/%s/?token=%s HTTP/1.1\r\nHost: things.ubidots.com\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s", UBIDOTS_DEVICE, UBIDOTS_TOKEN, (int)strlen(message),message);
titi9211 59:169324c8e604 151 printf("HTTP command %s\r\n", sbuffer);
titi9211 59:169324c8e604 152 wait(2.0);
titi9211 59:169324c8e604 153
titi9211 59:169324c8e604 154 /* Send http request to Ubidots */
titi9211 59:169324c8e604 155 printf("Sending HTTP request to ubidots.com...\n");
titi9211 59:169324c8e604 156 nsapi_size_t size = strlen(sbuffer);
titi9211 59:169324c8e604 157 printf("send %d [%.*s]\r\n", size, strstr(sbuffer, "\r\n") - sbuffer, sbuffer);
titi9211 59:169324c8e604 158 response = 0;
titi9211 59:169324c8e604 159 while(size)
titi9211 59:169324c8e604 160 {
titi9211 59:169324c8e604 161 response = socket.send(sbuffer+response, size);
titi9211 59:169324c8e604 162 if (response < 0) {
titi9211 59:169324c8e604 163 printf("Error sending data: %d\n", response);
titi9211 59:169324c8e604 164 socket.close();
titi9211 59:169324c8e604 165 return;
titi9211 59:169324c8e604 166 } else {
titi9211 59:169324c8e604 167 size -= response;
titi9211 59:169324c8e604 168 // Check if entire message was sent or not
titi9211 59:169324c8e604 169 printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
titi9211 59:169324c8e604 170 }
titi9211 59:169324c8e604 171 }
titi9211 59:169324c8e604 172
titi9211 59:169324c8e604 173 /* Receive a simple http response and print out the response line */
titi9211 59:169324c8e604 174 char respBuffer[64];
titi9211 59:169324c8e604 175 int rcount = socket.recv(respBuffer, sizeof respBuffer);
titi9211 59:169324c8e604 176 printf("recv %d [%.*s]\r\n", rcount, strstr(respBuffer, "\r\n") - respBuffer, respBuffer);
titi9211 59:169324c8e604 177 wait(10);
titi9211 59:169324c8e604 178 // Close the socket to return its memory and bring down the network interface
titi9211 59:169324c8e604 179 socket.close();
mbed_official 24:323569a565ec 180 }
mbed_official 0:857719181846 181 }
mbed_official 0:857719181846 182
mbed_official 0:857719181846 183 int main()
mbed_official 0:857719181846 184 {
mbed_official 37:3a31525e2971 185 int count = 0;
mbed_official 37:3a31525e2971 186
mbed_official 35:052c1ba06ce7 187 printf("WiFi example\n\n");
mbed_official 0:857719181846 188
titi9211 59:169324c8e604 189 /*
mbed_official 37:3a31525e2971 190 count = scan_demo(&wifi);
mbed_official 37:3a31525e2971 191 if (count == 0) {
mbed_official 37:3a31525e2971 192 printf("No WIFI APNs found - can't continue further.\n");
mbed_official 37:3a31525e2971 193 return -1;
titi9211 59:169324c8e604 194 }*/
mbed_official 0:857719181846 195
mbed_official 37:3a31525e2971 196 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
mbed_official 0:857719181846 197 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
mbed_official 0:857719181846 198 if (ret != 0) {
mbed_official 35:052c1ba06ce7 199 printf("\nConnection error\n");
mbed_official 0:857719181846 200 return -1;
mbed_official 0:857719181846 201 }
mbed_official 0:857719181846 202
mbed_official 35:052c1ba06ce7 203 printf("Success\n\n");
mbed_official 35:052c1ba06ce7 204 printf("MAC: %s\n", wifi.get_mac_address());
mbed_official 35:052c1ba06ce7 205 printf("IP: %s\n", wifi.get_ip_address());
mbed_official 35:052c1ba06ce7 206 printf("Netmask: %s\n", wifi.get_netmask());
mbed_official 35:052c1ba06ce7 207 printf("Gateway: %s\n", wifi.get_gateway());
mbed_official 35:052c1ba06ce7 208 printf("RSSI: %d\n\n", wifi.get_rssi());
mbed_official 0:857719181846 209
mbed_official 0:857719181846 210 http_demo(&wifi);
mbed_official 0:857719181846 211
mbed_official 0:857719181846 212 wifi.disconnect();
mbed_official 0:857719181846 213
mbed_official 35:052c1ba06ce7 214 printf("\nDone\n");
mbed_official 0:857719181846 215 }