mbed_wifi_MQTT_PM25

Dependencies:   mbed-os-example-wifi-DISCO_IOTBOARD_MQTT MQTT

Files at this revision

API Documentation at this revision

Comitter:
peng103617
Date:
Wed Dec 18 04:09:32 2019 +0000
Parent:
59:9bbcc1b368ba
Commit message:
MQTT_PM2.5

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os-example-wifi-DISCO_IOTBOARD_MQTT.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Apr 23 09:45:23 2019 +0000
+++ b/main.cpp	Wed Dec 18 04:09:32 2019 +0000
@@ -1,19 +1,3 @@
-/* WiFi Example
- * Copyright (c) 2018 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
 //MQTT+WIFI
 
 
@@ -50,9 +34,9 @@
 void messageArrived(MQTT::MessageData& md)
 {
     MQTT::Message &message = md.message;
-    logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
-    logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
-    ++arrivedcount;
+    //logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
+    //logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
+    //++arrivedcount;
 }
 
 
@@ -131,6 +115,7 @@
             size -= response;
             // Check if entire message was sent or not
             printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
+        
         }
     }
 
@@ -147,8 +132,89 @@
     socket.close();
 }
 
+
+Serial particle_sensor(PA_0,PA_1);//TX,RX
+Serial pc(USBTX,USBRX);
+
+float SDS018 ()
+{
+    uint8_t oneByte =0, myData[10],mCheck =0;
+    uint32_t samples_par = 1000;
+    int aux=0, t_init =0,i=0;
+    float PM2_5_A = 0, PM10_A = 0, PM2_5_sum = 0, PM10_sum = 0,PM2_5_avg = 0, PM10_avg = 0,PM2_5,PM10;
+    Timer t;
+    
+    time_t seconds = time(NULL);  
+    //printf("Unix Epoch time = %d\r\n", seconds);
+    //while(true){
+       // t.start();
+       // int time_start=t.read_ms();
+        //pc.printf("\nPARTICLE\n\r");
+        while (aux < samples_par) //while(t.read_ms()-t_init < time_avg)
+        {
+            if (aux==0)           
+            t_init = t.read_ms();
+            while (particle_sensor.readable()) { // whenever sensor transmits packet (every 1 s)
+                oneByte = particle_sensor.getc();   
+                if(oneByte == 0xAA) { 
+                    myData[0] =  oneByte;
+                    oneByte = particle_sensor.getc();
+                if(oneByte == 0xC0) { 
+                    myData[1] =  oneByte;
+                    mCheck = 0;
+                    for(i=0; i < 6; i++) { // DATA 1-6 received and crc calc
+                        myData[i+2] = particle_sensor.getc();
+                        //wait_ms(2);
+                        mCheck += myData[i+2];
+                        if(myData[6]==0x91)
+                       {
+                        //pc.printf("91\n\r");
+                        }
+                       if(myData[7]==0xA1)
+                       {
+                           //pc.printf("A1\n\r");
+                        }
+                    }
+                    myData[8] = particle_sensor.getc();
+                    myData[9] = particle_sensor.getc(); // tail (AB)
+                    if(mCheck == myData[8]) { // if checksum OK
+                        PM2_5 = ((uint16_t)myData[2] | (uint16_t)(myData[3]<<8));
+                        PM10 = ((uint16_t)myData[4] | (uint16_t)(myData[5]<<8));        
+                        PM2_5_A = (float) PM2_5/10;
+                        PM10_A = (float) PM10/10;       
+                        if(PM2_5_A*10 > 9999)
+                            PM2_5_A = 999.9;
+                        if(PM10_A*10 > 9999)
+                            PM10_A = 999.9;                     
+                        aux++;
+                        PM2_5_sum+=PM2_5_A;  // sum samples for future averaging
+                        
+                        PM10_sum+=PM10_A;  // sum samples for future averaging
+                        //printf("%3.2f,\t%3.2f\n\r", PM2_5_A, PM10_A); // CSV print:  PM2.5,  PM10
+                        //printf("PM2_5_A = %3.2f", PM2_5_A); //PM2.5
+                        return PM2_5_A;                   
+                        } 
+                    } // head2
+                } // head1 
+                                
+            } // while readable()
+           
+          } 
+}
+
+
+
+
+
+
 int main()
-{
+
+{    
+
+
+
+    pc.baud (115200) ;                       
+                     
     int count = 0;
 
     printf("WiFi example\n\n");
@@ -182,9 +248,9 @@
     
     // MQTT Example Start
     float version = 0.6;
-    char* topic = "test1";
+    char* topic = "test";
 
-    logMessage("HelloMQTT: version is %.2f\r\n", version);
+    //logMessage("HelloMQTT: version is %.2f\r\n", version);
 
     NetworkInterface* network = &wifi;
     if (!network) {
@@ -195,10 +261,14 @@
 
     MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
 
-    const char* hostname = "192.168.0.120";
+    const char* hostname = "172.20.10.3";
     int port = 1883;
     logMessage("Connecting to %s:%d\r\n", hostname, port);
+    
+    
+    
     int rc = mqttNetwork.connect(hostname, port);
+    
     if (rc != 0)
         logMessage("rc from TCP connect is %d\r\n", rc);
 
@@ -213,47 +283,34 @@
     if ((rc = client.subscribe(topic, MQTT::QOS2, messageArrived)) != 0)
         logMessage("rc from MQTT subscribe is %d\r\n", rc);
 
+
+
+
+
+
+  while(1)
+  {
+   
     MQTT::Message message;
-
     // QoS 0
-    char buf[100];
-    sprintf(buf, "Hello World!  QoS 0 message from app version %f\r\n", version);
-    message.qos = MQTT::QOS0;
+    
+    
+    char buf[100];       
+    printf("PM2.5 = %3.2f\r\n",SDS018 ());
+    sprintf(buf, "PM2.5 = %3.2f\r\n",SDS018 ());
+    
+       
+    message.qos = MQTT::QOS1;
     message.retained = false;
     message.dup = false;
     message.payload = (void*)buf;
     message.payloadlen = strlen(buf)+1;
     rc = client.publish(topic, message);
-    while (arrivedcount < 1)
-        client.yield(100);
-
-    // QoS 1
-    sprintf(buf, "Hello World!  QoS 1 message from app version %f\r\n", version);
-    message.qos = MQTT::QOS1;
-    message.payloadlen = strlen(buf)+1;
-    rc = client.publish(topic, message);
-    while (arrivedcount < 2)
-        client.yield(100);
-
-    // QoS 2
-    sprintf(buf, "Hello World!  QoS 2 message from app version %f\r\n", version);
-    message.qos = MQTT::QOS2;
-    message.payloadlen = strlen(buf)+1;
-    rc = client.publish(topic, message);
-    while (arrivedcount < 3)
-        client.yield(100);
-
-    if ((rc = client.unsubscribe(topic)) != 0)
-        logMessage("rc from unsubscribe was %d\r\n", rc);
-
-    if ((rc = client.disconnect()) != 0)
-        logMessage("rc from disconnect was %d\r\n", rc);
-
-    mqttNetwork.disconnect();
-
-    logMessage("Version %.2f: finish %d msgs\r\n", version, arrivedcount);
-
-    return 0;
+    client.yield(100);
+     
     
-    
+   }
+     
+      
+        
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os-example-wifi-DISCO_IOTBOARD_MQTT.lib	Wed Dec 18 04:09:32 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/cornetlin/code/mbed-os-example-wifi-DISCO_IOTBOARD_MQTT/#9bbcc1b368ba
--- a/mbed_app.json	Tue Apr 23 09:45:23 2019 +0000
+++ b/mbed_app.json	Wed Dec 18 04:09:32 2019 +0000
@@ -5,12 +5,12 @@
             "value": "internal"
         },
         "wifi-ssid": {
-            "help": "WiFi SSID",
-            "value": "\"note9\""
+            "help": "peng",
+            "value": "\"peng\""
         },
         "wifi-password": {
-            "help": "WiFi Password",
-            "value": "\"tp67651209\""
+            "help": "199685612",
+            "value": "\"199685612\""
         },
         "wifi-tx": {
             "help": "TX pin for serial connection to external device",