![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
a
Dependencies: EthernetInterface HTTPClient MFRC522 MbedJSONValue TextLCD mbed-rtos mbed
Fork of HEINEKEN_FRDM_K64F by
main.cpp@1:91f852e8c07f, 2016-03-02 (annotated)
- Committer:
- mslovic
- Date:
- Wed Mar 02 19:30:09 2016 +0000
- Revision:
- 1:91f852e8c07f
- Parent:
- 0:27d98e126a4f
- Child:
- 2:c8618b0777d3
Heineken_skoro_FINISH
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mslovic | 0:27d98e126a4f | 1 | #include "mbed.h" |
mslovic | 0:27d98e126a4f | 2 | #include "MFRC522.h" |
mslovic | 0:27d98e126a4f | 3 | #include "TextLCD.h" |
mslovic | 0:27d98e126a4f | 4 | #include "EthernetInterface.h" |
mslovic | 1:91f852e8c07f | 5 | #include <string> |
mslovic | 1:91f852e8c07f | 6 | #include "MbedJSONValue.h" |
mslovic | 0:27d98e126a4f | 7 | |
mslovic | 0:27d98e126a4f | 8 | #define MF_RESET PTD0 |
mslovic | 0:27d98e126a4f | 9 | |
mslovic | 0:27d98e126a4f | 10 | DigitalOut LedGreen(PTE26); |
mslovic | 0:27d98e126a4f | 11 | Serial pc(USBTX, USBRX); |
mslovic | 0:27d98e126a4f | 12 | TextLCD lcd(PTC3, PTC2, PTA2, PTB23, PTA1, PTB9, TextLCD::LCD20x4); // rs, e, d4-d7 |
mslovic | 0:27d98e126a4f | 13 | MFRC522 RfChip(PTD2, PTD3, PTD1, PTE25, PTD0); // mosi, miso, sclk, cs, reset |
mslovic | 0:27d98e126a4f | 14 | DigitalOut ventil(PTB18); |
mslovic | 0:27d98e126a4f | 15 | InterruptIn sensor(PTE24); |
mslovic | 0:27d98e126a4f | 16 | |
mslovic | 0:27d98e126a4f | 17 | |
mslovic | 0:27d98e126a4f | 18 | int rfID, Count, Calc; |
mslovic | 0:27d98e126a4f | 19 | float CalcMl; |
mslovic | 0:27d98e126a4f | 20 | float sum=0; |
mslovic | 0:27d98e126a4f | 21 | int RFID[15]; |
mslovic | 0:27d98e126a4f | 22 | |
mslovic | 0:27d98e126a4f | 23 | |
mslovic | 0:27d98e126a4f | 24 | void rpm() |
mslovic | 0:27d98e126a4f | 25 | { |
mslovic | 0:27d98e126a4f | 26 | Count++; |
mslovic | 0:27d98e126a4f | 27 | } |
mslovic | 0:27d98e126a4f | 28 | void ScreenSTART() |
mslovic | 0:27d98e126a4f | 29 | { |
mslovic | 0:27d98e126a4f | 30 | |
mslovic | 0:27d98e126a4f | 31 | lcd.cls(); |
mslovic | 1:91f852e8c07f | 32 | lcd.printf(" HEINEKEN\n\n"); |
mslovic | 0:27d98e126a4f | 33 | |
mslovic | 1:91f852e8c07f | 34 | lcd.printf(" Set your bracelet\n"); |
mslovic | 0:27d98e126a4f | 35 | lcd.printf(" against the reader"); |
mslovic | 0:27d98e126a4f | 36 | } |
mslovic | 1:91f852e8c07f | 37 | void ScreenNATOCI(int left) |
mslovic | 0:27d98e126a4f | 38 | { |
mslovic | 0:27d98e126a4f | 39 | lcd.cls(); |
mslovic | 1:91f852e8c07f | 40 | lcd.printf("Left %d beers.\n\n",left); |
mslovic | 1:91f852e8c07f | 41 | lcd.printf("Pour your beer!"); |
mslovic | 1:91f852e8c07f | 42 | wait(2); |
mslovic | 1:91f852e8c07f | 43 | ScreenSTART(); |
mslovic | 0:27d98e126a4f | 44 | |
mslovic | 0:27d98e126a4f | 45 | } |
mslovic | 0:27d98e126a4f | 46 | void ScreenNEMA() |
mslovic | 0:27d98e126a4f | 47 | { |
mslovic | 0:27d98e126a4f | 48 | lcd.cls(); |
mslovic | 1:91f852e8c07f | 49 | lcd.printf("Sorry no beear for you!"); |
mslovic | 0:27d98e126a4f | 50 | wait(2); |
mslovic | 0:27d98e126a4f | 51 | ScreenSTART(); |
mslovic | 0:27d98e126a4f | 52 | } |
mslovic | 0:27d98e126a4f | 53 | void ScreenTNX() |
mslovic | 0:27d98e126a4f | 54 | { |
mslovic | 0:27d98e126a4f | 55 | lcd.cls(); |
mslovic | 1:91f852e8c07f | 56 | lcd.printf("\nThank you for consumption!"); |
mslovic | 0:27d98e126a4f | 57 | wait(2); |
mslovic | 0:27d98e126a4f | 58 | ScreenSTART(); |
mslovic | 0:27d98e126a4f | 59 | } |
mslovic | 0:27d98e126a4f | 60 | void WrongID() |
mslovic | 0:27d98e126a4f | 61 | { |
mslovic | 0:27d98e126a4f | 62 | lcd.cls(); |
mslovic | 1:91f852e8c07f | 63 | lcd.printf("\n You do not have\n the correct bracelet!"); |
mslovic | 0:27d98e126a4f | 64 | wait(2); |
mslovic | 0:27d98e126a4f | 65 | ScreenSTART(); |
mslovic | 0:27d98e126a4f | 66 | } |
mslovic | 0:27d98e126a4f | 67 | |
mslovic | 0:27d98e126a4f | 68 | int main(void) |
mslovic | 0:27d98e126a4f | 69 | { |
mslovic | 1:91f852e8c07f | 70 | |
mslovic | 0:27d98e126a4f | 71 | //ventil = 0; |
mslovic | 0:27d98e126a4f | 72 | |
mslovic | 1:91f852e8c07f | 73 | lcd.printf("Starting..\n"); |
mslovic | 1:91f852e8c07f | 74 | pc.printf("Starting..\n"); |
mslovic | 0:27d98e126a4f | 75 | |
mslovic | 0:27d98e126a4f | 76 | EthernetInterface eth; |
mslovic | 0:27d98e126a4f | 77 | eth.init("10.10.0.2","255.255.255.0","10.10.0.1"); //Use DHCP |
mslovic | 0:27d98e126a4f | 78 | eth.connect(); |
mslovic | 0:27d98e126a4f | 79 | pc.printf("IP Address is %s\n\r", eth.getIPAddress()); |
mslovic | 0:27d98e126a4f | 80 | TCPSocketConnection sock; |
mslovic | 1:91f852e8c07f | 81 | |
mslovic | 0:27d98e126a4f | 82 | |
mslovic | 0:27d98e126a4f | 83 | RfChip.PCD_Init(); |
mslovic | 0:27d98e126a4f | 84 | |
mslovic | 0:27d98e126a4f | 85 | rfID = 0; |
mslovic | 0:27d98e126a4f | 86 | Count = 0; |
mslovic | 0:27d98e126a4f | 87 | ScreenSTART(); |
mslovic | 0:27d98e126a4f | 88 | |
mslovic | 1:91f852e8c07f | 89 | lcd.printf("Ready\n"); |
mslovic | 1:91f852e8c07f | 90 | pc.printf("Ready\n"); |
mslovic | 0:27d98e126a4f | 91 | while(1) { |
mslovic | 1:91f852e8c07f | 92 | |
mslovic | 0:27d98e126a4f | 93 | LedGreen = 0; |
mslovic | 0:27d98e126a4f | 94 | Count = 0; |
mslovic | 0:27d98e126a4f | 95 | |
mslovic | 0:27d98e126a4f | 96 | if ( ! RfChip.PICC_IsNewCardPresent()) { |
mslovic | 0:27d98e126a4f | 97 | wait_ms(500); |
mslovic | 0:27d98e126a4f | 98 | continue; |
mslovic | 0:27d98e126a4f | 99 | } |
mslovic | 0:27d98e126a4f | 100 | // Select one of the cards |
mslovic | 0:27d98e126a4f | 101 | if ( ! RfChip.PICC_ReadCardSerial()) { |
mslovic | 0:27d98e126a4f | 102 | wait_ms(500); |
mslovic | 0:27d98e126a4f | 103 | continue; |
mslovic | 0:27d98e126a4f | 104 | } |
mslovic | 0:27d98e126a4f | 105 | |
mslovic | 0:27d98e126a4f | 106 | LedGreen = 1; |
mslovic | 0:27d98e126a4f | 107 | |
mslovic | 0:27d98e126a4f | 108 | char cID[22]; |
mslovic | 0:27d98e126a4f | 109 | for (int i = 0; i < RfChip.uid.size; i++) { |
mslovic | 0:27d98e126a4f | 110 | sprintf(cID+i*2, "%X02", RfChip.uid.uidByte[i]); |
mslovic | 0:27d98e126a4f | 111 | } |
mslovic | 0:27d98e126a4f | 112 | |
mslovic | 0:27d98e126a4f | 113 | char http_cmd[1024]; |
mslovic | 1:91f852e8c07f | 114 | sprintf(http_cmd,"POST /rest/tap/%s HTTP/1.0\n\n",cID); |
mslovic | 1:91f852e8c07f | 115 | //pc.printf("\n\r%s\n\r",http_cmd); |
mslovic | 1:91f852e8c07f | 116 | |
mslovic | 0:27d98e126a4f | 117 | sock.connect("10.10.0.1", 8080); |
mslovic | 0:27d98e126a4f | 118 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
mslovic | 0:27d98e126a4f | 119 | |
mslovic | 0:27d98e126a4f | 120 | char buffer[300]; |
mslovic | 0:27d98e126a4f | 121 | int ret; |
mslovic | 0:27d98e126a4f | 122 | while (true) { |
mslovic | 0:27d98e126a4f | 123 | ret = sock.receive(buffer, sizeof(buffer)-1); |
mslovic | 0:27d98e126a4f | 124 | if (ret <= 0) |
mslovic | 0:27d98e126a4f | 125 | break; |
mslovic | 0:27d98e126a4f | 126 | buffer[ret] = '\0'; |
mslovic | 1:91f852e8c07f | 127 | //pc.printf("\n\r%s\n\r", buffer); |
mslovic | 1:91f852e8c07f | 128 | |
mslovic | 0:27d98e126a4f | 129 | } |
mslovic | 0:27d98e126a4f | 130 | sock.close(); |
mslovic | 1:91f852e8c07f | 131 | |
mslovic | 1:91f852e8c07f | 132 | string responseFULL(buffer); |
mslovic | 1:91f852e8c07f | 133 | int start = responseFULL.find("{",0); |
mslovic | 1:91f852e8c07f | 134 | int end = responseFULL.find("}",start); |
mslovic | 1:91f852e8c07f | 135 | |
mslovic | 1:91f852e8c07f | 136 | string response = responseFULL.substr(start, end-start+1); |
mslovic | 1:91f852e8c07f | 137 | //pc.printf("%s",response); |
mslovic | 1:91f852e8c07f | 138 | |
mslovic | 1:91f852e8c07f | 139 | MbedJSONValue responseJSON; |
mslovic | 1:91f852e8c07f | 140 | parse(responseJSON, response.c_str()); |
mslovic | 1:91f852e8c07f | 141 | |
mslovic | 1:91f852e8c07f | 142 | bool tap = responseJSON["tap"].get<bool>(); |
mslovic | 1:91f852e8c07f | 143 | int left = responseJSON["left"].get<int>(); |
mslovic | 1:91f852e8c07f | 144 | |
mslovic | 1:91f852e8c07f | 145 | pc.printf("tociti ili ne? %d",tap); |
mslovic | 1:91f852e8c07f | 146 | pc.printf("preostalo piva %d",left); |
mslovic | 1:91f852e8c07f | 147 | |
mslovic | 1:91f852e8c07f | 148 | if(tap!=1){ |
mslovic | 1:91f852e8c07f | 149 | ventil = 0; |
mslovic | 1:91f852e8c07f | 150 | ScreenNEMA(); |
mslovic | 1:91f852e8c07f | 151 | } |
mslovic | 1:91f852e8c07f | 152 | if(tap==1){ |
mslovic | 1:91f852e8c07f | 153 | ventil = 1; |
mslovic | 1:91f852e8c07f | 154 | ScreenNATOCI(left); |
mslovic | 1:91f852e8c07f | 155 | } |
mslovic | 1:91f852e8c07f | 156 | |
mslovic | 0:27d98e126a4f | 157 | } |
mslovic | 0:27d98e126a4f | 158 | |
mslovic | 0:27d98e126a4f | 159 | |
mslovic | 0:27d98e126a4f | 160 | } |
mslovic | 0:27d98e126a4f | 161 | |
mslovic | 0:27d98e126a4f | 162 | |
mslovic | 0:27d98e126a4f | 163 | |
mslovic | 0:27d98e126a4f | 164 |