This the the improved and updated project and program for the earlier automatic pill dispenser project. Includes and rfid, stepper, servo, led-ldr and a load sensor integrate. time stamp is sent to the cloud and saved..

Dependencies:   EthernetInterface FXAS21002 FXOS8700 M2XStreamClient MFRC522 Servo jsonlite mbed-rtos mbed

Fork of Accel_Mag_Gyro_SensorStream_K64F_AGM01_M2X by AT&T Developer Summit Hackathon 2016

Revision:
2:104683639374
Parent:
1:5e587e213940
--- a/main.cpp	Tue Apr 25 23:41:37 2017 +0000
+++ b/main.cpp	Thu Apr 26 17:52:37 2018 +0000
@@ -1,103 +1,179 @@
-/*
- * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
- * Copyright 2016-2017 NXP
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- * o Redistributions of source code must retain the above copyright notice, this list
- *   of conditions and the following disclaimer.
- *
- * o Redistributions in binary form must reproduce the above copyright notice, this
- *   list of conditions and the following disclaimer in the documentation and/or
- *   other materials provided with the distribution.
- *
- * o Neither the name of the copyright holder nor the names of its
- *   contributors may be used to endorse or promote products derived from this
- *   software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-#include "FXAS21002.h"
-#include "FXOS8700.h"  
+/////////Automated Pill Dispenser/////////////
 #include "mbed.h"
 #include "M2XStreamClient.h"
 #include "EthernetInterface.h"
-
-// Initialize Serial port
-Serial pc(USBTX, USBRX);
+#include "MFRC522.h"
+#include "Servo.h"
 
-// Initialize pins for I2C communication for sensors. Set jumpers J6,J7 in FRDM-STBC-AGM01 board accordingly.
-FXOS8700 accel(D14,D15);
-FXOS8700 mag(D14,D15);
-FXAS21002 gyro(D14,D15);
+#define SPI_SCLK    D13
+#define SPI_MISO    D12
+#define SPI_MOSI    D11 //pints for the RFID reader
+#define SPI_CS      D10
+#define MF_RESET    D9
+ 
+MFRC522   RfChip(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET); //for the RFID chip
+DigitalOut LedRed   (LED_RED); // LED Red
+DigitalOut LedGreen (LED_GREEN); // LED green
+DigitalOut LedBlue  (LED_BLUE); // LED blue
+
+DigitalOut pin1(D4); //output pins for the stepper motor
+DigitalOut pin2(D5);
+DigitalOut pin3(D6);
+DigitalOut pin4(D7);
+
+AnalogIn LDRpin(A0); //analog output pin for the LDR
+
+AnalogIn gp1(A2); //Pins for the weight sensor
+AnalogIn gp2(A3);
+
+Serial pc(USBTX, USBRX);
+Servo LRmyservo(PTC5);
 
 // Set Sensor Stream details
-char deviceId[] = "8b34bc421abf15b7ec6471fa19513a98"; // Device you want to push to
-char streamAcc[] = "acc_rms"; // Stream you want to push to
-char streamMag[] = "mag_rms"; // Stream you want to push to
-char streamGyr[] = "gyr_rms"; // Stream you want to push to
-char m2xKey[] = "737018ea33de7760ab346c85ae2d9d27"; // Your M2X API Key or Master API Key
+char deviceId[] = "8b23ea86abb3aa26711d4d6b92cbfe12"; // Device you want to push to
+char streamLdr[] = "ldr_data"; // Stream you want to push to
+char streamLoad[] = "load_data"; // Stream you want to push to
+char m2xKey[] = "e952c10fdf5d62986f9e229564c92c83"; // Your M2X API Key or Master API Key
 
-int main()
+//Sequence for the stepper motor
+int stepSequence[8][4] = 
 {
+  {0,0,0,1},
+  {0,0,1,1},
+  {0,0,1,0},
+  {0,1,1,0},
+  {0,1,0,0},
+  {1,1,0,0},
+  {1,0,0,0},
+  {1,0,0,1},
+};
+
+int main(){
+    wait_ms(3000);
+    //turn all LEDs off
+    LedRed   = 1;
+    LedGreen = 1;
+    LedBlue = 1;
+    
     // Intialize Ethernet connection
-EthernetInterface eth;
-eth.init();
-eth.connect();
-printf("Success. Connected!. Device IP Address is %s\r\n", eth.getIPAddress());
-
- // Initialize the M2X client
-Client client;
-M2XStreamClient m2xClient(&client, m2xKey);
-int ret;
-
-
-    // Configure Accelerometer FXOS8700, Magnetometer FXOS8700 & Gyroscope FXAS21002
-    accel.accel_config();
-    mag.mag_config();
-    gyro.gyro_config();
+    EthernetInterface eth;
+    eth.init();
+    eth.connect();
+    printf("Success. Connected!. Device IP Address is %s\r\n", eth.getIPAddress());
+    // Initialize the M2X client
+    Client client;
+    M2XStreamClient m2xClient(&client, m2xKey);
+    int ret;
+   
+    //float ldr_time[3];
+    float ldr_data=0.0;
+    //float load_time[3];  
+    float load_data=0.0;
+    
+    RfChip.PCD_Init(); // initialize RDID chip
+    
+    while (LedRed == 1) {  
+        //code for stepper motor
+        int count_third = 0;
+        for(int j = 0; j <= 512; j++){
+            if (count_third%3 == 0){  // rotate a third of a whole revolution
+                for (int i = 0; i < 8; i++){
+                    pin1 = stepSequence[i][0];
+                    pin2 = stepSequence[i][1];
+                    pin3 = stepSequence[i][2];
+                    pin4 = stepSequence[i][3];
+                    wait_ms(2);
+               }
+            }
+            count_third++;
+        }
+        LedRed   = 1;
+        LedGreen = 1;
+        LedBlue = 1;
+        wait_ms(500);
+        
+        //code for RFID reader
+        for ( int k = 0; k < 40; k++){
+            if ( ! RfChip.PICC_IsNewCardPresent()){
+                wait_ms(50);
+                continue;
+            }
+            LedRed   = 0;
 
-    float accel_data[3]; float accel_rms=0.0;
-    float mag_data[3];   float mag_rms=0.0;
-    float gyro_data[3];  float gyro_rms=0.0;
-       
-    printf("Begin Data Acquisition from FXOS8700 and FXAS21002....\r\n\r\n");
-    wait(0.5);
+            // Select one of the cards
+            if ( ! RfChip.PICC_ReadCardSerial()){
+                wait_ms(50);
+                continue;
+            }
+            // Print Card type
+            uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
+            wait_ms(100);
+        }
+    }
+    /////Servo Time!!!!!!/////
+    float range = .0009;
+    LRmyservo.calibrate(range,45);
+    bool stop_Servo = false;
+    bool pill_Detected = false;
+    while (stop_Servo == false) {
+        LRmyservo = 1.45;
+        wait_ms(400);
+        LRmyservo = 0.45;
+        wait_ms(4000);
+        
+        for (int m = 0; m < 20; m++){
+            float x = LDRpin * 1000;
+            if (x < 140){
+                pill_Detected = true;
+            }
+        }
+        float x = LDRpin * 1000;
+        if (pill_Detected == true && x > 200){
+            LedBlue = 0;
+            wait_ms(500);
+            stop_Servo = true;
+            ldr_data = 111;
+        }
+    }
+    ret = m2xClient.updateStreamValue(deviceId, streamLdr, ldr_data);
+    printf("send() returned %d\r\n", ret);
+    ldr_data = 0;
+    ret = m2xClient.updateStreamValue(deviceId, streamLdr, ldr_data);
+    printf("send() returned %d\r\n", ret);
+
     
-    while(1)
-    {
-      accel.acquire_accel_data_g(accel_data);
-      accel_rms = sqrt(((accel_data[0]*accel_data[0])+(accel_data[1]*accel_data[1])+(accel_data[2]*accel_data[2]))/3);
-      printf("%4.2f,\t%4.2f,\t%4.2f,\t",accel_data[0],accel_data[1],accel_data[2]);
-      wait(0.01);
-      
-      mag.acquire_mag_data_uT(mag_data);
-      printf("%4.2f,\t%4.2f,\t%4.2f,\t",mag_data[0],mag_data[1],mag_data[2]);
-      mag_rms = sqrt(((mag_data[0]*mag_data[0])+(mag_data[1]*mag_data[1])+(mag_data[2]*mag_data[2]))/3);
-      wait(0.01);
-      
-      gyro.acquire_gyro_data_dps(gyro_data);
-      printf("%4.2f,\t%4.2f,\t%4.2f\r\n",gyro_data[0],gyro_data[1],gyro_data[2]);
-      gyro_rms = sqrt(((gyro_data[0]*gyro_data[0])+(gyro_data[1]*gyro_data[1])+(gyro_data[2]*gyro_data[2]))/3);
-      wait(0.01);
-      
-      ret = m2xClient.updateStreamValue(deviceId, streamAcc, accel_rms);
-      printf("send() returned %d\r\n", ret);
-      ret = m2xClient.updateStreamValue(deviceId, streamMag, mag_rms);
-      printf("send() returned %d\r\n", ret);
-      ret = m2xClient.updateStreamValue(deviceId, streamGyr, gyro_rms);
-      printf("send() returned %d\r\n", ret);
-      wait(1);
+    //Weight Sensor!!!!!!//
+    //Calibrate Sensor
+//    float sum = 0;
+//    for (int i = 0; i< 1000; i ++){
+//        wait_ms(50);
+//        sum = (gp2) *10000 + sum;
+//    }
+    
+    float ave = 6877.67;
+//    float ave = sum/1000;
+    printf("the average is : %0.5f \r\n", ave);
+    bool stop_load = false;
+    while (stop_load == false) {
+        float weightOut1 = gp1*10000;
+        float weightOut2 = gp2*10000;
+        float difference = weightOut2-ave;
+        wait_ms(1000);
+        if (difference > 15){
+            printf("I made it here");
+            bool stop_load = true;
+            break;
+        }
     }
-      
-}
\ No newline at end of file
+    LedRed   = 1;
+    LedGreen = 0;
+    LedBlue = 1;
+         
+    //mag.acquire_mag_data_uT(mag_data);
+    load_data = 0;
+    ret = m2xClient.updateStreamValue(deviceId, streamLoad, load_data);
+    load_data = 222;
+    ret = m2xClient.updateStreamValue(deviceId, streamLoad, load_data);
+    load_data = 0;
+    ret = m2xClient.updateStreamValue(deviceId, streamLoad, load_data);
+}   
\ No newline at end of file