-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArduino Code.ino
More file actions
279 lines (234 loc) · 7.79 KB
/
Arduino Code.ino
File metadata and controls
279 lines (234 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 53
#define RST_PIN 5
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(8, 9, 10, 11,12, 13); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
const int buzzer = 7; // buzzer to arduino pin 7
int removeProductButton = 2; //remove product
int generateBillButton = 3; //generate bill
boolean removeItem = false;
String wifiName = "V40 ThinQ_3650";
String password = "mkmk78789";
String serverHost = "192.168.43.8";
String port = "80";
float totalPrice = 0.0;
String orderId = "";
String tagID = "";
void setup() {
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display }
pinMode(generateBillButton, INPUT_PULLUP);
pinMode(removeProductButton, INPUT_PULLUP);
pinMode(buzzer,OUTPUT); // set buzzer pin 7 as an output
Serial1.begin(115200); //esp8266 serial begin
sendCommand("AT", 5, "OK");
sendCommand("AT+CWMODE=1", 5, "OK");
sendCommand("AT+CWJAP=\"" + wifiName + "\",\"" + password + "\"", 10, "OK");
Serial.println("Welcome to ABC SuperMarket!");
lcd.setCursor(3,0);
lcd.print("Welcome to");
lcd.setCursor(0,1);
lcd.print("ABC SuperMarket!");
delay(1000);
lcd.clear(); // Clears the display
Serial.println("Please Wait...");
lcd.print("Please Wait...");
//Generating Unique Order Id.
orderId = sendToServer("GET /Test_site/generateOrderID.php?");
int backTickIndex = orderId.indexOf('`');
orderId = orderId.substring((backTickIndex + 1),(backTickIndex + 13));
//Serial.println("Order ID: " + orderId);
lcd.clear(); // Clears the display
lcd.print("Scan a Product...");
Serial.println("Scan a Product to Add into cart...");
}
void loop(){
int removeButtonVal = digitalRead(removeProductButton);
int generateBillVal = digitalRead(generateBillButton);
if(removeButtonVal == LOW)
removeProduct();
if(generateBillVal == LOW)
generateBill();
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
lcd.clear(); // Clears the display
lcd.print("Processing");
lcd.setCursor(0,1);
lcd.print("Request...");
Serial.println("Processing Request...");
tone(buzzer, 5000, 100);
// Serial.print("UID tag: ");
for (byte i = 0; i < mfrc522.uid.size; i++) {
tagID += String(mfrc522.uid.uidByte[i], HEX);
}
tagID.toUpperCase();
//Serial.print(tagID);
Serial.println();
String productdata= sendToServer("GET /Test_site/addProduct.php?orderId=" + orderId + "&productId=" + tagID);
trimOutput(productdata);
}
String sendToServer(String url) {
//place LCD Print("Processing...") if delay in final output.
String serverHTTP = url;
sendCommand("AT+CIPMUX=1", 5, "OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\"" + serverHost + "\"," + port, 10, "OK");
sendCommand("AT+CIPSEND=0," + String(serverHTTP.length() + 2), 5, ">");
Serial1.println(serverHTTP);
String serverOutput = readServer();
return serverOutput;
}
String readServer() {
String ch = "";
while (Serial1.available()) {
ch = Serial1.readStringUntil('\0');
//Serial.print(ch);
//delay(10);
}
return ch;
}
void trimOutput(String ch){
int backTick1 = ch.indexOf('`');
int backTick2 = ch.indexOf('`', backTick1 + 1);
int backTick3 = ch.indexOf('`', backTick2 + 1);
String price = ch.substring(backTick1 + 1, backTick2);
String product = ch.substring(backTick2 + 1, backTick3);
Serial.println();
if(removeItem == false){
Serial.println("Added: Rs. " + price + "\n@ " + product);
lcd.clear(); // Clears the display
lcd.print("Added: Rs." + price);
lcd.setCursor(0,1);
lcd.print("@" + product);
}
else if(removeItem == true){
if(price.equals("0")){
Serial.println("Product Not Found");
lcd.clear(); // Clears the display
lcd.print("Product Not Found");
delay(1000);
}
else
Serial.println("Removed: Rs. " + price + "\n@ " + product);
lcd.clear(); // Clears the display
lcd.print("Removed:Rs" + price);
lcd.setCursor(0,1);
lcd.print("@" + product);
}
//Success Buzzer place here.
tone(buzzer, 8000, 50);
delay(10);
tone(buzzer, 5000, 100);
totalSum(price);
tagID="";
}
void totalSum(String price){
if(removeItem == false){
totalPrice += price.toFloat();
}else if(removeItem == true){
totalPrice -= price.toFloat();
Serial.println("Remove Mode OFF...");
lcd.clear(); // Clears the display
lcd.print("Remove Mode OFF...");
delay(1000);
}
Serial.println("\nTotal Amount: Rs. " + String(totalPrice));
Serial.println("Scan a Product to add...");
lcd.clear(); // Clears the display
lcd.print("Tot Amt:Rs"+ String(totalPrice));
lcd.setCursor(0,1);
lcd.print("Scan a Product...");
}
void removeProduct(){
Serial.println("\nRemove Mode ON...");
Serial.println("Scan a product to remove...");
lcd.clear(); // Clears the display
lcd.print("Remove Mode ON...");
lcd.setCursor(0,1);
lcd.print("Scan to Remove...");
while(!mfrc522.PICC_IsNewCardPresent());
while(!mfrc522.PICC_ReadCardSerial());
lcd.clear(); // Clears the display
lcd.print("Processing");
lcd.setCursor(0,1);
lcd.print("Request...");
Serial.println("Processing Request...");
tone(buzzer, 5000, 100);
//mk Serial.print("UID tag: ");
tagID = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
tagID += String(mfrc522.uid.uidByte[i], HEX);
}
tagID.toUpperCase();
//mk Serial.print(tagID);
Serial.println();
String deleteResult = sendToServer("GET /Test_site/removeProduct.php?orderId=" + orderId + "&productId=" + tagID);
removeItem = true;
//Serial.println(deleteResult);
trimOutput(deleteResult);
removeItem = false;
}
void generateBill(){
Serial.println("Generating Bill...");
lcd.clear(); // Clears the display
lcd.print("Generating Bill...");
delay(1000);
lcd.clear(); // Clears the display
lcd.print("Tot Amt:Rs"+ String(totalPrice));
delay(500);
lcd.clear(); // Clears the display
delay(200);
lcd.print("Tot Amt:Rs"+ String(totalPrice));
delay(500);
//Try LCD Blink.
Serial.println("Order Id:" + orderId + "\n@ Total amount: Rs." + totalPrice);
lcd.clear(); // Clears the display
lcd.print("Order Id:");
lcd.setCursor(0,1);
lcd.print(orderId);
//delay(1000);
tone(buzzer, 10000, 1000);
Serial.println("Thank you for Shopping.");
while(1);
}
void sendCommand(String command, int waitCount, char readReply[]) {
boolean flag = true;
Serial1.println(command);
int count = 0;
while (!Serial1.find(readReply)) {
count++;
if (count == waitCount) {
Serial.println("Fail: " + command);
flag = false;
if(command.equals("AT+CWJAP=\"" + wifiName + "\",\"" + password + "\"")
|| command.equals("AT+CIPSTART=0,\"TCP\",\"" + serverHost + "\"," + port)){
Serial.println("Server Down. Please press RESET...");
while(1){
tone(buzzer, 1000);
delay(500);
noTone(buzzer);
delay(500);
}
//LCD Print here.
}
else{
Serial.println("Error: Please Scan Again.");
//LCD Print here.
//Failure Buzzer place here.
tone(buzzer, 10000, 1000);
}
break;
}
}
if(flag){
//mk Serial.println("Command Success: " + command);
}
}