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

Dependencies:   LM75B mbed

Siehe auch FTKL-Tagung 2016

main.cpp

Committer:
fpucher
Date:
2018-01-16
Revision:
3:c14eb9159a88
Parent:
2:63135b94c898

File content as of revision 3:c14eb9159a88:

/************************************************************************
 * 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>

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(p9, p10);       // M0, M3, (P1_0, P0_20) Bertl
Serial pc(USBTX, USBRX);
char recChar=0;
bool recFlag=false;

string fromEsp8266 = "";
string toEsp8266 = "";
string periodicMsg="";
long lastMsg = 0;
int url_decode(char *encoded_str, char *decoded_str);

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);
    return pValStr;
}

/********************************************************
 *  Process Requests from ESP8266
 *  Input: global string fromEsp8266
 ********************************************************/
void ProcessRequests()
{
    int chan,state,button;
    float Ain;
    char szAin[20],szT[200];
    string poti;

    // 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)
{
    // 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++;
    }
    // Terminate the new string with a NULL character to trim it off
    *decoded_str = 0;
    return 0;
}

void flushSerialBuffer()
{
    while (wifi.readable()) {
        wifi.getc();
    }
}

/********************************************************
 *  ISR readData and stored in global fromEsp8266 string
 ********************************************************/
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);
    while(1) {
        if(recFlag) {
            pc.printf("readData: %s\n", fromEsp8266);
            ProcessRequests();
            recFlag=false;
        }
        LedD1 = 1;
        wait(0.2);
        LedD1 = 0;
        wait(0.2);
    }
}