Thingspeak IoT using Nucleo-F411RE and IDW01m1
Dependencies: mbed X_NUCLEO_IDW01M1v2 NetworkSocketAPI
main.cpp@13:9a2016106dae, 2017-05-06 (annotated)
- Committer:
- imi121
- Date:
- Sat May 06 17:39:25 2017 +0000
- Revision:
- 13:9a2016106dae
- Parent:
- 12:80d8e97e81f2
- Child:
- 14:753cc5807f05
This is a project to interface Airquality MQ135 sensor with STM Nucleo -F401RE and IDW Wireless board. The outputs is available on thingspeak as well
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mridup | 0:dee849b0e6e6 | 1 | /* SpwfInterface NetworkSocketAPI Example Program |
mridup | 0:dee849b0e6e6 | 2 | * Copyright (c) 2015 ARM Limited |
mridup | 0:dee849b0e6e6 | 3 | * |
mridup | 0:dee849b0e6e6 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mridup | 0:dee849b0e6e6 | 5 | * you may not use this file except in compliance with the License. |
mridup | 0:dee849b0e6e6 | 6 | * You may obtain a copy of the License at |
mridup | 0:dee849b0e6e6 | 7 | * |
mridup | 0:dee849b0e6e6 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mridup | 0:dee849b0e6e6 | 9 | * |
mridup | 0:dee849b0e6e6 | 10 | * Unless required by applicable law or agreed to in writing, software |
mridup | 0:dee849b0e6e6 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mridup | 0:dee849b0e6e6 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mridup | 0:dee849b0e6e6 | 13 | * See the License for the specific language governing permissions and |
mridup | 0:dee849b0e6e6 | 14 | * limitations under the License. |
mridup | 0:dee849b0e6e6 | 15 | */ |
mridup | 0:dee849b0e6e6 | 16 | |
mridup | 0:dee849b0e6e6 | 17 | #include "mbed.h" |
mridup | 0:dee849b0e6e6 | 18 | #include "SpwfInterface.h" |
imi121 | 13:9a2016106dae | 19 | //#include "NTPClient.h" |
imi121 | 13:9a2016106dae | 20 | //#include "HTTPClient.h" |
mridup | 0:dee849b0e6e6 | 21 | #include "TCPSocket.h" |
imi121 | 13:9a2016106dae | 22 | #include "DHT11.h" |
mridup | 0:dee849b0e6e6 | 23 | |
mapellil | 8:74b827befe72 | 24 | |
imi121 | 13:9a2016106dae | 25 | #define PORT 1234 |
imi121 | 13:9a2016106dae | 26 | #define MAX_PENDING 1 |
imi121 | 13:9a2016106dae | 27 | /*Thing speak settings */ |
imi121 | 13:9a2016106dae | 28 | //NTPClient ntp; |
imi121 | 13:9a2016106dae | 29 | //HTTPClient http; |
imi121 | 13:9a2016106dae | 30 | |
imi121 | 13:9a2016106dae | 31 | //char* ntpServerUrl = "0.ca.pool.ntp.org"; |
imi121 | 13:9a2016106dae | 32 | #define IP "184.106.153.149" |
imi121 | 13:9a2016106dae | 33 | char* thingSpeakUrl = "http://api.thingspeak.com/update"; |
imi121 | 13:9a2016106dae | 34 | char* thingSpeakKey = "SBYDOEXE50MNOBSI"; |
mridup | 0:dee849b0e6e6 | 35 | //------------------------------------ |
mridup | 0:dee849b0e6e6 | 36 | // Hyperterminal configuration |
mridup | 0:dee849b0e6e6 | 37 | // 9600 bauds, 8-bit data, no parity |
mridup | 0:dee849b0e6e6 | 38 | //------------------------------------ |
mridup | 0:dee849b0e6e6 | 39 | |
mridup | 2:3a87dbea07a7 | 40 | Serial pc(USBTX, USBRX); |
mridup | 0:dee849b0e6e6 | 41 | DigitalOut myled(LED1); |
imi121 | 13:9a2016106dae | 42 | AnalogIn ain(A0); |
imi121 | 13:9a2016106dae | 43 | DHT11 sensor(PA_1); |
imi121 | 13:9a2016106dae | 44 | /*************************************/ |
mridup | 1:daf71fa3674c | 45 | |
mridup | 1:daf71fa3674c | 46 | //NUCLEO: D8->UART1_TX (PA_9), D2->UART1_RX (PA_10) |
mridup | 1:daf71fa3674c | 47 | SpwfSAInterface spwf(D8, D2, false); |
imi121 | 13:9a2016106dae | 48 | const double Rl = 10000.0; // Rl (Ohm) - Load resistance |
imi121 | 13:9a2016106dae | 49 | const double Vadc_5 = 0.0048828125; // ADC step 5V/1024 4,88mV (10bit ADC) |
mridup | 2:3a87dbea07a7 | 50 | |
imi121 | 13:9a2016106dae | 51 | //const double Vadc_33 = 0.0032226562; // ADC step 3,3V/1024 3,22mV (10bit ADC) |
imi121 | 13:9a2016106dae | 52 | double Vrl; // Output voltage |
imi121 | 13:9a2016106dae | 53 | double Rs; // Rs (Ohm) - Sensor resistance |
imi121 | 13:9a2016106dae | 54 | double ppm; // ppm |
imi121 | 13:9a2016106dae | 55 | double ratio; |
imi121 | 13:9a2016106dae | 56 | int temp; |
imi121 | 13:9a2016106dae | 57 | int humid; |
imi121 | 13:9a2016106dae | 58 | |
imi121 | 13:9a2016106dae | 59 | //Calculation of PPM |
imi121 | 13:9a2016106dae | 60 | void calculatePPM() |
imi121 | 13:9a2016106dae | 61 | { |
imi121 | 13:9a2016106dae | 62 | double lgPPM; |
imi121 | 13:9a2016106dae | 63 | Vrl = (double)ain*Vadc_5; // For 5V Vcc use Vadc_5 |
imi121 | 13:9a2016106dae | 64 | Rs = (5 - Vrl)/Vrl; // Calculate sensor resistance |
imi121 | 13:9a2016106dae | 65 | ratio = Rs/Rl; // Calculate ratio |
imi121 | 13:9a2016106dae | 66 | lgPPM = (log10(ratio) * -0.8)+ 0.9; // Calculate ppm |
imi121 | 13:9a2016106dae | 67 | ppm = pow(10,lgPPM); // Calculate ppm |
imi121 | 13:9a2016106dae | 68 | pc.printf("The ppm value is %lf \n",ppm); |
imi121 | 13:9a2016106dae | 69 | pc.printf("The raw Rs is %2.3lf\n",ratio); |
imi121 | 13:9a2016106dae | 70 | pc.printf("The raw voltage is %2.3lf \n",Vrl); |
imi121 | 13:9a2016106dae | 71 | } |
imi121 | 13:9a2016106dae | 72 | |
imi121 | 13:9a2016106dae | 73 | int http_demo(void) |
imi121 | 13:9a2016106dae | 74 | { |
imi121 | 13:9a2016106dae | 75 | TCPSocket socket(&spwf); |
imi121 | 13:9a2016106dae | 76 | char buffer[256]; |
imi121 | 13:9a2016106dae | 77 | char message[40]; |
imi121 | 13:9a2016106dae | 78 | int err; |
imi121 | 13:9a2016106dae | 79 | printf("Sending HTTP Data to thingspeak...\r\n"); |
imi121 | 13:9a2016106dae | 80 | |
imi121 | 13:9a2016106dae | 81 | // Open a socket on the network interface, and create a TCP connection to thingspeak |
imi121 | 13:9a2016106dae | 82 | //socket.open(&spwf); |
imi121 | 13:9a2016106dae | 83 | err=socket.connect(IP,80); // connecting to thingspeak |
imi121 | 13:9a2016106dae | 84 | if(err!=0) |
imi121 | 13:9a2016106dae | 85 | { |
imi121 | 13:9a2016106dae | 86 | pc.printf("\r\nCould not connect to Socket, err = %d!!\r\n", err); |
imi121 | 13:9a2016106dae | 87 | return -1; |
imi121 | 13:9a2016106dae | 88 | } else pc.printf("\r\nconnected to host server\r\n"); |
imi121 | 13:9a2016106dae | 89 | // Send a simple http request |
imi121 | 13:9a2016106dae | 90 | //buffer[0] = 0; |
imi121 | 13:9a2016106dae | 91 | //sprintf(buffer, "GET https://api.thingspeak.com/update?api_key=SBYDOEXE50MNOBSI&field1=%f&field2=%d&field3=%d",(float)ppm,temp,humid);//thingSpeakUrl,thingSpeakKey, |
imi121 | 13:9a2016106dae | 92 | sprintf(message,"field1=%f&field2=%d&field3=%d",ppm,temp,humid); |
imi121 | 13:9a2016106dae | 93 | printf("Message Length=%d\r\n",(int)strlen(message)); |
imi121 | 13:9a2016106dae | 94 | sprintf(buffer,"POST /update HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: close\r\nX-THINGSPEAKAPIKEY: %s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s",thingSpeakKey,(int)strlen(message),message); |
imi121 | 13:9a2016106dae | 95 | pc.printf("Request to %s\r\n", buffer); |
imi121 | 13:9a2016106dae | 96 | //char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n"; |
imi121 | 13:9a2016106dae | 97 | int scount = socket.send(buffer, (int)strlen(buffer)); |
imi121 | 13:9a2016106dae | 98 | printf("sent %d [%.*s]\r\n", scount, strstr(buffer, "\r\n")-buffer, buffer); |
imi121 | 13:9a2016106dae | 99 | |
imi121 | 13:9a2016106dae | 100 | // Recieve a simple http response and print out the response line |
imi121 | 13:9a2016106dae | 101 | char rbuffer[64]; |
imi121 | 13:9a2016106dae | 102 | int rcount = socket.recv(rbuffer, sizeof rbuffer); |
imi121 | 13:9a2016106dae | 103 | printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); |
imi121 | 13:9a2016106dae | 104 | |
imi121 | 13:9a2016106dae | 105 | // Close the socket to return its memory and bring down the network interface |
imi121 | 13:9a2016106dae | 106 | socket.close(); |
imi121 | 13:9a2016106dae | 107 | return 0; |
imi121 | 13:9a2016106dae | 108 | } |
imi121 | 13:9a2016106dae | 109 | |
imi121 | 13:9a2016106dae | 110 | int main() |
imi121 | 13:9a2016106dae | 111 | { |
mridup | 0:dee849b0e6e6 | 112 | int err; |
imi121 | 13:9a2016106dae | 113 | char * ssid = "VM957191-2G"; |
imi121 | 13:9a2016106dae | 114 | char * seckey = "krzvzkcw"; |
imi121 | 13:9a2016106dae | 115 | //char buffer[255]; |
imi121 | 13:9a2016106dae | 116 | //time_t ctTime; |
imi121 | 13:9a2016106dae | 117 | //Host server(IpAddr(), 123, ntpServerUrl); |
imi121 | 13:9a2016106dae | 118 | //ntp.setTime(server); |
imi121 | 13:9a2016106dae | 119 | pc.printf("\r\nAir Quality and Environment monitoring Application\r\n"); |
mridup | 0:dee849b0e6e6 | 120 | pc.printf("\r\nconnecting to AP\r\n"); |
mridup | 0:dee849b0e6e6 | 121 | |
mridup | 0:dee849b0e6e6 | 122 | if(spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2)) { |
mridup | 0:dee849b0e6e6 | 123 | pc.printf("\r\nnow connected\r\n"); |
mridup | 0:dee849b0e6e6 | 124 | } else { |
mridup | 0:dee849b0e6e6 | 125 | pc.printf("\r\nerror connecting to AP.\r\n"); |
mridup | 0:dee849b0e6e6 | 126 | return -1; |
mridup | 0:dee849b0e6e6 | 127 | } |
mapellil | 8:74b827befe72 | 128 | |
mridup | 0:dee849b0e6e6 | 129 | const char *ip = spwf.get_ip_address(); |
mridup | 0:dee849b0e6e6 | 130 | const char *mac = spwf.get_mac_address(); |
mridup | 0:dee849b0e6e6 | 131 | |
mridup | 0:dee849b0e6e6 | 132 | pc.printf("\r\nIP Address is: %s\r\n", (ip) ? ip : "No IP"); |
imi121 | 13:9a2016106dae | 133 | pc.printf("\r\nMAC Address is: %s\r\n", (mac) ? mac : "No MAC"); |
mridup | 0:dee849b0e6e6 | 134 | |
imi121 | 13:9a2016106dae | 135 | while(1) |
imi121 | 13:9a2016106dae | 136 | { |
imi121 | 13:9a2016106dae | 137 | wait(20); |
imi121 | 13:9a2016106dae | 138 | myled = !myled; |
imi121 | 13:9a2016106dae | 139 | calculatePPM(); |
imi121 | 13:9a2016106dae | 140 | err = sensor.readData(); |
imi121 | 13:9a2016106dae | 141 | if (err ==DHT11::OK) |
imi121 | 13:9a2016106dae | 142 | { |
imi121 | 13:9a2016106dae | 143 | temp=sensor.readTemperature(); |
imi121 | 13:9a2016106dae | 144 | humid=sensor.readHumidity(); |
imi121 | 13:9a2016106dae | 145 | pc.printf("\r\nT: %d oC, H: %d\r\n",temp,humid); |
imi121 | 13:9a2016106dae | 146 | } |
imi121 | 13:9a2016106dae | 147 | else |
imi121 | 13:9a2016106dae | 148 | printf("\r\nErr %i \n",err); |
imi121 | 13:9a2016106dae | 149 | int err=http_demo(); |
imi121 | 13:9a2016106dae | 150 | if(err ==0) |
imi121 | 13:9a2016106dae | 151 | pc.printf("Thingspeak update completed successfully\r\n"); |
imi121 | 13:9a2016106dae | 152 | else |
imi121 | 13:9a2016106dae | 153 | pc.printf("Error occurred %d\r\n",err); |
imi121 | 13:9a2016106dae | 154 | /* buffer[0] = 0; |
imi121 | 13:9a2016106dae | 155 | sprintf(buffer, "%s?key=%s&field1=%s&field2=%d&field3=%d", thingSpeakUrl, thingSpeakKey, , (float)ppm,temp,humid); |
imi121 | 13:9a2016106dae | 156 | printf("Request to %s\r\n", buffer); |
imi121 | 13:9a2016106dae | 157 | |
imi121 | 13:9a2016106dae | 158 | HTTPText resp; |
imi121 | 13:9a2016106dae | 159 | HTTPResult res = http.get(buffer, &resp); |
imi121 | 13:9a2016106dae | 160 | if (res == HTTP_OK) |
imi121 | 13:9a2016106dae | 161 | { |
imi121 | 13:9a2016106dae | 162 | printf("Result :\"%s\"\r\n", resp.gets()); |
imi121 | 13:9a2016106dae | 163 | } |
imi121 | 13:9a2016106dae | 164 | else |
imi121 | 13:9a2016106dae | 165 | { |
imi121 | 13:9a2016106dae | 166 | printf("Error %d\r\n", res); |
imi121 | 13:9a2016106dae | 167 | } */ |
imi121 | 13:9a2016106dae | 168 | |
imi121 | 13:9a2016106dae | 169 | |
imi121 | 13:9a2016106dae | 170 | |
mridup | 0:dee849b0e6e6 | 171 | } |
imi121 | 13:9a2016106dae | 172 | |
imi121 | 13:9a2016106dae | 173 | |
imi121 | 13:9a2016106dae | 174 | } |
mridup | 0:dee849b0e6e6 | 175 |