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.
Dependencies: mbed
main.cpp
- Committer:
- faker_71
- Date:
- 2020-07-21
- Revision:
- 5:dfea6fd1abba
- Parent:
- 4:40dd020463ea
- Child:
- 6:7c9cc335b762
File content as of revision 5:dfea6fd1abba:
// ESP8266 Static page WEB server to control Mbed
#include "mbed.h"
//#include "DS18B20.h"
Serial pc(USBTX, USBRX);
Serial esp(p28, p27); // tx, rx
//DS18B20 thermom(A0, DS18B20::RES_12_BIT);
// Standard Mbed LED definitions
DigitalOut led1(LED1); // (PTB18)
DigitalOut led2(LED2); // (PTB19)
DigitalOut led3(LED3); // (PTD1)
// Digital Out and In pins, can be configured to any suitable pin depending on Platform
DigitalOut Out1(p6);
DigitalOut Out2(p7);
DigitalOut Out3(p8);
DigitalOut reset(p26);
DigitalOut ledR(p17);
DigitalOut ledG(p16);
DigitalIn In1(p9);
DigitalIn In2(p10);
DigitalIn In3(p11);
PwmOut speaker(p21);
AnalogIn Ain1(p18);
AnalogIn Ain2(p19);
Timer t1;
Timer t2;
struct tm t;
int bufflen, DataRX, count, getcount, replycount, servreq, timeout;
int bufl, ipdLen, linkID, weberror, webcounter;
float temperature, AdcIn, Ht;
float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage
char Vcc[10];
char Temp[10];
char temp[10];
char webcount[8];
char lasthit[30];
char timebuf[30];
char type[16];
char type1[16];
char channel[2];
char cmdbuff[32];
char replybuff[1024];
//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 webdata[4096]; // This may need to be bigger depending on WEB browser used
char webbuff[4096*4]; // Currently using 1986 characters, Increase this if more web page data added
void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck();
void gettime(),gettemp(),getbattery(),setRTC(),beep();
// manual set RTC values
int minute =00; // 0-59
int hour =12; // 2-23
int dayofmonth =26; // 1-31
int month =8; // 1-12
int year =15; // last 2 digits
int port =80; // set server port
int SERVtimeout =5; // set server timeout in seconds in case link breaks.
// Serial Interrupt read ESP data
void callback()
{
//pc.printf("\n\r------------ callback is being called --------------\n\r");
led3=1;
while (esp.readable()) {
webbuff[count] = esp.getc();
count++;
}
if(strlen(webbuff)>bufflen) {
pc.printf("\f\n\r------------ webbuff over bufflen --------------\n\r");
DataRX=1;
led3=0;
}
}
int main()
{
reset=0;
pc.baud(115200);
pc.printf("\f\n\r------------ ESP8266 Hardware Reset psq --------------\n\r");
wait(0.5);
reset=1;
led1=1,led2=0,led3=0;
timeout=6000;
getcount=500;
getreply();
esp.baud(115200); // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F
if (time(NULL) < 1420070400) {
setRTC();
}
beep();
startserver();
while(1) {
if(DataRX==1) {
pc.printf("\f\n\r------------ main while > if --------------\n\r");
ReadWebData();
beep();
if (servreq == 1 && weberror == 0) {
pc.printf("\f\n\r------------ befor send page --------------\n\r");
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");
strcpy(lasthit, timebuf);
servreq=0;
}
}
}
// Static WEB page
void sendpage()
{
gettemp();
getbattery();
gettime();
// WEB page data
strcpy(webbuff, "<!DOCTYPE html>");
strcat(webbuff, "<html><head><title>RobotCar</title><meta name='viewport' content='width=device-width'/>");
strcat(webbuff, "</head><body><center><p><strong>Robot Car Remot Controller");
strcat(webbuff, "</strong></p><td style='vertical-align:top;'><strong>Battery level ");
strcat(webbuff, "<input type=\"text\" id=\"leftms\" size=4 value=250>%</strong>");
strcat(webbuff, "</td></p><tr ><strong>Now speed : </strong></tr><nobr id=\"speprint\">ready");
strcat(webbuff, "</nobr><br><tr ><strong>Now action : </strong></tr>");
strcat(webbuff, "<nobr id=\"funprint\">ready</nobr><table><tr><td></td><td>");
strcat(webbuff, "<button id='gobtn' type='button' style='width:100px;height:60px' value=\"GO\" onClick='send_mes(this.id,this.value)'>GO");
strcat(webbuff, "</button></td><td></td></tr><tr><td>");
strcat(webbuff, "<button id='leftbtn' type='button' style='width:100px;height:60px' value=\"LEFT\" onClick='send_mes(this.id,this.value)' >LEFT");
strcat(webbuff, "</button></td><td>");
strcat(webbuff, "<button id='stopbtn' type='button' style='width:100px;height:60px' value=\"STOP\" onClick='send_mes(this.id,this.value)' >STOP");
strcat(webbuff, "</button></td><td>");
strcat(webbuff, "<button id='rightbtn' type='button' style='width:100px;height:60px' value=\"RIGHT\" onClick='send_mes(this.id,this.value)' >RIGHT");
strcat(webbuff, "</button></td></tr><td></td><td>");
strcat(webbuff, "<button id='backbtn' type='button' style='width:100px;height:60px' value=\"BACK\" onClick='send_mes(this.id,this.value)' >BACK");
strcat(webbuff, "</button></td><td style='vertical-align:top; text-align:right;'></td></tr></table>");
/*
strcat(webbuff, "<strong>Addition functions:</strong><table>");
strcat(webbuff, "<td>");//ok
strcat(webbuff, "<button id='avoidbtn' type='button' style='width:100px;height:60px' value=\"AVOIDANCE\" onClick='send_mes_fun(this.id,this.value)' >");
strcat(webbuff, "AVOIDANCE</button></td><td>");
strcat(webbuff, "<button id='tracebtn' type='button' style='width:100px;height:60px' value=\"LINE TRACE\" onClick='send_mes_fun(this.id,this.value)' >LINE TRACE");
strcat(webbuff, "</button></td><td>");
strcat(webbuff, "<button id='defbtn' type='button' style='width:100px;height:60px' value=\"DEFAULT\" onClick='send_mes_fun(this.id,this.value)' >DEFAULT");
strcat(webbuff, "</button></td></table><table><td>");
strcat(webbuff, "</td><strong>Speed level:</strong>");
strcat(webbuff, "</table><td>");
strcat(webbuff, "<button id='sp1btn' type='button' style='width:100px;height:60px' value=\"SLOW\" onClick='send_mes_spe(this.id,this.value)' >SLOW");
strcat(webbuff, "</button></td><td>");
strcat(webbuff, "<button id='sp2btn' type='button' style='width:100px;height:60px' value=\"FAST\" onClick='send_mes_spe(this.id,this.value)' >FAST");
strcat(webbuff, "</button>");
strcat(webbuff, "</td>");
strcat(webbuff, "<td>");
strcat(webbuff, "<button id='sp3btn' type='button' style='width:100px;height:60px' value=\"FLY\" onClick='send_mes_spe(this.id,this.value)' >FLY");
strcat(webbuff, "</button>");
strcat(webbuff, "</td>");
strcat(webbuff, "<table>");
strcat(webbuff, "<td>");
strcat(webbuff, "</td>");
strcat(webbuff, "</table>"); */
strcat(webbuff, "</center>");
strcat(webbuff, "</body>");
strcat(webbuff, "</html>");
strcat(webbuff, "<script language=\"javascript\" type=\"text/javascript\">");
strcat(webbuff, "function send_mes(btnmes,btnval){");
strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;");
strcat(webbuff, "htmlacs(url);");
strcat(webbuff, "console.log(url);");
strcat(webbuff, "}");
strcat(webbuff, "function htmlacs(url) {");
strcat(webbuff, "var xhr = new XMLHttpRequest();");
strcat(webbuff, "xhr.open(\"GET\", url);");
strcat(webbuff, "xhr.send(\"\");");
strcat(webbuff, "}");
strcat(webbuff, "function send_mes_spe(btnmes,btnval){");
strcat(webbuff, "console.log(btnmes);");
strcat(webbuff, "if(document.getElementById(\"speprint\")){");
strcat(webbuff, "document.getElementById(\"speprint\").innerHTML=btnval;");
strcat(webbuff, "}");
strcat(webbuff, "}");
strcat(webbuff, "function send_mes_fun(btnmes,btnval){");
strcat(webbuff, "console.log(btnmes);");
strcat(webbuff, "if(document.getElementById(\"funprint\")){");
strcat(webbuff, "document.getElementById(\"funprint\").innerHTML=btnval;");
strcat(webbuff, "}");
strcat(webbuff, "}");
strcat(webbuff, "</script>");
// 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();
pc.printf("\n++++++++++ bufl is %d ++++++++++\r\n",bufl);
SendWEB(); // send web page
memset(webbuff, '\0', sizeof(webbuff));
sendcheck();
}
// wait for ESP "SEND OK" reply, then close IP to load web page
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();
}
// 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);
weberror=0;
int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
int i=0;
pc.printf("+++++++++++++++++succed+++++++++++++++++++++");
pc.printf("%s",webdata);
if( strstr(webdata, "LEFT") != NULL ) {
ledR=!ledR;
}
if( strstr(webdata, "RIGHT") != NULL ) {
ledG=!ledG;
}
pc.printf("+++++++++++++++++succed+++++++++++++++++++++");
if( strstr(webdata, "sp3btn=0") != NULL ) {
led1=!led1;
}
if( strstr(webdata, "led1=1") != NULL ) {
led1=1;
}
if( strstr(webdata, "led1=0") != NULL ) {
led1=0;
}
if( strstr(webdata, "Out1=1") != NULL ) {
Out1=1;
}
if( strstr(webdata, "Out1=0") != NULL ) {
Out1=0;
}
if( strstr(webdata, "Out2=1") != NULL ) {
Out2=1;
}
if( strstr(webdata, "Out2=0") != NULL ) {
Out2=0;
}
if( strstr(webdata, "Out3=1") != NULL ) {
Out3=1;
}
if( strstr(webdata, "Out3=0") != NULL ) {
Out3=0;
}
sprintf(channel, "%d",linkID);
if (strstr(webdata, "GET") != NULL) {
servreq=1;
}
if (strstr(webdata, "POST") != NULL) {
servreq=1;
}
webcounter++;
sprintf(webcount, "%d",webcounter);
} else {
memset(webbuff, '\0', sizeof(webbuff));
esp.attach(&callback);
weberror=1;
}
}
// Starts and restarts webserver if errors detected.
void startserver()
{
gettemp();
gettime();
pc.printf("\n\n RTC time %s\r\n\n",timebuf);
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);
pc.printf(webdata);
led2=1;
bufflen=200;
count=0;
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();
beep();
}
// ESP Command data send
void SendCMD()
{
esp.printf("%s", cmdbuff);
}
// Large WEB buffer data send
void SendWEB()
{
int i=0;
if(esp.writeable()) {
while(webbuff[i]!='\0') {
esp.putc(webbuff[i]);
i++;
pc.printf("%s",webbuff[i]);
}
}
pc.printf("\n++++++++++ send web info ++++++++++\r\n");
}
// Get Command and ESP status replies
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();
}
// Analog in example
void getbattery()
{
AdcIn=Ain1.read();
Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy
sprintf(Vcc,"%2.3f",Ht);
}
// Temperature example
void gettemp()
{
AdcIn=Ain2.read();
Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy
sprintf(Temp,"%2.3f",Ht);
}
// Get RTC time
void gettime()
{
time_t seconds = time(NULL);
strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
}
void beep()
{
speaker.period(1.0/2000); // 2000hz period
speaker = 0.5; //50% duty cycle - max volume
wait_ms(60);
speaker=0.0; // turn off audio
}
void setRTC()
{
t.tm_sec = (0); // 0-59
t.tm_min = (minute); // 0-59
t.tm_hour = (hour); // 0-23
t.tm_mday = (dayofmonth); // 1-31
t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format
t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year
set_time(mktime(&t)); // set RTC clock
}