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
Diff: main.cpp
- Revision:
- 2:63135b94c898
- Parent:
- 1:b9fd13f34c2d
- Child:
- 3:c14eb9159a88
--- a/main.cpp Sun Nov 06 15:36:07 2016 +0000 +++ b/main.cpp Sun Jan 14 19:49:08 2018 +0000 @@ -1,48 +1,217 @@ -#include <mbed.h> -//#include <config.h> // Bertl -#include <LM75B.h> +#include "mbed.h" +#include <stdio.h> +#include <string> +//using namespace std; DigitalOut LedD1(LED1); DigitalOut LedD2(LED2); DigitalOut LedD3(LED3); DigitalOut LedD4(LED4); +AnalogIn poti1(p19); +AnalogIn poti2(p20); + //Serial wifi(P1_0, P0_20); // Bertl Serial wifi(p9, p10); // M0 Serial pc(USBTX, USBRX); -LM75B sensor(p28, p27); +char recChar=0; +bool recFlag=false; -char str[30]; +//string fromEsp8266 = "Arduino_SD041"; +string fromEsp8266 = ""; +string toEsp8266 = ""; +string periodicMsg=""; +long lastMsg = 0; +int url_decode(char *encoded_str, char *decoded_str); -int main() -{ - wifi.printf("Hello M3 MQTT\n"); + string getPoti(char potiNo) { + char pValStr[20] = "ME-01"; + int n=-1; + // pc.printf("Poti %c selected\r\n", potiNo); + if (potiNo == '1') + n = sprintf(pValStr, "%d", poti1.read_u16()); + else if (potiNo == '2') + n = sprintf(pValStr, "%d", poti2.read_u16()); + if (n<=0) + sprintf(pValStr, "ME01"); + + pc.printf("Wert von Poti %c: %s\r\n", potiNo, pValStr); + //mcu.printf("_P%c:%s\r", potiNo, pValStr); + return pValStr; +} - while (1) { - wifi.scanf("%s", str); - //wifi.printf("str: %s\n", str); - if(str[0] == 'H' && str[1] == 'L') // 1. GET--> Serving Requests part in http-server - wifi.printf( "HL Msg: %s\n", str); - if(str[0] == 'M' && str[1] == 'V') // 1. GET--> Serving Requests part in http-server - ;//Move(); // ?reqest=MV - else if(str[0] == 'T') { - if (str[1] == 'L') - ;//TurnLeft(); - else if (str[1] == 'R') - ;//TurnRight(); - } else { // 3. GET--> Serving M0 Board part in http-server - if(str[7] == '1') { // ?M0=SD&chan=04&state=1 --> M0_SD_04_1 - LedD1 = (str[9] -'0'); - wifi.printf( "LedD1\n"); - } - if(str[7] == '2') - LedD2 = (str[9] -'0'); - if(str[7] == '3') - LedD3 = (str[9] -'0'); - if(str[7] == '4') - LedD4 = (str[9] -'0'); - if(str[3] == 'G') - wifi.printf( "Temp: %f\n", sensor.read()); +// Process Requests from ESP8266 +void ReadAndProcessRequests() { + int chan,state,button; + float Ain; + char szAin[20],szT[200]; + string poti; + +// while(1) { + //char inChar = 'A'; // get the new byte + //if (inChar != '\n') { + // fromEsp8266 += inChar; // add it to receive string + //} + //else + // while(wifi.readable()) { + // char inChar = (char)wifi.getc(); // get the new byte + // if (inChar != '\n') { + // fromEsp8266 += inChar; // add it to receive string + // } + // process string if end of line + if(fromEsp8266.substr(0,8)=="Arduino_") { // Valid command from ESP8266? + if(fromEsp8266.substr(8,2) == "SM") { // Set Digital if true + // --- Build Reply String ----------------------------------------------- + toEsp8266 = "Echoing your message: "; + toEsp8266 += fromEsp8266.substr(10,50); + wifi.printf("%s\n", toEsp8266); + // ---- Set Periodic Message String ------------------------------------- + url_decode((char *)toEsp8266.c_str(), (char *)szT ); + periodicMsg = szT; + } + else if(fromEsp8266.substr(8,2) == "SD") { // Set Digital if true + // --- Build Reply String ----------------------------------------------- + toEsp8266 = "Digital Channel "; + toEsp8266 += fromEsp8266.substr(10,2); //Digital Channel + toEsp8266 += " is "; + toEsp8266 += (fromEsp8266.substr(12,1)=="0") ? "LO" : "HI"; + // ---- Send Reply String ----------------------------------------------- + //toEspprintf(toEsp8266); // Send Reply String to ESP8266 + //pc.printf("%s\n", toEsp8266); // Send Reply String to Console + wifi.printf("%s\n", toEsp8266); // Send Reply String to Console +// if(wifi.writeable()) { +// wifi.printf("%s\n", toEsp8266); +// } + // --- Set Digital Channel State ---------------------------------------- + chan = atoi(fromEsp8266.substr(10,2).c_str()); + state = atoi(fromEsp8266.substr(12,1).c_str()); + //digitalWrite(chan, state); // Set Digital Output per request + //pc.printf("\nSTATE= "); + //pc.printf("%d", state); + LedD4 = state; + } + else if(fromEsp8266.substr(8,2) == "GD") { // Get Digital if true + // --- Get Digital Channel State ---------------------------------------- + chan = atoi(fromEsp8266.substr(10,2).c_str()); + state = LedD4.read(); // Set Digital Output per request + // --- Build Reply String ----------------------------------------------- + toEsp8266 = "Digital Channel "; + toEsp8266 += fromEsp8266.substr(10,2); //Digital Channel + toEsp8266 += " is "; + toEsp8266 += (state==0) ? "LO" : "HI"; + // ---- Send Reply String ----------------------------------------------- + //toEspprintf(toEsp8266); // Send Reply String to ESP8266 + wifi.printf("%s\n",toEsp8266); // Send Reply String to ESP + } + else if(fromEsp8266.substr(8,2) == "GA") { // Get Analog if true + // --- Get Analog Channel Reading --------------------------------------- + chan = atoi(fromEsp8266.substr(10,2).c_str()); + //Ain = 0.0048828 * (float) analogRead(chan); // Read analog input + //ftoa(Ain,szAin, 2); + poti = getPoti('1'); + // --- Build Reply String ----------------------------------------------- + //toEsp8266 = "Analog Channel "; + toEsp8266 = "Poti 1 "; + // toEsp8266 += fromEsp8266.substr(10,2); //Analog Channel + toEsp8266 += " is "; + toEsp8266 += poti; //string(szAin); + // ---- Send Reply String ----------------------------------------------- + //toEspprintf(toEsp8266); // Send Reply String to ESP8266 + wifi.printf("%s\n",toEsp8266); // Send Reply String to Console + } + else { + // ---- Send Reply String ----------------------------------------------- + toEsp8266 = "Arduino does not recognize request."; + //toEspprintf(toEsp8266); // Send Reply String to ESP8266 + //pc.printf("%s\n", toEsp8266); // Send Reply String to Console + wifi.printf("%s\n", toEsp8266); // Send Reply String to Console + } + } + fromEsp8266 = ""; + //toEspSerial.flush(); +// } +// } +} + +/******************************************************** + * URL Message Decoder + ********************************************************/ + +int url_decode(char *encoded_str, char *decoded_str) { + + // While we're not at the end of the string (current character not NULL) + while (*encoded_str) { + // Check to see if the current character is a % + if (*encoded_str == '%') { + + // Grab the next two characters and move encoded_str forwards + encoded_str++; + char high = *encoded_str; + encoded_str++; + char low = *encoded_str; + + // Convert ASCII 0-9A-F to a value 0-15 + if (high > 0x39) high -= 7; + high &= 0x0f; + + // Same again for the low byte: + if (low > 0x39) low -= 7; + low &= 0x0f; + + // Combine the two into a single byte and store in decoded_str: + *decoded_str = (high << 4) | low; + } else { + // All other characters copy verbatim + *decoded_str = *encoded_str; } - } // 2. GET-->Serving Web Pages for config and reset -} \ No newline at end of file + + // Move both pointers to the next character: + encoded_str++; + decoded_str++; + } + // Terminate the new string with a NULL character to trim it off + *decoded_str = 0; + return 0; +} +void flushSerialBuffer() { + while (wifi.readable()) { + wifi.getc(); + } +} + +bool semi = false; +bool blank = false; + +void readData() { + recChar = wifi.getc(); + if (recChar != '\n') { + if(recChar != ':') semi = true; + if(semi && (recChar != ' ')) blank = true; + if(semi && blank) + fromEsp8266 += recChar; // add it to receive string + } + else { + recFlag = true; + semi = false; blank = false; + flushSerialBuffer(); + } +} +int main() { + pc.baud(115200); + wifi.baud(115200); + pc.printf("\f\n\r-------------ESP8266 Arduino -------------\n\r"); + wifi.attach(&readData); + //wifi.printf("Hello WIFI\n"); + while(1) { + if(recFlag) { + pc.printf("readData: %s\n", fromEsp8266); + //fromEsp8266 = "Arduino_SD041"; + ReadAndProcessRequests(); + recFlag=false; + } + LedD1 = 1; + wait(0.2); + LedD1 = 0; + wait(0.2); + } +}