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