Etienne Charbonnier
/
plant_monitoring
Plant Monitoring CS
main.cpp@61:700f3f204adc, 2020-03-25 (annotated)
- Committer:
- titi9211
- Date:
- Wed Mar 25 16:06:17 2020 +0000
- Revision:
- 61:700f3f204adc
- Parent:
- 59:169324c8e604
- Child:
- 62:4763f2aa486c
quick modif
Who changed what in which revision?
User | Revision | Line number | New 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 | 59:169324c8e604 | 21 | #include "TSL2591.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 | 59:169324c8e604 | 99 | static DevI2C devI2c(PB_11,PB_10); |
titi9211 | 61:700f3f204adc | 100 | // static HTS221Sensor sen_hum_temp(&devI2c); |
titi9211 | 61:700f3f204adc | 101 | static DHT22 tempSensor(D2); // Vérifier que le capteur DHT22 est relié au PIN D2 |
titi9211 | 59:169324c8e604 | 102 | static I2C i2c1(I2C_SDA, I2C_SCL); |
titi9211 | 61:700f3f204adc | 103 | static TSL2591 sensor1(i2c1, TSL2591_ADDR); // 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 | 59:169324c8e604 | 139 | sensor1.getALS(); |
titi9211 | 59:169324c8e604 | 140 | sensor1.calcLux(); |
titi9211 | 59:169324c8e604 | 141 | lux = (float) sensor1.full; |
titi9211 | 59:169324c8e604 | 142 | printf("Soil moisture acquisition\n"); |
titi9211 | 59:169324c8e604 | 143 | moist_r = analog_value.read(); |
titi9211 | 59:169324c8e604 | 144 | printf("Soil moisture conversion\n"); |
titi9211 | 59:169324c8e604 | 145 | moist_v = moist_r*100; |
titi9211 | 59:169324c8e604 | 146 | |
titi9211 | 59:169324c8e604 | 147 | /* Construct content of HTTP command */ |
titi9211 | 61:700f3f204adc | 148 | 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 | 149 | printf("Content Length = %d\r\n", (int)strlen(message)); |
titi9211 | 59:169324c8e604 | 150 | |
titi9211 | 59:169324c8e604 | 151 | /* Construct HTTP command to send */ |
titi9211 | 59:169324c8e604 | 152 | 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 | 153 | printf("HTTP command %s\r\n", sbuffer); |
titi9211 | 59:169324c8e604 | 154 | wait(2.0); |
titi9211 | 59:169324c8e604 | 155 | |
titi9211 | 59:169324c8e604 | 156 | /* Send http request to Ubidots */ |
titi9211 | 59:169324c8e604 | 157 | printf("Sending HTTP request to ubidots.com...\n"); |
titi9211 | 59:169324c8e604 | 158 | nsapi_size_t size = strlen(sbuffer); |
titi9211 | 59:169324c8e604 | 159 | printf("send %d [%.*s]\r\n", size, strstr(sbuffer, "\r\n") - sbuffer, sbuffer); |
titi9211 | 59:169324c8e604 | 160 | response = 0; |
titi9211 | 59:169324c8e604 | 161 | while(size) |
titi9211 | 59:169324c8e604 | 162 | { |
titi9211 | 59:169324c8e604 | 163 | response = socket.send(sbuffer+response, size); |
titi9211 | 59:169324c8e604 | 164 | if (response < 0) { |
titi9211 | 59:169324c8e604 | 165 | printf("Error sending data: %d\n", response); |
titi9211 | 59:169324c8e604 | 166 | socket.close(); |
titi9211 | 59:169324c8e604 | 167 | return; |
titi9211 | 59:169324c8e604 | 168 | } else { |
titi9211 | 59:169324c8e604 | 169 | size -= response; |
titi9211 | 59:169324c8e604 | 170 | // Check if entire message was sent or not |
titi9211 | 59:169324c8e604 | 171 | printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
titi9211 | 59:169324c8e604 | 172 | } |
titi9211 | 59:169324c8e604 | 173 | } |
titi9211 | 59:169324c8e604 | 174 | |
titi9211 | 59:169324c8e604 | 175 | /* Receive a simple http response and print out the response line */ |
titi9211 | 59:169324c8e604 | 176 | char respBuffer[64]; |
titi9211 | 59:169324c8e604 | 177 | int rcount = socket.recv(respBuffer, sizeof respBuffer); |
titi9211 | 59:169324c8e604 | 178 | printf("recv %d [%.*s]\r\n", rcount, strstr(respBuffer, "\r\n") - respBuffer, respBuffer); |
titi9211 | 59:169324c8e604 | 179 | wait(10); |
titi9211 | 59:169324c8e604 | 180 | // Close the socket to return its memory and bring down the network interface |
titi9211 | 59:169324c8e604 | 181 | socket.close(); |
mbed_official | 24:323569a565ec | 182 | } |
mbed_official | 0:857719181846 | 183 | } |
mbed_official | 0:857719181846 | 184 | |
mbed_official | 0:857719181846 | 185 | int main() |
mbed_official | 0:857719181846 | 186 | { |
mbed_official | 37:3a31525e2971 | 187 | int count = 0; |
mbed_official | 37:3a31525e2971 | 188 | |
mbed_official | 35:052c1ba06ce7 | 189 | printf("WiFi example\n\n"); |
mbed_official | 0:857719181846 | 190 | |
titi9211 | 59:169324c8e604 | 191 | /* |
mbed_official | 37:3a31525e2971 | 192 | count = scan_demo(&wifi); |
mbed_official | 37:3a31525e2971 | 193 | if (count == 0) { |
mbed_official | 37:3a31525e2971 | 194 | printf("No WIFI APNs found - can't continue further.\n"); |
mbed_official | 37:3a31525e2971 | 195 | return -1; |
titi9211 | 59:169324c8e604 | 196 | }*/ |
mbed_official | 0:857719181846 | 197 | |
mbed_official | 37:3a31525e2971 | 198 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
mbed_official | 0:857719181846 | 199 | int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
mbed_official | 0:857719181846 | 200 | if (ret != 0) { |
mbed_official | 35:052c1ba06ce7 | 201 | printf("\nConnection error\n"); |
mbed_official | 0:857719181846 | 202 | return -1; |
mbed_official | 0:857719181846 | 203 | } |
mbed_official | 0:857719181846 | 204 | |
mbed_official | 35:052c1ba06ce7 | 205 | printf("Success\n\n"); |
mbed_official | 35:052c1ba06ce7 | 206 | printf("MAC: %s\n", wifi.get_mac_address()); |
mbed_official | 35:052c1ba06ce7 | 207 | printf("IP: %s\n", wifi.get_ip_address()); |
mbed_official | 35:052c1ba06ce7 | 208 | printf("Netmask: %s\n", wifi.get_netmask()); |
mbed_official | 35:052c1ba06ce7 | 209 | printf("Gateway: %s\n", wifi.get_gateway()); |
mbed_official | 35:052c1ba06ce7 | 210 | printf("RSSI: %d\n\n", wifi.get_rssi()); |
mbed_official | 0:857719181846 | 211 | |
mbed_official | 0:857719181846 | 212 | http_demo(&wifi); |
mbed_official | 0:857719181846 | 213 | |
mbed_official | 0:857719181846 | 214 | wifi.disconnect(); |
mbed_official | 0:857719181846 | 215 | |
mbed_official | 35:052c1ba06ce7 | 216 | printf("\nDone\n"); |
mbed_official | 0:857719181846 | 217 | } |