An alarm clock that wakes you up naturally!
Dependencies: 4DGL-uLCD-SE DebounceIn mbed RPCInterface
main.cpp@1:57e06832197a, 2017-05-03 (annotated)
- Committer:
- ooglezoiden
- Date:
- Wed May 03 15:05:12 2017 +0000
- Revision:
- 1:57e06832197a
- Parent:
- 0:397faf7a0a52
added Alexa support
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ooglezoiden | 0:397faf7a0a52 | 1 | #include "mbed.h" |
ooglezoiden | 0:397faf7a0a52 | 2 | #include "uLCD_4DGL.h" |
ooglezoiden | 0:397faf7a0a52 | 3 | #include "DebounceIn.h" |
ooglezoiden | 1:57e06832197a | 4 | #include "mywifi.h" |
ooglezoiden | 1:57e06832197a | 5 | #include "mbed_rpc.h" |
ooglezoiden | 0:397faf7a0a52 | 6 | |
ooglezoiden | 0:397faf7a0a52 | 7 | Timeout alarm; |
ooglezoiden | 0:397faf7a0a52 | 8 | Timer t; |
ooglezoiden | 1:57e06832197a | 9 | PwmOut lights(p24); |
ooglezoiden | 0:397faf7a0a52 | 10 | uLCD_4DGL uLCD(p9,p10,p11); |
ooglezoiden | 0:397faf7a0a52 | 11 | AnalogIn photocell(p15); |
ooglezoiden | 0:397faf7a0a52 | 12 | DebounceIn pb1(p8); |
ooglezoiden | 0:397faf7a0a52 | 13 | DebounceIn pb2(p7); |
ooglezoiden | 1:57e06832197a | 14 | DebounceIn pb3(p6); |
ooglezoiden | 1:57e06832197a | 15 | Serial blue(p13,p14); |
ooglezoiden | 1:57e06832197a | 16 | |
ooglezoiden | 1:57e06832197a | 17 | Serial pc(USBTX, USBRX); |
ooglezoiden | 1:57e06832197a | 18 | Serial esp(p28, p27); |
ooglezoiden | 1:57e06832197a | 19 | char ssid[32] = "Hotspot"; |
ooglezoiden | 1:57e06832197a | 20 | char pwd[32] = "1234567890"; |
ooglezoiden | 1:57e06832197a | 21 | char port[32] = "1035"; // must be port forwarded |
ooglezoiden | 1:57e06832197a | 22 | char timeout[32] = "28800"; // 28800 is max |
ooglezoiden | 1:57e06832197a | 23 | volatile int tx_in=0; |
ooglezoiden | 1:57e06832197a | 24 | volatile int tx_out=0; |
ooglezoiden | 1:57e06832197a | 25 | volatile int rx_in=0; |
ooglezoiden | 1:57e06832197a | 26 | volatile int rx_out=0; |
ooglezoiden | 1:57e06832197a | 27 | const int buffer_size = 4095; |
ooglezoiden | 1:57e06832197a | 28 | char tx_buffer[buffer_size+1]; |
ooglezoiden | 1:57e06832197a | 29 | char rx_buffer[buffer_size+1]; |
ooglezoiden | 1:57e06832197a | 30 | char cmdbuff[1024]; |
ooglezoiden | 1:57e06832197a | 31 | char rx_line[1024]; |
ooglezoiden | 1:57e06832197a | 32 | DigitalOut led1(LED1); |
ooglezoiden | 1:57e06832197a | 33 | DigitalOut led2(LED2); |
ooglezoiden | 1:57e06832197a | 34 | DigitalOut led3(LED3); |
ooglezoiden | 1:57e06832197a | 35 | DigitalOut led4(LED4); |
ooglezoiden | 0:397faf7a0a52 | 36 | |
ooglezoiden | 0:397faf7a0a52 | 37 | |
ooglezoiden | 1:57e06832197a | 38 | // ---------------- Light Functions For Alexa ------------------ |
ooglezoiden | 1:57e06832197a | 39 | |
ooglezoiden | 1:57e06832197a | 40 | |
ooglezoiden | 1:57e06832197a | 41 | |
ooglezoiden | 1:57e06832197a | 42 | void alexaLightsOn(Arguments *in, Reply *out){ |
ooglezoiden | 0:397faf7a0a52 | 43 | uLCD.cls(); |
ooglezoiden | 0:397faf7a0a52 | 44 | uLCD.printf("Turning on lights!"); |
ooglezoiden | 0:397faf7a0a52 | 45 | //lights = 1; |
ooglezoiden | 0:397faf7a0a52 | 46 | while(lights<=0.99f){ |
ooglezoiden | 0:397faf7a0a52 | 47 | lights = lights + .01f; |
ooglezoiden | 1:57e06832197a | 48 | wait_ms(100); |
ooglezoiden | 1:57e06832197a | 49 | } |
ooglezoiden | 1:57e06832197a | 50 | while(1);//do nothing until reset |
ooglezoiden | 1:57e06832197a | 51 | } |
ooglezoiden | 1:57e06832197a | 52 | |
ooglezoiden | 1:57e06832197a | 53 | void alexaLightsOff(Arguments *in, Reply *out){ |
ooglezoiden | 1:57e06832197a | 54 | uLCD.cls(); |
ooglezoiden | 1:57e06832197a | 55 | uLCD.printf("Turning off lights!"); |
ooglezoiden | 1:57e06832197a | 56 | //lights = 1; |
ooglezoiden | 1:57e06832197a | 57 | while(lights>=0.01f){ |
ooglezoiden | 1:57e06832197a | 58 | lights = lights - .01f; |
ooglezoiden | 1:57e06832197a | 59 | wait_ms(100); |
ooglezoiden | 0:397faf7a0a52 | 60 | } |
ooglezoiden | 0:397faf7a0a52 | 61 | while(1);//do nothing until reset |
ooglezoiden | 0:397faf7a0a52 | 62 | } |
ooglezoiden | 0:397faf7a0a52 | 63 | |
ooglezoiden | 1:57e06832197a | 64 | void alexaDimLights(Arguments *in, Reply *out){ |
ooglezoiden | 1:57e06832197a | 65 | float level = in->getArg<float>(); |
ooglezoiden | 1:57e06832197a | 66 | uLCD.cls(); |
ooglezoiden | 1:57e06832197a | 67 | uLCD.printf("Dimming lights to %f percent.", level); |
ooglezoiden | 1:57e06832197a | 68 | lights = level; |
ooglezoiden | 1:57e06832197a | 69 | } |
ooglezoiden | 1:57e06832197a | 70 | |
ooglezoiden | 1:57e06832197a | 71 | void lightsOn(){ |
ooglezoiden | 1:57e06832197a | 72 | uLCD.cls(); |
ooglezoiden | 1:57e06832197a | 73 | uLCD.printf("Turning on lights!"); |
ooglezoiden | 1:57e06832197a | 74 | lights = 1.0; |
ooglezoiden | 1:57e06832197a | 75 | //while(lights<=0.99f){ |
ooglezoiden | 1:57e06832197a | 76 | // lights = lights + 0.1f; |
ooglezoiden | 1:57e06832197a | 77 | // wait(1); |
ooglezoiden | 1:57e06832197a | 78 | //} |
ooglezoiden | 1:57e06832197a | 79 | while(1);//do nothing until reset |
ooglezoiden | 1:57e06832197a | 80 | } |
ooglezoiden | 1:57e06832197a | 81 | |
ooglezoiden | 1:57e06832197a | 82 | void alexaTimer(Arguments *in, Reply *out){ |
ooglezoiden | 1:57e06832197a | 83 | int time = in->getArg<int>(); |
ooglezoiden | 1:57e06832197a | 84 | alarm.attach(&lightsOn, time); |
ooglezoiden | 1:57e06832197a | 85 | t.start(); |
ooglezoiden | 1:57e06832197a | 86 | float curTime; |
ooglezoiden | 1:57e06832197a | 87 | while(1){ |
ooglezoiden | 1:57e06832197a | 88 | uLCD.cls(); |
ooglezoiden | 1:57e06832197a | 89 | curTime = t.read(); |
ooglezoiden | 1:57e06832197a | 90 | uLCD.printf("%f secs since alarm set.\n", curTime); |
ooglezoiden | 1:57e06832197a | 91 | } |
ooglezoiden | 1:57e06832197a | 92 | } |
ooglezoiden | 1:57e06832197a | 93 | |
ooglezoiden | 1:57e06832197a | 94 | // --------------------- Modes ------------------------ |
ooglezoiden | 1:57e06832197a | 95 | |
ooglezoiden | 1:57e06832197a | 96 | |
ooglezoiden | 1:57e06832197a | 97 | |
ooglezoiden | 0:397faf7a0a52 | 98 | void lightMode(){ |
ooglezoiden | 0:397faf7a0a52 | 99 | uLCD.cls(); |
ooglezoiden | 0:397faf7a0a52 | 100 | uLCD.printf("not bright out yet!\n"); |
ooglezoiden | 1:57e06832197a | 101 | while(photocell.read() < .8f){ |
ooglezoiden | 0:397faf7a0a52 | 102 | uLCD.cls(); |
ooglezoiden | 0:397faf7a0a52 | 103 | uLCD.printf("not bright out yet!\n"); |
ooglezoiden | 0:397faf7a0a52 | 104 | } |
ooglezoiden | 0:397faf7a0a52 | 105 | lightsOn(); |
ooglezoiden | 0:397faf7a0a52 | 106 | } |
ooglezoiden | 0:397faf7a0a52 | 107 | |
ooglezoiden | 0:397faf7a0a52 | 108 | void timerMode(int time){ |
ooglezoiden | 0:397faf7a0a52 | 109 | alarm.attach(&lightsOn, time); |
ooglezoiden | 0:397faf7a0a52 | 110 | t.start(); |
ooglezoiden | 0:397faf7a0a52 | 111 | float curTime; |
ooglezoiden | 0:397faf7a0a52 | 112 | while(1){ |
ooglezoiden | 0:397faf7a0a52 | 113 | uLCD.cls(); |
ooglezoiden | 0:397faf7a0a52 | 114 | curTime = t.read(); |
ooglezoiden | 0:397faf7a0a52 | 115 | uLCD.printf("%f secs since alarm set.\n", curTime); |
ooglezoiden | 0:397faf7a0a52 | 116 | } |
ooglezoiden | 0:397faf7a0a52 | 117 | } |
ooglezoiden | 0:397faf7a0a52 | 118 | |
ooglezoiden | 0:397faf7a0a52 | 119 | int getTime(){ |
ooglezoiden | 0:397faf7a0a52 | 120 | bool done = false; |
ooglezoiden | 0:397faf7a0a52 | 121 | int time[3] = {0,0,0}; |
ooglezoiden | 0:397faf7a0a52 | 122 | int modifying = 0; |
ooglezoiden | 0:397faf7a0a52 | 123 | uLCD.cls(); |
ooglezoiden | 0:397faf7a0a52 | 124 | uLCD.printf("Connect Bluetooth now.\n\n"); |
ooglezoiden | 0:397faf7a0a52 | 125 | uLCD.printf("Up and down change time\n left and right select unit.\n"); |
ooglezoiden | 0:397faf7a0a52 | 126 | //uLCD.printf("This message will display for five seconds. Then input your desired time."); |
ooglezoiden | 0:397faf7a0a52 | 127 | wait(5); |
ooglezoiden | 0:397faf7a0a52 | 128 | char bnum = 0; |
ooglezoiden | 0:397faf7a0a52 | 129 | char bhit = 0; |
ooglezoiden | 0:397faf7a0a52 | 130 | while(!done){ |
ooglezoiden | 0:397faf7a0a52 | 131 | uLCD.cls(); |
ooglezoiden | 0:397faf7a0a52 | 132 | uLCD.printf("Current timer will last:\n"); |
ooglezoiden | 0:397faf7a0a52 | 133 | uLCD.printf("%d hours, %d mins, %d secs\n", time[2], time[1], time[0]); |
ooglezoiden | 0:397faf7a0a52 | 134 | uLCD.printf("You are now modifying: "); |
ooglezoiden | 0:397faf7a0a52 | 135 | if(modifying == 0){ |
ooglezoiden | 0:397faf7a0a52 | 136 | uLCD.printf("Secs\n"); |
ooglezoiden | 0:397faf7a0a52 | 137 | } else if(modifying == 1){ |
ooglezoiden | 0:397faf7a0a52 | 138 | uLCD.printf("Mins\n"); |
ooglezoiden | 0:397faf7a0a52 | 139 | } else if(modifying == 2){ |
ooglezoiden | 0:397faf7a0a52 | 140 | uLCD.printf("Hours\n"); |
ooglezoiden | 0:397faf7a0a52 | 141 | } |
ooglezoiden | 0:397faf7a0a52 | 142 | uLCD.printf("Press 1 to set the timer to the current value.\n"); |
ooglezoiden | 0:397faf7a0a52 | 143 | if(blue.getc()=='!'){ |
ooglezoiden | 0:397faf7a0a52 | 144 | if(blue.getc()=='B'){ |
ooglezoiden | 0:397faf7a0a52 | 145 | bnum = blue.getc();//button number |
ooglezoiden | 0:397faf7a0a52 | 146 | bhit = blue.getc();//1=hit, 0=release |
ooglezoiden | 0:397faf7a0a52 | 147 | if(blue.getc()==char(~('!' + 'B' + bnum + bhit))){//checksum |
ooglezoiden | 0:397faf7a0a52 | 148 | switch(bnum){ |
ooglezoiden | 0:397faf7a0a52 | 149 | case '1'://number button 1 |
ooglezoiden | 0:397faf7a0a52 | 150 | if(bhit == '1'){ |
ooglezoiden | 0:397faf7a0a52 | 151 | done = true; |
ooglezoiden | 0:397faf7a0a52 | 152 | } |
ooglezoiden | 0:397faf7a0a52 | 153 | break; |
ooglezoiden | 0:397faf7a0a52 | 154 | case '5'://up arrow |
ooglezoiden | 0:397faf7a0a52 | 155 | if(bhit=='1'){ |
ooglezoiden | 0:397faf7a0a52 | 156 | time[modifying]++; |
ooglezoiden | 0:397faf7a0a52 | 157 | } |
ooglezoiden | 0:397faf7a0a52 | 158 | break; |
ooglezoiden | 0:397faf7a0a52 | 159 | case '6'://down arrow |
ooglezoiden | 0:397faf7a0a52 | 160 | if(bhit=='1'&&time[modifying]>0){ |
ooglezoiden | 0:397faf7a0a52 | 161 | time[modifying]--; |
ooglezoiden | 0:397faf7a0a52 | 162 | } |
ooglezoiden | 0:397faf7a0a52 | 163 | break; |
ooglezoiden | 0:397faf7a0a52 | 164 | case '7'://left arrow |
ooglezoiden | 0:397faf7a0a52 | 165 | if(bhit=='1'&&modifying>0){ |
ooglezoiden | 0:397faf7a0a52 | 166 | modifying--; |
ooglezoiden | 0:397faf7a0a52 | 167 | } |
ooglezoiden | 0:397faf7a0a52 | 168 | break; |
ooglezoiden | 0:397faf7a0a52 | 169 | case '8'://right arrow |
ooglezoiden | 0:397faf7a0a52 | 170 | if(bhit=='1'&&modifying<2){ |
ooglezoiden | 0:397faf7a0a52 | 171 | modifying++; |
ooglezoiden | 0:397faf7a0a52 | 172 | } |
ooglezoiden | 0:397faf7a0a52 | 173 | break; |
ooglezoiden | 0:397faf7a0a52 | 174 | } |
ooglezoiden | 0:397faf7a0a52 | 175 | } |
ooglezoiden | 0:397faf7a0a52 | 176 | } |
ooglezoiden | 0:397faf7a0a52 | 177 | } |
ooglezoiden | 0:397faf7a0a52 | 178 | } |
ooglezoiden | 0:397faf7a0a52 | 179 | return time[0] + time[1] * 60 + time[2] * 60 * 60; |
ooglezoiden | 0:397faf7a0a52 | 180 | |
ooglezoiden | 0:397faf7a0a52 | 181 | } |
ooglezoiden | 0:397faf7a0a52 | 182 | |
ooglezoiden | 1:57e06832197a | 183 | void alexaMode(){ |
ooglezoiden | 1:57e06832197a | 184 | uLCD.cls(); |
ooglezoiden | 1:57e06832197a | 185 | uLCD.printf("Alexa mode started!\n"); |
ooglezoiden | 1:57e06832197a | 186 | pc.baud(9600); |
ooglezoiden | 1:57e06832197a | 187 | esp.baud(9600); |
ooglezoiden | 1:57e06832197a | 188 | esp.attach(&Rx_interrupt, Serial::RxIrq); |
ooglezoiden | 1:57e06832197a | 189 | esp.attach(&Tx_interrupt, Serial::TxIrq); |
ooglezoiden | 1:57e06832197a | 190 | wait(5); |
ooglezoiden | 1:57e06832197a | 191 | connectToNetwork(); |
ooglezoiden | 1:57e06832197a | 192 | uLCD.printf("Connected to network!!\n"); |
ooglezoiden | 1:57e06832197a | 193 | wait(0.2); |
ooglezoiden | 1:57e06832197a | 194 | char rpc_in[256]; |
ooglezoiden | 1:57e06832197a | 195 | char rpc_out[256]; |
ooglezoiden | 1:57e06832197a | 196 | while (1) { |
ooglezoiden | 1:57e06832197a | 197 | getReply(); |
ooglezoiden | 1:57e06832197a | 198 | memset(&rpc_in[0], 0, sizeof(rpc_in)); |
ooglezoiden | 1:57e06832197a | 199 | memset(&rpc_out[0], 0, sizeof(rpc_out)); |
ooglezoiden | 1:57e06832197a | 200 | int length = (int)rx_line[3] - 48; // bytes 0 to 2 are trash; byte 3 is length of message |
ooglezoiden | 1:57e06832197a | 201 | if (length > 0 && length < 256) { |
ooglezoiden | 1:57e06832197a | 202 | for (int i = 0; i < length; i++) { |
ooglezoiden | 1:57e06832197a | 203 | rpc_in[i] = rx_line[i+4]; // bytes 4 to length+3 are the valid data |
ooglezoiden | 1:57e06832197a | 204 | } |
ooglezoiden | 1:57e06832197a | 205 | RPC::call(rpc_in, rpc_out); |
ooglezoiden | 1:57e06832197a | 206 | pc.printf("%s\n", rpc_out); |
ooglezoiden | 1:57e06832197a | 207 | } |
ooglezoiden | 1:57e06832197a | 208 | // lambda function is event-triggered and non-persistent |
ooglezoiden | 1:57e06832197a | 209 | // after it terminates, we need to close the existing connection and start another one |
ooglezoiden | 1:57e06832197a | 210 | strcpy(cmdbuff, "srv:close()\r\n"); |
ooglezoiden | 1:57e06832197a | 211 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 212 | wait(.5); |
ooglezoiden | 1:57e06832197a | 213 | getReply(); |
ooglezoiden | 1:57e06832197a | 214 | strcpy(cmdbuff, "srv=net.createServer(net.TCP,"); |
ooglezoiden | 1:57e06832197a | 215 | strcat(cmdbuff, timeout); |
ooglezoiden | 1:57e06832197a | 216 | strcat(cmdbuff, ")\r\n"); |
ooglezoiden | 1:57e06832197a | 217 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 218 | wait(.5); |
ooglezoiden | 1:57e06832197a | 219 | getReply(); |
ooglezoiden | 1:57e06832197a | 220 | strcpy(cmdbuff, "srv:listen("); |
ooglezoiden | 1:57e06832197a | 221 | strcat(cmdbuff, port); |
ooglezoiden | 1:57e06832197a | 222 | strcat(cmdbuff, ",function(conn)\r\n"); |
ooglezoiden | 1:57e06832197a | 223 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 224 | wait(.5); |
ooglezoiden | 1:57e06832197a | 225 | getReply(); |
ooglezoiden | 1:57e06832197a | 226 | strcpy(cmdbuff, "conn:on(\"receive\", function(conn, payload) \r\n"); |
ooglezoiden | 1:57e06832197a | 227 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 228 | wait(.5); |
ooglezoiden | 1:57e06832197a | 229 | getReply(); |
ooglezoiden | 1:57e06832197a | 230 | strcpy(cmdbuff, "print(payload)\r\n"); |
ooglezoiden | 1:57e06832197a | 231 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 232 | wait(.5); |
ooglezoiden | 1:57e06832197a | 233 | getReply(); |
ooglezoiden | 1:57e06832197a | 234 | strcpy(cmdbuff, "end)\r\n"); |
ooglezoiden | 1:57e06832197a | 235 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 236 | wait(.5); |
ooglezoiden | 1:57e06832197a | 237 | getReply(); |
ooglezoiden | 1:57e06832197a | 238 | strcpy(cmdbuff, "end)\r\n"); |
ooglezoiden | 1:57e06832197a | 239 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 240 | wait(.5); |
ooglezoiden | 1:57e06832197a | 241 | getReply(); |
ooglezoiden | 1:57e06832197a | 242 | } |
ooglezoiden | 1:57e06832197a | 243 | } |
ooglezoiden | 1:57e06832197a | 244 | |
ooglezoiden | 1:57e06832197a | 245 | |
ooglezoiden | 1:57e06832197a | 246 | |
ooglezoiden | 1:57e06832197a | 247 | // --------------- Main Function ---------------------- |
ooglezoiden | 1:57e06832197a | 248 | |
ooglezoiden | 1:57e06832197a | 249 | |
ooglezoiden | 1:57e06832197a | 250 | |
ooglezoiden | 0:397faf7a0a52 | 251 | int main() { |
ooglezoiden | 0:397faf7a0a52 | 252 | lights = 0; |
ooglezoiden | 1:57e06832197a | 253 | uLCD.printf("LIGHT_ALARM V2.0 Donn Green\n\n"); |
ooglezoiden | 0:397faf7a0a52 | 254 | uLCD.printf("Press Button 1 to begin light activation mode\n"); |
ooglezoiden | 0:397faf7a0a52 | 255 | uLCD.printf("Press Button 2 to begin timer activation mode\n"); |
ooglezoiden | 1:57e06832197a | 256 | uLCD.printf("Press Button 3 to begin smart home mode\n"); |
ooglezoiden | 0:397faf7a0a52 | 257 | pb1.mode(PullDown); |
ooglezoiden | 0:397faf7a0a52 | 258 | pb2.mode(PullDown); |
ooglezoiden | 1:57e06832197a | 259 | pb3.mode(PullDown); |
ooglezoiden | 0:397faf7a0a52 | 260 | lights.period(1.0f/25000.0f); |
ooglezoiden | 1:57e06832197a | 261 | RPCFunction rpcOn(&alexaLightsOn, "turnOn"); |
ooglezoiden | 1:57e06832197a | 262 | RPCFunction rpcOff(&alexaLightsOff, "turnOff"); |
ooglezoiden | 1:57e06832197a | 263 | RPCFunction rpcDim(&alexaDimLights, "dim"); |
ooglezoiden | 1:57e06832197a | 264 | RPCFunction rpcSet(&alexaTimer, "set"); |
ooglezoiden | 1:57e06832197a | 265 | while(!(pb1||pb2||pb3)){ |
ooglezoiden | 0:397faf7a0a52 | 266 | //do nothing until a button is pressed |
ooglezoiden | 0:397faf7a0a52 | 267 | } |
ooglezoiden | 0:397faf7a0a52 | 268 | if(pb1){ |
ooglezoiden | 0:397faf7a0a52 | 269 | lightMode(); |
ooglezoiden | 0:397faf7a0a52 | 270 | } |
ooglezoiden | 0:397faf7a0a52 | 271 | else if(pb2){ |
ooglezoiden | 0:397faf7a0a52 | 272 | int time = getTime(); |
ooglezoiden | 0:397faf7a0a52 | 273 | timerMode(time); |
ooglezoiden | 0:397faf7a0a52 | 274 | } |
ooglezoiden | 1:57e06832197a | 275 | else if(pb3){ |
ooglezoiden | 1:57e06832197a | 276 | alexaMode(); |
ooglezoiden | 1:57e06832197a | 277 | } |
ooglezoiden | 0:397faf7a0a52 | 278 | } |
ooglezoiden | 1:57e06832197a | 279 | |
ooglezoiden | 1:57e06832197a | 280 | |
ooglezoiden | 1:57e06832197a | 281 | |
ooglezoiden | 1:57e06832197a | 282 | // ------------ WIFI Functions --------------------- |
ooglezoiden | 1:57e06832197a | 283 | |
ooglezoiden | 1:57e06832197a | 284 | |
ooglezoiden | 1:57e06832197a | 285 | |
ooglezoiden | 1:57e06832197a | 286 | void connectToNetwork() { |
ooglezoiden | 1:57e06832197a | 287 | pc.printf("# Resetting ESP\r\n"); |
ooglezoiden | 1:57e06832197a | 288 | uLCD.cls(); |
ooglezoiden | 1:57e06832197a | 289 | uLCD.printf("# Resetting ESP\r\n"); |
ooglezoiden | 1:57e06832197a | 290 | strcpy(cmdbuff,"node.restart()\r\n"); |
ooglezoiden | 1:57e06832197a | 291 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 292 | wait(5); |
ooglezoiden | 1:57e06832197a | 293 | getReply(); |
ooglezoiden | 1:57e06832197a | 294 | |
ooglezoiden | 1:57e06832197a | 295 | led1=1,led2=0,led3=0; |
ooglezoiden | 1:57e06832197a | 296 | pc.printf("# Setting Mode\r\n"); |
ooglezoiden | 1:57e06832197a | 297 | uLCD.cls(); |
ooglezoiden | 1:57e06832197a | 298 | uLCD.printf("# Setting Mode\r\n"); |
ooglezoiden | 1:57e06832197a | 299 | strcpy(cmdbuff, "wifi.setmode(wifi.STATION)\r\n"); |
ooglezoiden | 1:57e06832197a | 300 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 301 | getReply(); |
ooglezoiden | 1:57e06832197a | 302 | |
ooglezoiden | 1:57e06832197a | 303 | wait(2); |
ooglezoiden | 1:57e06832197a | 304 | led1=0,led2=1,led3=0; |
ooglezoiden | 1:57e06832197a | 305 | pc.printf("# Connecting to AP\r\n"); |
ooglezoiden | 1:57e06832197a | 306 | pc.printf("# ssid = %s\t\tpwd = %s\r\n", ssid, pwd); |
ooglezoiden | 1:57e06832197a | 307 | uLCD.cls(); |
ooglezoiden | 1:57e06832197a | 308 | uLCD.printf("# ssid = %s\t\tpwd = %s\r\n"); |
ooglezoiden | 1:57e06832197a | 309 | strcpy(cmdbuff, "wifi.sta.config(\""); |
ooglezoiden | 1:57e06832197a | 310 | strcat(cmdbuff, ssid); |
ooglezoiden | 1:57e06832197a | 311 | strcat(cmdbuff, "\",\""); |
ooglezoiden | 1:57e06832197a | 312 | strcat(cmdbuff, pwd); |
ooglezoiden | 1:57e06832197a | 313 | strcat(cmdbuff, "\")\r\n"); |
ooglezoiden | 1:57e06832197a | 314 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 315 | getReply(); |
ooglezoiden | 1:57e06832197a | 316 | |
ooglezoiden | 1:57e06832197a | 317 | wait(2); |
ooglezoiden | 1:57e06832197a | 318 | led1=0,led2=0,led3=1; |
ooglezoiden | 1:57e06832197a | 319 | pc.printf("# Get IP Address\r\n"); |
ooglezoiden | 1:57e06832197a | 320 | uLCD.cls(); |
ooglezoiden | 1:57e06832197a | 321 | uLCD.printf("# Get IP Address\r\n"); |
ooglezoiden | 1:57e06832197a | 322 | strcpy(cmdbuff, "print(wifi.sta.getip())\r\n"); |
ooglezoiden | 1:57e06832197a | 323 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 324 | getReply(); |
ooglezoiden | 1:57e06832197a | 325 | |
ooglezoiden | 1:57e06832197a | 326 | wait(2); |
ooglezoiden | 1:57e06832197a | 327 | led1=1,led2=0,led3=0; |
ooglezoiden | 1:57e06832197a | 328 | pc.printf("# Get Connection Status\r\n"); |
ooglezoiden | 1:57e06832197a | 329 | uLCD.cls(); |
ooglezoiden | 1:57e06832197a | 330 | uLCD.printf("# Get Connection Status\r\n"); |
ooglezoiden | 1:57e06832197a | 331 | strcpy(cmdbuff, "print(wifi.sta.status())\r\n"); |
ooglezoiden | 1:57e06832197a | 332 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 333 | getReply(); |
ooglezoiden | 1:57e06832197a | 334 | |
ooglezoiden | 1:57e06832197a | 335 | wait(2); |
ooglezoiden | 1:57e06832197a | 336 | led1=0,led2=1,led3=0; |
ooglezoiden | 1:57e06832197a | 337 | pc.printf("# Listen on Port\r\n"); |
ooglezoiden | 1:57e06832197a | 338 | uLCD.cls(); |
ooglezoiden | 1:57e06832197a | 339 | uLCD.printf("# Listen on Port\r\n"); |
ooglezoiden | 1:57e06832197a | 340 | strcpy(cmdbuff, "srv=net.createServer(net.TCP,"); |
ooglezoiden | 1:57e06832197a | 341 | strcat(cmdbuff, timeout); |
ooglezoiden | 1:57e06832197a | 342 | strcat(cmdbuff, ")\r\n"); |
ooglezoiden | 1:57e06832197a | 343 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 344 | getReply(); |
ooglezoiden | 1:57e06832197a | 345 | |
ooglezoiden | 1:57e06832197a | 346 | wait(2); |
ooglezoiden | 1:57e06832197a | 347 | led1=0,led2=0,led3=1; |
ooglezoiden | 1:57e06832197a | 348 | strcpy(cmdbuff, "srv:listen("); |
ooglezoiden | 1:57e06832197a | 349 | strcat(cmdbuff, port); |
ooglezoiden | 1:57e06832197a | 350 | strcat(cmdbuff, ",function(conn)\r\n"); |
ooglezoiden | 1:57e06832197a | 351 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 352 | getReply(); |
ooglezoiden | 1:57e06832197a | 353 | |
ooglezoiden | 1:57e06832197a | 354 | wait(2); |
ooglezoiden | 1:57e06832197a | 355 | led1=1,led2=0,led3=0; |
ooglezoiden | 1:57e06832197a | 356 | strcpy(cmdbuff, "conn:on(\"receive\", function(conn, payload) \r\n"); |
ooglezoiden | 1:57e06832197a | 357 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 358 | getReply(); |
ooglezoiden | 1:57e06832197a | 359 | |
ooglezoiden | 1:57e06832197a | 360 | wait(2); |
ooglezoiden | 1:57e06832197a | 361 | led1=0,led2=1,led3=0; |
ooglezoiden | 1:57e06832197a | 362 | //strcpy(cmdbuff, "conn:send('"); |
ooglezoiden | 1:57e06832197a | 363 | //strcat(cmdbuff, reportStatus()); |
ooglezoiden | 1:57e06832197a | 364 | //strcat(cmdbuff, "')\r\n"); |
ooglezoiden | 1:57e06832197a | 365 | //sendCMD(); |
ooglezoiden | 1:57e06832197a | 366 | //getReply(); |
ooglezoiden | 1:57e06832197a | 367 | |
ooglezoiden | 1:57e06832197a | 368 | wait(2); |
ooglezoiden | 1:57e06832197a | 369 | led1=0,led2=0,led3=1; |
ooglezoiden | 1:57e06832197a | 370 | strcpy(cmdbuff, "print(payload)\r\n"); |
ooglezoiden | 1:57e06832197a | 371 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 372 | getReply(); |
ooglezoiden | 1:57e06832197a | 373 | |
ooglezoiden | 1:57e06832197a | 374 | wait(2); |
ooglezoiden | 1:57e06832197a | 375 | led1=1,led2=0,led3=0; |
ooglezoiden | 1:57e06832197a | 376 | strcpy(cmdbuff, "end)\r\n"); |
ooglezoiden | 1:57e06832197a | 377 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 378 | getReply(); |
ooglezoiden | 1:57e06832197a | 379 | |
ooglezoiden | 1:57e06832197a | 380 | wait(2); |
ooglezoiden | 1:57e06832197a | 381 | led1=0,led2=1,led3=0; |
ooglezoiden | 1:57e06832197a | 382 | strcpy(cmdbuff, "end)\r\n"); |
ooglezoiden | 1:57e06832197a | 383 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 384 | getReply(); |
ooglezoiden | 1:57e06832197a | 385 | |
ooglezoiden | 1:57e06832197a | 386 | strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n"); |
ooglezoiden | 1:57e06832197a | 387 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 388 | getReply(); |
ooglezoiden | 1:57e06832197a | 389 | wait(0.2); |
ooglezoiden | 1:57e06832197a | 390 | strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n"); |
ooglezoiden | 1:57e06832197a | 391 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 392 | getReply(); |
ooglezoiden | 1:57e06832197a | 393 | wait(0.2); |
ooglezoiden | 1:57e06832197a | 394 | strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n"); |
ooglezoiden | 1:57e06832197a | 395 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 396 | getReply(); |
ooglezoiden | 1:57e06832197a | 397 | wait(0.2); |
ooglezoiden | 1:57e06832197a | 398 | strcpy(cmdbuff, "else\r\n"); |
ooglezoiden | 1:57e06832197a | 399 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 400 | getReply(); |
ooglezoiden | 1:57e06832197a | 401 | wait(0.2); |
ooglezoiden | 1:57e06832197a | 402 | strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n"); |
ooglezoiden | 1:57e06832197a | 403 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 404 | getReply(); |
ooglezoiden | 1:57e06832197a | 405 | wait(0.2); |
ooglezoiden | 1:57e06832197a | 406 | strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n"); |
ooglezoiden | 1:57e06832197a | 407 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 408 | getReply(); |
ooglezoiden | 1:57e06832197a | 409 | wait(0.2); |
ooglezoiden | 1:57e06832197a | 410 | strcpy(cmdbuff,"tmr.stop(0)\r\n"); |
ooglezoiden | 1:57e06832197a | 411 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 412 | getReply(); |
ooglezoiden | 1:57e06832197a | 413 | wait(0.2); |
ooglezoiden | 1:57e06832197a | 414 | strcpy(cmdbuff,"end\r\n"); |
ooglezoiden | 1:57e06832197a | 415 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 416 | getReply(); |
ooglezoiden | 1:57e06832197a | 417 | wait(0.2); |
ooglezoiden | 1:57e06832197a | 418 | strcpy(cmdbuff,"end)\r\n"); |
ooglezoiden | 1:57e06832197a | 419 | sendCMD(); |
ooglezoiden | 1:57e06832197a | 420 | getReply(); |
ooglezoiden | 1:57e06832197a | 421 | wait(0.2); |
ooglezoiden | 1:57e06832197a | 422 | |
ooglezoiden | 1:57e06832197a | 423 | pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); |
ooglezoiden | 1:57e06832197a | 424 | } |
ooglezoiden | 1:57e06832197a | 425 | |
ooglezoiden | 1:57e06832197a | 426 | void sendCMD() |
ooglezoiden | 1:57e06832197a | 427 | { |
ooglezoiden | 1:57e06832197a | 428 | int i; |
ooglezoiden | 1:57e06832197a | 429 | char temp_char; |
ooglezoiden | 1:57e06832197a | 430 | bool empty; |
ooglezoiden | 1:57e06832197a | 431 | i = 0; |
ooglezoiden | 1:57e06832197a | 432 | // Start Critical Section - don't interrupt while changing global buffer variables |
ooglezoiden | 1:57e06832197a | 433 | NVIC_DisableIRQ(UART1_IRQn); |
ooglezoiden | 1:57e06832197a | 434 | empty = (tx_in == tx_out); |
ooglezoiden | 1:57e06832197a | 435 | while ((i==0) || (cmdbuff[i-1] != '\n')) { |
ooglezoiden | 1:57e06832197a | 436 | // Wait if buffer full |
ooglezoiden | 1:57e06832197a | 437 | if (((tx_in + 1) % buffer_size) == tx_out) { |
ooglezoiden | 1:57e06832197a | 438 | // End Critical Section - need to let interrupt routine empty buffer by sending |
ooglezoiden | 1:57e06832197a | 439 | NVIC_EnableIRQ(UART1_IRQn); |
ooglezoiden | 1:57e06832197a | 440 | while (((tx_in + 1) % buffer_size) == tx_out) { |
ooglezoiden | 1:57e06832197a | 441 | } |
ooglezoiden | 1:57e06832197a | 442 | // Start Critical Section - don't interrupt while changing global buffer variables |
ooglezoiden | 1:57e06832197a | 443 | NVIC_DisableIRQ(UART1_IRQn); |
ooglezoiden | 1:57e06832197a | 444 | } |
ooglezoiden | 1:57e06832197a | 445 | tx_buffer[tx_in] = cmdbuff[i]; |
ooglezoiden | 1:57e06832197a | 446 | i++; |
ooglezoiden | 1:57e06832197a | 447 | tx_in = (tx_in + 1) % buffer_size; |
ooglezoiden | 1:57e06832197a | 448 | } |
ooglezoiden | 1:57e06832197a | 449 | if (esp.writeable() && (empty)) { |
ooglezoiden | 1:57e06832197a | 450 | temp_char = tx_buffer[tx_out]; |
ooglezoiden | 1:57e06832197a | 451 | tx_out = (tx_out + 1) % buffer_size; |
ooglezoiden | 1:57e06832197a | 452 | // Send first character to start tx interrupts, if stopped |
ooglezoiden | 1:57e06832197a | 453 | esp.putc(temp_char); |
ooglezoiden | 1:57e06832197a | 454 | } |
ooglezoiden | 1:57e06832197a | 455 | // End Critical Section |
ooglezoiden | 1:57e06832197a | 456 | NVIC_EnableIRQ(UART1_IRQn); |
ooglezoiden | 1:57e06832197a | 457 | return; |
ooglezoiden | 1:57e06832197a | 458 | } |
ooglezoiden | 1:57e06832197a | 459 | |
ooglezoiden | 1:57e06832197a | 460 | // Read a line from the large rx buffer from rx interrupt routine |
ooglezoiden | 1:57e06832197a | 461 | void getReply() { |
ooglezoiden | 1:57e06832197a | 462 | int i; |
ooglezoiden | 1:57e06832197a | 463 | i = 0; |
ooglezoiden | 1:57e06832197a | 464 | // Start Critical Section - don't interrupt while changing global buffer variables |
ooglezoiden | 1:57e06832197a | 465 | NVIC_DisableIRQ(UART1_IRQn); |
ooglezoiden | 1:57e06832197a | 466 | // Loop reading rx buffer characters until end of line character |
ooglezoiden | 1:57e06832197a | 467 | while ((i==0) || (rx_line[i-1] != '\r')) { |
ooglezoiden | 1:57e06832197a | 468 | // Wait if buffer empty |
ooglezoiden | 1:57e06832197a | 469 | if (rx_in == rx_out) { |
ooglezoiden | 1:57e06832197a | 470 | // End Critical Section - need to allow rx interrupt to get new characters for buffer |
ooglezoiden | 1:57e06832197a | 471 | NVIC_EnableIRQ(UART1_IRQn); |
ooglezoiden | 1:57e06832197a | 472 | while (rx_in == rx_out) { |
ooglezoiden | 1:57e06832197a | 473 | } |
ooglezoiden | 1:57e06832197a | 474 | // Start Critical Section - don't interrupt while changing global buffer variables |
ooglezoiden | 1:57e06832197a | 475 | NVIC_DisableIRQ(UART1_IRQn); |
ooglezoiden | 1:57e06832197a | 476 | } |
ooglezoiden | 1:57e06832197a | 477 | rx_line[i] = rx_buffer[rx_out]; |
ooglezoiden | 1:57e06832197a | 478 | i++; |
ooglezoiden | 1:57e06832197a | 479 | rx_out = (rx_out + 1) % buffer_size; |
ooglezoiden | 1:57e06832197a | 480 | } |
ooglezoiden | 1:57e06832197a | 481 | // End Critical Section |
ooglezoiden | 1:57e06832197a | 482 | NVIC_EnableIRQ(UART1_IRQn); |
ooglezoiden | 1:57e06832197a | 483 | rx_line[i-1] = 0; |
ooglezoiden | 1:57e06832197a | 484 | return; |
ooglezoiden | 1:57e06832197a | 485 | } |
ooglezoiden | 1:57e06832197a | 486 | |
ooglezoiden | 1:57e06832197a | 487 | // Interupt Routine to read in data from serial port |
ooglezoiden | 1:57e06832197a | 488 | void Rx_interrupt() { |
ooglezoiden | 1:57e06832197a | 489 | //led3=1; |
ooglezoiden | 1:57e06832197a | 490 | // Loop just in case more than one character is in UART's receive FIFO buffer |
ooglezoiden | 1:57e06832197a | 491 | // Stop if buffer full |
ooglezoiden | 1:57e06832197a | 492 | while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { |
ooglezoiden | 1:57e06832197a | 493 | rx_buffer[rx_in] = esp.getc(); |
ooglezoiden | 1:57e06832197a | 494 | // Uncomment to Echo to USB serial to watch data flow |
ooglezoiden | 1:57e06832197a | 495 | pc.putc(rx_buffer[rx_in]); |
ooglezoiden | 1:57e06832197a | 496 | rx_in = (rx_in + 1) % buffer_size; |
ooglezoiden | 1:57e06832197a | 497 | } |
ooglezoiden | 1:57e06832197a | 498 | return; |
ooglezoiden | 1:57e06832197a | 499 | } |
ooglezoiden | 1:57e06832197a | 500 | |
ooglezoiden | 1:57e06832197a | 501 | |
ooglezoiden | 1:57e06832197a | 502 | // Interupt Routine to write out data to serial port |
ooglezoiden | 1:57e06832197a | 503 | void Tx_interrupt() { |
ooglezoiden | 1:57e06832197a | 504 | //led2=1; |
ooglezoiden | 1:57e06832197a | 505 | // Loop to fill more than one character in UART's transmit FIFO buffer |
ooglezoiden | 1:57e06832197a | 506 | // Stop if buffer empty |
ooglezoiden | 1:57e06832197a | 507 | while ((esp.writeable()) && (tx_in != tx_out)) { |
ooglezoiden | 1:57e06832197a | 508 | esp.putc(tx_buffer[tx_out]); |
ooglezoiden | 1:57e06832197a | 509 | tx_out = (tx_out + 1) % buffer_size; |
ooglezoiden | 1:57e06832197a | 510 | } |
ooglezoiden | 1:57e06832197a | 511 | //led2=0; |
ooglezoiden | 1:57e06832197a | 512 | return; |
ooglezoiden | 1:57e06832197a | 513 | } |