Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- fpucher
- Date:
- 2018-01-14
- Revision:
- 2:63135b94c898
- Parent:
- 1:b9fd13f34c2d
- Child:
- 3:c14eb9159a88
File content as of revision 2:63135b94c898:
#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); 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) { 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; } // 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; } // 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); } }