System to manage inventory for a cooler. Allows real time check in of products via RFID, as well as check out. Also includes a scale to weigh products upon check out, and has a real time temperature tracking system that sounds an alert if the cooler gets too hot.

Dependencies:   DHT11 HX711 MFRC522 SDFileSystemEditied mbed

Files at this revision

API Documentation at this revision

Comitter:
vincentrc
Date:
Wed Mar 07 17:52:23 2018 +0000
Commit message:
Initial commit.

Changed in this revision

DHT11.lib Show annotated file Show diff for this revision Revisions of this file
HX711.lib Show annotated file Show diff for this revision Revisions of this file
MFRC522.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
functions.cpp Show annotated file Show diff for this revision Revisions of this file
functions.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
product.h Show annotated file Show diff for this revision Revisions of this file
scale.cpp Show annotated file Show diff for this revision Revisions of this file
scale.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DHT11.lib	Wed Mar 07 17:52:23 2018 +0000
@@ -0,0 +1,1 @@
+http://os.mbed.com/users/fossum_13/code/DHT11/#5da6f6de3e42
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HX711.lib	Wed Mar 07 17:52:23 2018 +0000
@@ -0,0 +1,1 @@
+http://os.mbed.com/users/padte/code/HX711/#a2666eae3d06
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MFRC522.lib	Wed Mar 07 17:52:23 2018 +0000
@@ -0,0 +1,1 @@
+http://os.mbed.com/users/AtomX/code/MFRC522/#63d729186747
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Wed Mar 07 17:52:23 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/vincentrc/code/SDFileSystemEditied/#9d37b27e3fb2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/functions.cpp	Wed Mar 07 17:52:23 2018 +0000
@@ -0,0 +1,520 @@
+#include <stdio.h>
+#include <iostream>
+#include <stdlib.h>
+#include <string.h>
+#include <string>
+#include "functions.h"
+#include "product.h"
+#include "HX711.h"
+#include "Dht11.h"
+
+Dht11 sensor(D7);
+float maxtemp = 80;
+DigitalOut buzzer(D2);
+DigitalOut LED(PTB22);
+
+volatile float Farenheit;
+volatile int Humidity;
+extern int id[10];
+extern int flag1;
+extern int flag2;
+
+//Adds a new product type to the database
+struct product *addproduct(struct product *head)
+{
+    struct product *newp = (struct product*)malloc(sizeof( struct product));
+    char name[20];
+    unsigned int pid1, pid2, pid3, pid4;
+    float weight, priceper;
+
+    fflush(stdin);
+
+    printf("\n\rName:\n\r");
+    scanf("%20s", &name);
+    fflush(stdin);
+    printf("\n\rpid(1):\n\r");
+    scanf("%d", &pid1);
+    printf("\n\rpid(2):\n\r");
+    scanf("%d", &pid2);
+    printf("\n\rpid(3):\n\r");
+    scanf("%d", &pid3);
+    printf("\n\rpid(4):\n\r");
+    scanf("%d", &pid4);
+    printf("\n\rPriceper:\n\r");
+    scanf("%f", &priceper);
+
+    strcpy(newp->pname, name);
+    newp->pid[0] = pid1;
+    newp->pid[1] = pid2;
+    newp->pid[2] = pid3;
+    newp->pid[3] = pid4;
+    newp->count = 1;
+    newp->priceper = priceper;
+
+
+    newp->next = head;
+    head = newp;
+
+    std::cout << "\n\r\n\r";
+    
+    return head;
+}
+
+//Sorts the list by product name alphabetical order
+struct product *sortlist(struct product *head)
+{
+    struct product *temp, *ptr, *prev;
+    int x, check;
+
+    while(1)
+    {
+        check = 0;
+        ptr = head;
+        prev = head;
+
+        while((ptr->next != NULL) && (ptr != NULL))
+        {
+            if(strcmp(ptr->pname, ptr->next->pname) > 0)
+            {
+                if(ptr == head)
+                {
+                    temp = ptr->next->next;
+                    ptr->next->next = ptr;
+                    head = ptr->next;
+                    ptr->next = temp;
+                    check = 1;
+                    
+                }
+
+                else
+                {
+                    temp = ptr->next->next;
+                    prev->next = ptr->next;
+                    ptr->next->next = ptr;
+                    ptr->next = temp;
+                    check = 1;
+                }
+            }
+
+            if(ptr->next == NULL)
+                break;
+            else
+            {
+                prev = ptr;
+                ptr = ptr->next;
+            }
+        }
+
+        //Breaks if no swap occured
+        if(check == 0)
+            break;
+    }
+
+    return head;
+}
+
+//Displays the inventory list to the user
+struct product* displaylist(struct product *head)
+{
+    struct product *ptr;
+
+    if(head == NULL)
+    {
+        printf("\n\r\n\rNothing in stock!\n\r\n\r");
+        return head;
+    }
+
+    head = sortlist(head);
+
+    ptr = head;
+
+    while(ptr != NULL)
+    {
+        //Doesn't print out of stock items
+        if(ptr->count <= 0)
+        {
+            ptr = ptr->next;
+            continue;
+        }
+            
+        //Clears input buffer
+        fflush(stdin);
+
+        printf("\n\r%15s:    ID: %4d%4d%4d%4d    Price/lb: %6.2f    In Stock: %d\n\r", ptr->pname, ptr->pid[0], ptr->pid[1], 
+        ptr->pid[2], ptr->pid[3], ptr->priceper, ptr->count);
+        ptr = ptr->next;
+    }
+
+    printf("\n\r\n\r");
+    return head;
+}
+
+//Frees the memory allocated for the inventory list
+void freelist(struct product *head)
+{
+    struct product *ptr, *temp;
+
+    ptr = head;
+
+    while(ptr != NULL)
+    {
+        temp = ptr;
+        ptr = ptr->next;
+        free(temp);
+    }
+}
+
+//Saves the inventory list to a csv file
+void savelist(struct product *head)
+{
+    FILE *fp;
+    struct product *ptr;
+
+    ptr = head;
+
+    //Open file pointer to output file
+    if((fp = fopen("/sd/inventory.txt", "w")) == NULL)
+        perror("Error");
+
+    while(ptr != NULL)
+    {
+        //Outputs attributes
+        fprintf(fp, "%s,%d,%d,%d,%d,%d,%.3f,\n", ptr->pname,
+            ptr->pid[0], ptr->pid[1], ptr->pid[2], ptr->pid[3], ptr->count, ptr->priceper);
+
+        ptr = ptr->next;
+    }
+
+    fclose(fp);
+}
+
+//Loads the inventory list from the csv file 
+struct product* loadlist()
+{
+    FILE *fp;
+    char line[512], *token;
+    int x, flag = 0;
+    struct product *newprod, *head, *prev;
+    
+
+    //Opens file pointer to input file
+    if((fp = fopen("/sd/inventory.txt", "r")) == NULL)
+    {
+        printf("\n\r\n\rInventory is empty:\n\r\n\r");
+        return NULL;
+    }
+
+    
+    //Reads in next line of file
+    while(fgets(line, 512, fp) != NULL)
+    {
+        //Breaks when end of file is reached
+        if(feof(fp))
+            break;
+
+        //Allocates memory for the product
+        newprod = (struct product*)malloc(sizeof(struct product));
+
+        //Adds the product to the list in order (flag is 0 for the first product only)
+        if(flag == 0)
+            head = newprod;
+        else
+            prev->next = newprod;
+
+        //Moves prev up
+        prev = newprod;
+        newprod->next = NULL;
+
+        //Breaks csv line into tokens with comma as the delimiter 
+        token = strtok(line, ",");
+
+        //Iterates once for each attribute in the product structure
+        for(x=0;x<7;x++)
+        {
+            if(x == 0)
+                strcpy(newprod->pname, token);
+
+            else if(x == 1)
+                newprod->pid[0] = atoi(token);
+                
+            else if(x == 2)
+                newprod->pid[1] = atoi(token);
+                
+            else if(x == 3)
+                newprod->pid[2] = atoi(token);    
+                
+            else if(x == 4)
+                newprod->pid[3] = atoi(token); 
+                
+            else if(x == 5)
+                newprod->count = atoi(token);
+            
+            else
+                newprod->priceper = atof(token);
+
+            //Goes to next token
+            token = strtok(NULL, ",");
+        }
+
+        //Sets flag so it's 1 for all iterations except the first
+        flag = 1;
+
+    }
+
+    fclose(fp);
+    return head;
+}
+
+//Finds a product by its name and returns it or NULL if not found
+struct product* findbyname(char name[50], struct product *head)
+{
+    struct product *ptr;
+    
+    ptr = head;
+
+    while(ptr != NULL)
+    {
+        if(strcmp(ptr->pname, name) == 0)
+            return ptr;
+
+        ptr = ptr->next;
+    }
+
+    return NULL;
+
+}
+
+struct product* findbyid(int id[10], struct product *head)
+{
+    struct product *ptr;
+    int x;
+    
+    ptr = head;
+
+    while(ptr != NULL)
+    {
+        for(x=0;x<4;x++)
+        {
+            if(id[x] != ptr->pid[x])
+                break;
+                
+            if(x == 3)
+                return ptr;
+        }
+        
+
+        ptr = ptr->next;
+    }
+
+    return NULL;
+}
+
+//Checks in an item of given name by incrementing that product's count attribute
+struct product* checkin(struct product *head)
+{
+    struct product *ptr;
+
+    char entry[20];
+
+        printf("\n\r\n\rPlease enter product name:\n\r\n\r");
+
+        fflush(stdin);
+        scanf("%20s", &entry);
+        fflush(stdin);
+        if((ptr = findbyname(entry, head)) == NULL)
+            printf("\n\r\n\rNo such product!\n\r\n\r");
+        else
+            ptr->count++;
+
+        return head;
+}
+
+//Checks a product out of inventory, and gives a price 
+struct product* checkout(struct product *head, HX711 scale1)
+{
+    struct product *ptr;
+    char entry[20];
+    float weight = 3.5, price;
+    int x, y;
+ 
+
+    printf("\n\r\n\rPress:\n\r1:  rfid\n\r2:  manual\n\r\n\r");
+    
+    scanf("%20s", &entry);
+
+    if(strcmp("1", entry) == 0)
+    {
+        printf("\n\r\n\rPlease scan rfid:\n\r\n\r");
+        
+        flag1 = 0;
+        flag2 = 0;
+    
+        //Waits for rfid interrupt
+        while(flag2 == 0)
+        {
+            wait(0.5);
+            continue;
+        }
+        
+        if((ptr = findbyid(id, head)) == NULL)
+        {
+            printf("\n\r\n\rNo such item!\n\r\n\r");
+            flag1 = 1;
+            return head;
+        }
+
+        else if(ptr->count <= 0)
+        {
+            printf("\n\r\n\rOut of stock!\n\r\n\r");
+            flag1 = 1;
+            return head;        
+        }
+        
+    }
+    
+    else if(strcmp("2", entry) == 0)
+    {
+        printf("\n\r\n\rPlease enter product name:\n\r\n\r");
+        fflush(stdin);
+        scanf("%20s", &entry);
+        fflush(stdin);
+    
+
+        if((ptr = findbyname(entry, head)) == NULL)
+        {
+            printf("\n\r\n\rNo such item!\n\r\n\r");
+            return head;
+        }
+
+        else if(ptr->count <= 0)
+        {
+            printf("\n\r\n\rOut of stock!\n\r\n\r");
+            return head;
+        }
+    }
+    else
+    {
+        printf("\n\r\n\rIncorrect entry\n\r\n\r");
+        return head;
+    }
+
+    
+    weight = scale(scale1);
+    price = ptr->priceper * weight;
+    printf("\n\r\n\rPrice: %f\n\r\n\r", price);
+    ptr->count--;   
+
+
+    flag1 = 1;
+    return head;
+}
+
+//Temperature sensor rti isr
+void timer_temp_hum_isr()
+{
+    sensor.read();
+    Farenheit = sensor.getFahrenheit();
+    Humidity = sensor.getHumidity();
+    
+    if(Farenheit > maxtemp)
+    {
+        buzzer = 1;
+        LED = 0;
+    }
+    else
+    {
+       buzzer = 0;
+       LED = 1;
+    }
+}
+
+//Prints the current teperature
+void getstatus()
+{
+    printf("\n\rCurrent Temperature: %.2f F\n\r", Farenheit);
+}
+
+//Sets max alowable temperature
+void setmax()
+{
+    printf("\n\r\n\rEnter max temperature:  ");
+    scanf("%f", &maxtemp);
+    printf("\n\r\n\r");
+}
+
+struct product* findrfid(int rfid[4], struct product *head)
+{
+    FILE *fp;
+    char line[512], *token;
+    int x, flag = 0, match = 0;
+    struct product *newprod, *ptr;
+    uint8_t temp;
+
+    //Opens file pointer to input file
+    if((fp = fopen("/sd/rfiddata.txt", "r")) == NULL)
+        perror("Error");
+
+
+    //Reads in next line of file
+    while(fgets(line, 512, fp) != NULL)
+    {
+        match = 0;
+        
+        //Breaks csv line into tokens with comma as the delimiter 
+        token = strtok(line, ",");
+
+        for(x=0;x<4;x++)
+        {
+            if(rfid[x] != atoi(token))
+                break;
+              
+            //Goes to next token
+            token = strtok(NULL, ",");
+
+            if(x == 3)
+                match = 1;
+        }
+
+        if(match == 1)
+        {
+            
+            if((ptr = findbyname(token, head)) != NULL)
+            {
+                ptr->count++;
+                fclose(fp);
+                return head;
+            }
+            
+            //Allocates memory for the product
+            newprod = (struct product*)malloc(sizeof(struct product));
+
+            //Fills in new product attributes
+            strcpy(newprod->pname, token);
+            newprod->count = 1;
+            
+            //Goes to next token
+            token = strtok(NULL, ",");
+            newprod->priceper = atof(token);
+            
+            //Fills id
+            newprod->pid[0] = rfid[0];
+            newprod->pid[1] = rfid[1];
+            newprod->pid[2] = rfid[2];
+            newprod->pid[3] = rfid[3];
+
+            //Adds to list
+            newprod->next = head;
+            head = newprod;
+            break;
+        }
+
+        else
+        {
+            continue;
+        }
+
+    }
+
+    fclose(fp);
+    return head;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/functions.h	Wed Mar 07 17:52:23 2018 +0000
@@ -0,0 +1,23 @@
+#ifndef functions_H
+#define functions_H
+
+#include "HX711.h"
+
+struct product* addproduct(struct product *head);
+struct product* sortlist(struct product *head);
+struct product* displaylist(struct product *head);
+void freelist(struct product *head);
+void savelist(struct product *head);
+struct product* loadlist();
+struct product* findbyname(char name[50], struct product *head);
+struct product* findbyid(int id[10], struct product *head);
+struct product* checkin(struct product *head);
+struct product* checkout(struct product *head, HX711 scale1);
+float scale(HX711 scale);
+void timer_temp_hum_isr();
+void getstatus();
+void setmax();
+struct product* findrfid(int rfid[4], struct product *head);
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Mar 07 17:52:23 2018 +0000
@@ -0,0 +1,115 @@
+#include "mbed.h"
+#include <stdio.h>
+#include <iostream>
+#include <stdlib.h>
+#include <string.h>
+#include "functions.h"
+#include "product.h"
+#include "HX711.h"
+#include "SDFileSystem.h"
+#include "Dht11.h"
+#include "MFRC522.h"
+#define SPI_MOSI    D11
+#define SPI_MISO    D12
+#define SPI_SCLK    D13
+#define SPI_CS      D10
+#define MF_RESET    D9
+
+
+SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
+MFRC522   RfChip(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET);
+
+Ticker ticker;
+Ticker rfidtick;
+
+void rfidsense();
+struct product *head = NULL;
+int id[10] = {0,0,0,0,0,0,0,0,0,0};
+int flag1 =1, flag2;
+
+int main()
+{
+    //Declarations and initializations
+    char k[5];
+    HX711 scale1(A0,A1);
+    ticker.attach(&timer_temp_hum_isr, 2);
+    rfidtick.attach(&rfidsense, .25);
+    head = loadlist();
+    RfChip.PCD_Init();
+    
+
+    while(1)
+    {
+        printf("\n\r1:  Add product to database\n\r2:  Check in product\n\r3:  See inventory\n\r4:  "
+            "Check out product\n\r5:  Cooler Status\n\r6:  Set max temperature\n\r7:  Quit\n\r\n\r");
+        scanf("%5s", &k);
+        
+        
+        //User choices
+        if(strcmp(k, "1") == 0)
+            head = addproduct(head);
+
+        else if(strcmp(k, "2") == 0)
+            head = checkin(head);
+
+        else if(strcmp(k, "3") == 0)
+            head = displaylist(head);
+
+        else if(strcmp(k, "4") == 0)
+            head = checkout(head, scale1);
+            
+        else if(strcmp(k, "5") == 0)
+            getstatus();
+            
+        else if(strcmp(k, "6") == 0)
+            setmax();
+
+        else if(strcmp(k, "7") == 0)
+            break;
+
+        //Clears input buffer
+        fflush(stdin);
+    }
+    
+    if(head == NULL)
+        return 0;
+
+
+    savelist(head);
+
+    freelist(head);
+    return 0;
+}
+
+void rfidsense()
+{
+    
+
+     // Look for new cards
+     if ( ! RfChip.PICC_IsNewCardPresent())
+     {
+       return;
+     }
+
+     // Select one of the cards
+     if ( ! RfChip.PICC_ReadCardSerial())
+     {
+       return;
+     }
+
+           
+    for (uint8_t i = 0; i < RfChip.uid.size; i++)
+    {
+        id[i] = RfChip.uid.uidByte[i]; 
+        printf(" %d ", RfChip.uid.uidByte[i]);
+    }
+            
+    //Add corresponding product to list based on rfid
+    if(flag1 == 1)
+        head = findrfid(id, head);  
+          
+    flag2 = 1;
+    
+    printf("\n\r\n\rRFID Scanned!!\n\r\n\r");
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Mar 07 17:52:23 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/5571c4ff569f
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/product.h	Wed Mar 07 17:52:23 2018 +0000
@@ -0,0 +1,19 @@
+#ifndef product_H
+#define product_H
+
+//Product structure
+struct product
+{
+    char pname[50];
+    unsigned int pid[4];
+    unsigned int count;
+    float priceper;
+    float weight;
+
+    struct product *next;
+
+};
+
+
+#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scale.cpp	Wed Mar 07 17:52:23 2018 +0000
@@ -0,0 +1,38 @@
+
+#include "functions.h"
+#include "HX711.h"
+#include <stdio.h>
+
+float scale(HX711 scale)
+{
+    float weight;
+    int x;
+    
+        fflush(stdin);
+        getchar();
+        printf("\n\r\n\rPlease remove all weight from scale, "
+        "then press any button to zero the scale\n\r\n\r");
+        getchar();
+         scale.tare();
+        
+        printf("\n\r\n\rPress any button again to begin weighing!\n\r\n\r");
+        getchar();
+        
+
+         weight = scale.getGram();
+
+        
+        //Handles negligible weight and calibration factor
+        if((weight < 0.5) && (weight > -0.5))
+            weight =0;
+        else
+           weight *= (15.625);
+            
+        //Converts to pounds
+        weight *= 0.0022046;
+            
+        printf("\n\r\n\rWeight:  %.2f lbs\n\r\n\r", weight);
+            
+            
+        return weight;  
+}
\ No newline at end of file