Boiler
Dependencies: mbed
main.cpp@0:39d9e1d04d26, 2016-04-09 (annotated)
- Committer:
- DaniusKalv
- Date:
- Sat Apr 09 22:02:15 2016 +0000
- Revision:
- 0:39d9e1d04d26
boilMe
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DaniusKalv | 0:39d9e1d04d26 | 1 | #include "mbed.h" |
DaniusKalv | 0:39d9e1d04d26 | 2 | #include <string> |
DaniusKalv | 0:39d9e1d04d26 | 3 | |
DaniusKalv | 0:39d9e1d04d26 | 4 | Serial pc(USBTX, USBRX); |
DaniusKalv | 0:39d9e1d04d26 | 5 | Serial wifi(p28,p27); |
DaniusKalv | 0:39d9e1d04d26 | 6 | Timer t; |
DaniusKalv | 0:39d9e1d04d26 | 7 | |
DaniusKalv | 0:39d9e1d04d26 | 8 | DigitalOut myled(LED1); |
DaniusKalv | 0:39d9e1d04d26 | 9 | |
DaniusKalv | 0:39d9e1d04d26 | 10 | int initializeWifi(); |
DaniusKalv | 0:39d9e1d04d26 | 11 | |
DaniusKalv | 0:39d9e1d04d26 | 12 | void serverResponse(); |
DaniusKalv | 0:39d9e1d04d26 | 13 | |
DaniusKalv | 0:39d9e1d04d26 | 14 | int main() { |
DaniusKalv | 0:39d9e1d04d26 | 15 | pc.baud(115200); |
DaniusKalv | 0:39d9e1d04d26 | 16 | wifi.baud(115200); |
DaniusKalv | 0:39d9e1d04d26 | 17 | pc.printf("Ready to boil!\r\n"); |
DaniusKalv | 0:39d9e1d04d26 | 18 | initializeWifi(); |
DaniusKalv | 0:39d9e1d04d26 | 19 | |
DaniusKalv | 0:39d9e1d04d26 | 20 | char buffer[50]; |
DaniusKalv | 0:39d9e1d04d26 | 21 | int bufferIndex = 0; |
DaniusKalv | 0:39d9e1d04d26 | 22 | |
DaniusKalv | 0:39d9e1d04d26 | 23 | while(1) { |
DaniusKalv | 0:39d9e1d04d26 | 24 | |
DaniusKalv | 0:39d9e1d04d26 | 25 | if(pc.readable()){ |
DaniusKalv | 0:39d9e1d04d26 | 26 | wifi.putc(pc.getc()); |
DaniusKalv | 0:39d9e1d04d26 | 27 | } |
DaniusKalv | 0:39d9e1d04d26 | 28 | |
DaniusKalv | 0:39d9e1d04d26 | 29 | if(wifi.readable()){ |
DaniusKalv | 0:39d9e1d04d26 | 30 | char c = wifi.getc(); |
DaniusKalv | 0:39d9e1d04d26 | 31 | pc.putc(c); |
DaniusKalv | 0:39d9e1d04d26 | 32 | if (c != 10){ |
DaniusKalv | 0:39d9e1d04d26 | 33 | buffer[bufferIndex] = c; |
DaniusKalv | 0:39d9e1d04d26 | 34 | bufferIndex++; |
DaniusKalv | 0:39d9e1d04d26 | 35 | } |
DaniusKalv | 0:39d9e1d04d26 | 36 | else{ |
DaniusKalv | 0:39d9e1d04d26 | 37 | if(strstr(buffer, "Alive") != NULL){ |
DaniusKalv | 0:39d9e1d04d26 | 38 | pc.printf("Yayy\r\n"); |
DaniusKalv | 0:39d9e1d04d26 | 39 | serverResponse();//Do the response |
DaniusKalv | 0:39d9e1d04d26 | 40 | } |
DaniusKalv | 0:39d9e1d04d26 | 41 | memset(buffer, 0, 50); |
DaniusKalv | 0:39d9e1d04d26 | 42 | bufferIndex = 0; |
DaniusKalv | 0:39d9e1d04d26 | 43 | } |
DaniusKalv | 0:39d9e1d04d26 | 44 | } |
DaniusKalv | 0:39d9e1d04d26 | 45 | } |
DaniusKalv | 0:39d9e1d04d26 | 46 | } |
DaniusKalv | 0:39d9e1d04d26 | 47 | |
DaniusKalv | 0:39d9e1d04d26 | 48 | int initializeWifi(){ |
DaniusKalv | 0:39d9e1d04d26 | 49 | t.reset(); |
DaniusKalv | 0:39d9e1d04d26 | 50 | t.start(); |
DaniusKalv | 0:39d9e1d04d26 | 51 | wifi.printf("AT+RST\r\n"); |
DaniusKalv | 0:39d9e1d04d26 | 52 | while(t.read() < 0.2){ |
DaniusKalv | 0:39d9e1d04d26 | 53 | while(wifi.readable()) pc.putc(wifi.getc()); |
DaniusKalv | 0:39d9e1d04d26 | 54 | } |
DaniusKalv | 0:39d9e1d04d26 | 55 | t.stop(); |
DaniusKalv | 0:39d9e1d04d26 | 56 | wifi.printf("AT+CWJAP=\"Liutauras WiFi\",\"12345678\"\r\n"); |
DaniusKalv | 0:39d9e1d04d26 | 57 | t.reset(); |
DaniusKalv | 0:39d9e1d04d26 | 58 | t.start(); |
DaniusKalv | 0:39d9e1d04d26 | 59 | while(t.read() < 7){ |
DaniusKalv | 0:39d9e1d04d26 | 60 | while(wifi.readable()) pc.putc(wifi.getc()); |
DaniusKalv | 0:39d9e1d04d26 | 61 | } |
DaniusKalv | 0:39d9e1d04d26 | 62 | t.stop(); |
DaniusKalv | 0:39d9e1d04d26 | 63 | t.reset(); |
DaniusKalv | 0:39d9e1d04d26 | 64 | wifi.printf("AT+CIFSR\r\n"); |
DaniusKalv | 0:39d9e1d04d26 | 65 | t.start(); |
DaniusKalv | 0:39d9e1d04d26 | 66 | while(t.read() < 0.2){ |
DaniusKalv | 0:39d9e1d04d26 | 67 | while(wifi.readable()) pc.putc(wifi.getc()); |
DaniusKalv | 0:39d9e1d04d26 | 68 | } |
DaniusKalv | 0:39d9e1d04d26 | 69 | |
DaniusKalv | 0:39d9e1d04d26 | 70 | t.stop(); |
DaniusKalv | 0:39d9e1d04d26 | 71 | t.reset(); |
DaniusKalv | 0:39d9e1d04d26 | 72 | wifi.printf("AT+CIPMUX=1\r\n"); |
DaniusKalv | 0:39d9e1d04d26 | 73 | t.start(); |
DaniusKalv | 0:39d9e1d04d26 | 74 | while(t.read() < 0.2){ |
DaniusKalv | 0:39d9e1d04d26 | 75 | while(wifi.readable()) pc.putc(wifi.getc()); |
DaniusKalv | 0:39d9e1d04d26 | 76 | } |
DaniusKalv | 0:39d9e1d04d26 | 77 | |
DaniusKalv | 0:39d9e1d04d26 | 78 | t.stop(); |
DaniusKalv | 0:39d9e1d04d26 | 79 | t.reset(); |
DaniusKalv | 0:39d9e1d04d26 | 80 | wifi.printf("AT+CIPSERVER=1,80\r\n"); |
DaniusKalv | 0:39d9e1d04d26 | 81 | t.start(); |
DaniusKalv | 0:39d9e1d04d26 | 82 | while(t.read() < 0.2){ |
DaniusKalv | 0:39d9e1d04d26 | 83 | while(wifi.readable()) pc.putc(wifi.getc()); |
DaniusKalv | 0:39d9e1d04d26 | 84 | } |
DaniusKalv | 0:39d9e1d04d26 | 85 | return true; |
DaniusKalv | 0:39d9e1d04d26 | 86 | } |
DaniusKalv | 0:39d9e1d04d26 | 87 | |
DaniusKalv | 0:39d9e1d04d26 | 88 | void serverResponse(){ |
DaniusKalv | 0:39d9e1d04d26 | 89 | string temp1 = "HTTP/1.1 200 OK\r\n"; |
DaniusKalv | 0:39d9e1d04d26 | 90 | string temp2 = "Server: SuperBoiler\r\n"; |
DaniusKalv | 0:39d9e1d04d26 | 91 | string temp3 = "Content-Type: application/json\r\n"; |
DaniusKalv | 0:39d9e1d04d26 | 92 | string temp4 = "Connection: Closed\r\n"; |
DaniusKalv | 0:39d9e1d04d26 | 93 | string temp5 = "\"message\":\"BoilMe\""; |
DaniusKalv | 0:39d9e1d04d26 | 94 | |
DaniusKalv | 0:39d9e1d04d26 | 95 | wifi.printf("AT+CIPSEND=0,%d\r\n", sizeof(temp1)+sizeof(temp2)+sizeof(temp3)+sizeof(temp4)+sizeof(temp5)); |
DaniusKalv | 0:39d9e1d04d26 | 96 | t.reset(); |
DaniusKalv | 0:39d9e1d04d26 | 97 | t.start(); |
DaniusKalv | 0:39d9e1d04d26 | 98 | while(t.read() < 4){ |
DaniusKalv | 0:39d9e1d04d26 | 99 | while(wifi.readable()) pc.putc(wifi.getc()); |
DaniusKalv | 0:39d9e1d04d26 | 100 | } |
DaniusKalv | 0:39d9e1d04d26 | 101 | t.stop(); |
DaniusKalv | 0:39d9e1d04d26 | 102 | pc.printf("%s", temp1); |
DaniusKalv | 0:39d9e1d04d26 | 103 | pc.printf("%s", temp2); |
DaniusKalv | 0:39d9e1d04d26 | 104 | pc.printf("%s", temp3); |
DaniusKalv | 0:39d9e1d04d26 | 105 | pc.printf("%s", temp4); |
DaniusKalv | 0:39d9e1d04d26 | 106 | pc.printf("%s", temp5); |
DaniusKalv | 0:39d9e1d04d26 | 107 | wifi.printf("%s", temp1); |
DaniusKalv | 0:39d9e1d04d26 | 108 | wifi.printf("%s", temp2); |
DaniusKalv | 0:39d9e1d04d26 | 109 | wifi.printf("%s", temp3); |
DaniusKalv | 0:39d9e1d04d26 | 110 | wifi.printf("%s", temp4); |
DaniusKalv | 0:39d9e1d04d26 | 111 | wifi.printf("%s", temp5); |
DaniusKalv | 0:39d9e1d04d26 | 112 | |
DaniusKalv | 0:39d9e1d04d26 | 113 | //wifi.printf("HTTP/1.1 200 OK\r\n"); |
DaniusKalv | 0:39d9e1d04d26 | 114 | //wifi.printf("Server: SuperBoiler\r\n"); |
DaniusKalv | 0:39d9e1d04d26 | 115 | //wifi.printf("Content-Length: 88\r\n"); |
DaniusKalv | 0:39d9e1d04d26 | 116 | //wifi.printf("Content-Type: application/json\r\n"); |
DaniusKalv | 0:39d9e1d04d26 | 117 | //wifi.printf("Connection: Closed\r\n"); |
DaniusKalv | 0:39d9e1d04d26 | 118 | //wifi.printf("\"message\":\"BoilMe\""); |
DaniusKalv | 0:39d9e1d04d26 | 119 | } |