M0 communication to configurable-Web-Server (MQTT) Version 0.1

Dependencies:   LM75B mbed

Siehe auch FTKL-Tagung 2016

Revision:
3:c14eb9159a88
Parent:
2:63135b94c898
--- a/main.cpp	Sun Jan 14 19:49:08 2018 +0000
+++ b/main.cpp	Tue Jan 16 13:34:44 2018 +0000
@@ -1,170 +1,151 @@
+/************************************************************************
+ * Program  : ESP8266_HTTP_MQTT_Server from _Arduino_Esp_Comm
+ * Descr.   : M3-ESP8266-Communication similar to Arduino communication
+ * Input    : Get data from Esp8266 from serial
+ *            Process data (i.e. digital and analog channels)
+              ToDo: apply diffrent sensors over I2C, SPI, ...(i.e. LM75,...)
+ * Output   : replay to Esp8266 over serial
+ ************************************************************************/
+
 #include "mbed.h"
 #include <stdio.h>
 #include <string>
-//using namespace std; 
 
 DigitalOut  LedD1(LED1);
 DigitalOut  LedD2(LED2);
 DigitalOut  LedD3(LED3);
 DigitalOut  LedD4(LED4);
+BusOut ledbus(LED1, LED2, LED3, LED4);
 AnalogIn poti1(p19);
 AnalogIn poti2(p20);
 
-
-//Serial wifi(P1_0, P0_20); // Bertl
-Serial wifi(p9, p10);       // M0
+Serial wifi(p9, p10);       // M0, M3, (P1_0, P0_20) Bertl
 Serial pc(USBTX, USBRX);
 char recChar=0;
 bool recFlag=false;
 
-//string fromEsp8266 = "Arduino_SD041";
 string fromEsp8266 = "";
 string toEsp8266 = "";
 string periodicMsg="";
 long lastMsg = 0;
 int url_decode(char *encoded_str, char *decoded_str);
 
- string getPoti(char potiNo) {
+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 (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"); 
-    
+        sprintf(pValStr, "ME01");
+
     pc.printf("Wert von Poti %c: %s\r\n", potiNo, pValStr);
-    //mcu.printf("_P%c:%s\r", potiNo, pValStr);
     return pValStr;
-}    
+}
 
-// Process Requests from ESP8266
-void ReadAndProcessRequests() {
+/********************************************************
+ *  Process Requests from ESP8266
+ *  Input: global string fromEsp8266
+ ********************************************************/
+void ProcessRequests()
+{
     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();
-//        }
-//    }
+
+    // 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 -----------------------------------------------
+            wifi.printf("%s\n", toEsp8266);             // Send Reply String to Console
+            // --- Set Digital Channel State ----------------------------------------
+            chan = atoi(fromEsp8266.substr(10,2).c_str());
+            state = atoi(fromEsp8266.substr(12,1).c_str());
+            ledbus[chan-1] = state;                     // Set Digital Output per request
+
+        } 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 -----------------------------------------------
+            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(chan);
+            // --- Build Reply String -----------------------------------------------
+            toEsp8266 = "Poti ";
+            toEsp8266 += fromEsp8266.substr(10,2); //Analog Channel
+            toEsp8266 += " is ";
+            toEsp8266 += poti; //string(szAin);
+            // ---- Send Reply String -----------------------------------------------
+            wifi.printf("%s\n",toEsp8266);             // Send Reply String to ESP
+        } else {
+            // ---- Send Reply String -----------------------------------------------
+            toEsp8266 = "Arduino does not recognize request.";
+            //pc.printf("%s\n", toEsp8266);             // Send Reply String to Console
+            wifi.printf("%s\n", toEsp8266);             // Send Reply String to ESP
+        }
+    }
+    fromEsp8266 = "";
+    //toEspSerial.flush();
 }
 
 /********************************************************
  *  URL Message Decoder
  ********************************************************/
- 
-int url_decode(char *encoded_str, char *decoded_str) {
-   
+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;
         }
-    
+
         // Move both pointers to the next character:
         encoded_str++;
         decoded_str++;
@@ -173,40 +154,46 @@
     *decoded_str = 0;
     return 0;
 }
-void flushSerialBuffer() { 
-    while (wifi.readable()) { 
-        wifi.getc(); 
-    } 
+
+void flushSerialBuffer()
+{
+    while (wifi.readable()) {
+        wifi.getc();
+    }
 }
 
+/********************************************************
+ *  ISR readData and stored in global fromEsp8266 string
+ ********************************************************/
 bool semi = false;
-bool blank = false;  
-  
-void readData() {
+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;
+            fromEsp8266 += recChar;                        // add it to receive string
+    } else {
+        recFlag = true;
+        semi = false;
+        blank = false;
         flushSerialBuffer();
     }
 }
-int main() {
+
+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();
+            pc.printf("readData: %s\n", fromEsp8266);
+            ProcessRequests();
             recFlag=false;
         }
         LedD1 = 1;