Regenerating PPM signal based on distances from ultrasonic sensors, ESP8266 for connectin via wifi. Autonomous quadcopter behaviour, autonomou height holding. Flying direction based on front and back ultrasonic sensors.

Dependencies:   ConfigFile HCSR04 PID PPM2 mbed-rtos mbed

ESP8266/Server.h

Committer:
edy05
Date:
2017-10-27
Revision:
9:86a5af9935b1
Parent:
2:d172c9963f87
Child:
10:bb9c778f8e3e

File content as of revision 9:86a5af9935b1:

#include "mbed.h"
#include "rtos.h"
#include "hardware.h"


RawSerial esp(p9, p10); // tx, rx
DigitalOut  reset(p8);

// Standard Mbed LED definitions
DigitalOut  led1(LED1);      // (PTB18)
DigitalOut  led3(LED3);     // (PTD1)

Timer t1;
Timer t2;

char cmdbuff[32];
char replybuff[1024];
char webcount[8];
char webdata[1024]; // This may need to be bigger depending on WEB browser used
char webbuff[4096];     // Currently using 1986 characters, Increase this if more web page data added
char type[16];
char channel[2];
char ip_address[20];
int bufflen, DataRX, count, getcount, replycount, servreq, timeout;
int bufl, ipdLen, linkID, weberror, webcounter;

void getreply(); void SendCMD();
void startserver();
void ReadWebData();
void sendpage();
void sendcheck();
void get_ip_address(char web_data[1024]);
void SendWEB();

void serverRun();
void serverMain();
void serverLoop();
void callback();

int port        =80;  // set server port
int SERVtimeout =5;    // set server timeout in seconds in case link breaks.



//serverThread.start(serverRun);

void callback(){
    //pc.printf("callback!!!!!!!!!!!!!!!!_________________________\n\r");
    led3 = 1;
    while (esp.readable()) {
        webbuff[count] = esp.getc();
        count++;
    }
    if(strlen(webbuff)>bufflen) {
        DataRX=1;
        led3=0;
    }
}


void serverRun(){
    reset=0;
    pc.baud(115200);

    pc.printf("\f\n\r------------ ESP8266 Hardware Reset --------------\n\r");
    wait(0.5);
    reset=1;
    timeout=6000;
    getcount=500;
    getreply();
    esp.baud(115200);   // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F
    startserver();
    loadConfigFile();
    while(1){
        if(DataRX==1) {
            ReadWebData();
            if (servreq == 1 && weberror == 0) {
                    sendpage();
            }
            esp.attach(&callback);
            pc.printf(" IPD Data:\r\n\n Link ID = %d,\r\n IPD Header Length = %d \r\n IPD Type = %s\r\n", linkID, ipdLen, type);
            pc.printf("\n\n  HTTP Packet: \n\n%s\n", webdata);
            pc.printf("  Web Characters sent : %d\n\n", bufl);
            pc.printf("  -------------------------------------\n\n");
            servreq=0;
        }
        Thread::wait(1000);
    }
}


