Franz Pucher
/
ESP8266_HTTP_MQTT_Server
M0 communication to configurable-Web-Server (MQTT) Version 0.1
- Arduino IDE
- Sensoren
- ESP-Programmierung
- Web-Server
- Netzwerktopologie
- Browser basierende Kommunikation
- M2M Kommunikation
- MQTT over TLS-M3-Projekt (1/2018)
Siehe auch FTKL-Tagung 2016
main.cpp@2:63135b94c898, 2018-01-14 (annotated)
- Committer:
- fpucher
- Date:
- Sun Jan 14 19:49:08 2018 +0000
- Revision:
- 2:63135b94c898
- Parent:
- 1:b9fd13f34c2d
- Child:
- 3:c14eb9159a88
New Program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fpucher | 2:63135b94c898 | 1 | #include "mbed.h" |
fpucher | 2:63135b94c898 | 2 | #include <stdio.h> |
fpucher | 2:63135b94c898 | 3 | #include <string> |
fpucher | 2:63135b94c898 | 4 | //using namespace std; |
fpucher | 0:d764cafa5989 | 5 | |
fpucher | 1:b9fd13f34c2d | 6 | DigitalOut LedD1(LED1); |
fpucher | 1:b9fd13f34c2d | 7 | DigitalOut LedD2(LED2); |
fpucher | 1:b9fd13f34c2d | 8 | DigitalOut LedD3(LED3); |
fpucher | 1:b9fd13f34c2d | 9 | DigitalOut LedD4(LED4); |
fpucher | 2:63135b94c898 | 10 | AnalogIn poti1(p19); |
fpucher | 2:63135b94c898 | 11 | AnalogIn poti2(p20); |
fpucher | 2:63135b94c898 | 12 | |
fpucher | 0:d764cafa5989 | 13 | |
fpucher | 1:b9fd13f34c2d | 14 | //Serial wifi(P1_0, P0_20); // Bertl |
fpucher | 1:b9fd13f34c2d | 15 | Serial wifi(p9, p10); // M0 |
fpucher | 0:d764cafa5989 | 16 | Serial pc(USBTX, USBRX); |
fpucher | 2:63135b94c898 | 17 | char recChar=0; |
fpucher | 2:63135b94c898 | 18 | bool recFlag=false; |
fpucher | 0:d764cafa5989 | 19 | |
fpucher | 2:63135b94c898 | 20 | //string fromEsp8266 = "Arduino_SD041"; |
fpucher | 2:63135b94c898 | 21 | string fromEsp8266 = ""; |
fpucher | 2:63135b94c898 | 22 | string toEsp8266 = ""; |
fpucher | 2:63135b94c898 | 23 | string periodicMsg=""; |
fpucher | 2:63135b94c898 | 24 | long lastMsg = 0; |
fpucher | 2:63135b94c898 | 25 | int url_decode(char *encoded_str, char *decoded_str); |
fpucher | 0:d764cafa5989 | 26 | |
fpucher | 2:63135b94c898 | 27 | string getPoti(char potiNo) { |
fpucher | 2:63135b94c898 | 28 | char pValStr[20] = "ME-01"; |
fpucher | 2:63135b94c898 | 29 | int n=-1; |
fpucher | 2:63135b94c898 | 30 | // pc.printf("Poti %c selected\r\n", potiNo); |
fpucher | 2:63135b94c898 | 31 | if (potiNo == '1') |
fpucher | 2:63135b94c898 | 32 | n = sprintf(pValStr, "%d", poti1.read_u16()); |
fpucher | 2:63135b94c898 | 33 | else if (potiNo == '2') |
fpucher | 2:63135b94c898 | 34 | n = sprintf(pValStr, "%d", poti2.read_u16()); |
fpucher | 2:63135b94c898 | 35 | if (n<=0) |
fpucher | 2:63135b94c898 | 36 | sprintf(pValStr, "ME01"); |
fpucher | 2:63135b94c898 | 37 | |
fpucher | 2:63135b94c898 | 38 | pc.printf("Wert von Poti %c: %s\r\n", potiNo, pValStr); |
fpucher | 2:63135b94c898 | 39 | //mcu.printf("_P%c:%s\r", potiNo, pValStr); |
fpucher | 2:63135b94c898 | 40 | return pValStr; |
fpucher | 2:63135b94c898 | 41 | } |
fpucher | 0:d764cafa5989 | 42 | |
fpucher | 2:63135b94c898 | 43 | // Process Requests from ESP8266 |
fpucher | 2:63135b94c898 | 44 | void ReadAndProcessRequests() { |
fpucher | 2:63135b94c898 | 45 | int chan,state,button; |
fpucher | 2:63135b94c898 | 46 | float Ain; |
fpucher | 2:63135b94c898 | 47 | char szAin[20],szT[200]; |
fpucher | 2:63135b94c898 | 48 | string poti; |
fpucher | 2:63135b94c898 | 49 | |
fpucher | 2:63135b94c898 | 50 | // while(1) { |
fpucher | 2:63135b94c898 | 51 | //char inChar = 'A'; // get the new byte |
fpucher | 2:63135b94c898 | 52 | //if (inChar != '\n') { |
fpucher | 2:63135b94c898 | 53 | // fromEsp8266 += inChar; // add it to receive string |
fpucher | 2:63135b94c898 | 54 | //} |
fpucher | 2:63135b94c898 | 55 | //else |
fpucher | 2:63135b94c898 | 56 | // while(wifi.readable()) { |
fpucher | 2:63135b94c898 | 57 | // char inChar = (char)wifi.getc(); // get the new byte |
fpucher | 2:63135b94c898 | 58 | // if (inChar != '\n') { |
fpucher | 2:63135b94c898 | 59 | // fromEsp8266 += inChar; // add it to receive string |
fpucher | 2:63135b94c898 | 60 | // } |
fpucher | 2:63135b94c898 | 61 | // process string if end of line |
fpucher | 2:63135b94c898 | 62 | if(fromEsp8266.substr(0,8)=="Arduino_") { // Valid command from ESP8266? |
fpucher | 2:63135b94c898 | 63 | if(fromEsp8266.substr(8,2) == "SM") { // Set Digital if true |
fpucher | 2:63135b94c898 | 64 | // --- Build Reply String ----------------------------------------------- |
fpucher | 2:63135b94c898 | 65 | toEsp8266 = "Echoing your message: "; |
fpucher | 2:63135b94c898 | 66 | toEsp8266 += fromEsp8266.substr(10,50); |
fpucher | 2:63135b94c898 | 67 | wifi.printf("%s\n", toEsp8266); |
fpucher | 2:63135b94c898 | 68 | // ---- Set Periodic Message String ------------------------------------- |
fpucher | 2:63135b94c898 | 69 | url_decode((char *)toEsp8266.c_str(), (char *)szT ); |
fpucher | 2:63135b94c898 | 70 | periodicMsg = szT; |
fpucher | 2:63135b94c898 | 71 | } |
fpucher | 2:63135b94c898 | 72 | else if(fromEsp8266.substr(8,2) == "SD") { // Set Digital if true |
fpucher | 2:63135b94c898 | 73 | // --- Build Reply String ----------------------------------------------- |
fpucher | 2:63135b94c898 | 74 | toEsp8266 = "Digital Channel "; |
fpucher | 2:63135b94c898 | 75 | toEsp8266 += fromEsp8266.substr(10,2); //Digital Channel |
fpucher | 2:63135b94c898 | 76 | toEsp8266 += " is "; |
fpucher | 2:63135b94c898 | 77 | toEsp8266 += (fromEsp8266.substr(12,1)=="0") ? "LO" : "HI"; |
fpucher | 2:63135b94c898 | 78 | // ---- Send Reply String ----------------------------------------------- |
fpucher | 2:63135b94c898 | 79 | //toEspprintf(toEsp8266); // Send Reply String to ESP8266 |
fpucher | 2:63135b94c898 | 80 | //pc.printf("%s\n", toEsp8266); // Send Reply String to Console |
fpucher | 2:63135b94c898 | 81 | wifi.printf("%s\n", toEsp8266); // Send Reply String to Console |
fpucher | 2:63135b94c898 | 82 | // if(wifi.writeable()) { |
fpucher | 2:63135b94c898 | 83 | // wifi.printf("%s\n", toEsp8266); |
fpucher | 2:63135b94c898 | 84 | // } |
fpucher | 2:63135b94c898 | 85 | // --- Set Digital Channel State ---------------------------------------- |
fpucher | 2:63135b94c898 | 86 | chan = atoi(fromEsp8266.substr(10,2).c_str()); |
fpucher | 2:63135b94c898 | 87 | state = atoi(fromEsp8266.substr(12,1).c_str()); |
fpucher | 2:63135b94c898 | 88 | //digitalWrite(chan, state); // Set Digital Output per request |
fpucher | 2:63135b94c898 | 89 | //pc.printf("\nSTATE= "); |
fpucher | 2:63135b94c898 | 90 | //pc.printf("%d", state); |
fpucher | 2:63135b94c898 | 91 | LedD4 = state; |
fpucher | 2:63135b94c898 | 92 | } |
fpucher | 2:63135b94c898 | 93 | else if(fromEsp8266.substr(8,2) == "GD") { // Get Digital if true |
fpucher | 2:63135b94c898 | 94 | // --- Get Digital Channel State ---------------------------------------- |
fpucher | 2:63135b94c898 | 95 | chan = atoi(fromEsp8266.substr(10,2).c_str()); |
fpucher | 2:63135b94c898 | 96 | state = LedD4.read(); // Set Digital Output per request |
fpucher | 2:63135b94c898 | 97 | // --- Build Reply String ----------------------------------------------- |
fpucher | 2:63135b94c898 | 98 | toEsp8266 = "Digital Channel "; |
fpucher | 2:63135b94c898 | 99 | toEsp8266 += fromEsp8266.substr(10,2); //Digital Channel |
fpucher | 2:63135b94c898 | 100 | toEsp8266 += " is "; |
fpucher | 2:63135b94c898 | 101 | toEsp8266 += (state==0) ? "LO" : "HI"; |
fpucher | 2:63135b94c898 | 102 | // ---- Send Reply String ----------------------------------------------- |
fpucher | 2:63135b94c898 | 103 | //toEspprintf(toEsp8266); // Send Reply String to ESP8266 |
fpucher | 2:63135b94c898 | 104 | wifi.printf("%s\n",toEsp8266); // Send Reply String to ESP |
fpucher | 2:63135b94c898 | 105 | } |
fpucher | 2:63135b94c898 | 106 | else if(fromEsp8266.substr(8,2) == "GA") { // Get Analog if true |
fpucher | 2:63135b94c898 | 107 | // --- Get Analog Channel Reading --------------------------------------- |
fpucher | 2:63135b94c898 | 108 | chan = atoi(fromEsp8266.substr(10,2).c_str()); |
fpucher | 2:63135b94c898 | 109 | //Ain = 0.0048828 * (float) analogRead(chan); // Read analog input |
fpucher | 2:63135b94c898 | 110 | //ftoa(Ain,szAin, 2); |
fpucher | 2:63135b94c898 | 111 | poti = getPoti('1'); |
fpucher | 2:63135b94c898 | 112 | // --- Build Reply String ----------------------------------------------- |
fpucher | 2:63135b94c898 | 113 | //toEsp8266 = "Analog Channel "; |
fpucher | 2:63135b94c898 | 114 | toEsp8266 = "Poti 1 "; |
fpucher | 2:63135b94c898 | 115 | // toEsp8266 += fromEsp8266.substr(10,2); //Analog Channel |
fpucher | 2:63135b94c898 | 116 | toEsp8266 += " is "; |
fpucher | 2:63135b94c898 | 117 | toEsp8266 += poti; //string(szAin); |
fpucher | 2:63135b94c898 | 118 | // ---- Send Reply String ----------------------------------------------- |
fpucher | 2:63135b94c898 | 119 | //toEspprintf(toEsp8266); // Send Reply String to ESP8266 |
fpucher | 2:63135b94c898 | 120 | wifi.printf("%s\n",toEsp8266); // Send Reply String to Console |
fpucher | 2:63135b94c898 | 121 | } |
fpucher | 2:63135b94c898 | 122 | else { |
fpucher | 2:63135b94c898 | 123 | // ---- Send Reply String ----------------------------------------------- |
fpucher | 2:63135b94c898 | 124 | toEsp8266 = "Arduino does not recognize request."; |
fpucher | 2:63135b94c898 | 125 | //toEspprintf(toEsp8266); // Send Reply String to ESP8266 |
fpucher | 2:63135b94c898 | 126 | //pc.printf("%s\n", toEsp8266); // Send Reply String to Console |
fpucher | 2:63135b94c898 | 127 | wifi.printf("%s\n", toEsp8266); // Send Reply String to Console |
fpucher | 2:63135b94c898 | 128 | } |
fpucher | 2:63135b94c898 | 129 | } |
fpucher | 2:63135b94c898 | 130 | fromEsp8266 = ""; |
fpucher | 2:63135b94c898 | 131 | //toEspSerial.flush(); |
fpucher | 2:63135b94c898 | 132 | // } |
fpucher | 2:63135b94c898 | 133 | // } |
fpucher | 2:63135b94c898 | 134 | } |
fpucher | 2:63135b94c898 | 135 | |
fpucher | 2:63135b94c898 | 136 | /******************************************************** |
fpucher | 2:63135b94c898 | 137 | * URL Message Decoder |
fpucher | 2:63135b94c898 | 138 | ********************************************************/ |
fpucher | 2:63135b94c898 | 139 | |
fpucher | 2:63135b94c898 | 140 | int url_decode(char *encoded_str, char *decoded_str) { |
fpucher | 2:63135b94c898 | 141 | |
fpucher | 2:63135b94c898 | 142 | // While we're not at the end of the string (current character not NULL) |
fpucher | 2:63135b94c898 | 143 | while (*encoded_str) { |
fpucher | 2:63135b94c898 | 144 | // Check to see if the current character is a % |
fpucher | 2:63135b94c898 | 145 | if (*encoded_str == '%') { |
fpucher | 2:63135b94c898 | 146 | |
fpucher | 2:63135b94c898 | 147 | // Grab the next two characters and move encoded_str forwards |
fpucher | 2:63135b94c898 | 148 | encoded_str++; |
fpucher | 2:63135b94c898 | 149 | char high = *encoded_str; |
fpucher | 2:63135b94c898 | 150 | encoded_str++; |
fpucher | 2:63135b94c898 | 151 | char low = *encoded_str; |
fpucher | 2:63135b94c898 | 152 | |
fpucher | 2:63135b94c898 | 153 | // Convert ASCII 0-9A-F to a value 0-15 |
fpucher | 2:63135b94c898 | 154 | if (high > 0x39) high -= 7; |
fpucher | 2:63135b94c898 | 155 | high &= 0x0f; |
fpucher | 2:63135b94c898 | 156 | |
fpucher | 2:63135b94c898 | 157 | // Same again for the low byte: |
fpucher | 2:63135b94c898 | 158 | if (low > 0x39) low -= 7; |
fpucher | 2:63135b94c898 | 159 | low &= 0x0f; |
fpucher | 2:63135b94c898 | 160 | |
fpucher | 2:63135b94c898 | 161 | // Combine the two into a single byte and store in decoded_str: |
fpucher | 2:63135b94c898 | 162 | *decoded_str = (high << 4) | low; |
fpucher | 2:63135b94c898 | 163 | } else { |
fpucher | 2:63135b94c898 | 164 | // All other characters copy verbatim |
fpucher | 2:63135b94c898 | 165 | *decoded_str = *encoded_str; |
fpucher | 0:d764cafa5989 | 166 | } |
fpucher | 2:63135b94c898 | 167 | |
fpucher | 2:63135b94c898 | 168 | // Move both pointers to the next character: |
fpucher | 2:63135b94c898 | 169 | encoded_str++; |
fpucher | 2:63135b94c898 | 170 | decoded_str++; |
fpucher | 2:63135b94c898 | 171 | } |
fpucher | 2:63135b94c898 | 172 | // Terminate the new string with a NULL character to trim it off |
fpucher | 2:63135b94c898 | 173 | *decoded_str = 0; |
fpucher | 2:63135b94c898 | 174 | return 0; |
fpucher | 2:63135b94c898 | 175 | } |
fpucher | 2:63135b94c898 | 176 | void flushSerialBuffer() { |
fpucher | 2:63135b94c898 | 177 | while (wifi.readable()) { |
fpucher | 2:63135b94c898 | 178 | wifi.getc(); |
fpucher | 2:63135b94c898 | 179 | } |
fpucher | 2:63135b94c898 | 180 | } |
fpucher | 2:63135b94c898 | 181 | |
fpucher | 2:63135b94c898 | 182 | bool semi = false; |
fpucher | 2:63135b94c898 | 183 | bool blank = false; |
fpucher | 2:63135b94c898 | 184 | |
fpucher | 2:63135b94c898 | 185 | void readData() { |
fpucher | 2:63135b94c898 | 186 | recChar = wifi.getc(); |
fpucher | 2:63135b94c898 | 187 | if (recChar != '\n') { |
fpucher | 2:63135b94c898 | 188 | if(recChar != ':') semi = true; |
fpucher | 2:63135b94c898 | 189 | if(semi && (recChar != ' ')) blank = true; |
fpucher | 2:63135b94c898 | 190 | if(semi && blank) |
fpucher | 2:63135b94c898 | 191 | fromEsp8266 += recChar; // add it to receive string |
fpucher | 2:63135b94c898 | 192 | } |
fpucher | 2:63135b94c898 | 193 | else { |
fpucher | 2:63135b94c898 | 194 | recFlag = true; |
fpucher | 2:63135b94c898 | 195 | semi = false; blank = false; |
fpucher | 2:63135b94c898 | 196 | flushSerialBuffer(); |
fpucher | 2:63135b94c898 | 197 | } |
fpucher | 2:63135b94c898 | 198 | } |
fpucher | 2:63135b94c898 | 199 | int main() { |
fpucher | 2:63135b94c898 | 200 | pc.baud(115200); |
fpucher | 2:63135b94c898 | 201 | wifi.baud(115200); |
fpucher | 2:63135b94c898 | 202 | pc.printf("\f\n\r-------------ESP8266 Arduino -------------\n\r"); |
fpucher | 2:63135b94c898 | 203 | wifi.attach(&readData); |
fpucher | 2:63135b94c898 | 204 | //wifi.printf("Hello WIFI\n"); |
fpucher | 2:63135b94c898 | 205 | while(1) { |
fpucher | 2:63135b94c898 | 206 | if(recFlag) { |
fpucher | 2:63135b94c898 | 207 | pc.printf("readData: %s\n", fromEsp8266); |
fpucher | 2:63135b94c898 | 208 | //fromEsp8266 = "Arduino_SD041"; |
fpucher | 2:63135b94c898 | 209 | ReadAndProcessRequests(); |
fpucher | 2:63135b94c898 | 210 | recFlag=false; |
fpucher | 2:63135b94c898 | 211 | } |
fpucher | 2:63135b94c898 | 212 | LedD1 = 1; |
fpucher | 2:63135b94c898 | 213 | wait(0.2); |
fpucher | 2:63135b94c898 | 214 | LedD1 = 0; |
fpucher | 2:63135b94c898 | 215 | wait(0.2); |
fpucher | 2:63135b94c898 | 216 | } |
fpucher | 2:63135b94c898 | 217 | } |