Wi-Fi enabled automatic drink mixer. Open up the webpage, select you drink and/or mixer and hit dispense. Be amazed as it mixes your drinks for you! Extendable and customizable.
Fork of ECE_4180_Lab_4_Robot_Webserver by
main.cpp@6:5c3c1f1c2985, 2015-12-06 (annotated)
- Committer:
- anewton8
- Date:
- Sun Dec 06 22:20:05 2015 +0000
- Revision:
- 6:5c3c1f1c2985
- Parent:
- 5:cfceccd5ccb1
Working stuff! Note: webpage can't exceed 2048 characters (whitespace has been removed to fit this)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:e2a155f50119 | 1 | // ESP8266 Static page WEB server to control Mbed |
star297 | 0:e2a155f50119 | 2 | |
star297 | 0:e2a155f50119 | 3 | #include "mbed.h" |
chrisprice92 | 5:cfceccd5ccb1 | 4 | #include "rtos.h" |
star297 | 0:e2a155f50119 | 5 | |
anewton8 | 6:5c3c1f1c2985 | 6 | Serial pc(USBTX, USBRX); |
chrisprice92 | 5:cfceccd5ccb1 | 7 | RawSerial esp(p28, p27); // tx, rx |
anewton8 | 6:5c3c1f1c2985 | 8 | DigitalOut reset(p29); // Reset for esp |
star297 | 2:d4c6bc0f2dc4 | 9 | |
star297 | 0:e2a155f50119 | 10 | // Standard Mbed LED definitions |
4180_1 | 4:40dd020463ea | 11 | DigitalOut led1(LED1); // (PTB18) |
4180_1 | 4:40dd020463ea | 12 | DigitalOut led2(LED2); // (PTB19) |
4180_1 | 4:40dd020463ea | 13 | DigitalOut led3(LED3); // (PTD1) |
chrisprice92 | 5:cfceccd5ccb1 | 14 | DigitalOut led4(LED4); |
star297 | 0:e2a155f50119 | 15 | |
anewton8 | 6:5c3c1f1c2985 | 16 | DigitalIn pb(p8); |
star297 | 0:e2a155f50119 | 17 | |
anewton8 | 6:5c3c1f1c2985 | 18 | // Drink stuff |
anewton8 | 6:5c3c1f1c2985 | 19 | #define TYPE_DRINK 0 |
anewton8 | 6:5c3c1f1c2985 | 20 | #define TYPE_MIXER 1 |
anewton8 | 6:5c3c1f1c2985 | 21 | #define RELAY_NONE -1 |
anewton8 | 6:5c3c1f1c2985 | 22 | #define DRINK_RELAY_WHISKY 0 |
anewton8 | 6:5c3c1f1c2985 | 23 | #define DRINK_RELAY_GIN 1 |
anewton8 | 6:5c3c1f1c2985 | 24 | #define MIXER_RELAY_COKE 2 |
anewton8 | 6:5c3c1f1c2985 | 25 | #define MIXER_RELAY_TONIC 3 |
anewton8 | 6:5c3c1f1c2985 | 26 | DigitalOut gRelays[] = {p22, p23, p24, p25}; // Relay Pins |
anewton8 | 6:5c3c1f1c2985 | 27 | int gDrinkRelayVal = RELAY_NONE; |
anewton8 | 6:5c3c1f1c2985 | 28 | int gMixerRelayVal = RELAY_NONE; |
anewton8 | 6:5c3c1f1c2985 | 29 | int gPourLatch = false; |
anewton8 | 6:5c3c1f1c2985 | 30 | void startPour(int type, int drinkRelay, float timeMs); |
anewton8 | 6:5c3c1f1c2985 | 31 | void stopDrinkPour(void const *args); |
anewton8 | 6:5c3c1f1c2985 | 32 | void stopMixerPour(void const *args); |
anewton8 | 6:5c3c1f1c2985 | 33 | RtosTimer *gStopDrinkTimer; |
anewton8 | 6:5c3c1f1c2985 | 34 | RtosTimer *gStopMixerTimer; |
star297 | 0:e2a155f50119 | 35 | |
anewton8 | 6:5c3c1f1c2985 | 36 | // Webserver stuff |
star297 | 0:e2a155f50119 | 37 | Timer t1; |
star297 | 1:71ed1afbf344 | 38 | Timer t2; |
star297 | 1:71ed1afbf344 | 39 | struct tm t; |
star297 | 0:e2a155f50119 | 40 | |
star297 | 3:f7febfa77784 | 41 | int bufflen, DataRX, count, getcount, replycount, servreq, timeout; |
star297 | 2:d4c6bc0f2dc4 | 42 | int bufl, ipdLen, linkID, weberror, webcounter; |
star297 | 2:d4c6bc0f2dc4 | 43 | char webcount[8]; |
star297 | 3:f7febfa77784 | 44 | char lasthit[30]; |
star297 | 0:e2a155f50119 | 45 | char timebuf[30]; |
star297 | 0:e2a155f50119 | 46 | char type[16]; |
star297 | 0:e2a155f50119 | 47 | char type1[16]; |
star297 | 0:e2a155f50119 | 48 | char channel[2]; |
star297 | 3:f7febfa77784 | 49 | char cmdbuff[32]; |
anewton8 | 6:5c3c1f1c2985 | 50 | char replybuff[4096]; |
anewton8 | 6:5c3c1f1c2985 | 51 | char webdata[4096]; // This may need to be bigger depending on WEB browser used |
anewton8 | 6:5c3c1f1c2985 | 52 | char webbuff[8192]; // Currently using 1986 characters, Increase this if more web page data added |
star297 | 0:e2a155f50119 | 53 | |
star297 | 0:e2a155f50119 | 54 | void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck(); |
anewton8 | 6:5c3c1f1c2985 | 55 | void setRTC(); |
star297 | 0:e2a155f50119 | 56 | |
anewton8 | 6:5c3c1f1c2985 | 57 | void otherThread(void const *args); |
chrisprice92 | 5:cfceccd5ccb1 | 58 | |
star297 | 1:71ed1afbf344 | 59 | // manual set RTC values |
4180_1 | 4:40dd020463ea | 60 | int minute =00; // 0-59 |
4180_1 | 4:40dd020463ea | 61 | int hour =12; // 2-23 |
4180_1 | 4:40dd020463ea | 62 | int dayofmonth =26; // 1-31 |
4180_1 | 4:40dd020463ea | 63 | int month =8; // 1-12 |
star297 | 1:71ed1afbf344 | 64 | int year =15; // last 2 digits |
star297 | 2:d4c6bc0f2dc4 | 65 | |
4180_1 | 4:40dd020463ea | 66 | int port =80; // set server port |
4180_1 | 4:40dd020463ea | 67 | int SERVtimeout =5; // set server timeout in seconds in case link breaks. |
4180_1 | 4:40dd020463ea | 68 | |
star297 | 0:e2a155f50119 | 69 | // Serial Interrupt read ESP data |
4180_1 | 4:40dd020463ea | 70 | void callback() |
4180_1 | 4:40dd020463ea | 71 | { |
4180_1 | 4:40dd020463ea | 72 | led3=1; |
chrisprice92 | 5:cfceccd5ccb1 | 73 | led4 = 0; |
4180_1 | 4:40dd020463ea | 74 | while (esp.readable()) { |
chrisprice92 | 5:cfceccd5ccb1 | 75 | led4 = !led4; |
4180_1 | 4:40dd020463ea | 76 | webbuff[count] = esp.getc(); |
4180_1 | 4:40dd020463ea | 77 | count++; |
4180_1 | 4:40dd020463ea | 78 | } |
chrisprice92 | 5:cfceccd5ccb1 | 79 | |
chrisprice92 | 5:cfceccd5ccb1 | 80 | led2 = 0; |
4180_1 | 4:40dd020463ea | 81 | if(strlen(webbuff)>bufflen) { |
4180_1 | 4:40dd020463ea | 82 | DataRX=1; |
4180_1 | 4:40dd020463ea | 83 | led3=0; |
4180_1 | 4:40dd020463ea | 84 | } |
star297 | 0:e2a155f50119 | 85 | } |
star297 | 0:e2a155f50119 | 86 | |
anewton8 | 6:5c3c1f1c2985 | 87 | |
anewton8 | 6:5c3c1f1c2985 | 88 | // Stop drink relay |
anewton8 | 6:5c3c1f1c2985 | 89 | void stopDrinkPour(void const *args) { |
anewton8 | 6:5c3c1f1c2985 | 90 | //drinkTimer.detach(); |
anewton8 | 6:5c3c1f1c2985 | 91 | gRelays[gDrinkRelayVal] = 0; |
anewton8 | 6:5c3c1f1c2985 | 92 | gDrinkRelayVal = RELAY_NONE; |
anewton8 | 6:5c3c1f1c2985 | 93 | // pc.printf("Relays: %d,%d,%d,%d\n\r", gRelays[0], gRelays[1], gRelays[2], gRelays[3]); |
anewton8 | 6:5c3c1f1c2985 | 94 | } |
anewton8 | 6:5c3c1f1c2985 | 95 | |
anewton8 | 6:5c3c1f1c2985 | 96 | void stopMixerPour(void const *args) { |
anewton8 | 6:5c3c1f1c2985 | 97 | //mixerTimer.detach(); |
anewton8 | 6:5c3c1f1c2985 | 98 | gRelays[gMixerRelayVal] = 0; |
anewton8 | 6:5c3c1f1c2985 | 99 | gMixerRelayVal = RELAY_NONE; |
anewton8 | 6:5c3c1f1c2985 | 100 | // pc.printf("Relays: %d,%d,%d,%d\n\r", gRelays[0], gRelays[1], gRelays[2], gRelays[3]); |
anewton8 | 6:5c3c1f1c2985 | 101 | } |
anewton8 | 6:5c3c1f1c2985 | 102 | |
anewton8 | 6:5c3c1f1c2985 | 103 | void startPour(int type, int relayVal, float timeMs) { |
anewton8 | 6:5c3c1f1c2985 | 104 | switch (relayVal) { |
anewton8 | 6:5c3c1f1c2985 | 105 | case DRINK_RELAY_WHISKY: |
anewton8 | 6:5c3c1f1c2985 | 106 | case DRINK_RELAY_GIN: |
anewton8 | 6:5c3c1f1c2985 | 107 | case MIXER_RELAY_COKE: |
anewton8 | 6:5c3c1f1c2985 | 108 | case MIXER_RELAY_TONIC: |
anewton8 | 6:5c3c1f1c2985 | 109 | // Good values |
anewton8 | 6:5c3c1f1c2985 | 110 | break; |
anewton8 | 6:5c3c1f1c2985 | 111 | case RELAY_NONE: |
anewton8 | 6:5c3c1f1c2985 | 112 | return; // Do nothing for this pour |
anewton8 | 6:5c3c1f1c2985 | 113 | default: |
anewton8 | 6:5c3c1f1c2985 | 114 | pc.printf("StartPour invalid relay val %d!\n\r", relayVal); |
anewton8 | 6:5c3c1f1c2985 | 115 | return; |
anewton8 | 6:5c3c1f1c2985 | 116 | } |
anewton8 | 6:5c3c1f1c2985 | 117 | |
anewton8 | 6:5c3c1f1c2985 | 118 | // Pour! |
anewton8 | 6:5c3c1f1c2985 | 119 | gRelays[relayVal] = 1; |
anewton8 | 6:5c3c1f1c2985 | 120 | //pc.printf("Relays: %d,%d,%d,%d\n\r", gRelays[0], gRelays[1], gRelays[2], gRelays[3]); |
anewton8 | 6:5c3c1f1c2985 | 121 | if (type == TYPE_DRINK) { |
anewton8 | 6:5c3c1f1c2985 | 122 | //drinkTimer.attach(&stopDrinkPour, 1.0); |
anewton8 | 6:5c3c1f1c2985 | 123 | gStopDrinkTimer->start(1000); |
anewton8 | 6:5c3c1f1c2985 | 124 | } else if (type == TYPE_MIXER) { |
anewton8 | 6:5c3c1f1c2985 | 125 | //mixerTimer.attach(&stopMixerPour, 0.5); |
anewton8 | 6:5c3c1f1c2985 | 126 | gStopMixerTimer->start(500); |
anewton8 | 6:5c3c1f1c2985 | 127 | } |
anewton8 | 6:5c3c1f1c2985 | 128 | } |
anewton8 | 6:5c3c1f1c2985 | 129 | |
anewton8 | 6:5c3c1f1c2985 | 130 | void otherThread(void const *args) { |
anewton8 | 6:5c3c1f1c2985 | 131 | pb.mode(PullUp); |
anewton8 | 6:5c3c1f1c2985 | 132 | RtosTimer stopDrinkTimer(stopDrinkPour, osTimerOnce); |
anewton8 | 6:5c3c1f1c2985 | 133 | RtosTimer stopMixerTimer(stopMixerPour, osTimerOnce); |
anewton8 | 6:5c3c1f1c2985 | 134 | |
anewton8 | 6:5c3c1f1c2985 | 135 | gStopDrinkTimer = &stopDrinkTimer; |
anewton8 | 6:5c3c1f1c2985 | 136 | gStopMixerTimer = &stopMixerTimer; |
anewton8 | 6:5c3c1f1c2985 | 137 | |
anewton8 | 6:5c3c1f1c2985 | 138 | while (1) { |
anewton8 | 6:5c3c1f1c2985 | 139 | if (gPourLatch) { |
anewton8 | 6:5c3c1f1c2985 | 140 | startPour(TYPE_DRINK, gDrinkRelayVal, 1000); |
anewton8 | 6:5c3c1f1c2985 | 141 | startPour(TYPE_MIXER, gMixerRelayVal, 500); |
anewton8 | 6:5c3c1f1c2985 | 142 | gPourLatch = false; |
anewton8 | 6:5c3c1f1c2985 | 143 | } |
anewton8 | 6:5c3c1f1c2985 | 144 | //gRelays[0] = !pb; |
anewton8 | 6:5c3c1f1c2985 | 145 | Thread::wait(50); |
anewton8 | 6:5c3c1f1c2985 | 146 | } |
anewton8 | 6:5c3c1f1c2985 | 147 | } |
4180_1 | 4:40dd020463ea | 148 | int main() |
chrisprice92 | 5:cfceccd5ccb1 | 149 | { |
anewton8 | 6:5c3c1f1c2985 | 150 | gRelays[0] = 0; |
anewton8 | 6:5c3c1f1c2985 | 151 | gRelays[1] = 0; |
anewton8 | 6:5c3c1f1c2985 | 152 | gRelays[2] = 0; |
anewton8 | 6:5c3c1f1c2985 | 153 | gRelays[3] = 0; |
anewton8 | 6:5c3c1f1c2985 | 154 | |
anewton8 | 6:5c3c1f1c2985 | 155 | Thread tT(otherThread); |
anewton8 | 6:5c3c1f1c2985 | 156 | |
4180_1 | 4:40dd020463ea | 157 | reset=0; |
star297 | 0:e2a155f50119 | 158 | pc.baud(115200); |
4180_1 | 4:40dd020463ea | 159 | |
4180_1 | 4:40dd020463ea | 160 | pc.printf("\f\n\r------------ ESP8266 Hardware Reset --------------\n\r"); |
4180_1 | 4:40dd020463ea | 161 | wait(0.5); |
4180_1 | 4:40dd020463ea | 162 | reset=1; |
4180_1 | 4:40dd020463ea | 163 | led1=1,led2=0,led3=0; |
4180_1 | 4:40dd020463ea | 164 | timeout=6000; |
4180_1 | 4:40dd020463ea | 165 | getcount=500; |
4180_1 | 4:40dd020463ea | 166 | getreply(); |
star297 | 3:f7febfa77784 | 167 | esp.baud(115200); // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F |
4180_1 | 4:40dd020463ea | 168 | if (time(NULL) < 1420070400) { |
4180_1 | 4:40dd020463ea | 169 | setRTC(); |
4180_1 | 4:40dd020463ea | 170 | } |
anewton8 | 6:5c3c1f1c2985 | 171 | |
star297 | 0:e2a155f50119 | 172 | startserver(); |
4180_1 | 4:40dd020463ea | 173 | |
anewton8 | 6:5c3c1f1c2985 | 174 | //Thread tPourThread(pourThread); |
chrisprice92 | 5:cfceccd5ccb1 | 175 | |
4180_1 | 4:40dd020463ea | 176 | while(1) { |
4180_1 | 4:40dd020463ea | 177 | if(DataRX==1) { |
star297 | 0:e2a155f50119 | 178 | ReadWebData(); |
4180_1 | 4:40dd020463ea | 179 | if (servreq == 1 && weberror == 0) { |
anewton8 | 6:5c3c1f1c2985 | 180 | sendpage(); |
4180_1 | 4:40dd020463ea | 181 | } |
4180_1 | 4:40dd020463ea | 182 | esp.attach(&callback); |
4180_1 | 4:40dd020463ea | 183 | pc.printf(" IPD Data:\r\n\n Link ID = %d,\r\n IPD Header Length = %d \r\n IPD Type = %s\r\n", linkID, ipdLen, type); |
4180_1 | 4:40dd020463ea | 184 | pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata); |
star297 | 0:e2a155f50119 | 185 | pc.printf(" Web Characters sent : %d\n\n", bufl); |
star297 | 0:e2a155f50119 | 186 | pc.printf(" -------------------------------------\n\n"); |
star297 | 2:d4c6bc0f2dc4 | 187 | strcpy(lasthit, timebuf); |
4180_1 | 4:40dd020463ea | 188 | servreq=0; |
4180_1 | 4:40dd020463ea | 189 | } |
star297 | 0:e2a155f50119 | 190 | } |
4180_1 | 4:40dd020463ea | 191 | } |
chrisprice92 | 5:cfceccd5ccb1 | 192 | |
4180_1 | 4:40dd020463ea | 193 | // Static WEB page |
star297 | 0:e2a155f50119 | 194 | void sendpage() |
4180_1 | 4:40dd020463ea | 195 | { |
anewton8 | 6:5c3c1f1c2985 | 196 | // WEB page data |
anewton8 | 6:5c3c1f1c2985 | 197 | strcpy(webbuff, "\ |
anewton8 | 6:5c3c1f1c2985 | 198 | <html>\ |
anewton8 | 6:5c3c1f1c2985 | 199 | <head>\ |
anewton8 | 6:5c3c1f1c2985 | 200 | <meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0;'>\ |
anewton8 | 6:5c3c1f1c2985 | 201 | <title>Sir MixaBot</title>\ |
anewton8 | 6:5c3c1f1c2985 | 202 | <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css' integrity='sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7' crossorigin='anonymous'>\ |
anewton8 | 6:5c3c1f1c2985 | 203 | <style>\ |
anewton8 | 6:5c3c1f1c2985 | 204 | body {\ |
anewton8 | 6:5c3c1f1c2985 | 205 | background: url(http://i.imgur.com/NrfPRXU.jpg?1);\ |
anewton8 | 6:5c3c1f1c2985 | 206 | background-size: cover;\ |
anewton8 | 6:5c3c1f1c2985 | 207 | background-repeat: no-repeat;\ |
anewton8 | 6:5c3c1f1c2985 | 208 | }\ |
anewton8 | 6:5c3c1f1c2985 | 209 | select {\ |
anewton8 | 6:5c3c1f1c2985 | 210 | width: 100%;\ |
anewton8 | 6:5c3c1f1c2985 | 211 | }\ |
anewton8 | 6:5c3c1f1c2985 | 212 | .center {\ |
anewton8 | 6:5c3c1f1c2985 | 213 | margin: auto;\ |
anewton8 | 6:5c3c1f1c2985 | 214 | }\ |
anewton8 | 6:5c3c1f1c2985 | 215 | .container {\ |
anewton8 | 6:5c3c1f1c2985 | 216 | display:inline-block;\ |
anewton8 | 6:5c3c1f1c2985 | 217 | }\ |
anewton8 | 6:5c3c1f1c2985 | 218 | h1 {\ |
anewton8 | 6:5c3c1f1c2985 | 219 | font-family: 'Lucida Bright', Georgia, serif;\ |
anewton8 | 6:5c3c1f1c2985 | 220 | font-size: 72px;\ |
anewton8 | 6:5c3c1f1c2985 | 221 | font-style: normal;\ |
anewton8 | 6:5c3c1f1c2985 | 222 | font-variant: normal;\ |
anewton8 | 6:5c3c1f1c2985 | 223 | font-weight: 400;\ |
anewton8 | 6:5c3c1f1c2985 | 224 | line-height: 60px;\ |
anewton8 | 6:5c3c1f1c2985 | 225 | color: rgba(100, 100, 100, 1.0);\ |
anewton8 | 6:5c3c1f1c2985 | 226 | -webkit-text-stroke: 1px black;\ |
anewton8 | 6:5c3c1f1c2985 | 227 | background: rgba(255, 255, 255, 0.9);\ |
anewton8 | 6:5c3c1f1c2985 | 228 | background-size: contain;\ |
anewton8 | 6:5c3c1f1c2985 | 229 | padding: 5px;\ |
anewton8 | 6:5c3c1f1c2985 | 230 | } \ |
anewton8 | 6:5c3c1f1c2985 | 231 | form {\ |
anewton8 | 6:5c3c1f1c2985 | 232 | width: 200px;\ |
anewton8 | 6:5c3c1f1c2985 | 233 | margin-top: 10px;\ |
anewton8 | 6:5c3c1f1c2985 | 234 | background: rgba(255, 255, 255, 0.9);\ |
anewton8 | 6:5c3c1f1c2985 | 235 | background-size: contain;\ |
anewton8 | 6:5c3c1f1c2985 | 236 | -webkit-box-shadow: 10px 10px 10px rgba(0, 0, 0, 0.7);\ |
anewton8 | 6:5c3c1f1c2985 | 237 | -moz-box-shadow: 10px 10px 10px rgba(0, 0, 0, 0.7);\ |
anewton8 | 6:5c3c1f1c2985 | 238 | box-shadow: 10px 10px 10px rgba(0, 0, 0, 0.7);\ |
anewton8 | 6:5c3c1f1c2985 | 239 | border-radius: 8px;\ |
anewton8 | 6:5c3c1f1c2985 | 240 | padding: 8px;\ |
anewton8 | 6:5c3c1f1c2985 | 241 | font-family: 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', Geneva, Verdana, sans-serif;\ |
anewton8 | 6:5c3c1f1c2985 | 242 | font-size: 14px;\ |
anewton8 | 6:5c3c1f1c2985 | 243 | font-style: normal;\ |
anewton8 | 6:5c3c1f1c2985 | 244 | font-variant: normal;\ |
anewton8 | 6:5c3c1f1c2985 | 245 | font-weight: 400;\ |
anewton8 | 6:5c3c1f1c2985 | 246 | line-height: 20px;\ |
anewton8 | 6:5c3c1f1c2985 | 247 | }\ |
anewton8 | 6:5c3c1f1c2985 | 248 | </style>\ |
anewton8 | 6:5c3c1f1c2985 | 249 | </head>\ |
anewton8 | 6:5c3c1f1c2985 | 250 | <body>\ |
anewton8 | 6:5c3c1f1c2985 | 251 | <h1>Sir MixaBot</h1>\ |
anewton8 | 6:5c3c1f1c2985 | 252 | <div class='container'>\ |
anewton8 | 6:5c3c1f1c2985 | 253 | <form method='POST'>\ |
anewton8 | 6:5c3c1f1c2985 | 254 | <label>Drink</label>\ |
anewton8 | 6:5c3c1f1c2985 | 255 | <select name='drink'>\ |
anewton8 | 6:5c3c1f1c2985 | 256 | <option value=''>-- None --</option>\ |
anewton8 | 6:5c3c1f1c2985 | 257 | <option value='whisky'>Whisky</option>\ |
anewton8 | 6:5c3c1f1c2985 | 258 | <option value='gin'>Gin</option>\ |
anewton8 | 6:5c3c1f1c2985 | 259 | </select>\ |
anewton8 | 6:5c3c1f1c2985 | 260 | <br />\ |
anewton8 | 6:5c3c1f1c2985 | 261 | <br />\ |
anewton8 | 6:5c3c1f1c2985 | 262 | <label>Mixer</label>\ |
anewton8 | 6:5c3c1f1c2985 | 263 | <select name='mixer'>\ |
anewton8 | 6:5c3c1f1c2985 | 264 | <option value=''>-- None --</option>\ |
anewton8 | 6:5c3c1f1c2985 | 265 | <option value='coke'>Coke</option>\ |
anewton8 | 6:5c3c1f1c2985 | 266 | <option value='tonic'>Tonic</option>\ |
anewton8 | 6:5c3c1f1c2985 | 267 | </select> \ |
anewton8 | 6:5c3c1f1c2985 | 268 | <br />\ |
anewton8 | 6:5c3c1f1c2985 | 269 | <br />\ |
anewton8 | 6:5c3c1f1c2985 | 270 | <button type='submit' class='btn btn-block btn-lg btn-danger'>Dispense</button>\ |
anewton8 | 6:5c3c1f1c2985 | 271 | </form>\ |
anewton8 | 6:5c3c1f1c2985 | 272 | </div>\ |
anewton8 | 6:5c3c1f1c2985 | 273 | </body>\ |
anewton8 | 6:5c3c1f1c2985 | 274 | </html>"); |
4180_1 | 4:40dd020463ea | 275 | |
4180_1 | 4:40dd020463ea | 276 | // end of WEB page data |
4180_1 | 4:40dd020463ea | 277 | bufl = strlen(webbuff); // get total page buffer length |
star297 | 3:f7febfa77784 | 278 | sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length. |
4180_1 | 4:40dd020463ea | 279 | timeout=200; |
4180_1 | 4:40dd020463ea | 280 | getcount=7; |
star297 | 3:f7febfa77784 | 281 | SendCMD(); |
4180_1 | 4:40dd020463ea | 282 | getreply(); |
star297 | 0:e2a155f50119 | 283 | SendWEB(); // send web page |
star297 | 3:f7febfa77784 | 284 | memset(webbuff, '\0', sizeof(webbuff)); |
4180_1 | 4:40dd020463ea | 285 | sendcheck(); |
star297 | 0:e2a155f50119 | 286 | } |
star297 | 0:e2a155f50119 | 287 | |
star297 | 3:f7febfa77784 | 288 | // wait for ESP "SEND OK" reply, then close IP to load web page |
star297 | 0:e2a155f50119 | 289 | void sendcheck() |
star297 | 0:e2a155f50119 | 290 | { |
4180_1 | 4:40dd020463ea | 291 | weberror=1; |
4180_1 | 4:40dd020463ea | 292 | timeout=500; |
4180_1 | 4:40dd020463ea | 293 | getcount=24; |
4180_1 | 4:40dd020463ea | 294 | t2.reset(); |
4180_1 | 4:40dd020463ea | 295 | t2.start(); |
4180_1 | 4:40dd020463ea | 296 | while(weberror==1 && t2.read() <5) { |
star297 | 0:e2a155f50119 | 297 | getreply(); |
4180_1 | 4:40dd020463ea | 298 | if (strstr(replybuff, "SEND OK") != NULL) { |
4180_1 | 4:40dd020463ea | 299 | weberror=0; // wait for valid SEND OK |
star297 | 0:e2a155f50119 | 300 | } |
4180_1 | 4:40dd020463ea | 301 | } |
4180_1 | 4:40dd020463ea | 302 | if(weberror==1) { // restart connection |
4180_1 | 4:40dd020463ea | 303 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); |
4180_1 | 4:40dd020463ea | 304 | timeout=500; |
4180_1 | 4:40dd020463ea | 305 | getcount=10; |
4180_1 | 4:40dd020463ea | 306 | SendCMD(); |
4180_1 | 4:40dd020463ea | 307 | getreply(); |
4180_1 | 4:40dd020463ea | 308 | pc.printf(replybuff); |
star297 | 3:f7febfa77784 | 309 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
4180_1 | 4:40dd020463ea | 310 | timeout=500; |
4180_1 | 4:40dd020463ea | 311 | getcount=10; |
4180_1 | 4:40dd020463ea | 312 | SendCMD(); |
4180_1 | 4:40dd020463ea | 313 | getreply(); |
4180_1 | 4:40dd020463ea | 314 | pc.printf(replybuff); |
4180_1 | 4:40dd020463ea | 315 | } else { |
4180_1 | 4:40dd020463ea | 316 | sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection |
4180_1 | 4:40dd020463ea | 317 | SendCMD(); |
4180_1 | 4:40dd020463ea | 318 | getreply(); |
4180_1 | 4:40dd020463ea | 319 | pc.printf(replybuff); |
4180_1 | 4:40dd020463ea | 320 | } |
4180_1 | 4:40dd020463ea | 321 | t2.reset(); |
4180_1 | 4:40dd020463ea | 322 | } |
star297 | 0:e2a155f50119 | 323 | |
4180_1 | 4:40dd020463ea | 324 | // Reads and processes GET and POST web data |
star297 | 0:e2a155f50119 | 325 | void ReadWebData() |
4180_1 | 4:40dd020463ea | 326 | { |
4180_1 | 4:40dd020463ea | 327 | wait_ms(200); |
star297 | 3:f7febfa77784 | 328 | esp.attach(NULL); |
4180_1 | 4:40dd020463ea | 329 | count=0; |
4180_1 | 4:40dd020463ea | 330 | DataRX=0; |
4180_1 | 4:40dd020463ea | 331 | weberror=0; |
4180_1 | 4:40dd020463ea | 332 | memset(webdata, '\0', sizeof(webdata)); |
star297 | 3:f7febfa77784 | 333 | int x = strcspn (webbuff,"+"); |
4180_1 | 4:40dd020463ea | 334 | if(x) { |
chrisprice92 | 5:cfceccd5ccb1 | 335 | pc.printf("DATA: %s", webdata); |
4180_1 | 4:40dd020463ea | 336 | strcpy(webdata, webbuff + x); |
4180_1 | 4:40dd020463ea | 337 | weberror=0; |
4180_1 | 4:40dd020463ea | 338 | int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); |
anewton8 | 6:5c3c1f1c2985 | 339 | |
4180_1 | 4:40dd020463ea | 340 | sprintf(channel, "%d",linkID); |
4180_1 | 4:40dd020463ea | 341 | if (strstr(webdata, "GET") != NULL) { |
4180_1 | 4:40dd020463ea | 342 | servreq=1; |
4180_1 | 4:40dd020463ea | 343 | } |
4180_1 | 4:40dd020463ea | 344 | if (strstr(webdata, "POST") != NULL) { |
anewton8 | 6:5c3c1f1c2985 | 345 | int drinkVal = RELAY_NONE; |
anewton8 | 6:5c3c1f1c2985 | 346 | int mixerVal = RELAY_NONE; |
anewton8 | 6:5c3c1f1c2985 | 347 | if(strstr(webdata, "drink=whisky") != NULL) { |
anewton8 | 6:5c3c1f1c2985 | 348 | drinkVal = DRINK_RELAY_WHISKY; |
anewton8 | 6:5c3c1f1c2985 | 349 | } else if (strstr(webdata, "drink=gin") != NULL ) { |
anewton8 | 6:5c3c1f1c2985 | 350 | drinkVal = DRINK_RELAY_GIN; |
anewton8 | 6:5c3c1f1c2985 | 351 | } |
anewton8 | 6:5c3c1f1c2985 | 352 | |
anewton8 | 6:5c3c1f1c2985 | 353 | if(strstr(webdata, "mixer=coke") != NULL) { |
anewton8 | 6:5c3c1f1c2985 | 354 | mixerVal = MIXER_RELAY_COKE; |
anewton8 | 6:5c3c1f1c2985 | 355 | } else if (strstr(webdata, "mixer=tonic") != NULL ) { |
anewton8 | 6:5c3c1f1c2985 | 356 | mixerVal = MIXER_RELAY_TONIC; |
anewton8 | 6:5c3c1f1c2985 | 357 | } |
anewton8 | 6:5c3c1f1c2985 | 358 | |
anewton8 | 6:5c3c1f1c2985 | 359 | if (gDrinkRelayVal == RELAY_NONE && gMixerRelayVal == RELAY_NONE) { |
anewton8 | 6:5c3c1f1c2985 | 360 | gDrinkRelayVal = drinkVal; |
anewton8 | 6:5c3c1f1c2985 | 361 | gMixerRelayVal = mixerVal; |
anewton8 | 6:5c3c1f1c2985 | 362 | gPourLatch = true; |
anewton8 | 6:5c3c1f1c2985 | 363 | } |
4180_1 | 4:40dd020463ea | 364 | servreq=1; |
4180_1 | 4:40dd020463ea | 365 | } |
star297 | 2:d4c6bc0f2dc4 | 366 | webcounter++; |
star297 | 2:d4c6bc0f2dc4 | 367 | sprintf(webcount, "%d",webcounter); |
4180_1 | 4:40dd020463ea | 368 | } else { |
4180_1 | 4:40dd020463ea | 369 | memset(webbuff, '\0', sizeof(webbuff)); |
4180_1 | 4:40dd020463ea | 370 | esp.attach(&callback); |
4180_1 | 4:40dd020463ea | 371 | weberror=1; |
4180_1 | 4:40dd020463ea | 372 | } |
star297 | 0:e2a155f50119 | 373 | } |
star297 | 0:e2a155f50119 | 374 | // Starts and restarts webserver if errors detected. |
star297 | 0:e2a155f50119 | 375 | void startserver() |
star297 | 0:e2a155f50119 | 376 | { |
star297 | 1:71ed1afbf344 | 377 | pc.printf("\n\n RTC time %s\r\n\n",timebuf); |
4180_1 | 4:40dd020463ea | 378 | pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); |
star297 | 3:f7febfa77784 | 379 | strcpy(cmdbuff,"AT+RST\r\n"); |
4180_1 | 4:40dd020463ea | 380 | timeout=8000; |
4180_1 | 4:40dd020463ea | 381 | getcount=1000; |
star297 | 0:e2a155f50119 | 382 | SendCMD(); |
star297 | 1:71ed1afbf344 | 383 | getreply(); |
star297 | 3:f7febfa77784 | 384 | pc.printf(replybuff); |
star297 | 3:f7febfa77784 | 385 | pc.printf("%d",count); |
star297 | 3:f7febfa77784 | 386 | if (strstr(replybuff, "OK") != NULL) { |
star297 | 1:71ed1afbf344 | 387 | pc.printf("\n++++++++++ Starting Server ++++++++++\r\n"); |
4180_1 | 4:40dd020463ea | 388 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections. |
4180_1 | 4:40dd020463ea | 389 | timeout=500; |
4180_1 | 4:40dd020463ea | 390 | getcount=20; |
star297 | 1:71ed1afbf344 | 391 | SendCMD(); |
star297 | 2:d4c6bc0f2dc4 | 392 | getreply(); |
4180_1 | 4:40dd020463ea | 393 | pc.printf(replybuff); |
star297 | 3:f7febfa77784 | 394 | sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port); |
4180_1 | 4:40dd020463ea | 395 | timeout=500; |
4180_1 | 4:40dd020463ea | 396 | getcount=20; |
star297 | 1:71ed1afbf344 | 397 | SendCMD(); |
star297 | 3:f7febfa77784 | 398 | getreply(); |
4180_1 | 4:40dd020463ea | 399 | pc.printf(replybuff); |
4180_1 | 4:40dd020463ea | 400 | wait(1); |
star297 | 3:f7febfa77784 | 401 | sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout); |
4180_1 | 4:40dd020463ea | 402 | timeout=500; |
4180_1 | 4:40dd020463ea | 403 | getcount=50; |
star297 | 3:f7febfa77784 | 404 | SendCMD(); |
star297 | 2:d4c6bc0f2dc4 | 405 | getreply(); |
4180_1 | 4:40dd020463ea | 406 | pc.printf(replybuff); |
4180_1 | 4:40dd020463ea | 407 | wait(5); |
4180_1 | 4:40dd020463ea | 408 | pc.printf("\n Getting Server IP \r\n"); |
star297 | 3:f7febfa77784 | 409 | strcpy(cmdbuff, "AT+CIFSR\r\n"); |
4180_1 | 4:40dd020463ea | 410 | timeout=2500; |
4180_1 | 4:40dd020463ea | 411 | getcount=200; |
4180_1 | 4:40dd020463ea | 412 | while(weberror==0) { |
4180_1 | 4:40dd020463ea | 413 | SendCMD(); |
4180_1 | 4:40dd020463ea | 414 | getreply(); |
4180_1 | 4:40dd020463ea | 415 | if (strstr(replybuff, "0.0.0.0") == NULL) { |
4180_1 | 4:40dd020463ea | 416 | weberror=1; // wait for valid IP |
star297 | 1:71ed1afbf344 | 417 | } |
4180_1 | 4:40dd020463ea | 418 | } |
4180_1 | 4:40dd020463ea | 419 | pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n"); |
4180_1 | 4:40dd020463ea | 420 | pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n"); |
4180_1 | 4:40dd020463ea | 421 | replybuff[strlen(replybuff)-1] = '\0'; |
4180_1 | 4:40dd020463ea | 422 | //char* IP = replybuff + 5; |
4180_1 | 4:40dd020463ea | 423 | sprintf(webdata,"%s", replybuff); |
4180_1 | 4:40dd020463ea | 424 | pc.printf(webdata); |
4180_1 | 4:40dd020463ea | 425 | led2=1; |
4180_1 | 4:40dd020463ea | 426 | bufflen=200; |
4180_1 | 4:40dd020463ea | 427 | count=0; |
star297 | 1:71ed1afbf344 | 428 | pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); |
star297 | 1:71ed1afbf344 | 429 | esp.attach(&callback); |
4180_1 | 4:40dd020463ea | 430 | } else { |
4180_1 | 4:40dd020463ea | 431 | pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n"); |
4180_1 | 4:40dd020463ea | 432 | while(1) {} |
4180_1 | 4:40dd020463ea | 433 | } |
4180_1 | 4:40dd020463ea | 434 | t2.reset(); |
4180_1 | 4:40dd020463ea | 435 | t2.start(); |
4180_1 | 4:40dd020463ea | 436 | } |
star297 | 0:e2a155f50119 | 437 | // ESP Command data send |
star297 | 0:e2a155f50119 | 438 | void SendCMD() |
star297 | 0:e2a155f50119 | 439 | { |
4180_1 | 4:40dd020463ea | 440 | esp.printf("%s", cmdbuff); |
4180_1 | 4:40dd020463ea | 441 | } |
star297 | 0:e2a155f50119 | 442 | // Large WEB buffer data send |
star297 | 0:e2a155f50119 | 443 | void SendWEB() |
4180_1 | 4:40dd020463ea | 444 | { |
star297 | 0:e2a155f50119 | 445 | int i=0; |
star297 | 0:e2a155f50119 | 446 | if(esp.writeable()) { |
4180_1 | 4:40dd020463ea | 447 | while(webbuff[i]!='\0') { |
4180_1 | 4:40dd020463ea | 448 | esp.putc(webbuff[i]); |
4180_1 | 4:40dd020463ea | 449 | i++; |
4180_1 | 4:40dd020463ea | 450 | } |
4180_1 | 4:40dd020463ea | 451 | } |
4180_1 | 4:40dd020463ea | 452 | } |
4180_1 | 4:40dd020463ea | 453 | // Get Command and ESP status replies |
star297 | 0:e2a155f50119 | 454 | void getreply() |
4180_1 | 4:40dd020463ea | 455 | { |
star297 | 3:f7febfa77784 | 456 | memset(replybuff, '\0', sizeof(replybuff)); |
4180_1 | 4:40dd020463ea | 457 | t1.reset(); |
4180_1 | 4:40dd020463ea | 458 | t1.start(); |
4180_1 | 4:40dd020463ea | 459 | replycount=0; |
star297 | 3:f7febfa77784 | 460 | while(t1.read_ms()< timeout && replycount < getcount) { |
star297 | 0:e2a155f50119 | 461 | if(esp.readable()) { |
4180_1 | 4:40dd020463ea | 462 | replybuff[replycount] = esp.getc(); |
4180_1 | 4:40dd020463ea | 463 | replycount++; |
star297 | 2:d4c6bc0f2dc4 | 464 | } |
4180_1 | 4:40dd020463ea | 465 | } |
4180_1 | 4:40dd020463ea | 466 | t1.stop(); |
star297 | 0:e2a155f50119 | 467 | } |
star297 | 2:d4c6bc0f2dc4 | 468 | |
star297 | 1:71ed1afbf344 | 469 | void setRTC() |
star297 | 1:71ed1afbf344 | 470 | { |
4180_1 | 4:40dd020463ea | 471 | t.tm_sec = (0); // 0-59 |
4180_1 | 4:40dd020463ea | 472 | t.tm_min = (minute); // 0-59 |
4180_1 | 4:40dd020463ea | 473 | t.tm_hour = (hour); // 0-23 |
4180_1 | 4:40dd020463ea | 474 | t.tm_mday = (dayofmonth); // 1-31 |
4180_1 | 4:40dd020463ea | 475 | t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format |
4180_1 | 4:40dd020463ea | 476 | t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year |
4180_1 | 4:40dd020463ea | 477 | set_time(mktime(&t)); // set RTC clock |
chrisprice92 | 5:cfceccd5ccb1 | 478 | } |