Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DHT11 NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
Fork of AirQuality_DHT11_Thingspeak_WiFi by
main.cpp
00001 /* SpwfInterface NetworkSocketAPI Example Program 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "SpwfInterface.h" 00019 //#include "NTPClient.h" 00020 //#include "HTTPClient.h" 00021 #include "TCPSocket.h" 00022 #include "DHT11.h" 00023 00024 00025 #define PORT 1234 00026 #define MAX_PENDING 1 00027 /*Thing speak settings */ 00028 //NTPClient ntp; 00029 //HTTPClient http; 00030 00031 //char* ntpServerUrl = "0.ca.pool.ntp.org"; 00032 #define IP "184.106.153.149" 00033 char* thingSpeakUrl = "http://api.thingspeak.com/update"; 00034 char* thingSpeakKey = "H1KAGWGPJBSHYFH0"; 00035 //------------------------------------ 00036 // Hyperterminal configuration 00037 // 9600 bauds, 8-bit data, no parity 00038 //------------------------------------ 00039 00040 Serial pc(USBTX, USBRX); 00041 DigitalOut myled(LED1); 00042 AnalogIn ain(A0); 00043 DHT11 sensor(PA_1); 00044 /*************************************/ 00045 00046 //NUCLEO: D8->UART1_TX (PA_9), D2->UART1_RX (PA_10) 00047 SpwfSAInterface spwf(D8, D2, false); 00048 const double Rl = 10000.0; // Rl (Ohm) - Load resistance 00049 const double Vadc_5 = 0.0048828125; // ADC step 5V/1024 4,88mV (10bit ADC) 00050 00051 //const double Vadc_33 = 0.0032226562; // ADC step 3,3V/1024 3,22mV (10bit ADC) 00052 double Vrl; // Output voltage 00053 double Rs; // Rs (Ohm) - Sensor resistance 00054 double ppm; // ppm 00055 double ratio; 00056 int testvalue; 00057 int humid; 00058 00059 //Calculation of PPM 00060 /*void calculatePPM() 00061 { 00062 double lgPPM; 00063 Vrl = (double)ain*Vadc_5; // For 5V Vcc use Vadc_5 00064 Rs = (5 - Vrl)/Vrl; // Calculate sensor resistance 00065 ratio = Rs/Rl; // Calculate ratio 00066 lgPPM = (log10(ratio) * -0.8)+ 0.9; // Calculate ppm 00067 ppm = pow(10,lgPPM); // Calculate ppm 00068 pc.printf("The ppm value is %lf \n",ppm); 00069 pc.printf("The raw Rs is %2.3lf\n",ratio); 00070 pc.printf("The raw voltage is %2.3lf \n",Vrl); 00071 } 00072 */ 00073 int http_demo(void) 00074 { 00075 TCPSocket socket(&spwf); 00076 char buffer[256]; 00077 char message[40]; 00078 int err; 00079 printf("Sending HTTP Data to thingspeak...\r\n"); 00080 00081 // Open a socket on the network interface, and create a TCP connection to thingspeak 00082 //socket.open(&spwf); 00083 err=socket.connect(IP,80); // connecting to thingspeak 00084 if(err!=0) 00085 { 00086 pc.printf("\r\nCould not connect to Socket, err = %d!!\r\n", err); 00087 return -1; 00088 } else pc.printf("\r\nconnected to host server\r\n"); 00089 // Send a simple http request 00090 buffer[0] = 0; 00091 //sprintf(buffer, "GET https://api.thingspeak.com/update?api_key=SBYDOEXE50MNOBSI&field1=%f&field2=%d&field3=%d",(float)ppm,temp,humid);//thingSpeakUrl,thingSpeakKey, 00092 sprintf(message,"field1=%d",testvalue); 00093 printf("Message Length=%d\r\n",(int)strlen(message)); 00094 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); 00095 pc.printf("Request to %s\r\n", buffer); 00096 //char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n"; 00097 int scount = socket.send(buffer, (int)strlen(buffer)); 00098 printf("sent %d [%.*s]\r\n", scount, strstr(buffer, "\r\n")-buffer, buffer); 00099 00100 // Recieve a simple http response and print out the response line 00101 char rbuffer[64]; 00102 int rcount = socket.recv(rbuffer, sizeof rbuffer); 00103 printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); 00104 00105 // Close the socket to return its memory and bring down the network interface 00106 socket.close(); 00107 return 0; 00108 } 00109 00110 int main() 00111 { 00112 int err; 00113 char * ssid = "18-2F"; 00114 char * seckey = "062432779"; 00115 //char buffer[255; 00116 //time_t ctTime; 00117 //Host server(IpAddr(), 123, ntpServerUrl); 00118 //ntp.setTime(server); 00119 pc.printf("\r\nAir Quality and Environment monitoring Application\r\n"); 00120 pc.printf("\r\nconnecting to AP\r\n"); 00121 00122 if(spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2)) { 00123 pc.printf("\r\nnow connected\r\n"); 00124 } else { 00125 pc.printf("\r\nerror connecting to AP.\r\n"); 00126 return -1; 00127 } 00128 00129 const char *ip = spwf.get_ip_address(); 00130 const char *mac = spwf.get_mac_address(); 00131 00132 pc.printf("\r\nIP Address is: %s\r\n", (ip) ? ip : "No IP"); 00133 pc.printf("\r\nMAC Address is: %s\r\n", (mac) ? mac : "No MAC"); 00134 pc.printf("\r\nInternet is already\r\n"); 00135 while(1) 00136 { 00137 wait(20); 00138 testvalue++; 00139 /*myled = !myled; 00140 //calculatePPM(); 00141 // err = sensor.readData(); 00142 // if (err ==DHT11::OK) 00143 // { 00144 // temp=sensor.readTemperature(); 00145 // humid=sensor.readHumidity(); 00146 // pc.printf("\r\nT: %d oC, H: %d\r\n",temp,humid); 00147 // } 00148 // else 00149 // printf("\r\nErr %i \n",err); 00150 // int err=http_demo(); 00151 // if(err ==0) 00152 // pc.printf("Thingspeak update completed successfully\r\n"); 00153 // else 00154 pc.printf("Error occurred %d\r\n",err);*/ 00155 http_demo(); 00156 // buffer[0] = 0; 00157 printf("%s?key=%s&field1=%d\r\n", thingSpeakUrl, thingSpeakKey, testvalue); 00158 printf("Request to %s\r\n", thingSpeakUrl); 00159 00160 // HTTPText resp; 00161 // HTTPResult res = http.get(buffer, &resp); 00162 // if (res == HTTP_OK) 00163 // { 00164 // printf("Result :\"%s\"\r\n", resp.gets()); 00165 // } 00166 // else 00167 // { 00168 // printf("Error %d\r\n", res); 00169 // } 00170 00171 00172 00173 } 00174 00175 00176 } 00177
Generated on Sat Jul 16 2022 20:06:46 by
1.7.2
