INSAT_MiniPRoject

Dependencies:   MQTT NDefLib NetworkSocketAPI Servo Light_Sensor_Nucleo X_NUCLEO_IDW01M1v2 mbed

Fork of IDW01M1_Cloud_IBM by ST

Revision:
25:9dab18979609
Parent:
24:a59eb8bdc431
--- a/main.cpp	Thu Nov 24 13:22:03 2016 +0000
+++ b/main.cpp	Tue Feb 20 19:24:38 2018 +0000
@@ -15,25 +15,50 @@
  */
 
 #include "mbed.h"
+
+
 #include "SpwfInterface.h"
+#include "XNucleo6180XA1.h"
 #include "TCPSocket.h"
 #include "MQTTClient.h"
 #include "MQTTWiFi.h"
+ #include "Servo/Servo.h"
+
 #include <ctype.h>
-#include "x_nucleo_iks01a1.h"
-#include "X_NUCLEO_NFC01A1.h"
-#include "NDefLib/NDefNfcTag.h"
+//#include "x_nucleo_iks01a1.h"
+//#include "X_NUCLEO_NFC01A1.h"
+//#include "NDefLib/NDefNfcTag.h"
 #include "NDefLib/RecordType/RecordURI.h"
 
+/* Definitions ---------------------------------------------------------------*/
+
+#define VL6180X_I2C_SDA   D14 
+#define VL6180X_I2C_SCL   D15 
+
+
+/* Variables -----------------------------------------------------------------*/
+
+static XNucleo6180XA1 *board = NULL;
+int status;
+    
+    
 //------------------------------------
 // Hyperterminal configuration
 // 9600 bauds, 8-bit data, no parity
 //------------------------------------
-Serial pc(SERIAL_TX, SERIAL_RX); 
+//Serial pc(SERIAL_TX, SERIAL_RX); 
 DigitalOut myled(LED1);
 bool quickstartMode = true;    
 
-#define ORG_QUICKSTART           // comment to connect to play.internetofthings.ibmcloud.com
+ Servo myservo(D11);
+
+
+
+ 
+
+ //float Moisture = 0 ;
+
+#define ORG_QUICKSTART           // comment to connect to play.internetofthings.ibmcloud.co
 //#define SUBSCRIBE              // uncomment to subscribe to broker msgs (not to be used with IBM broker) 
 #define X_NUCLEO_NFC01A1_PRESENT // uncomment to add NFC support
     
@@ -48,10 +73,10 @@
 #define AUTH_TOKEN ""
 #define DEFAULT_TYPE_NAME "iotsample-mbed-Nucleo"
 #else   // not def ORG_QUICKSTART
-#define ORG "play"             // connect to play.internetofthings.ibmcloud.com/ For a registered connection, replace with your org
-#define ID ""       // For a registered connection, replace with your id
+#define ORG ""             // connect to play.internetofthings.ibmcloud.com/ For a registered connection, replace with your org
+#define ID ""       // For a registered connection, replace with your id  0080E1B8D765
 #define AUTH_TOKEN ""// For a registered connection, replace with your auth-token
-#define DEFAULT_TYPE_NAME "sensor"
+#define DEFAULT_TYPE_NAME ""
 #endif
 #define TOPIC  "iot-2/evt/status/fmt/json" 
 
@@ -60,8 +85,8 @@
 #define MQTT_TLS_PORT 8883
 #define IBM_IOT_PORT MQTT_PORT
 // WiFi network credential
-#define SSID   ""  // Network must be visible otherwise it can't connect
-#define PASSW  ""
+#define SSID   "STM"  // Network must be visible otherwise it can't connect
+#define PASSW  "STMdemoPWD"
 #warning "Wifi SSID & password empty"
     
 char id[30] = ID;                 // mac without colons  
@@ -79,9 +104,9 @@
 int retryAttempt = 0;
 char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
 
-PressureSensor *pressure_sensor;
-HumiditySensor *humidity_sensor;
-TempSensor *temp_sensor1;
+
+int32_t axes[3];
+
 
 MQTT::Message message;
 MQTTString TopicName={TOPIC};
@@ -112,7 +137,7 @@
     // Construct clientId - d:org:type:id
     char clientId[strlen(org) + strlen(type) + strlen(id) + 5];  
     sprintf(clientId, "d:%s:%s:%s", org, type, id);  
