Gabriele Di Simone
/
NUCLEO-F401RE_ROPE_WIFI_SLAVE
Slave program
main.cpp@0:79a1c40a9a1b, 2015-11-16 (annotated)
- Committer:
- GabDiSi
- Date:
- Mon Nov 16 19:57:54 2015 +0000
- Revision:
- 0:79a1c40a9a1b
V0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GabDiSi | 0:79a1c40a9a1b | 1 | #include <string.h> |
GabDiSi | 0:79a1c40a9a1b | 2 | #include "mbed.h" |
GabDiSi | 0:79a1c40a9a1b | 3 | |
GabDiSi | 0:79a1c40a9a1b | 4 | Serial pc(USBTX, USBRX); // tx, rx |
GabDiSi | 0:79a1c40a9a1b | 5 | Serial serial6(PA_11, PA_12); // tx, rx |
GabDiSi | 0:79a1c40a9a1b | 6 | DigitalOut led1(LED1); |
GabDiSi | 0:79a1c40a9a1b | 7 | |
GabDiSi | 0:79a1c40a9a1b | 8 | int ESP8266limitResponse = 999999; |
GabDiSi | 0:79a1c40a9a1b | 9 | |
GabDiSi | 0:79a1c40a9a1b | 10 | char ESP8266responseOkStr[] = "OK\r\n"; |
GabDiSi | 0:79a1c40a9a1b | 11 | char ESP8266responseGetStr[] = "OK\r\n> "; |
GabDiSi | 0:79a1c40a9a1b | 12 | char ESP8266responseReadyStr[] = "ready\r\n"; |
GabDiSi | 0:79a1c40a9a1b | 13 | char ESP8266responseSendStr[] = "SEND OK\r\n\r\n"; |
GabDiSi | 0:79a1c40a9a1b | 14 | char ESP8266responseClosedStr[] = "CLOSED\r\n"; |
GabDiSi | 0:79a1c40a9a1b | 15 | |
GabDiSi | 0:79a1c40a9a1b | 16 | char ESP8266initStr[] = "ROPE WIFI - TECHNOLOGY DAY"; |
GabDiSi | 0:79a1c40a9a1b | 17 | char ESP8266doneStr[] = "done!"; |
GabDiSi | 0:79a1c40a9a1b | 18 | char ESP8266resetStr[] = "Init ESP8266... "; |
GabDiSi | 0:79a1c40a9a1b | 19 | char ESP8266clientModeStr[] = "Client mode... "; |
GabDiSi | 0:79a1c40a9a1b | 20 | char ESP8266wifiConnectionStr[] = "WiFi connection... "; |
GabDiSi | 0:79a1c40a9a1b | 21 | char ESP8266serverConnectionStr[] = "Server connection... "; |
GabDiSi | 0:79a1c40a9a1b | 22 | char ESP8266sendingCharsStr[] = "Sending chars... "; |
GabDiSi | 0:79a1c40a9a1b | 23 | char ESP8266sendingRequestStr[] = "Sending request... "; |
GabDiSi | 0:79a1c40a9a1b | 24 | |
GabDiSi | 0:79a1c40a9a1b | 25 | int ESP8266readResponse(char* delimiter) { |
GabDiSi | 0:79a1c40a9a1b | 26 | int i = 0, j = 0; |
GabDiSi | 0:79a1c40a9a1b | 27 | char c, exc; |
GabDiSi | 0:79a1c40a9a1b | 28 | |
GabDiSi | 0:79a1c40a9a1b | 29 | while(j<ESP8266limitResponse) { |
GabDiSi | 0:79a1c40a9a1b | 30 | |
GabDiSi | 0:79a1c40a9a1b | 31 | c = serial6.getc(); |
GabDiSi | 0:79a1c40a9a1b | 32 | pc.putc(c); |
GabDiSi | 0:79a1c40a9a1b | 33 | |
GabDiSi | 0:79a1c40a9a1b | 34 | if(c == delimiter[i]) { |
GabDiSi | 0:79a1c40a9a1b | 35 | i++; |
GabDiSi | 0:79a1c40a9a1b | 36 | |
GabDiSi | 0:79a1c40a9a1b | 37 | if(delimiter[i] == '\0') { |
GabDiSi | 0:79a1c40a9a1b | 38 | return (int) (exc-48); |
GabDiSi | 0:79a1c40a9a1b | 39 | }; |
GabDiSi | 0:79a1c40a9a1b | 40 | } else { |
GabDiSi | 0:79a1c40a9a1b | 41 | exc = c; |
GabDiSi | 0:79a1c40a9a1b | 42 | i = 0; |
GabDiSi | 0:79a1c40a9a1b | 43 | }; |
GabDiSi | 0:79a1c40a9a1b | 44 | }; |
GabDiSi | 0:79a1c40a9a1b | 45 | |
GabDiSi | 0:79a1c40a9a1b | 46 | return 0; |
GabDiSi | 0:79a1c40a9a1b | 47 | }; |
GabDiSi | 0:79a1c40a9a1b | 48 | |
GabDiSi | 0:79a1c40a9a1b | 49 | void ESP8266init(char* ssid, char* password) { |
GabDiSi | 0:79a1c40a9a1b | 50 | led1 = 1; |
GabDiSi | 0:79a1c40a9a1b | 51 | |
GabDiSi | 0:79a1c40a9a1b | 52 | pc.baud(115200); |
GabDiSi | 0:79a1c40a9a1b | 53 | serial6.baud(115200); |
GabDiSi | 0:79a1c40a9a1b | 54 | |
GabDiSi | 0:79a1c40a9a1b | 55 | pc.printf("%s\r\n", ESP8266initStr); |
GabDiSi | 0:79a1c40a9a1b | 56 | |
GabDiSi | 0:79a1c40a9a1b | 57 | pc.printf("%s", ESP8266resetStr); |
GabDiSi | 0:79a1c40a9a1b | 58 | serial6.printf("AT+RST\r\n"); |
GabDiSi | 0:79a1c40a9a1b | 59 | |
GabDiSi | 0:79a1c40a9a1b | 60 | ESP8266readResponse(ESP8266responseOkStr); |
GabDiSi | 0:79a1c40a9a1b | 61 | ESP8266readResponse(ESP8266responseReadyStr); |
GabDiSi | 0:79a1c40a9a1b | 62 | pc.printf("%s\r\n", ESP8266doneStr); |
GabDiSi | 0:79a1c40a9a1b | 63 | led1 = 0; |
GabDiSi | 0:79a1c40a9a1b | 64 | pc.printf("%s", ESP8266clientModeStr); |
GabDiSi | 0:79a1c40a9a1b | 65 | serial6.printf("AT+CWMODE=1\r\n"); |
GabDiSi | 0:79a1c40a9a1b | 66 | ESP8266readResponse(ESP8266responseOkStr); |
GabDiSi | 0:79a1c40a9a1b | 67 | pc.printf("%s\r\n", ESP8266doneStr); |
GabDiSi | 0:79a1c40a9a1b | 68 | |
GabDiSi | 0:79a1c40a9a1b | 69 | pc.printf("%s", ESP8266wifiConnectionStr); |
GabDiSi | 0:79a1c40a9a1b | 70 | serial6.printf("AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, password); |
GabDiSi | 0:79a1c40a9a1b | 71 | ESP8266readResponse(ESP8266responseOkStr); |
GabDiSi | 0:79a1c40a9a1b | 72 | pc.printf("%s\r\n", ESP8266doneStr); |
GabDiSi | 0:79a1c40a9a1b | 73 | |
GabDiSi | 0:79a1c40a9a1b | 74 | led1 = 0; |
GabDiSi | 0:79a1c40a9a1b | 75 | |
GabDiSi | 0:79a1c40a9a1b | 76 | }; |
GabDiSi | 0:79a1c40a9a1b | 77 | |
GabDiSi | 0:79a1c40a9a1b | 78 | int ESP8266get(char* domain, char* url) { |
GabDiSi | 0:79a1c40a9a1b | 79 | int n1, n2; |
GabDiSi | 0:79a1c40a9a1b | 80 | |
GabDiSi | 0:79a1c40a9a1b | 81 | n1 = strlen(domain); |
GabDiSi | 0:79a1c40a9a1b | 82 | n2 = strlen(url); |
GabDiSi | 0:79a1c40a9a1b | 83 | |
GabDiSi | 0:79a1c40a9a1b | 84 | pc.printf("%s", ESP8266serverConnectionStr); |
GabDiSi | 0:79a1c40a9a1b | 85 | serial6.printf("AT+CIPSTART=\"TCP\",\"%s\",80\r\n", domain); |
GabDiSi | 0:79a1c40a9a1b | 86 | ESP8266readResponse(ESP8266responseOkStr); |
GabDiSi | 0:79a1c40a9a1b | 87 | pc.printf("%s\r\n", ESP8266doneStr); |
GabDiSi | 0:79a1c40a9a1b | 88 | |
GabDiSi | 0:79a1c40a9a1b | 89 | pc.printf("%s", ESP8266sendingCharsStr); |
GabDiSi | 0:79a1c40a9a1b | 90 | serial6.printf("AT+CIPSEND=%d\r\n", 11+n1+1+n2+13); |
GabDiSi | 0:79a1c40a9a1b | 91 | ESP8266readResponse(ESP8266responseGetStr); |
GabDiSi | 0:79a1c40a9a1b | 92 | pc.printf("%s\r\n", ESP8266doneStr); |
GabDiSi | 0:79a1c40a9a1b | 93 | |
GabDiSi | 0:79a1c40a9a1b | 94 | pc.printf("%s", ESP8266sendingRequestStr); |
GabDiSi | 0:79a1c40a9a1b | 95 | serial6.printf("GET http://%s/%s HTTP/1.0\r\n\r\n", domain, url); |
GabDiSi | 0:79a1c40a9a1b | 96 | ESP8266readResponse(ESP8266responseSendStr); |
GabDiSi | 0:79a1c40a9a1b | 97 | pc.printf("%s\r\n", ESP8266doneStr); |
GabDiSi | 0:79a1c40a9a1b | 98 | |
GabDiSi | 0:79a1c40a9a1b | 99 | return ESP8266readResponse(ESP8266responseClosedStr); |
GabDiSi | 0:79a1c40a9a1b | 100 | }; |
GabDiSi | 0:79a1c40a9a1b | 101 | |
GabDiSi | 0:79a1c40a9a1b | 102 | void init() { |
GabDiSi | 0:79a1c40a9a1b | 103 | ESP8266init("Telecom-47520601", "famigliaquino2000router10"); |
GabDiSi | 0:79a1c40a9a1b | 104 | }; |
GabDiSi | 0:79a1c40a9a1b | 105 | |
GabDiSi | 0:79a1c40a9a1b | 106 | void checkState() { |
GabDiSi | 0:79a1c40a9a1b | 107 | int response; |
GabDiSi | 0:79a1c40a9a1b | 108 | |
GabDiSi | 0:79a1c40a9a1b | 109 | while(1) { |
GabDiSi | 0:79a1c40a9a1b | 110 | char domain[] = "gabrieledisimone.it"; |
GabDiSi | 0:79a1c40a9a1b | 111 | char url[] = "neapolisRead.php"; |
GabDiSi | 0:79a1c40a9a1b | 112 | |
GabDiSi | 0:79a1c40a9a1b | 113 | response = ESP8266get(domain, url); |
GabDiSi | 0:79a1c40a9a1b | 114 | pc.printf("response:%d\r\n", response); |
GabDiSi | 0:79a1c40a9a1b | 115 | |
GabDiSi | 0:79a1c40a9a1b | 116 | switch(response) { |
GabDiSi | 0:79a1c40a9a1b | 117 | case 1: |
GabDiSi | 0:79a1c40a9a1b | 118 | |
GabDiSi | 0:79a1c40a9a1b | 119 | break; |
GabDiSi | 0:79a1c40a9a1b | 120 | |
GabDiSi | 0:79a1c40a9a1b | 121 | case 2: |
GabDiSi | 0:79a1c40a9a1b | 122 | |
GabDiSi | 0:79a1c40a9a1b | 123 | break; |
GabDiSi | 0:79a1c40a9a1b | 124 | |
GabDiSi | 0:79a1c40a9a1b | 125 | case 3: |
GabDiSi | 0:79a1c40a9a1b | 126 | |
GabDiSi | 0:79a1c40a9a1b | 127 | break; |
GabDiSi | 0:79a1c40a9a1b | 128 | |
GabDiSi | 0:79a1c40a9a1b | 129 | case 4: |
GabDiSi | 0:79a1c40a9a1b | 130 | |
GabDiSi | 0:79a1c40a9a1b | 131 | break; |
GabDiSi | 0:79a1c40a9a1b | 132 | |
GabDiSi | 0:79a1c40a9a1b | 133 | default: |
GabDiSi | 0:79a1c40a9a1b | 134 | |
GabDiSi | 0:79a1c40a9a1b | 135 | break; |
GabDiSi | 0:79a1c40a9a1b | 136 | }; |
GabDiSi | 0:79a1c40a9a1b | 137 | wait(3); |
GabDiSi | 0:79a1c40a9a1b | 138 | }; |
GabDiSi | 0:79a1c40a9a1b | 139 | }; |
GabDiSi | 0:79a1c40a9a1b | 140 | |
GabDiSi | 0:79a1c40a9a1b | 141 | int main() { |
GabDiSi | 0:79a1c40a9a1b | 142 | init(); |
GabDiSi | 0:79a1c40a9a1b | 143 | checkState(); |
GabDiSi | 0:79a1c40a9a1b | 144 | }; |