Thingspeak IoT using Nucleo-F411RE and IDW01m1

Dependencies:   mbed X_NUCLEO_IDW01M1v2 NetworkSocketAPI

Revision:
14:753cc5807f05
Parent:
13:9a2016106dae
--- a/main.cpp	Sat May 06 17:39:25 2017 +0000
+++ b/main.cpp	Thu Nov 14 04:21:20 2019 +0000
@@ -16,22 +16,14 @@
 
 #include "mbed.h"
 #include "SpwfInterface.h"
-//#include "NTPClient.h"
-//#include "HTTPClient.h"
 #include "TCPSocket.h"
-#include "DHT11.h"
-
 
 #define PORT 1234
 #define MAX_PENDING 1
-/*Thing speak settings */
-//NTPClient ntp;
-//HTTPClient http;
  
-//char* ntpServerUrl = "0.ca.pool.ntp.org";
 #define IP "184.106.153.149" 
 char* thingSpeakUrl = "http://api.thingspeak.com/update";
-char* thingSpeakKey = "SBYDOEXE50MNOBSI";
+char* thingSpeakKey = "HJM51JXEXCLTX2KQ";
 //------------------------------------
 // Hyperterminal configuration
 // 9600 bauds, 8-bit data, no parity
@@ -39,36 +31,12 @@
 
 Serial pc(USBTX, USBRX);
 DigitalOut myled(LED1);
-AnalogIn   ain(A0);
-DHT11 sensor(PA_1);
+
 /*************************************/
 
-//NUCLEO: D8->UART1_TX (PA_9), D2->UART1_RX (PA_10)
 SpwfSAInterface spwf(D8, D2, false);
-const double Rl      = 10000.0;               // Rl (Ohm) - Load resistance
-const double Vadc_5  = 0.0048828125;         // ADC step 5V/1024 4,88mV (10bit ADC)
-
-//const double Vadc_33 = 0.0032226562;         // ADC step 3,3V/1024 3,22mV (10bit ADC)
-double Vrl;                                  // Output voltage
-double Rs;                                   // Rs (Ohm) - Sensor resistance
-double ppm;                                  // ppm
-double ratio;
-int temp;
-int humid;   
-
-//Calculation of PPM
-void calculatePPM() 
-{
-  double lgPPM;
-  Vrl = (double)ain*Vadc_5;             // For 5V Vcc use Vadc_5
-  Rs = (5 - Vrl)/Vrl;                   // Calculate sensor resistance
-  ratio = Rs/Rl;                             // Calculate ratio
-  lgPPM = (log10(ratio) * -0.8)+ 0.9;        // Calculate ppm
-  ppm = pow(10,lgPPM);                       // Calculate ppm
-  pc.printf("The ppm value is %lf \n",ppm);
-  pc.printf("The raw Rs is %2.3lf\n",ratio);
-  pc.printf("The raw voltage is %2.3lf \n",Vrl);
-}
+int temp=5;
+int humid=1;
 
 int http_demo(void)
 {
@@ -81,15 +49,15 @@
     // Open a socket on the network interface, and create a TCP connection to thingspeak
     //socket.open(&spwf);
     err=socket.connect(IP,80); // connecting to thingspeak
-    if(err!=0) 
-    {
-      pc.printf("\r\nCould not connect to Socket, err = %d!!\r\n", err); 
-      return -1;
-    } else pc.printf("\r\nconnected to host server\r\n"); 
+    if(err!=0) {
+        pc.printf("\r\nCould not connect to Socket, err = %d!!\r\n", err); 
+        return -1;
+        } 
+    else 
+        pc.printf("\r\nconnected to host server\r\n"); 
+    
     // Send a simple http request
-    //buffer[0] = 0;
-    //sprintf(buffer, "GET https://api.thingspeak.com/update?api_key=SBYDOEXE50MNOBSI&field1=%f&field2=%d&field3=%d",(float)ppm,temp,humid);//thingSpeakUrl,thingSpeakKey,
-    sprintf(message,"field1=%f&field2=%d&field3=%d",ppm,temp,humid);
+    sprintf(message,"field1=%d&field2=%d",temp,humid);
     printf("Message Length=%d\r\n",(int)strlen(message));
     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);
     pc.printf("Request to %s\r\n", buffer);
@@ -110,13 +78,9 @@
 int main() 
 {
     int err;    
-    char * ssid = "VM957191-2G";
-    char * seckey = "krzvzkcw";  
-    //char buffer[255];
-    //time_t ctTime;
-    //Host server(IpAddr(), 123, ntpServerUrl);
-    //ntp.setTime(server);
-    pc.printf("\r\nAir Quality and Environment monitoring Application\r\n");     
+    char * ssid = "iPhone";
+    char * seckey = "abcd1234";  
+    pc.printf("\r\nNucleo F411RE + WiFi + Thingspeak\r\n");     
     pc.printf("\r\nconnecting to AP\r\n");
             
     if(spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2)) {      
@@ -134,42 +98,14 @@
     
     while(1) 
     { 
-      wait(20);
-      myled = !myled;
-      calculatePPM();
-      err = sensor.readData();
-        if (err ==DHT11::OK)
-        {
-         temp=sensor.readTemperature();
-         humid=sensor.readHumidity();
-         pc.printf("\r\nT: %d oC, H: %d\r\n",temp,humid);   
-        }
-         else
-         printf("\r\nErr %i \n",err);
-         int err=http_demo();
-         if(err ==0)
-         pc.printf("Thingspeak update completed successfully\r\n");
-         else
-         pc.printf("Error occurred %d\r\n",err);
-       /* buffer[0] = 0;
-        sprintf(buffer, "%s?key=%s&field1=%s&field2=%d&field3=%d", thingSpeakUrl, thingSpeakKey, , (float)ppm,temp,humid);
-        printf("Request to %s\r\n", buffer);
-        
-        HTTPText resp;
-        HTTPResult res = http.get(buffer, &resp);
-        if (res == HTTP_OK)
-        {
-            printf("Result :\"%s\"\r\n", resp.gets());
-        }
+        wait(20);
+        myled = !myled;
+   
+        int err=http_demo();
+        if(err==0)
+            pc.printf("Thingspeak update completed successfully\r\n");
         else
-        {
-            printf("Error %d\r\n", res);
-        } */        
-             
-            
-      
+            pc.printf("Error occurred %d\r\n",err); 
     }
-    
- 
 }