Franz Pucher
/
ESP8266_HTTP_MQTT_Server
M0 communication to configurable-Web-Server (MQTT) Version 0.1
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /************************************************************************ 00002 * Program : ESP8266_HTTP_MQTT_Server from _Arduino_Esp_Comm 00003 * Descr. : M3-ESP8266-Communication similar to Arduino communication 00004 * Input : Get data from Esp8266 from serial 00005 * Process data (i.e. digital and analog channels) 00006 ToDo: apply diffrent sensors over I2C, SPI, ...(i.e. LM75,...) 00007 * Output : replay to Esp8266 over serial 00008 ************************************************************************/ 00009 00010 #include "mbed.h" 00011 #include <stdio.h> 00012 #include <string> 00013 00014 DigitalOut LedD1(LED1); 00015 DigitalOut LedD2(LED2); 00016 DigitalOut LedD3(LED3); 00017 DigitalOut LedD4(LED4); 00018 BusOut ledbus(LED1, LED2, LED3, LED4); 00019 AnalogIn poti1(p19); 00020 AnalogIn poti2(p20); 00021 00022 Serial wifi(p9, p10); // M0, M3, (P1_0, P0_20) Bertl 00023 Serial pc(USBTX, USBRX); 00024 char recChar=0; 00025 bool recFlag=false; 00026 00027 string fromEsp8266 = ""; 00028 string toEsp8266 = ""; 00029 string periodicMsg=""; 00030 long lastMsg = 0; 00031 int url_decode(char *encoded_str, char *decoded_str); 00032 00033 string getPoti(char potiNo) 00034 { 00035 char pValStr[20] = "ME-01"; 00036 int n=-1; 00037 // pc.printf("Poti %c selected\r\n", potiNo); 00038 if (potiNo == 1) 00039 n = sprintf(pValStr, "%d", poti1.read_u16()); 00040 else if (potiNo == 2) 00041 n = sprintf(pValStr, "%d", poti2.read_u16()); 00042 if (n<=0) 00043 sprintf(pValStr, "ME01"); 00044 00045 pc.printf("Wert von Poti %c: %s\r\n", potiNo, pValStr); 00046 return pValStr; 00047 } 00048 00049 /******************************************************** 00050 * Process Requests from ESP8266 00051 * Input: global string fromEsp8266 00052 ********************************************************/ 00053 void ProcessRequests() 00054 { 00055 int chan,state,button; 00056 float Ain; 00057 char szAin[20],szT[200]; 00058 string poti; 00059 00060 // process string if end of line 00061 if(fromEsp8266.substr(0,8)=="Arduino_") { // Valid command from ESP8266? 00062 if(fromEsp8266.substr(8,2) == "SM") { // Set Digital if true 00063 // --- Build Reply String ----------------------------------------------- 00064 toEsp8266 = "Echoing your message: "; 00065 toEsp8266 += fromEsp8266.substr(10,50); 00066 wifi.printf("%s\n", toEsp8266); 00067 // ---- Set Periodic Message String ------------------------------------- 00068 url_decode((char *)toEsp8266.c_str(), (char *)szT ); 00069 periodicMsg = szT; 00070 } else if(fromEsp8266.substr(8,2) == "SD") { // Set Digital if true 00071 // --- Build Reply String ----------------------------------------------- 00072 toEsp8266 = "Digital Channel "; 00073 toEsp8266 += fromEsp8266.substr(10,2); //Digital Channel 00074 toEsp8266 += " is "; 00075 toEsp8266 += (fromEsp8266.substr(12,1)=="0") ? "LO" : "HI"; 00076 // ---- Send Reply String ----------------------------------------------- 00077 wifi.printf("%s\n", toEsp8266); // Send Reply String to Console 00078 // --- Set Digital Channel State ---------------------------------------- 00079 chan = atoi(fromEsp8266.substr(10,2).c_str()); 00080 state = atoi(fromEsp8266.substr(12,1).c_str()); 00081 ledbus[chan-1] = state; // Set Digital Output per request 00082 00083 } else if(fromEsp8266.substr(8,2) == "GD") { // Get Digital if true 00084 // --- Get Digital Channel State ---------------------------------------- 00085 chan = atoi(fromEsp8266.substr(10,2).c_str()); 00086 state = LedD4.read(); // Set Digital Output per request 00087 // --- Build Reply String ----------------------------------------------- 00088 toEsp8266 = "Digital Channel "; 00089 toEsp8266 += fromEsp8266.substr(10,2); // Digital Channel 00090 toEsp8266 += " is "; 00091 toEsp8266 += (state==0) ? "LO" : "HI"; 00092 // ---- Send Reply String ----------------------------------------------- 00093 wifi.printf("%s\n",toEsp8266); // Send Reply String to ESP 00094 } else if(fromEsp8266.substr(8,2) == "GA") { // Get Analog if true 00095 // --- Get Analog Channel Reading --------------------------------------- 00096 chan = atoi(fromEsp8266.substr(10,2).c_str()); 00097 //Ain = 0.0048828 * (float) analogRead(chan); // Read analog input 00098 //ftoa(Ain,szAin, 2); 00099 poti = getPoti(chan); 00100 // --- Build Reply String ----------------------------------------------- 00101 toEsp8266 = "Poti "; 00102 toEsp8266 += fromEsp8266.substr(10,2); //Analog Channel 00103 toEsp8266 += " is "; 00104 toEsp8266 += poti; //string(szAin); 00105 // ---- Send Reply String ----------------------------------------------- 00106 wifi.printf("%s\n",toEsp8266); // Send Reply String to ESP 00107 } else { 00108 // ---- Send Reply String ----------------------------------------------- 00109 toEsp8266 = "Arduino does not recognize request."; 00110 //pc.printf("%s\n", toEsp8266); // Send Reply String to Console 00111 wifi.printf("%s\n", toEsp8266); // Send Reply String to ESP 00112 } 00113 } 00114 fromEsp8266 = ""; 00115 //toEspSerial.flush(); 00116 } 00117 00118 /******************************************************** 00119 * URL Message Decoder 00120 ********************************************************/ 00121 int url_decode(char *encoded_str, char *decoded_str) 00122 { 00123 // While we're not at the end of the string (current character not NULL) 00124 while (*encoded_str) { 00125 // Check to see if the current character is a % 00126 if (*encoded_str == '%') { 00127 00128 // Grab the next two characters and move encoded_str forwards 00129 encoded_str++; 00130 char high = *encoded_str; 00131 encoded_str++; 00132 char low = *encoded_str; 00133 00134 // Convert ASCII 0-9A-F to a value 0-15 00135 if (high > 0x39) high -= 7; 00136 high &= 0x0f; 00137 00138 // Same again for the low byte: 00139 if (low > 0x39) low -= 7; 00140 low &= 0x0f; 00141 00142 // Combine the two into a single byte and store in decoded_str: 00143 *decoded_str = (high << 4) | low; 00144 } else { 00145 // All other characters copy verbatim 00146 *decoded_str = *encoded_str; 00147 } 00148 00149 // Move both pointers to the next character: 00150 encoded_str++; 00151 decoded_str++; 00152 } 00153 // Terminate the new string with a NULL character to trim it off 00154 *decoded_str = 0; 00155 return 0; 00156 } 00157 00158 void flushSerialBuffer() 00159 { 00160 while (wifi.readable()) { 00161 wifi.getc(); 00162 } 00163 } 00164 00165 /******************************************************** 00166 * ISR readData and stored in global fromEsp8266 string 00167 ********************************************************/ 00168 bool semi = false; 00169 bool blank = false; 00170 00171 void readData() 00172 { 00173 recChar = wifi.getc(); 00174 if (recChar != '\n') { 00175 if(recChar != ':') semi = true; 00176 if(semi && (recChar != ' ')) blank = true; 00177 if(semi && blank) 00178 fromEsp8266 += recChar; // add it to receive string 00179 } else { 00180 recFlag = true; 00181 semi = false; 00182 blank = false; 00183 flushSerialBuffer(); 00184 } 00185 } 00186 00187 int main() 00188 { 00189 pc.baud(115200); 00190 wifi.baud(115200); 00191 pc.printf("\f\n\r-------------ESP8266 Arduino -------------\n\r"); 00192 wifi.attach(&readData); 00193 while(1) { 00194 if(recFlag) { 00195 pc.printf("readData: %s\n", fromEsp8266); 00196 ProcessRequests(); 00197 recFlag=false; 00198 } 00199 LedD1 = 1; 00200 wait(0.2); 00201 LedD1 = 0; 00202 wait(0.2); 00203 } 00204 }
Generated on Wed Jul 13 2022 07:58:18 by 1.7.2