TES WEBSERVER ESP8266 STM32F401-RE

Dependencies:   mbed

Fork of ESP8266-WEB-Mbed-Controller by Paul Staron

Committer:
qrimly
Date:
Mon Apr 03 02:48:55 2017 +0000
Revision:
4:54d4cfc9ef4b
Parent:
3:f7febfa77784
TEST ESP8266 WEBSERVER

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:e2a155f50119 1 // ESP8266 Static page WEB server to control Mbed
star297 0:e2a155f50119 2
star297 0:e2a155f50119 3 #include "mbed.h"
star297 0:e2a155f50119 4
star297 0:e2a155f50119 5 Serial pc(USBTX, USBRX);
qrimly 4:54d4cfc9ef4b 6 Serial esp(PB_6, PA_10); // tx, rx
star297 2:d4c6bc0f2dc4 7
star297 0:e2a155f50119 8 // Standard Mbed LED definitions
qrimly 4:54d4cfc9ef4b 9 DigitalOut led1(D11); // (PTB18)
qrimly 4:54d4cfc9ef4b 10 DigitalOut led2(D12); // (PTB19)
qrimly 4:54d4cfc9ef4b 11 DigitalOut led3(D13); // (PTD1)
star297 0:e2a155f50119 12
star297 0:e2a155f50119 13 // Digital Out and In pins, can be configured to any suitable pin depending on Platform
star297 2:d4c6bc0f2dc4 14 DigitalOut Out1(D7);
star297 2:d4c6bc0f2dc4 15 DigitalOut Out2(D8);
star297 2:d4c6bc0f2dc4 16 DigitalOut Out3(D4);
star297 0:e2a155f50119 17
star297 2:d4c6bc0f2dc4 18 DigitalIn In1(A2);
star297 2:d4c6bc0f2dc4 19 DigitalIn In2(A3);
star297 2:d4c6bc0f2dc4 20 DigitalIn In3(A4);
star297 0:e2a155f50119 21
star297 2:d4c6bc0f2dc4 22 PwmOut speaker(D5);
star297 2:d4c6bc0f2dc4 23 AnalogIn BATin(A1);
star297 0:e2a155f50119 24
star297 0:e2a155f50119 25 Timer t1;
star297 1:71ed1afbf344 26 Timer t2;
star297 1:71ed1afbf344 27
star297 1:71ed1afbf344 28 struct tm t;
star297 0:e2a155f50119 29
star297 3:f7febfa77784 30 int bufflen, DataRX, count, getcount, replycount, servreq, timeout;
star297 2:d4c6bc0f2dc4 31 int bufl, ipdLen, linkID, weberror, webcounter;
star297 0:e2a155f50119 32 float temperature, AdcIn, Ht;
star297 0:e2a155f50119 33 float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage
star297 0:e2a155f50119 34 char Vcc[10];
star297 0:e2a155f50119 35 char Temp[10];
star297 0:e2a155f50119 36 char temp[10];
star297 2:d4c6bc0f2dc4 37 char webcount[8];
star297 3:f7febfa77784 38 char lasthit[30];
star297 0:e2a155f50119 39 char timebuf[30];
star297 0:e2a155f50119 40 char type[16];
star297 0:e2a155f50119 41 char type1[16];
star297 0:e2a155f50119 42 char channel[2];
star297 3:f7febfa77784 43 char cmdbuff[32];
star297 3:f7febfa77784 44 char replybuff[512];
star297 0:e2a155f50119 45 char webdata[1024]; // This may need to be bigger depending on WEB browser used
star297 3:f7febfa77784 46 char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added
star297 0:e2a155f50119 47
star297 0:e2a155f50119 48 void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck();
star297 2:d4c6bc0f2dc4 49 void gettime(),gettemp(),getbattery(),setRTC(),beep();
star297 0:e2a155f50119 50
star297 1:71ed1afbf344 51 // manual set RTC values
star297 3:f7febfa77784 52 int minute =20; // 0-59
star297 3:f7febfa77784 53 int hour =9; // 2-23
star297 3:f7febfa77784 54 int dayofmonth =20; // 1-31
star297 1:71ed1afbf344 55 int month =2; // 1-12
star297 1:71ed1afbf344 56 int year =15; // last 2 digits
star297 2:d4c6bc0f2dc4 57
star297 3:f7febfa77784 58 int port =8266; // set server port
star297 3:f7febfa77784 59 int SERVtimeout =5; // set server timeout in seconds incase link breaks.
star297 1:71ed1afbf344 60
star297 0:e2a155f50119 61 // Serial Interrupt read ESP data
star297 0:e2a155f50119 62 void callback() {
star297 3:f7febfa77784 63 while (esp.readable()) {webbuff[count] = esp.getc();count++;}
star297 3:f7febfa77784 64 if(strlen(webbuff)>bufflen){DataRX=1;}
star297 0:e2a155f50119 65 }
star297 0:e2a155f50119 66
star297 0:e2a155f50119 67 int main() {
star297 0:e2a155f50119 68 led1=1,led2=1,led3=1;
star297 0:e2a155f50119 69 pc.baud(115200);
star297 3:f7febfa77784 70 esp.baud(115200); // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F
star297 1:71ed1afbf344 71 if (time(NULL) < 1420070400) {setRTC();}
star297 2:d4c6bc0f2dc4 72 beep();
star297 0:e2a155f50119 73 startserver();
star297 0:e2a155f50119 74
star297 3:f7febfa77784 75 while(1){
star297 0:e2a155f50119 76 if(DataRX==1){
star297 0:e2a155f50119 77 ReadWebData();
star297 2:d4c6bc0f2dc4 78 beep();
star297 3:f7febfa77784 79 if (servreq == 1 && weberror == 0){sendpage();}
star297 3:f7febfa77784 80 esp.attach(&callback);
star297 0:e2a155f50119 81 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);
star297 0:e2a155f50119 82 pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata);
star297 0:e2a155f50119 83 pc.printf(" Web Characters sent : %d\n\n", bufl);
star297 0:e2a155f50119 84 pc.printf(" -------------------------------------\n\n");
star297 2:d4c6bc0f2dc4 85 strcpy(lasthit, timebuf);
star297 0:e2a155f50119 86 servreq=0;
star297 3:f7febfa77784 87 }
star297 0:e2a155f50119 88 }
star297 0:e2a155f50119 89 }
star297 0:e2a155f50119 90 // Static WEB page
star297 0:e2a155f50119 91 void sendpage()
star297 0:e2a155f50119 92 {
star297 3:f7febfa77784 93 gettemp();
star297 3:f7febfa77784 94 getbattery();gettime();
star297 3:f7febfa77784 95
star297 2:d4c6bc0f2dc4 96 // WEB page data
star297 3:f7febfa77784 97 strcpy(webbuff, "<!DOCTYPE html>");
star297 3:f7febfa77784 98 strcat(webbuff, "<html><head><title>ESP8266 Mbed</title></head>");
star297 3:f7febfa77784 99 strcat(webbuff, "<body>");
star297 3:f7febfa77784 100 strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>ESP8266 Mbed Web Controller</h1>");
star297 3:f7febfa77784 101 strcat(webbuff, "Hit Count - ");
star297 3:f7febfa77784 102 strcat(webbuff, webcount);
star297 3:f7febfa77784 103 strcat(webbuff, "<br>Last Hit - ");
star297 3:f7febfa77784 104 strcat(webbuff, lasthit);
star297 3:f7febfa77784 105 strcat(webbuff, "</div><br /><hr>");
star297 3:f7febfa77784 106 strcat(webbuff, "<h3>Mbed RTC Time -&nbsp&nbsp");
star297 3:f7febfa77784 107 strcat(webbuff, timebuf);
star297 3:f7febfa77784 108 strcat(webbuff, "</h3>\r\n");
star297 3:f7febfa77784 109 strcat(webbuff, "<p><form method=\"POST\"><strong> Temperature:&nbsp&nbsp<input type=\"text\" size=6 value=\"");
star297 3:f7febfa77784 110 strcat(webbuff, Temp);
star297 3:f7febfa77784 111 strcat(webbuff, "\"> <sup>O</sup>C <form method=\"POST\"> <strong> &nbsp&nbspBattery:&nbsp&nbsp<input type=\"text\" size=4 value=\"");
star297 3:f7febfa77784 112 strcat(webbuff, Vcc);
star297 3:f7febfa77784 113 strcat(webbuff, "\"> </sup>V");
star297 3:f7febfa77784 114 if(led1==1){strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked> Red LED off");
star297 3:f7febfa77784 115 strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" > Red LED on");}
star297 3:f7febfa77784 116 else{strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" > Red LED off");
star297 3:f7febfa77784 117 strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked> Red LED on");}
star297 3:f7febfa77784 118 if(Out1==0){strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" checked> Digital Out 1 off");
star297 3:f7febfa77784 119 strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" > Digital Out 1 on");}
star297 3:f7febfa77784 120 else{strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" > Digital Out 1 off");
star297 3:f7febfa77784 121 strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" checked> Digital Out 1 on");}
star297 3:f7febfa77784 122 if(Out2==0){strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" checked> Digital Out 2 off");
star297 3:f7febfa77784 123 strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" > Digital Out 2 on");}
star297 3:f7febfa77784 124 else{strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" > Digital Out 2 off");
star297 3:f7febfa77784 125 strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" checked> Digital Out 2 on");}
star297 3:f7febfa77784 126 if(Out3==0){strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" checked> Digital Out 3 off");
star297 3:f7febfa77784 127 strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" > Digital Out 3 on");}
star297 3:f7febfa77784 128 else{strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" > Digital Out 3 off");
star297 3:f7febfa77784 129 strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" checked> Digital Out 3 on");}
star297 3:f7febfa77784 130 if(In1==0){strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"0\" > Digital In 1");}
star297 3:f7febfa77784 131 else{strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"1\" checked> Digital In 1");}
star297 3:f7febfa77784 132 if(In2==0){strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"0\" > Digital In 2");}
star297 3:f7febfa77784 133 else{strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"1\" checked> Digital In 2");}
star297 3:f7febfa77784 134 if(In3==0){strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"0\" > Digital In 3");}
star297 3:f7febfa77784 135 else{strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"1\" checked> Digital In 3");}
star297 3:f7febfa77784 136 strcat(webbuff, "</strong><p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;");
star297 3:f7febfa77784 137 strcat(webbuff, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);");
star297 3:f7febfa77784 138 strcat(webbuff, "background-image:linear-gradient(to bottom, #3498db, #2980b9);");
star297 3:f7febfa77784 139 strcat(webbuff, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:");
star297 3:f7febfa77784 140 strcat(webbuff, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;");
star297 3:f7febfa77784 141 strcat(webbuff, "background: #3cb0fd;");
star297 3:f7febfa77784 142 strcat(webbuff, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);");
star297 3:f7febfa77784 143 strcat(webbuff, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);");
star297 3:f7febfa77784 144 strcat(webbuff, "text-decoration:none;\"></form></span>");
star297 3:f7febfa77784 145 strcat(webbuff, "<p/><h2>How to use:</h2><ul>");
star297 3:f7febfa77784 146 strcat(webbuff, "<li>Select the Radio buttons to control the digital out pins.</li>");
star297 3:f7febfa77784 147 strcat(webbuff, "<li>Click 'Send-Refresh' to send.</li>");
star297 3:f7febfa77784 148 strcat(webbuff, "<li>Use the 'Send-Refresh' button to refresh the data.</li>");
star297 3:f7febfa77784 149 strcat(webbuff, "</ul>");
star297 3:f7febfa77784 150 strcat(webbuff, "</body></html>");
star297 0:e2a155f50119 151 // end of WEB page data
star297 3:f7febfa77784 152 bufl = strlen(webbuff); // get total page buffer length
star297 3:f7febfa77784 153 sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
star297 3:f7febfa77784 154 timeout=200;getcount=7;
star297 3:f7febfa77784 155 SendCMD();
star297 3:f7febfa77784 156 getreply();
star297 0:e2a155f50119 157 SendWEB(); // send web page
star297 3:f7febfa77784 158 memset(webbuff, '\0', sizeof(webbuff));
star297 0:e2a155f50119 159 sendcheck();
star297 0:e2a155f50119 160 }
star297 0:e2a155f50119 161
star297 3:f7febfa77784 162 // wait for ESP "SEND OK" reply, then close IP to load web page
star297 0:e2a155f50119 163 void sendcheck()
star297 0:e2a155f50119 164 {
star297 3:f7febfa77784 165 weberror=1;timeout=500;getcount=24;
star297 3:f7febfa77784 166 t2.reset();t2.start();
star297 3:f7febfa77784 167 while(weberror==1 && t2.read() <5){
star297 0:e2a155f50119 168 getreply();
star297 3:f7febfa77784 169 if (strstr(replybuff, "SEND OK") != NULL) {weberror=0;} // wait for valid SEND OK
star297 0:e2a155f50119 170 }
star297 3:f7febfa77784 171 if(weberror==1){ // restart connection
star297 3:f7febfa77784 172 strcpy(cmdbuff, "AT+CIPMUX=1\r\n");
star297 3:f7febfa77784 173 timeout=500;getcount=10;
star297 3:f7febfa77784 174 SendCMD();getreply();
star297 3:f7febfa77784 175 sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
star297 3:f7febfa77784 176 timeout=500;getcount=10;
star297 3:f7febfa77784 177 SendCMD();getreply();
star297 3:f7febfa77784 178 }
star297 3:f7febfa77784 179 else{
star297 3:f7febfa77784 180 sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection
star297 3:f7febfa77784 181 SendCMD();}
star297 3:f7febfa77784 182 t2.reset();
star297 0:e2a155f50119 183 }
star297 0:e2a155f50119 184
star297 0:e2a155f50119 185 // Reads and processes GET and POST web data
star297 0:e2a155f50119 186 void ReadWebData()
star297 0:e2a155f50119 187 {
star297 3:f7febfa77784 188 wait_ms(200);
star297 3:f7febfa77784 189 esp.attach(NULL);
star297 3:f7febfa77784 190 count=0;DataRX=0;weberror=0;
star297 3:f7febfa77784 191 memset(webdata, '\0', sizeof(webdata));
star297 3:f7febfa77784 192 int x = strcspn (webbuff,"+");
star297 0:e2a155f50119 193 if(x){
star297 3:f7febfa77784 194 strcpy(webdata, webbuff + x);weberror=0;
star297 0:e2a155f50119 195 int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
star297 0:e2a155f50119 196 if( strstr(webdata, "led1=1") != NULL ) {led1=0;}
star297 0:e2a155f50119 197 if( strstr(webdata, "led1=0") != NULL ) {led1=1;}
star297 0:e2a155f50119 198 if( strstr(webdata, "Out1=1") != NULL ) {Out1=1;}
star297 0:e2a155f50119 199 if( strstr(webdata, "Out1=0") != NULL ) {Out1=0;}
star297 0:e2a155f50119 200 if( strstr(webdata, "Out2=1") != NULL ) {Out2=1;}
star297 0:e2a155f50119 201 if( strstr(webdata, "Out2=0") != NULL ) {Out2=0;}
star297 0:e2a155f50119 202 if( strstr(webdata, "Out3=1") != NULL ) {Out3=1;}
star297 0:e2a155f50119 203 if( strstr(webdata, "Out3=0") != NULL ) {Out3=0;}
star297 0:e2a155f50119 204 sprintf(channel, "%d",linkID);
star297 0:e2a155f50119 205 if (strstr(webdata, "GET") != NULL) {servreq=1;}
star297 0:e2a155f50119 206 if (strstr(webdata, "POST") != NULL) {servreq=1;}
star297 2:d4c6bc0f2dc4 207 webcounter++;
star297 2:d4c6bc0f2dc4 208 sprintf(webcount, "%d",webcounter);
star297 0:e2a155f50119 209 }
star297 3:f7febfa77784 210 else {
star297 3:f7febfa77784 211 memset(webbuff, '\0', sizeof(webbuff));
star297 3:f7febfa77784 212 esp.attach(&callback);weberror=1;
star297 3:f7febfa77784 213 }
star297 0:e2a155f50119 214 }
star297 0:e2a155f50119 215 // Starts and restarts webserver if errors detected.
star297 0:e2a155f50119 216 void startserver()
star297 0:e2a155f50119 217 {
star297 3:f7febfa77784 218 gettemp();gettime();
star297 1:71ed1afbf344 219 pc.printf("\n\n RTC time %s\r\n\n",timebuf);
star297 1:71ed1afbf344 220 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
star297 3:f7febfa77784 221 strcpy(cmdbuff,"AT+RST\r\n");
star297 3:f7febfa77784 222 timeout=2000;getcount=600;
star297 0:e2a155f50119 223 SendCMD();
star297 1:71ed1afbf344 224 getreply();
star297 3:f7febfa77784 225 pc.printf(replybuff);
star297 3:f7febfa77784 226 pc.printf("%d",count);
star297 3:f7febfa77784 227 if (strstr(replybuff, "OK") != NULL) {
star297 1:71ed1afbf344 228 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n");
star297 3:f7febfa77784 229 strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections.
star297 3:f7febfa77784 230 timeout=500;getcount=10;
star297 1:71ed1afbf344 231 SendCMD();
star297 2:d4c6bc0f2dc4 232 getreply();
star297 3:f7febfa77784 233 sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
star297 3:f7febfa77784 234 timeout=500;getcount=10;
star297 1:71ed1afbf344 235 SendCMD();
star297 3:f7febfa77784 236 getreply();
star297 3:f7febfa77784 237 sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout);
star297 3:f7febfa77784 238 timeout=500;getcount=50;
star297 3:f7febfa77784 239 SendCMD();
star297 2:d4c6bc0f2dc4 240 getreply();
star297 1:71ed1afbf344 241 pc.printf("\n Getting Server IP \r\n");
star297 3:f7febfa77784 242 strcpy(cmdbuff, "AT+CIFSR\r\n");
star297 2:d4c6bc0f2dc4 243 timeout=1000;getcount=50;
star297 1:71ed1afbf344 244 while(weberror==0){
star297 1:71ed1afbf344 245 SendCMD();getreply();
star297 3:f7febfa77784 246 if (strstr(replybuff, "0.0.0.0") == NULL) {weberror=1;} // wait for valid IP
star297 1:71ed1afbf344 247 }
star297 1:71ed1afbf344 248 pc.printf("\n Enter WEB address in your browser \r\n\n");
star297 3:f7febfa77784 249 replybuff[strlen(replybuff) - 8] = '\0';
star297 3:f7febfa77784 250 char* IP = replybuff + 5;
star297 3:f7febfa77784 251 sprintf(webdata," http://%s:%d", IP, port);
star297 3:f7febfa77784 252 pc.printf(webdata);
star297 1:71ed1afbf344 253 led2=0;wait(2);led2=1;
star297 3:f7febfa77784 254 bufflen=200;count=0;
star297 1:71ed1afbf344 255 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
star297 1:71ed1afbf344 256 esp.attach(&callback);
star297 0:e2a155f50119 257 }
star297 1:71ed1afbf344 258 else{
star297 1:71ed1afbf344 259 pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n");
star297 1:71ed1afbf344 260 while(1){}
star297 2:d4c6bc0f2dc4 261 }
star297 2:d4c6bc0f2dc4 262 t2.reset();t2.start();beep();
star297 0:e2a155f50119 263 }
star297 0:e2a155f50119 264 // ESP Command data send
star297 0:e2a155f50119 265 void SendCMD()
star297 0:e2a155f50119 266 {
star297 3:f7febfa77784 267 esp.printf("%s", cmdbuff);
star297 0:e2a155f50119 268 }
star297 0:e2a155f50119 269 // Large WEB buffer data send
star297 0:e2a155f50119 270 void SendWEB()
star297 0:e2a155f50119 271 {
star297 0:e2a155f50119 272 int i=0;
star297 0:e2a155f50119 273 if(esp.writeable()) {
star297 3:f7febfa77784 274 while(webbuff[i]!='\0') {esp.putc(webbuff[i]);i++;}
star297 0:e2a155f50119 275 }
star297 0:e2a155f50119 276 }
star297 0:e2a155f50119 277 // Get Cammand and ESP status replies
star297 0:e2a155f50119 278 void getreply()
star297 0:e2a155f50119 279 {
star297 3:f7febfa77784 280 memset(replybuff, '\0', sizeof(replybuff));
star297 3:f7febfa77784 281 t1.reset(); t1.start();replycount=0;
star297 3:f7febfa77784 282 while(t1.read_ms()< timeout && replycount < getcount) {
star297 0:e2a155f50119 283 if(esp.readable()) {
star297 3:f7febfa77784 284 replybuff[replycount] = esp.getc();replycount++;
star297 0:e2a155f50119 285 }
star297 2:d4c6bc0f2dc4 286 }
star297 2:d4c6bc0f2dc4 287 t1.stop();
star297 0:e2a155f50119 288 }
star297 0:e2a155f50119 289 // Analog in example
star297 0:e2a155f50119 290 void getbattery()
star297 0:e2a155f50119 291 {
star297 0:e2a155f50119 292 AdcIn=BATin.read();
star297 2:d4c6bc0f2dc4 293 Ht = (AdcIn*3.328f*(R1+R2)/R2); // set the numeric to the exact MCU analog reference voltage for greater accuracy
star297 0:e2a155f50119 294 sprintf(Vcc,"%2.3f",Ht);
star297 0:e2a155f50119 295 }
star297 0:e2a155f50119 296 // Temperature example
star297 0:e2a155f50119 297 void gettemp()
star297 2:d4c6bc0f2dc4 298 {
qrimly 4:54d4cfc9ef4b 299 //temperature=thermom.GetTemperature(); // comment this out if no sensor connected
qrimly 4:54d4cfc9ef4b 300 temperature = 21.357; // include for dummy value
star297 0:e2a155f50119 301 sprintf(Temp,"%3.3f",temperature);
star297 0:e2a155f50119 302 }
star297 0:e2a155f50119 303 // Get RTC time
star297 0:e2a155f50119 304 void gettime()
star297 0:e2a155f50119 305 {
star297 0:e2a155f50119 306 time_t seconds = time(NULL);
star297 0:e2a155f50119 307 strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
star297 0:e2a155f50119 308 }
star297 0:e2a155f50119 309
star297 2:d4c6bc0f2dc4 310 void beep()
star297 2:d4c6bc0f2dc4 311 {
star297 2:d4c6bc0f2dc4 312 speaker.period(1.0/2000); // 2000hz period
star297 2:d4c6bc0f2dc4 313 speaker = 0.5; //50% duty cycle - max volume
star297 2:d4c6bc0f2dc4 314 wait_ms(60);
star297 2:d4c6bc0f2dc4 315 speaker=0.0; // turn off audio
star297 2:d4c6bc0f2dc4 316 }
star297 2:d4c6bc0f2dc4 317
star297 1:71ed1afbf344 318 void setRTC()
star297 1:71ed1afbf344 319 {
star297 1:71ed1afbf344 320 t.tm_sec = (0); // 0-59
star297 1:71ed1afbf344 321 t.tm_min = (minute); // 0-59
star297 1:71ed1afbf344 322 t.tm_hour = (hour); // 0-23
star297 1:71ed1afbf344 323 t.tm_mday = (dayofmonth); // 1-31
star297 1:71ed1afbf344 324 t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format
star297 1:71ed1afbf344 325 t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year
star297 1:71ed1afbf344 326 set_time(mktime(&t)); // set RTC clock
star297 1:71ed1afbf344 327 }