Aeris Workshop program to read Moisture sensor and send the data to AerCloud using MQTT

Dependencies:   MQTT mbed-src

Committer:
agoel
Date:
Tue Jun 16 19:04:09 2015 +0000
Revision:
0:9c4dbd5b0a33
Child:
1:4e6e185783c2
First Version of Aeris Moisture Workshop to be used on 18/Jun/2015

Who changed what in which revision?

UserRevisionLine numberNew contents of line
agoel 0:9c4dbd5b0a33 1 /*
agoel 0:9c4dbd5b0a33 2 Dragon Fly Sample Program for MultiTech Connected Cooler Workshop - reading value from Alcohol and Moisture sesnor
agoel 0:9c4dbd5b0a33 3 */
agoel 0:9c4dbd5b0a33 4
agoel 0:9c4dbd5b0a33 5 // AerCloud sample code for Dragon Fly Sample Program for MultiTech Connected Cooler - created for following hardware:
agoel 0:9c4dbd5b0a33 6 // ST Nucleo F411RE http://developer.mbed.org/platforms/ST-Nucleo-F411RE/
agoel 0:9c4dbd5b0a33 7 // ST Sensor Board http://developer.mbed.org/teams/ST-Americas-mbed-Team/wiki/Getting-Started-with-Nucleo-Sensors
agoel 0:9c4dbd5b0a33 8 // MultiTech Socket Modem Shield: http://developer.mbed.org/components/Multi-Tech-SocketModem-Arduino-Shield-MT/
agoel 0:9c4dbd5b0a33 9 // MultiTech http://developer.mbed.org/teams/Multi-Hackers/code/MTSAS_Cellular_HTTP_Example/
agoel 0:9c4dbd5b0a33 10 // Details on AerCloud: http://www.aeris.com/technology/aercloud/
agoel 0:9c4dbd5b0a33 11 // AerCloud Developer Forum: https://developer.aeris.com/
agoel 0:9c4dbd5b0a33 12 //
agoel 0:9c4dbd5b0a33 13 // Sample writes moistureValue & alcoholValue to an AerCloud container
agoel 0:9c4dbd5b0a33 14 //
agoel 0:9c4dbd5b0a33 15 // Dependencies:
agoel 0:9c4dbd5b0a33 16 // mbed HTTP Client Libary
agoel 0:9c4dbd5b0a33 17 // mbed MQTT Client Libary
agoel 0:9c4dbd5b0a33 18 // DragonFly Support Library
agoel 0:9c4dbd5b0a33 19 // mtsas Modem Library
agoel 0:9c4dbd5b0a33 20 // mbed Library
agoel 0:9c4dbd5b0a33 21 //
agoel 0:9c4dbd5b0a33 22 // To get the sample running, you'll need to fill in the following parameters below
agoel 0:9c4dbd5b0a33 23 // Your cellular provider's APN: APN
agoel 0:9c4dbd5b0a33 24 // AerCloud API Key: AC_APIKEY
agoel 0:9c4dbd5b0a33 25 // AerCloud Account ID: acId
agoel 0:9c4dbd5b0a33 26 // AerCloud Container: AC_CONTAINER
agoel 0:9c4dbd5b0a33 27 //
agoel 0:9c4dbd5b0a33 28 // and you'll also need to create an AerCloud contianer with the schema
agoel 0:9c4dbd5b0a33 29 // described below
agoel 0:9c4dbd5b0a33 30 //
agoel 0:9c4dbd5b0a33 31
agoel 0:9c4dbd5b0a33 32 #include "mbed.h"
agoel 0:9c4dbd5b0a33 33 #include "mtsas.h"
agoel 0:9c4dbd5b0a33 34 #include "HTTPClient.h"
agoel 0:9c4dbd5b0a33 35 #include "PubSubClient.h"
agoel 0:9c4dbd5b0a33 36
agoel 0:9c4dbd5b0a33 37 //------------------------------------------------------------------------------------
agoel 0:9c4dbd5b0a33 38 // Connectivity Parameters
agoel 0:9c4dbd5b0a33 39 //------------------------------------------------------------------------------------
agoel 0:9c4dbd5b0a33 40
agoel 0:9c4dbd5b0a33 41 //! Aeris SIM does not use PIN
agoel 0:9c4dbd5b0a33 42 #define SIMPIN NULL
agoel 0:9c4dbd5b0a33 43
agoel 0:9c4dbd5b0a33 44 //! The APN of your Aeris SIM
agoel 0:9c4dbd5b0a33 45 // You can find the APN for your SIM at https://aerport.aeris.com
agoel 0:9c4dbd5b0a33 46 char _APN[] = "aer.aerisapn.net";
agoel 0:9c4dbd5b0a33 47
agoel 0:9c4dbd5b0a33 48 //! User name and password are not required to use Aeris APN
agoel 0:9c4dbd5b0a33 49 #define USERNAME NULL
agoel 0:9c4dbd5b0a33 50
agoel 0:9c4dbd5b0a33 51 //! User name and password are not required to use Aeris APN
agoel 0:9c4dbd5b0a33 52 #define PASSWORD NULL
agoel 0:9c4dbd5b0a33 53
agoel 0:9c4dbd5b0a33 54 // ------------------------------------------------------------------------------------
agoel 0:9c4dbd5b0a33 55 // AerCloud Paramers
agoel 0:9c4dbd5b0a33 56 // ------------------------------------------------------------------------------------
agoel 0:9c4dbd5b0a33 57
agoel 0:9c4dbd5b0a33 58 // AerCloud BASE URL
agoel 0:9c4dbd5b0a33 59 #define AC_BASE "http://api.aercloud.aeris.com/v1"
agoel 0:9c4dbd5b0a33 60
agoel 0:9c4dbd5b0a33 61 //! AerCloud API Key
agoel 0:9c4dbd5b0a33 62 // You can find your api key at https://aerport.aeris.com"
agoel 0:9c4dbd5b0a33 63 char _AERCLOUD_API_KEY[] = "ff767bee-f5bf-403b-badb-fde13bc2b756";
agoel 0:9c4dbd5b0a33 64
agoel 0:9c4dbd5b0a33 65 //! Aercloud Account Id
agoel 0:9c4dbd5b0a33 66 // You can find your account id at https://aerport.aeris.com
agoel 0:9c4dbd5b0a33 67 char _AERCLOUD_ACCOUNT_ID[] = "10975";
agoel 0:9c4dbd5b0a33 68
agoel 0:9c4dbd5b0a33 69 // AerCloud Container
agoel 0:9c4dbd5b0a33 70 // The name of the AerCloud Container to write data into
agoel 0:9c4dbd5b0a33 71 char _AERCLOUD_CONTAINER[] = "AERIS_WORKSHOP_MAIN";
agoel 0:9c4dbd5b0a33 72 char _AERCLOUD_CONTAINER_ALARM[] = "AERIS_WORKSHOP_ALARM";
agoel 0:9c4dbd5b0a33 73
agoel 0:9c4dbd5b0a33 74 // AerCloud Device Id
agoel 0:9c4dbd5b0a33 75 // The Device Id that is registered and provisioned at AerCloud
agoel 0:9c4dbd5b0a33 76 // (random value here program automatically detects and provision it)
agoel 0:9c4dbd5b0a33 77 char _AERCLOUD_DEVICE_ID[] = "*****-nucleo-0001-***********";
agoel 0:9c4dbd5b0a33 78
agoel 0:9c4dbd5b0a33 79 char _host[] = "mqtt.aercloud.aeris.com";
agoel 0:9c4dbd5b0a33 80 int _port = 1883;
agoel 0:9c4dbd5b0a33 81
agoel 0:9c4dbd5b0a33 82 //------------------------------------------------------------------------------------
agoel 0:9c4dbd5b0a33 83 // AerCloud SCL (Device) Data Model
agoel 0:9c4dbd5b0a33 84 // ------------------------------------------------------------------------------------
agoel 0:9c4dbd5b0a33 85 //
agoel 0:9c4dbd5b0a33 86 // Code assumes an AerCloud Data Model (AERIS_WORKSHOP_MAIN) with following fields
agoel 0:9c4dbd5b0a33 87 //
agoel 0:9c4dbd5b0a33 88 // moistureSensorValue FLOAT Value read frrom moisture sensor
agoel 0:9c4dbd5b0a33 89 // moistureProcessedValue FLOAT Processed value of moisture sensor (LOW / MEDIUM / HIGH)
agoel 0:9c4dbd5b0a33 90 // alcoholSensorValue FLOAT Value read frrom alcohol sensor
agoel 0:9c4dbd5b0a33 91 // alcoholProcessedValue FLOAT Processed value of alcohol sensor (LOW / MEDIUM / HIGH)
agoel 0:9c4dbd5b0a33 92 // sclid INT Device-id
agoel 0:9c4dbd5b0a33 93 // timeStamp LONG timestamp when event occurred
agoel 0:9c4dbd5b0a33 94 //
agoel 0:9c4dbd5b0a33 95 // Code assumes an AerCloud Data Model (AERIS_WORKSHOP_ALARM) with following fields
agoel 0:9c4dbd5b0a33 96 //
agoel 0:9c4dbd5b0a33 97 // alarmText STRING alarm string
agoel 0:9c4dbd5b0a33 98 // timeStamp LONG time at which alarn has been generated
agoel 0:9c4dbd5b0a33 99 // sclid INT Device-id
agoel 0:9c4dbd5b0a33 100 //
agoel 0:9c4dbd5b0a33 101 //------------------------------------------------------------------------------------
agoel 0:9c4dbd5b0a33 102
agoel 0:9c4dbd5b0a33 103
agoel 0:9c4dbd5b0a33 104 // Define alcoholSensor and moisture input PIN (Arduino connector namings A0 - PA_0, A1 - PA_1, A2 - PC_4, A3 - PB_0, A4 - PC_1, A5 - PC_0
agoel 0:9c4dbd5b0a33 105 AnalogIn moistureSensor(PC_2); // A0 - PA_0
agoel 0:9c4dbd5b0a33 106 AnalogIn alcoholSensor(PB_0); // A3
agoel 0:9c4dbd5b0a33 107
agoel 0:9c4dbd5b0a33 108 float moistureValue = 0.0f;
agoel 0:9c4dbd5b0a33 109 float alcoholValue = 0.0f;
agoel 0:9c4dbd5b0a33 110 float moistureProcessedValue = 0.0f;
agoel 0:9c4dbd5b0a33 111 float alcoholProcessedValue = 0.0f;
agoel 0:9c4dbd5b0a33 112
agoel 0:9c4dbd5b0a33 113 float moistureLastPeakValue = 0.0f;
agoel 0:9c4dbd5b0a33 114 float alcoholLastPeakValue = 0.0f;
agoel 0:9c4dbd5b0a33 115
agoel 0:9c4dbd5b0a33 116 // Enumeration for Range
agoel 0:9c4dbd5b0a33 117 #define LOW 1
agoel 0:9c4dbd5b0a33 118 #define MEDIUM 2
agoel 0:9c4dbd5b0a33 119 #define HIGH 3
agoel 0:9c4dbd5b0a33 120
agoel 0:9c4dbd5b0a33 121 char str[512];
agoel 0:9c4dbd5b0a33 122 char *simImei;
agoel 0:9c4dbd5b0a33 123
agoel 0:9c4dbd5b0a33 124 const char* HttpResultToString(HTTPResult r)
agoel 0:9c4dbd5b0a33 125 {
agoel 0:9c4dbd5b0a33 126 switch(r) {
agoel 0:9c4dbd5b0a33 127 case HTTP_PROCESSING:
agoel 0:9c4dbd5b0a33 128 return "HTTP_PROCESSING";
agoel 0:9c4dbd5b0a33 129 case HTTP_PARSE:
agoel 0:9c4dbd5b0a33 130 return "HTTP_PARSE";
agoel 0:9c4dbd5b0a33 131 case HTTP_DNS:
agoel 0:9c4dbd5b0a33 132 return "HTTP_DNS";
agoel 0:9c4dbd5b0a33 133 case HTTP_PRTCL:
agoel 0:9c4dbd5b0a33 134 return "HTTP_PRTCL";
agoel 0:9c4dbd5b0a33 135 case HTTP_NOTFOUND:
agoel 0:9c4dbd5b0a33 136 return "HTTP_NOTFOUND - 404";
agoel 0:9c4dbd5b0a33 137 case HTTP_REFUSED:
agoel 0:9c4dbd5b0a33 138 return "HTTP_REFUSED - 403";
agoel 0:9c4dbd5b0a33 139 case HTTP_ERROR:
agoel 0:9c4dbd5b0a33 140 return "HTTP_ERROR";
agoel 0:9c4dbd5b0a33 141 case HTTP_TIMEOUT:
agoel 0:9c4dbd5b0a33 142 return "HTTP_TIMEOUT";
agoel 0:9c4dbd5b0a33 143 case HTTP_CONN:
agoel 0:9c4dbd5b0a33 144 return "HTTP_CONN";
agoel 0:9c4dbd5b0a33 145 case HTTP_CLOSED:
agoel 0:9c4dbd5b0a33 146 return "HTTP_CLOSED";
agoel 0:9c4dbd5b0a33 147 case HTTP_OK:
agoel 0:9c4dbd5b0a33 148 return "HTTP_OK";
agoel 0:9c4dbd5b0a33 149 }
agoel 0:9c4dbd5b0a33 150 return "UNKNOWN ERROR CODE";
agoel 0:9c4dbd5b0a33 151 }
agoel 0:9c4dbd5b0a33 152
agoel 0:9c4dbd5b0a33 153 void callback(char* topic, char* payload, unsigned int len) {
agoel 0:9c4dbd5b0a33 154 logInfo("topic: [%s]\r\npayload: [%s]", topic, payload);
agoel 0:9c4dbd5b0a33 155 }
agoel 0:9c4dbd5b0a33 156
agoel 0:9c4dbd5b0a33 157 int main()
agoel 0:9c4dbd5b0a33 158 {
agoel 0:9c4dbd5b0a33 159 bool simProvisioned = false;
agoel 0:9c4dbd5b0a33 160 int ret;
agoel 0:9c4dbd5b0a33 161
agoel 0:9c4dbd5b0a33 162 printf("Dragon Fly Sample Program for MultiTech Board - Sample Workshop Program ...\n\r");
agoel 0:9c4dbd5b0a33 163
agoel 0:9c4dbd5b0a33 164 //Sets the log level to INFO, higher log levels produce more log output.
agoel 0:9c4dbd5b0a33 165 //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
agoel 0:9c4dbd5b0a33 166 MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);
agoel 0:9c4dbd5b0a33 167
agoel 0:9c4dbd5b0a33 168 /** STMicro Nucelo F411RE
agoel 0:9c4dbd5b0a33 169 * The supported jumper configurations of the MTSAS do not line up with
agoel 0:9c4dbd5b0a33 170 * the pin mapping of the Nucleo F411RE. Therefore, the MTSAS serial TX
agoel 0:9c4dbd5b0a33 171 * pin (JP8 Pin 2) must be manually jumped to Serial1 RX (Shield pin D2)
agoel 0:9c4dbd5b0a33 172 * and the MTSAS serial RX pin (JP9 Pin 2) pin must be manually jumped to
agoel 0:9c4dbd5b0a33 173 * Serial1 TX (Shield pin D8).
agoel 0:9c4dbd5b0a33 174 */
agoel 0:9c4dbd5b0a33 175 MTSSerialFlowControl* io = new MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
agoel 0:9c4dbd5b0a33 176
agoel 0:9c4dbd5b0a33 177 //Sets the baud rate for communicating with the radio
agoel 0:9c4dbd5b0a33 178 io->baud(115200);
agoel 0:9c4dbd5b0a33 179
agoel 0:9c4dbd5b0a33 180 //Creates a radio object
agoel 0:9c4dbd5b0a33 181 Cellular* radio = CellularFactory::create(io);
agoel 0:9c4dbd5b0a33 182
agoel 0:9c4dbd5b0a33 183 if (! radio) {
agoel 0:9c4dbd5b0a33 184 logFatal("failed to create Cellular object - exiting");
agoel 0:9c4dbd5b0a33 185 printf("failed to create Cellular object - exiting.\n\r");
agoel 0:9c4dbd5b0a33 186 return 1;
agoel 0:9c4dbd5b0a33 187 }
agoel 0:9c4dbd5b0a33 188
agoel 0:9c4dbd5b0a33 189 // radio->configureSignals(D11,D7,RESET); // Modified Flow Control for ST Sensor Board D11
agoel 0:9c4dbd5b0a33 190 Transport::setTransport(radio); // Required to control Cell Radio vs WiFi solution
agoel 0:9c4dbd5b0a33 191
agoel 0:9c4dbd5b0a33 192 while (radio->setApn(_APN) != MTS_SUCCESS) {
agoel 0:9c4dbd5b0a33 193 logError("failed to set APN [%s]", _APN);
agoel 0:9c4dbd5b0a33 194 printf("failed to set APN.\n\r");
agoel 0:9c4dbd5b0a33 195 wait(2);
agoel 0:9c4dbd5b0a33 196 }
agoel 0:9c4dbd5b0a33 197
agoel 0:9c4dbd5b0a33 198 while (! radio->connect()) {
agoel 0:9c4dbd5b0a33 199 logError("failed to bring up PPP link");
agoel 0:9c4dbd5b0a33 200 printf("failed to bring up PPP link.\n\r");
agoel 0:9c4dbd5b0a33 201 wait(2);
agoel 0:9c4dbd5b0a33 202 }
agoel 0:9c4dbd5b0a33 203
agoel 0:9c4dbd5b0a33 204 printf("Signal Strength: %d\n\r", radio->getSignalStrength()); //Check the signal strength should be above 8
agoel 0:9c4dbd5b0a33 205
agoel 0:9c4dbd5b0a33 206 // If you suspect a connectivity issue, uncomment the code below and if ping works. If you are not getting a
agoel 0:9c4dbd5b0a33 207 // valid ping, there's a connectivity problem. First step is to verify you've got the right APN set
agoel 0:9c4dbd5b0a33 208 //
agoel 0:9c4dbd5b0a33 209 // Try pinging default server "8.8.8.8" (Google's DNS)
agoel 0:9c4dbd5b0a33 210 int ping_valid = 0;
agoel 0:9c4dbd5b0a33 211 while (ping_valid == 0) {
agoel 0:9c4dbd5b0a33 212 ping_valid = radio->ping();
agoel 0:9c4dbd5b0a33 213 // or can use ping_valid = radio->ping("www.google.com");
agoel 0:9c4dbd5b0a33 214 printf("Ping Valid: %s\n\r", ping_valid ? "true" : "false");
agoel 0:9c4dbd5b0a33 215 if (ping_valid == 0) {
agoel 0:9c4dbd5b0a33 216 wait(3);
agoel 0:9c4dbd5b0a33 217 printf("wait 3");
agoel 0:9c4dbd5b0a33 218 }
agoel 0:9c4dbd5b0a33 219 }
agoel 0:9c4dbd5b0a33 220
agoel 0:9c4dbd5b0a33 221 // Get the IMEI of the SIM, this will act as SCL-id on AerCloud
agoel 0:9c4dbd5b0a33 222 string sImei;
agoel 0:9c4dbd5b0a33 223 sImei = radio->getEquipmentIdentifier();
agoel 0:9c4dbd5b0a33 224 simImei = (char*) sImei.c_str();
agoel 0:9c4dbd5b0a33 225 strcpy (_AERCLOUD_DEVICE_ID, simImei);
agoel 0:9c4dbd5b0a33 226
agoel 0:9c4dbd5b0a33 227 // Create http client
agoel 0:9c4dbd5b0a33 228 HTTPClient http;
agoel 0:9c4dbd5b0a33 229
agoel 0:9c4dbd5b0a33 230 // Check if SIM is provisioned in AerCloud
agoel 0:9c4dbd5b0a33 231 printf("\r\nIs the SIM provisioned? ");
agoel 0:9c4dbd5b0a33 232 char url[512];
agoel 0:9c4dbd5b0a33 233 snprintf(url, sizeof(url), "%s/%s/scls/%s?apiKey=%s", AC_BASE, _AERCLOUD_ACCOUNT_ID, simImei, _AERCLOUD_API_KEY);
agoel 0:9c4dbd5b0a33 234 ret = http.get(url, str, 128);
agoel 0:9c4dbd5b0a33 235 printf("URL : %s", url);
agoel 0:9c4dbd5b0a33 236 printf("STR : %s", str);
agoel 0:9c4dbd5b0a33 237
agoel 0:9c4dbd5b0a33 238 if (ret == HTTP_OK) {
agoel 0:9c4dbd5b0a33 239 // We're already provisioned
agoel 0:9c4dbd5b0a33 240 printf("Yes\r\n");
agoel 0:9c4dbd5b0a33 241 printf("Page fetched successfully - read %d characters\r\n", strlen(str));
agoel 0:9c4dbd5b0a33 242 printf("Result: %s\r\n", str);
agoel 0:9c4dbd5b0a33 243 simProvisioned = true;
agoel 0:9c4dbd5b0a33 244 } else {
agoel 0:9c4dbd5b0a33 245 printf("No\r\n");
agoel 0:9c4dbd5b0a33 246 printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode());
agoel 0:9c4dbd5b0a33 247
agoel 0:9c4dbd5b0a33 248 //SIM is not provisioned. trying to provision it...
agoel 0:9c4dbd5b0a33 249 char url[512];
agoel 0:9c4dbd5b0a33 250 snprintf(url, sizeof(url), "%s/%s/scls?apiKey=%s", AC_BASE, _AERCLOUD_ACCOUNT_ID, _AERCLOUD_API_KEY);
agoel 0:9c4dbd5b0a33 251 snprintf(str, sizeof(str), "{\"sclId\":\"%s\"}\0",simImei);
agoel 0:9c4dbd5b0a33 252 HTTPJson outText(str);
agoel 0:9c4dbd5b0a33 253 HTTPJson inText(str, 512);
agoel 0:9c4dbd5b0a33 254 ret = http.post(url, outText, &inText);
agoel 0:9c4dbd5b0a33 255
agoel 0:9c4dbd5b0a33 256 if (!ret) {
agoel 0:9c4dbd5b0a33 257 printf("Executed POST successfully - read %d characters\r\n", strlen(str));
agoel 0:9c4dbd5b0a33 258 printf("Result: %s\r\n", str);
agoel 0:9c4dbd5b0a33 259 simProvisioned = true;
agoel 0:9c4dbd5b0a33 260 } else {
agoel 0:9c4dbd5b0a33 261 if(http.getHTTPResponseCode() == 200)
agoel 0:9c4dbd5b0a33 262 simProvisioned = true;
agoel 0:9c4dbd5b0a33 263 printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode());
agoel 0:9c4dbd5b0a33 264 }
agoel 0:9c4dbd5b0a33 265 }
agoel 0:9c4dbd5b0a33 266
agoel 0:9c4dbd5b0a33 267 PubSubClient mqtt(_host, _port, callback);
agoel 0:9c4dbd5b0a33 268 unsigned int connectRetry = 0;
agoel 0:9c4dbd5b0a33 269 while(! mqtt.connect(_AERCLOUD_DEVICE_ID, _AERCLOUD_ACCOUNT_ID, _AERCLOUD_API_KEY)) {
agoel 0:9c4dbd5b0a33 270 logError("failed to connect to AerCloud Server");
agoel 0:9c4dbd5b0a33 271 wait(5);
agoel 0:9c4dbd5b0a33 272 connectRetry++;
agoel 0:9c4dbd5b0a33 273 if (connectRetry > 5) break;
agoel 0:9c4dbd5b0a33 274 }
agoel 0:9c4dbd5b0a33 275
agoel 0:9c4dbd5b0a33 276 //POST data to containers if SIM has been successfully provisioned
agoel 0:9c4dbd5b0a33 277 if (simProvisioned) {
agoel 0:9c4dbd5b0a33 278 while(1) {
agoel 0:9c4dbd5b0a33 279
agoel 0:9c4dbd5b0a33 280 // Read data from sensor
agoel 0:9c4dbd5b0a33 281 char alarmStringM[512] = "";
agoel 0:9c4dbd5b0a33 282 bool generateAlertM = false;
agoel 0:9c4dbd5b0a33 283
agoel 0:9c4dbd5b0a33 284 moistureValue = moistureSensor;
agoel 0:9c4dbd5b0a33 285 printf("Moisture value read : %f \n\n",moistureValue);
agoel 0:9c4dbd5b0a33 286 if (moistureValue > .5f) {
agoel 0:9c4dbd5b0a33 287 moistureProcessedValue = HIGH;
agoel 0:9c4dbd5b0a33 288 } else if (moistureValue > .25f) {
agoel 0:9c4dbd5b0a33 289 moistureProcessedValue = MEDIUM;
agoel 0:9c4dbd5b0a33 290 } else {
agoel 0:9c4dbd5b0a33 291 moistureProcessedValue = LOW;
agoel 0:9c4dbd5b0a33 292 }
agoel 0:9c4dbd5b0a33 293
agoel 0:9c4dbd5b0a33 294 if (moistureLastPeakValue != moistureProcessedValue) { //condition to trigger alarm
agoel 0:9c4dbd5b0a33 295 if (moistureLastPeakValue == 0) { // first time
agoel 0:9c4dbd5b0a33 296 moistureLastPeakValue = moistureProcessedValue;
agoel 0:9c4dbd5b0a33 297 generateAlertM = false;
agoel 0:9c4dbd5b0a33 298 } else {
agoel 0:9c4dbd5b0a33 299 if (moistureLastPeakValue == LOW) {
agoel 0:9c4dbd5b0a33 300 if (moistureProcessedValue == MEDIUM) {
agoel 0:9c4dbd5b0a33 301 strcpy (alarmStringM, "Moisture level going up from LOW to MEDIUM");
agoel 0:9c4dbd5b0a33 302 } else if (moistureProcessedValue == HIGH) {
agoel 0:9c4dbd5b0a33 303 strcpy (alarmStringM, "Moisture level going up from LOW to HIGH");
agoel 0:9c4dbd5b0a33 304 }
agoel 0:9c4dbd5b0a33 305 } else if (moistureLastPeakValue == MEDIUM) {
agoel 0:9c4dbd5b0a33 306 if (moistureProcessedValue == LOW) {
agoel 0:9c4dbd5b0a33 307 strcpy (alarmStringM, "Moisture level going down from MEDIUM to LOW");
agoel 0:9c4dbd5b0a33 308 } else if (moistureProcessedValue == HIGH) {
agoel 0:9c4dbd5b0a33 309 strcpy (alarmStringM, "Moisture level going up from MEDIUM to HIGH");
agoel 0:9c4dbd5b0a33 310 }
agoel 0:9c4dbd5b0a33 311 } else if (moistureLastPeakValue == HIGH) {
agoel 0:9c4dbd5b0a33 312 if (moistureProcessedValue == LOW) {
agoel 0:9c4dbd5b0a33 313 strcpy (alarmStringM, "Moisture level going down from HIGH to LOW");
agoel 0:9c4dbd5b0a33 314 } else if (moistureProcessedValue == MEDIUM) {
agoel 0:9c4dbd5b0a33 315 strcpy (alarmStringM, "Moisture level going down from HIGH to MEDIUM");
agoel 0:9c4dbd5b0a33 316 }
agoel 0:9c4dbd5b0a33 317 }
agoel 0:9c4dbd5b0a33 318 moistureLastPeakValue = moistureProcessedValue;
agoel 0:9c4dbd5b0a33 319 generateAlertM = true;
agoel 0:9c4dbd5b0a33 320 }
agoel 0:9c4dbd5b0a33 321 }
agoel 0:9c4dbd5b0a33 322
agoel 0:9c4dbd5b0a33 323 char alarmStringA[512] = "";
agoel 0:9c4dbd5b0a33 324 bool generateAlertA = false;
agoel 0:9c4dbd5b0a33 325
agoel 0:9c4dbd5b0a33 326 alcoholValue = 1 - alcoholSensor; // as per example logic value needs to be subtracted from 1 (inverse value)
agoel 0:9c4dbd5b0a33 327 printf("Alcohol value read : %f \n\n",alcoholValue);
agoel 0:9c4dbd5b0a33 328 if (alcoholValue > .5f) {
agoel 0:9c4dbd5b0a33 329 alcoholProcessedValue = HIGH;
agoel 0:9c4dbd5b0a33 330 } else if (alcoholValue > .25f) {
agoel 0:9c4dbd5b0a33 331 alcoholProcessedValue = MEDIUM;
agoel 0:9c4dbd5b0a33 332 } else {
agoel 0:9c4dbd5b0a33 333 alcoholProcessedValue = LOW;
agoel 0:9c4dbd5b0a33 334 }
agoel 0:9c4dbd5b0a33 335
agoel 0:9c4dbd5b0a33 336 if (alcoholLastPeakValue != alcoholProcessedValue) { //condition to trigger alarm
agoel 0:9c4dbd5b0a33 337 if (alcoholLastPeakValue == 0) { // first time
agoel 0:9c4dbd5b0a33 338 alcoholLastPeakValue = alcoholProcessedValue;
agoel 0:9c4dbd5b0a33 339 generateAlertA = false;
agoel 0:9c4dbd5b0a33 340 } else {
agoel 0:9c4dbd5b0a33 341 if (alcoholLastPeakValue == LOW) {
agoel 0:9c4dbd5b0a33 342 if (alcoholProcessedValue == MEDIUM) {
agoel 0:9c4dbd5b0a33 343 strcpy (alarmStringA, "Alcohol level going up from LOW to MEDIUM");
agoel 0:9c4dbd5b0a33 344 } else if (alcoholProcessedValue == HIGH) {
agoel 0:9c4dbd5b0a33 345 strcpy (alarmStringA, "Alcohol level going up from LOW to HIGH");
agoel 0:9c4dbd5b0a33 346 }
agoel 0:9c4dbd5b0a33 347 } else if (alcoholLastPeakValue == MEDIUM) {
agoel 0:9c4dbd5b0a33 348 if (alcoholProcessedValue == LOW) {
agoel 0:9c4dbd5b0a33 349 strcpy (alarmStringA, "Alcohol level going down from MEDIUM to LOW");
agoel 0:9c4dbd5b0a33 350 } else if (alcoholProcessedValue == HIGH) {
agoel 0:9c4dbd5b0a33 351 strcpy (alarmStringA, "Alcohol level going up from MEDIUM to HIGH");
agoel 0:9c4dbd5b0a33 352 }
agoel 0:9c4dbd5b0a33 353 } else if (alcoholLastPeakValue == HIGH) {
agoel 0:9c4dbd5b0a33 354 if (alcoholProcessedValue == LOW) {
agoel 0:9c4dbd5b0a33 355 strcpy (alarmStringA, "Alcohol level going down from HIGH to LOW");
agoel 0:9c4dbd5b0a33 356 } else if (alcoholProcessedValue == MEDIUM) {
agoel 0:9c4dbd5b0a33 357 strcpy (alarmStringA, "Alcohol level going down from HIGH to MEDIUM");
agoel 0:9c4dbd5b0a33 358 }
agoel 0:9c4dbd5b0a33 359 }
agoel 0:9c4dbd5b0a33 360 alcoholLastPeakValue = alcoholProcessedValue;
agoel 0:9c4dbd5b0a33 361 generateAlertA = true;
agoel 0:9c4dbd5b0a33 362 }
agoel 0:9c4dbd5b0a33 363 }
agoel 0:9c4dbd5b0a33 364
agoel 0:9c4dbd5b0a33 365 // POST data to AerCloud using MQTT
agoel 0:9c4dbd5b0a33 366 time_t seconds = time(NULL);
agoel 0:9c4dbd5b0a33 367 sprintf(str,"{\"moistureSensorValue\": %.5f, \"moistureProcessedValue\": %.2f, \"alcoholSensorValue\": %.5f, \"alcoholProcessedValue\": %.2f, \"sclId\":\"%s\", \"timeStamp\": %u}", moistureValue, moistureProcessedValue, alcoholValue, alcoholProcessedValue, simImei, seconds);
agoel 0:9c4dbd5b0a33 368
agoel 0:9c4dbd5b0a33 369 printf("str = %s \r\n",str);
agoel 0:9c4dbd5b0a33 370 time_t beforeTime = time(NULL);
agoel 0:9c4dbd5b0a33 371 printf("\r\nAcquired data going to post now... %u \r\n", beforeTime);
agoel 0:9c4dbd5b0a33 372 if (! mqtt.publish(_AERCLOUD_CONTAINER, str)) {
agoel 0:9c4dbd5b0a33 373 printf("failed to publish: [%s]", str);
agoel 0:9c4dbd5b0a33 374 }
agoel 0:9c4dbd5b0a33 375 wait_ms (100); // wait for 100 ms to get the data published
agoel 0:9c4dbd5b0a33 376 printf("\r\nPosted data took ... %u seconds \r\n", time(NULL)-beforeTime );
agoel 0:9c4dbd5b0a33 377
agoel 0:9c4dbd5b0a33 378 // post alert for Moisture
agoel 0:9c4dbd5b0a33 379 if (generateAlertM) {
agoel 0:9c4dbd5b0a33 380 sprintf(str,"{\"alarmText\": \"%s\", \"sclId\":\"%s\", \"timeStamp\": %u}", alarmStringM, simImei, seconds);
agoel 0:9c4dbd5b0a33 381 printf("str = %s \r\n",str);
agoel 0:9c4dbd5b0a33 382 time_t beforeTime = time(NULL);
agoel 0:9c4dbd5b0a33 383 printf("\r\nAcquired data going to post now... %u \r\n", beforeTime);
agoel 0:9c4dbd5b0a33 384 if (! mqtt.publish(_AERCLOUD_CONTAINER_ALARM, str)) {
agoel 0:9c4dbd5b0a33 385 printf("failed to publish: [%s]", str);
agoel 0:9c4dbd5b0a33 386 }
agoel 0:9c4dbd5b0a33 387 wait_ms (100); // wait for 100 ms to get the data published
agoel 0:9c4dbd5b0a33 388 printf("\r\nPosted data took ... %u seconds \r\n", time(NULL)-beforeTime );
agoel 0:9c4dbd5b0a33 389 }
agoel 0:9c4dbd5b0a33 390
agoel 0:9c4dbd5b0a33 391 // post alert for Alcohol
agoel 0:9c4dbd5b0a33 392 if (generateAlertA) {
agoel 0:9c4dbd5b0a33 393 sprintf(str,"{\"alarmText\": \"%s\", \"sclId\":\"%s\", \"timeStamp\": %u}", alarmStringA, simImei, seconds);
agoel 0:9c4dbd5b0a33 394 printf("str = %s \r\n",str);
agoel 0:9c4dbd5b0a33 395 time_t beforeTime = time(NULL);
agoel 0:9c4dbd5b0a33 396 printf("\r\nAcquired data going to post now... %u \r\n", beforeTime);
agoel 0:9c4dbd5b0a33 397 if (! mqtt.publish(_AERCLOUD_CONTAINER_ALARM, str)) {
agoel 0:9c4dbd5b0a33 398 printf("failed to publish: [%s]", str);
agoel 0:9c4dbd5b0a33 399 }
agoel 0:9c4dbd5b0a33 400 wait_ms (100); // wait for 100 ms to get the data published
agoel 0:9c4dbd5b0a33 401 printf("\r\nPosted data took ... %u seconds \r\n", time(NULL)-beforeTime );
agoel 0:9c4dbd5b0a33 402 }
agoel 0:9c4dbd5b0a33 403
agoel 0:9c4dbd5b0a33 404 printf("Will be measuring again after 1 seconds !!!! \r\n\n");
agoel 0:9c4dbd5b0a33 405 wait (1);
agoel 0:9c4dbd5b0a33 406 moistureValue = 0;
agoel 0:9c4dbd5b0a33 407 }
agoel 0:9c4dbd5b0a33 408 }
agoel 0:9c4dbd5b0a33 409
agoel 0:9c4dbd5b0a33 410 //Disconnect ppp link of radio
agoel 0:9c4dbd5b0a33 411 mqtt.disconnect();
agoel 0:9c4dbd5b0a33 412 //Disconnect ppp link of radio
agoel 0:9c4dbd5b0a33 413 radio->disconnect();
agoel 0:9c4dbd5b0a33 414 }