Cayenne demo

Dependencies:   Cayenne-MQTT-mbed NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed Servo

Fork of Cayenne-X-NUCLEO-IDW01M1-TMP36 by myDevicesIoT

Files at this revision

API Documentation at this revision

Comitter:
rozendhyan
Date:
Tue Aug 29 07:58:37 2017 +0000
Parent:
5:294a8b1bca28
Child:
7:fed254c30643
Commit message:
work on BME280 , still use extend wifi board

Changed in this revision

BMP180/BME280.hpp Show annotated file Show diff for this revision Revisions of this file
TMP36.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BMP180/BME280.hpp	Tue Aug 29 07:58:37 2017 +0000
@@ -0,0 +1,67 @@
+#include "mbed.h"
+#define size 14
+Serial sensor(PA_11,PA_12);
+Serial pc(USBTX,USBRX);
+int AT[3] = {0xA5,0x52,0xF7};
+class BME280{
+public:
+    void judgement_data(char buf[]);
+    int Temp_read();
+
+private:
+    void receive();
+    void send_AT();
+    char buf[size];
+    int Lux,Temp,Preesure,Hum,Height;
+
+};
+void BME280::judgement_data(char buf[]){
+    int i=0;
+    if(buf[i++]== 0x5A){
+        //pc.printf("first_step\n");
+        if(buf[i++]== 0x5A){
+            //pc.printf("second_step\n");
+           //i++;//go to data type
+            if(buf[i] == 0x15){
+                //pc.printf("Lux type\n");
+                i++;//04
+                Lux = ((buf[i+1]<<24) | (buf[i+2]<<16) | (buf[i+3]<<8) | buf[i+4])/100;
+                //Lux = (0x00<<24) | (0x00<<16) | (0xFE<<8) | 0x40;//test
+                //pc.printf("Lux = %d\n", Lux);
+            }else if(buf[i] == 0x45){
+                //pc.printf("temp type\n");
+                i++;//0A
+                Temp = ((buf[i+1]<<8)|(buf[i+2]))/100;
+                Preesure = ((buf[i+3]<<24)|(buf[i+4]<<16)|(buf[i+5]<<8)|(buf[i+6]))/100;
+                Hum = ((buf[i+7]<<8)|(buf[i+8]))/100;
+                Height = ((buf[i+9]<<8)|(buf[i+10]));
+                //pc.printf("Temp = %d\n", Temp);
+                //pc.printf("Preesure = %d\n", Preesure);
+                //pc.printf("Hum = %d\n", Hum);
+                //pc.printf("Height = %d\n", Height);
+            }
+        }
+    }
+}
+void BME280::receive(){
+    for(int i=0;i<size;i++){
+        buf[i] = sensor.getc();  
+        //pc.putc(buf[i]);
+    }
+    
+}
+void BME280::send_AT(){
+    for(int i=0;i<3;i++){
+        sensor.putc(AT[i]);
+        //pc.putc(AT[i]);
+    }
+}
+int BME280::Temp_read(){
+    send_AT();
+    //wait(0.2);
+    receive();
+    //wait(0.2);
+    judgement_data(this->buf);
+    
+    return Temp;
+}
--- a/TMP36.lib	Fri Nov 11 18:31:11 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/zchen311/code/TMP36/#ab3d7d0c34ce
--- a/main.cpp	Fri Nov 11 18:31:11 2016 +0000
+++ b/main.cpp	Tue Aug 29 07:58:37 2017 +0000
@@ -7,16 +7,16 @@
 #include "CayenneMQTTClient.h"
 #include "MQTTNetworkIDW01M1.h"
 #include "SpwfInterface.h"
-#include "TMP36.h"
+#include "BME280.hpp"
 
 // WiFi network info.
-char* ssid = "ssid";
-char* wifiPassword = "wifiPassword";
+char* ssid = "megu megu fire";
+char* wifiPassword = "66666667";
 
 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
-char* username = "MQTT_USERNAME";
-char* password = "MQTT_PASSWORD";
-char* clientID = "CLIENT_ID";
+char* username = "1f764db0-122b-11e7-b235-9359079e690e";
+char* password = "0d4e9d94e7897df9af861007853ed3f906b0627d";
+char* clientID = "8ff46d80-8c81-11e7-a491-d751ec027e48";
 
 SpwfSAInterface interface(D8, D2); // TX, RX
 MQTTNetwork<SpwfSAInterface> network(interface);
@@ -136,12 +136,14 @@
 {
     // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
     MQTTTimer timer(5000);
-    TMP36 tmpSensor(A5);
+    //TMP36 tmpSensor(A5);
+    BME280 bmpSensor;
+    
     
     while (true) {
         // Yield to allow MQTT message processing.
         mqttClient.yield(1000);
-
+        //printf("%d\n",bmpSensor->Temp_read());
         // Check that we are still connected, if not, reconnect.
         if (!network.connected() || !mqttClient.connected()) {
             network.disconnect();
@@ -152,11 +154,11 @@
                 printf("Reconnect failed, retrying\n");
             }
         }
-
+        printf("before you post data,your temp is %d\n",bmpSensor.Temp_read());
         // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
         if (timer.expired()) {
             int error = 0;
-            if ((error = mqttClient.publishData(DATA_TOPIC, 5, TYPE_TEMPERATURE, UNIT_CELSIUS, tmpSensor.read())) != CAYENNE_SUCCESS) {
+            if ((error = mqttClient.publishData(DATA_TOPIC, 5, TYPE_TEMPERATURE, UNIT_CELSIUS,(float)bmpSensor.Temp_read())) != CAYENNE_SUCCESS) {
                 printf("Publish temperature failed, error: %d\n", error);
             }
             // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
@@ -173,7 +175,7 @@
     // Initialize the network interface.
     printf("Initializing interface\n");
     interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2);
-
+    printf("connect AP successful\n");
     // Set the default function that receives Cayenne messages.
     mqttClient.setDefaultMessageHandler(messageArrived);
 
@@ -187,9 +189,12 @@
     }
 
     if (mqttClient.connected())
+        printf("client disconnect\n");
         mqttClient.disconnect();
     if (network.connected())
+        printf("network disconnect\n");
         network.disconnect();
+    
 
     return 0;
 }