v2 for lunchbox
Dependencies: HTTPClient PWM_Tone_Library PinDetect_KL25Z Queue cc3000_hostdriver_mbedsocket mbed
Fork of kragl by
Revision 1:90b792629d5e, committed 2015-11-26
- Comitter:
- inezraharjo
- Date:
- Thu Nov 26 04:17:27 2015 +0000
- Parent:
- 0:a77a884aa413
- Child:
- 2:f47fb78ba7b0
- Commit message:
- buzzer becomes digital output (no more pwm), things moved to the analog input/output pins, wifi connection and php functionalities add-ons
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Nov 09 06:00:22 2015 +0000
+++ b/main.cpp Thu Nov 26 04:17:27 2015 +0000
@@ -12,7 +12,10 @@
* (http://developer.mbed.org/users/Kojto/code/cc3000_hostdriver_mbedsocket/)
* - HTTPClient (http://developer.mbed.org/users/donatien/code/HTTPClient/)
*/
-
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
#include "mbed.h"
#include "cc3000.h"
#include "HTTPClient.h"
@@ -49,8 +52,11 @@
PinDetect p1(D4);
PinDetect p2(D6);
PinDetect p3(D7);
-PinDetect reed(D8);
-PwmOut Buzzer(D9);
+PinDetect reed(A3);
+DigitalOut RGBindicator(D2);
+DigitalOut Buzzer(A2);
+DigitalOut warningLED(A4);
+DigitalOut correctLED(A5);
int code[] = {0, 0, 0, 0};
int passwordcounter = 0;
@@ -58,7 +64,7 @@
bool reedIsOpen = false;
bool alarmOn = false;
bool boxOpenLegal = false;
-int password[] = {3, 3, 1, 2};
+int password[] = {1, 1, 1, 1};
float C_3 = 1000000/Do3,
Cs_3 = 1000000/Do3s,
D_3 = 1000000/Re3,
@@ -109,12 +115,18 @@
int i = 1;
code[passwordcounter] = i;
passwordcounter++;
+ if (codeEntered==false){
+ correctLED = 1;
+ }
//buttonQ.Put(&i);
}
void key1Released( void )
{
//pc.printf("Key Released\n");
+ if (codeEntered==false){
+ correctLED = 0;
+ }
}
void key1PressedHeld( void )
@@ -129,12 +141,18 @@
code[passwordcounter] = i;
passwordcounter++;
//buttonQ.Put(&i);
+ if (codeEntered==false){
+ correctLED = 1;
+ }
}
void key2Released( void )
{
//pc.printf("Key Released\n");
+ if (codeEntered==false){
+ correctLED = 0;
+ }
}
void key2PressedHeld( void)
@@ -149,12 +167,18 @@
code[passwordcounter] = i;
passwordcounter++;
//buttonQ.Put(&i);
+ if (codeEntered==false){
+ correctLED = 1;
+ }
}
void key3Released( void )
{
//pc.printf("Key Released\n");
+ if (codeEntered==false){
+ correctLED = 0;
+ }
}
void key3PressedHeld( void )
@@ -166,13 +190,18 @@
{
pc.printf("Reed Open\n");
reedIsOpen = 1;
-
}
void reedClosed( void )
{
pc.printf("Reed Closed\n");
reedIsOpen = 0;
+ if (boxOpenLegal==true){
+ warningLED =1;
+ correctLED =0;
+ boxOpenLegal = false;
+ }
+ Buzzer = 0;
}
void reedOpenHeld( void )
@@ -180,13 +209,13 @@
//pc.printf("Key Pressed Held\n");
}
-
int main()
{
// by default, it's red
led_red = 0;
led_green = 1;
-
+ warningLED = 1;
+ correctLED = 0;
// print message to indicate the program has started
pc.printf("CC3000 Lunch Theft\r\n");
wifi.init();
@@ -219,17 +248,16 @@
reed.attach_asserted_held( &reedOpenHeld);
reed.setAssertValue( 0 );
reed.setSampleFrequency( 10000 );
-
- Tune(Buzzer, C_4, 4); //4 Octave C beat 4/16
- wait_ms(250);
- Tune(Buzzer, D_4, 4); //4 Octave D beat 4/16
- wait_ms(250);
- Tune(Buzzer, E_4, 4); //4 Octave E beat 4/16
- wait_ms(250);
-
- int i;
+
+ Buzzer = 1; //4 Octave C beat 4/16
+ wait_ms(1000);
+ Buzzer = 0;
+
+ clock_t start_t, current_t, total_t;
+ //int counterforpasswordcheck = 0;
+ //int i;
int init = 0; // init wifi done
-
+ start_t = clock();
while (1) {
if (init == 0) {
@@ -246,41 +274,84 @@
led_green = 0;
}
} else {
- sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?isOpen=0&alarmOn=0");
+ pc.printf("Test first\r\n");
+ sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?isOpen=NO&alarmOn=OFF&boxLocked=YES");
int ret = http.get(url, str, 128);
init = 1;
- int tries = 5;
- while (ret && tries > 0) {
+ int tries = 15;
+ while (tries > 0) {
ret = http.get(url, str, 128);
if (!ret) {
pc.printf("Requested %s\r\n", url);
pc.printf("Page fetched successfully - read %d characters\r\n",
strlen(str));
pc.printf("Result: %s\r\n", str);
+ break;
} else {
pc.printf("Error - ret = %d - HTTP return code = %d\r\n",
ret,
http.getHTTPResponseCode());
}
tries--;
- }
-
+ }
}
}
-
+ current_t = clock();
+ total_t = (double)(current_t - start_t) / CLOCKS_PER_SEC;
+ //pc.printf("total_t: %d\r\n", total_t);
+ if (!reedIsOpen && (total_t%2)==0){ ever t seconds
+ pc.printf("SENDING TO URL\n");
+ sprintf(url, "http://www.charlesding.net/kragl/pincode_read.php?");
+ int ret = http.get(url, str, 128);
+ pc.printf("Trying to Request %s\r\n", url);
+ int tries = 50;
+ while (tries > 0) {
+ pc.printf("still in while lop #%d", tries);
+ if (!ret) {
+ pc.printf("Requested %s\r\n", url);
+ pc.printf("Page fetched successfully - read %d characters\r\n",
+ strlen(str));
+ pc.printf("Result: %s\r\n", str);
+ char* s = strtok(str, "\n");
+ char test[] = {s[5]};
+ int num = atoi(test);
+ if (num==1){
+ codeEntered = true;
+ } else {
+ codeEntered = false;
+ }
+ break;
+ } else {
+ pc.printf("Error - ret = %d - HTTP return code = %d\r\n",
+ ret,
+ http.getHTTPResponseCode());
+
+ }
+ tries--;
+ ret = http.get(url, str, 128);
+ }
+ }
+ wait(0.3);
+
+
+
if (reedIsOpen && codeEntered) {
pc.printf("LEGAL OPENING");
boxOpenLegal = true;
codeEntered = false;
+ Buzzer = 0;
wait(0.3);
} else if (reedIsOpen && !boxOpenLegal) {
// turn on alarm;
- pc.printf("ALARM TIME\n");
+ pc.printf("ALARM TIME\r\n");
+ Buzzer = 1;
+
+ /*
for(i=0; i<tones_num; i++) {
Auto_tunes(Buzzer, tones[i], 4); // Auto performance
Stop_tunes(Buzzer);
}
- boxOpenLegal = false;
+ */
if(wifi.is_connected() == false) {
// try to connect
@@ -297,56 +368,133 @@
} else {
// get input url and then return the value
pc.printf("SENDING TO URL\n");
- sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?isOpen=1&alarmOn=1");
+ sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?isOpen=YES1&alarmOn=ON&boxLocked=NO");
int ret = http.get(url, str, 128);
pc.printf("Trying to Request %s\r\n", url);
- int tries = 5;
- while (ret && tries > 0) {
+ int tries = 20;
+ while (tries > 0) {
if (!ret) {
pc.printf("Requested %s\r\n", url);
pc.printf("Page fetched successfully - read %d characters\r\n",
strlen(str));
pc.printf("Result: %s\r\n", str);
+ break;
} else {
pc.printf("Error - ret = %d - HTTP return code = %d\r\n",
ret,
http.getHTTPResponseCode());
tries--;
}
+ ret = http.get(url, str, 128);
}
}
-
-
wait(0.3);
} else if (!reedIsOpen && boxOpenLegal) {
+ Buzzer = 0;
boxOpenLegal = false;
+ pc.printf("SENDING TO URL\n");
+ sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?isOpen=NO&alarmOn=OFF&boxLocked=YES");
+
+ int ret = http.get(url, str, 128);
+ pc.printf("Trying to Request %s\r\n", url);
+ int tries = 20;
+ while (tries > 0) {
+ ret = http.get(url, str, 128);
+ if (!ret) {
+ pc.printf("Requested %s\r\n", url);
+ pc.printf("Page fetched successfully - read %d characters\r\n",
+ strlen(str));
+ pc.printf("Result: %s\r\n", str);
+ break;
+ } else {
+ pc.printf("Error - ret = %d - HTTP return code = %d\r\n",
+ ret,
+ http.getHTTPResponseCode());
+ tries--;
+ }
+ }
wait(0.3);
}
- if (passwordcounter==sizeof(code)/4) {
+ if (!codeEntered && passwordcounter==sizeof(code)/4) {
codeEntered = true;
+
+ sprintf(url, "http://www.charlesding.net/kragl/pincode_read.php?");
+ int ret2 = http.get(url, str, 128);
+
+ pc.printf("Trying to Request %s\r\n", url);
+ int tries = 20;
+ char* s;
+ while (tries > 0) {
+ pc.printf("try %d\n",tries);
+ if (!ret2) {
+ pc.printf("Requested %s\r\n", url);
+ pc.printf("Page fetched successfully - read %d characters\r\n",
+ strlen(str));
+ pc.printf("Result: %s\r\n", str);
+ s = strtok(str, "\n");
+ pc.printf("code string: %s\r\n",s);
+ int num;
+
+ if (true){
+ for (int c=1; c<5; c++){
+ char test[] = {s[c]};
+ num = atoi(test);
+ password[c-1] = num;
+ }
+ }
+ pc.printf("Password in the system: %d%d%d%d", password[0], password[1], password[2], password[3]);
+ break;
+ } else {
+ pc.printf("Error - ret = %d - HTTP return code = %d\r\n",
+ ret2,
+ http.getHTTPResponseCode());
+ }
+ tries--;
+ ret2 = http.get(url, str, 128);
+ }
+ wait(0.3);
+
for (int j=0; j<sizeof(code)/4; j++) {
if (code[j]!=password[j]) {
codeEntered = false;
}
}
+ char test[] = {s[5]};
+ int num = atoi(test);
+ if (num==1){
+ codeEntered = true;
+ }
+ if (codeEntered==true){
+ correctLED = 1;
+ warningLED = 0;
+ } else {
+ correctLED = 0;
+ warningLED = 1;
+ }
pc.printf("CODE IS ENTERED\n");
passwordcounter = 0;
pc.printf("SENDING TO URL\n");
- sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?isOpen=0&alarmOn=0");
+ if (codeEntered==false){
+ sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?isOpen=NO&alarmOn=OFF&authorized=NO");
+ } else {
+ sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?isOpen=NO&alarmOn=OFF&authorized=YES");
+ }
int ret = http.get(url, str, 128);
pc.printf("Trying to Request %s\r\n", url);
- int tries = 5;
- while (ret && tries > 0) {
+ tries = 20;
+ while (tries > 0) {
+ ret = http.get(url, str, 128);
if (!ret) {
pc.printf("Requested %s\r\n", url);
pc.printf("Page fetched successfully - read %d characters\r\n",
strlen(str));
pc.printf("Result: %s\r\n", str);
+ break;
} else {
pc.printf("Error - ret = %d - HTTP return code = %d\r\n",
ret,
@@ -356,8 +504,13 @@
}
+ }
+ //counterforpasswordcheck++;
+ //pc.printf("C%d ", counterforpasswordcheck);
}
+
+
- }
+
}