-    sprintf(subscription_url, "%s.%s/#/device/%s/sensor/", org, "internetofthings.ibmcloud.com",id);
+    sprintf(subscription_url, "%s.%s/#/device/%s/light/", org, "internetofthings.ibmcloud.com",id);
 
     // Network debug statements 
     LOG("=====================================\n\r");
@@ -198,19 +223,46 @@
     }
 }
 
+
+    
 int publish(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTWiFi* ipstack)
 {
     MQTT::Message message;
     char* pubTopic = TOPIC;
-            
+              /* Creates the 6180XA1 expansion board singleton obj. */
+              
+
+
+
+    /* Initializes the 6180XA1 expansion board with default values. */
+    status = board->init_board(); 
+    
     char buf[MQTT_MAX_PAYLOAD_SIZE];
-    float temp, press, hum;
-    temp_sensor1->GetTemperature(&temp);
-    pressure_sensor->GetPressure(&press);
-    humidity_sensor->GetHumidity(&hum);
+   
+  uint32_t lux, dist;
+ 
+     board->sensor_top->get_distance(&dist);
+        board->sensor_top->get_lux(&lux);
+        
+
+if ((lux<40)&&(lux>10))
+  {   for(int i=5; i<160; i++) {
+             myservo = i/160.0;
+             wait(0.01);
+         }
+      }   
+     else    
+   if ((lux<180)&&(lux>150)) 
+     {    for(int i=5; i<170; i++) {
+             myservo = i/170.0;
+             wait(0.01);
+             }
+         }
+         
+//
     sprintf(buf,
-     "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Temp\":%0.4f,\"Pressure\":%0.4f,\"Humidity\":%0.4f}}",
-              temp, press, hum);
+     "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Distance\": %1.0d, \"Lux\": %1.0d}}",    
+              dist,lux);
     message.qos = MQTT::QOS0;
     message.retained = false;
     message.dup = false;
@@ -228,18 +280,18 @@
     const char * seckey = PASSW;
     SpwfSAInterface spwf(D8, D2, false);
     
+    DevI2C *device_i2c = new DevI2C(VL6180X_I2C_SDA, VL6180X_I2C_SCL);
+  board = XNucleo6180XA1::instance(device_i2c, A3, A0, D13, D0);
+
+
+ 
+    
 //    Timer tyeld;
     myled=0;
-    DevI2C *i2c = new DevI2C(I2C_SDA, I2C_SCL);
-    i2c->frequency(400000);    
+
     
-    X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(i2c);   
-    pressure_sensor = mems_expansion_board->pt_sensor;
-    temp_sensor1 = mems_expansion_board->ht_sensor;  
-    humidity_sensor = mems_expansion_board->ht_sensor;  
-    
-    pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");     
-    pc.printf("\r\nconnecting to AP\r\n");            
+    printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");     
+    printf("\r\nconnecting to AP\r\n");            
 
    quickstartMode=false;
    if (strcmp(org, "quickstart") == 0){quickstartMode = true;}
@@ -263,27 +315,7 @@
       while (true)
       wait(1.0); // Permanent failures - don't retry
    }
-#ifdef X_NUCLEO_NFC01A1_PRESENT      
-   // program NFC with broker URL        
-   X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(*i2c, NULL, X_NUCLEO_NFC01A1::DEFAULT_GPO_PIN, X_NUCLEO_NFC01A1::DEFAULT_RF_DISABLE_PIN, NC,NC,NC);  
-   NDefLib::NDefNfcTag& tag = nfcNucleo->getM24SR().getNDefTag();
-   printf("NFC Init done: !\r\n");
-   //open the i2c session with the nfc chip
-   if(tag.openSession()){
-       //create the NDef message and record
-       NDefLib::Message msg;
-       NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTPS, subscription_url);
-       msg.addRecord(&rUri);
-        //write the tag
-        if(tag.write(msg)){
-            printf("Tag writed \r\n");
-        }
-        //close the i2c session
-        if(!tag.closeSession()){
-            printf("Error Closing the session\r\n");
-        }
-    }else printf("Error open Session\r\n");             
-#endif    
+
    myled=1;         
    int count = 0;    
 //    tyeld.start();