// Static WEB page
void sendpage(){
    strcpy(webbuff, "<!DOCTYPE html>");
    strcat(webbuff, "<html><head><title>ESP8266 Mbed LPC1768</title></head>");
    strcat(webbuff, "<body>");
    strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>ESP8266 Mbed IoT Web PID Controller</h1>");
    strcat(webbuff, "Hit Count - ");
    strcat(webbuff, webcount);
    strcat(webbuff, "</div><br /><hr>");
    strcat(webbuff, "<p><form method=\"POST\">");
    if(led1==0) {
        strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked>  LED 1 off");
        strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" >  LED 1 on<br>");
    } else {
        strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" >  LED 1 off");
        strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked>  LED 1 on<br>");
    }
    strcat(webbuff, "P: <input type=\"text\" name=\"proportional\" size=6 value=\"");
    ConvertToCharArray(_P);
    strcat(webbuff, _str);
    strcat(webbuff, "\"><br>");
    strcat(webbuff, "I: <input type=\"text\" name=\"integral\" size=6 value=\"");
    ConvertToCharArray(_I);
    strcat(webbuff, _str);
    strcat(webbuff, "\"><br>");
    strcat(webbuff, "D: <input type=\"text\" name=\"derivative\" size=6 value=\"");
    ConvertToCharArray(_D);
    strcat(webbuff, _str);
    strcat(webbuff, "\"><br>");
    strcat(webbuff, "<p><input type=\"radio\" name=\"nothing\" value=\"1\" checked>");
    strcat(webbuff, "<p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;");
    strcat(webbuff, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);");
    strcat(webbuff, "background-image:linear-gradient(to bottom, #3498db, #2980b9);");
    strcat(webbuff, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:");
    strcat(webbuff, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;");
    strcat(webbuff, "background: #3cb0fd;");
    strcat(webbuff, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);");
    strcat(webbuff, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);");
    strcat(webbuff, "text-decoration:none;\"></form>");
    strcat(webbuff, "</body></html>");
// end of WEB page data
    bufl = strlen(webbuff); // get total page buffer length
    sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
    timeout=200;
    getcount=7;
    SendCMD();
    getreply();
    SendWEB();  // send web page
    memset(webbuff, '\0', sizeof(webbuff));
    sendcheck();

   
}

void sendcheck(){
    weberror=1;
    timeout=500;
    getcount=24;
    t2.reset();
    t2.start();
    while(weberror==1 && t2.read() <5) {
        getreply();
        if (strstr(replybuff, "SEND OK") != NULL) {
            weberror=0;   // wait for valid SEND OK
        }
    }
    if(weberror==1) { // restart connection
        strcpy(cmdbuff, "AT+CIPMUX=1\r\n");
        timeout=500;
        getcount=10;
        SendCMD();
        getreply();
        pc.printf(replybuff);
        sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
        timeout=500;
        getcount=10;
        SendCMD();
        getreply();
        pc.printf(replybuff);
    } else {
        sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection
        SendCMD();
        getreply();
        pc.printf(replybuff);
    }
    t2.reset();
}

// Large WEB buffer data send
void SendWEB()
{
    int i=0;
    if(esp.writeable()) {
        while(webbuff[i]!='\0') {
            esp.putc(webbuff[i]);
            i++;
        }
    }
}

// Reads and processes GET and POST web data
void ReadWebData(){
    wait_ms(200);
    esp.attach(NULL);
    count=0;
    DataRX=0;
    weberror=0;
    memset(webdata, '\0', sizeof(webdata));
    int x = strcspn (webbuff,"+");
    if(x) {
        strcpy(webdata, webbuff + x);
        pc.printf("webdata received: %s", webdata);
        weberror=0;
        int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
        if (strstr(webdata, "led1=1") != NULL ) {
            led1=1;
        }
        if (strstr(webdata, "led1=0") != NULL ) {
            led1=0;
        }
        if (strstr(webdata, "proportional") != NULL ){
            pc.printf("\n\r looking for data \n\r");
            char* p_webdata = strstr(webdata, "proportional");
            p_webdata = p_webdata + strlen("proportional") + 1;
            int i = 0;
            while(*p_webdata != '&'){
                pc.printf("%c", *p_webdata);
                _str[i] = *p_webdata;
                p_webdata += 1;   
                i++; 
            }
            pc.printf("\n\r");
            _str[i] = '\0';
            pc.printf("proportional: %s", _str);
            _P = atof(_str);
            pc.printf("\n\r end of looking for data \n\r");
        }
        if (strstr(webdata, "integral") != NULL){
            pc.printf("\n\r looking for data \n\r");
            char* p_webdata = strstr(webdata, "integral");
            p_webdata = p_webdata + strlen("integral") + 1;
            int i = 0;
            while(*p_webdata != '&'){
                pc.printf("%c", *p_webdata);
                _str[i] = *p_webdata;
                p_webdata += 1;   
                i++; 
            }
            pc.printf("\n\r");
            _str[i] = '\0';
            pc.printf("integral: %s", _str);
            _I = atof(_str);
            pc.printf("\n\r end of looking for data \n\r");    
            
        }
        if (strstr(webdata, "derivative") != NULL){
            pc.printf("\n\r looking for data \n\r");
            char* p_webdata = strstr(webdata, "derivative");
            p_webdata = p_webdata + strlen("derivative") + 1;
            int i = 0;
            while(*p_webdata != '&'){
                pc.printf("%c", *p_webdata);
                _str[i] = *p_webdata;
                p_webdata += 1;   
                i++; 
            }
            pc.printf("\n\r");
            _str[i] = '\0';
            pc.printf("derivative: %s", _str);
            _D = atof(_str);
            pc.printf("\n\r end of looking for data \n\r");       
        }
        sprintf(channel, "%d",linkID);
        if (strstr(webdata, "GET") != NULL) {
            servreq=1;
        }
        if (strstr(webdata, "POST") != NULL) {
            servreq=1;
        }
        webcounter++;
        sprintf(webcount, "%d",webcounter);
        // Save data to configFile
    }else{
        memset(webbuff, '\0', sizeof(webbuff));
        esp.attach(&callback);
        weberror=1;        
    }    
    
}

void startserver(){
    pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
    strcpy(cmdbuff,"AT+RST\r\n");
    timeout=8000;
    getcount=1000;
    SendCMD();
    getreply();  
    pc.printf(replybuff);
    pc.printf("%d",count);
    if (strstr(replybuff, "OK") != NULL) {
        pc.printf("\n++++++++++ Starting Server ++++++++++\r\n");
        strcpy(cmdbuff, "AT+CIPMUX=1\r\n");  // set multiple connections.
        timeout=500;
        getcount=20;
        SendCMD();
        getreply();
        pc.printf(replybuff);
        sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
        timeout=500;
        getcount=20;
        SendCMD();
        getreply();
        pc.printf(replybuff);
        wait(1);
        sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout);
        timeout=500;
        getcount=50;
        SendCMD();
        getreply();
        pc.printf(replybuff);
        wait(5);
        pc.printf("\n Getting Server IP \r\n");
        strcpy(cmdbuff, "AT+CIFSR\r\n");
        timeout=2500;
        getcount=200;  
        while(weberror==0) {
            SendCMD();
            getreply();
            if (strstr(replybuff, "0.0.0.0") == NULL) {
                weberror=1;   // wait for valid IP
            }
        }
        pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n");
        pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n");
        replybuff[strlen(replybuff)-1] = '\0';
        //char* IP = replybuff + 5;
        sprintf(webdata,"%s", replybuff);
        //SAVE IP ADDRESS
        get_ip_address(webdata);
        pc.printf("ip_address: %s\n\r",ip_address);
        pc.printf(webdata);
        pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
        esp.attach(&callback);
    } else {
        pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n");
        while(1) {}
    }
    t2.reset();
    t2.start();
}

void SendCMD()
{
    esp.printf("%s", cmdbuff);
}

void getreply()
{
    memset(replybuff, '\0', sizeof(replybuff));
    t1.reset();
    t1.start();
    replycount=0;
    while(t1.read_ms()< timeout && replycount < getcount) {
        if(esp.readable()) {
            replybuff[replycount] = esp.getc();
            replycount++;
        }
    }
    t1.stop();
}

void get_ip_address(char web_data[1024]){
    char pre_text[] = "+CIFSR:STAIP,\"";
    char* p_text = strstr(webdata, pre_text);
    p_text = p_text+strlen(pre_text);
    char* p_text2 = strstr(p_text, "\"");
    for(int i = 0;;i++){
        if(*(p_text+i) == *p_text2){
            ip_address[i] = '\0';
            break;
        }
        ip_address[i] = *(p_text+i);
            
    }
}