IoT Alarm System
Dependencies: ESP8266NodeMCUInterface JPEGCamera SDFileSystem IoT_Security xbee_lib
Fork of HUZZAHESP8266-web-control-LPC1768 by
Diff: main.cpp
- Revision:
- 5:bc0296a5ad8a
- Parent:
- 4:40dd020463ea
- Child:
- 6:bcbe1be26da5
--- a/main.cpp Fri Aug 28 01:37:50 2015 +0000
+++ b/main.cpp Fri Mar 18 19:10:19 2016 +0000
@@ -1,58 +1,57 @@
// 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)
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
-// 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);
-
-DigitalIn In1(p9);
-DigitalIn In2(p10);
-DigitalIn In3(p11);
-
-PwmOut speaker(p21);
+// some test values to show on web page
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;
+/*
+char ssid[32] = "hsd"; // enter WiFi router ssid inside the quotes
+char pwd [32] = "austin123"; // enter WiFi router password inside the quotes
+*/
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];
+
+// things for sending/receiving data over serial
+volatile int tx_in=0;
+volatile int tx_out=0;
+volatile int rx_in=0;
+volatile int rx_out=0;
+const int buffer_size = 4095;
+char tx_buffer[buffer_size+1];
+char rx_buffer[buffer_size+1];
+void Tx_interrupt();
+void Rx_interrupt();
+void send_line();
+void read_line();
+
+int DataRX;
+int update;
+int count;
+char cmdbuff[1024];
+char replybuff[4096];
+char webdata[4096]; // 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 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
-
-void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck();
-void gettime(),gettemp(),getbattery(),setRTC(),beep();
-
+void SendCMD(),getreply(),ReadWebData(),startserver();
+void gettime(),setRTC(),gettemp(),getbattery();
+char rx_line[1024];
+int port =80; // set server port
+int SERVtimeout =5; // set server timeout in seconds in case link breaks.
+struct tm t;
// manual set RTC values
int minute =00; // 0-59
int hour =12; // 2-23
@@ -60,339 +59,328 @@
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()
-{
- led3=1;
- while (esp.readable()) {
- webbuff[count] = esp.getc();
- count++;
- }
- if(strlen(webbuff)>bufflen) {
- DataRX=1;
- led3=0;
- }
-}
-
int main()
{
- reset=0;
- pc.baud(115200);
-
- pc.printf("\f\n\r------------ ESP8266 Hardware Reset --------------\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
+ pc.baud(9600);
+ esp.baud(9600);
+ led1=1,led2=0,led3=0, led4=0;
+ // Setup a serial interrupt function to receive data
+ esp.attach(&Rx_interrupt, Serial::RxIrq);
+ // Setup a serial interrupt function to transmit data
+ esp.attach(&Tx_interrupt, Serial::TxIrq);
if (time(NULL) < 1420070400) {
setRTC();
}
- beep();
startserver();
-
+ DataRX=0;
+ count=0;
while(1) {
if(DataRX==1) {
ReadWebData();
- beep();
- 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");
- strcpy(lasthit, timebuf);
- servreq=0;
+ esp.attach(&Rx_interrupt, Serial::RxIrq);
+ }
+ if(update==1) // update time, hit count, and analog levels in the HUZZAH chip
+ {
+ // get new values
+ gettime();
+ gettemp();
+ getbattery();
+ count++;
+ // send new values
+ sprintf(cmdbuff, "count,time,analog1,analog2=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,Temp,Vcc);
+ SendCMD();
+ getreply();
+ update=0;
}
}
}
-// Static WEB page
-void sendpage()
-{
- gettemp();
- getbattery();
- gettime();
-
-// WEB page data
- 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 Controller</h1>");
- strcat(webbuff, "Hit Count - ");
- strcat(webbuff, webcount);
- strcat(webbuff, "<br>Last Hit - ");
- strcat(webbuff, lasthit);
- strcat(webbuff, "</div><br /><hr>");
- strcat(webbuff, "<h3>Mbed RTC Time -  ");
- strcat(webbuff, timebuf);
- strcat(webbuff, "</h3>\r\n");
- strcat(webbuff, "<p><form method=\"POST\"><strong> Analog 1:  <input type=\"text\" size=6 value=\"");
- strcat(webbuff, Temp);
- strcat(webbuff, "\"> </sup>V <form method=\"POST\"> <strong>   Analog 2:  <input type=\"text\" size=4 value=\"");
- strcat(webbuff, Vcc);
- strcat(webbuff, "\"> </sup>V");
- 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");
- } 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");
- }
- if(Out1==0) {
- strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" checked> Digital Out 1 off");
- strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" > Digital Out 1 on");
- } else {
- strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" > Digital Out 1 off");
- strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" checked> Digital Out 1 on");
- }
- if(Out2==0) {
- strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" checked> Digital Out 2 off");
- strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" > Digital Out 2 on");
- } else {
- strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" > Digital Out 2 off");
- strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" checked> Digital Out 2 on");
- }
- if(Out3==0) {
- strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" checked> Digital Out 3 off");
- strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" > Digital Out 3 on");
- } else {
- strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" > Digital Out 3 off");
- strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" checked> Digital Out 3 on");
- }
- if(In1==0) {
- strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"0\" > Digital In 1");
- } else {
- strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"1\" checked> Digital In 1");
- }
- if(In2==0) {
- strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"0\" > Digital In 2");
- } else {
- strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"1\" checked> Digital In 2");
- }
- if(In3==0) {
- strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"0\" > Digital In 3");
- } else {
- strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"1\" checked> Digital In 3");
- }
- strcat(webbuff, "</strong><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></span>");
- strcat(webbuff, "<p/><h2>How to use:</h2><ul>");
- strcat(webbuff, "<li>Select the Radio buttons to control the digital out pins.</li>");
- strcat(webbuff, "<li>Click 'Send-Refresh' to send.</li>");
- strcat(webbuff, "<li>Use the 'Send-Refresh' button to refresh the data.</li>");
- strcat(webbuff, "</ul>");
- 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();
-}
-
-// 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;
+ esp.attach(NULL,Serial::RxIrq);
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);
- 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;
+ strcpy(webdata, rx_buffer);
+ memset(rx_buffer, '\0', sizeof(rx_buffer));
+ rx_in = 0;
+ rx_out = 0;
+ // check web data for form information
+ if( strstr(webdata, "check=led1v") != NULL ) {
+ led1=!led1;
+ }
+ if( strstr(webdata, "check=led2v") != NULL ) {
+ led2=!led2;
+ }
+ if( strstr(webdata, "check=led3v") != NULL ) {
+ led3=!led3;
+ }
+ if( strstr(webdata, "check=led4v") != NULL ) {
+ led4=!led4;
+ }
+ if( strstr(webdata, "POST") != NULL ) { // set update flag if POST request
+ update=1;
+ }
+ if( strstr(webdata, "GET") != NULL && strstr(webdata, "favicon") == NULL ) { // set update flag for GET request but do not want to update for favicon requests
+ update=1;
}
}
-// Starts and restarts webserver if errors detected.
+// Starts webserver
void startserver()
{
+ gettime();
gettemp();
- gettime();
- pc.printf("\n\n RTC time %s\r\n\n",timebuf);
+ getbattery();
pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
- strcpy(cmdbuff,"AT+RST\r\n");
- timeout=8000;
- getcount=1000;
+ strcpy(cmdbuff,"node.restart()\r\n");
+ SendCMD();
+ wait(2);
+ getreply();
+
+ pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> ");
+
+ // initial values
+ sprintf(cmdbuff, "count,time,analog1,analog2=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,Temp,Vcc);
+ SendCMD();
+ getreply();
+ wait(0.5);
+
+ //create server
+ sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout);
+ SendCMD();
+ getreply();
+ wait(0.5);
+ strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n");
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;
+ wait(0.3);
+ strcpy(cmdbuff,"conn:on(\"receive\",function(conn,payload) \r\n");
+ SendCMD();
+ getreply();
+ wait(0.3);
+
+ //print data to mbed
+ strcpy(cmdbuff,"print(payload)\r\n");
+ SendCMD();
+ getreply();
+ wait(0.2);
+
+ //web page data
+ strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><body><h1>ESP8266 Mbed IoT Web Controller</h1>')\r\n");
+ SendCMD();
+ getreply();
+ wait(0.4);
+ strcpy(cmdbuff,"conn:send('Hit count: '..count..'')\r\n");
SendCMD();
getreply();
- pc.printf(replybuff);
- sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
- timeout=500;
- getcount=20;
+ wait(0.2);
+ strcpy(cmdbuff,"conn:send('<br>Last hit (based on mbed RTC time): '..time..'<br><hr>')\r\n");
+ SendCMD();
+ getreply();
+ wait(0.4);
+ strcpy(cmdbuff,"conn:send('Analog 1: '..analog1..' V<br>Analog 2: '..analog2..' V<br><hr>')\r\n");
SendCMD();
getreply();
- pc.printf(replybuff);
- wait(1);
- sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout);
- timeout=500;
- getcount=50;
+ wait(0.3);
+ strcpy(cmdbuff,"conn:send('<form method=\"POST\"')\r\n");
+ SendCMD();
+ getreply();
+ wait(0.3);
+ strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led1v\"> flip LED1')\r\n");
+ SendCMD();
+ getreply();
+ wait(0.3);
+ strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led2v\"> flip LED2')\r\n");
+ SendCMD();
+ getreply();
+ wait(0.3);
+ strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led3v\"> flip LED3')\r\n");
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();
+ wait(0.3);
+ strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led4v\"> flip LED4')\r\n");
+ SendCMD();
+ getreply();
+ wait(0.3);
+ strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"send-refresh\"></form>')\r\n");
+ SendCMD();
+ getreply();
+ wait(0.3);
+ strcpy(cmdbuff, "conn:send('<p><h2>How to use:</h2><ul><li>Select a checkbox to flip on/off</li><li>Click Send-Refresh to send data and refresh values</li></ul></body></html>')\r\n");
+ SendCMD();
+ getreply();
+ wait(0.5);
+ // end web page data
+ strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection
+ SendCMD();
+ getreply();
+ wait(0.3);
+ strcpy(cmdbuff, "end)\r\n");
+ SendCMD();
+ getreply();
+ wait(0.2);
+ strcpy(cmdbuff, "end)\r\n");
+ SendCMD();
+ getreply();
+ wait(0.2);
+
+ strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n");
+ SendCMD();
+ getreply();
+ wait(0.2);
+ strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n");
+ SendCMD();
+ getreply();
+ wait(0.2);
+ strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n");
+ SendCMD();
+ getreply();
+ wait(0.2);
+ strcpy(cmdbuff, "else\r\n");
+ SendCMD();
+ getreply();
+ wait(0.2);
+ strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n");
+ SendCMD();
+ getreply();
+ wait(0.2);
+ strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n");
+ SendCMD();
+ getreply();
+ wait(0.2);
+ strcpy(cmdbuff,"tmr.stop(0)\r\n");
+ SendCMD();
+ getreply();
+ wait(0.2);
+ strcpy(cmdbuff,"end\r\n");
+ SendCMD();
+ getreply();
+ wait(0.2);
+ strcpy(cmdbuff,"end)\r\n");
+ SendCMD();
+ getreply();
+ wait(0.2);
+
+ pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
}
+
+
// ESP Command data send
void SendCMD()
{
- esp.printf("%s", cmdbuff);
+ int i;
+ char temp_char;
+ bool empty;
+ i = 0;
+// Start Critical Section - don't interrupt while changing global buffer variables
+ NVIC_DisableIRQ(UART1_IRQn);
+ empty = (tx_in == tx_out);
+ while ((i==0) || (cmdbuff[i-1] != '\n')) {
+// Wait if buffer full
+ if (((tx_in + 1) % buffer_size) == tx_out) {
+// End Critical Section - need to let interrupt routine empty buffer by sending
+ NVIC_EnableIRQ(UART1_IRQn);
+ while (((tx_in + 1) % buffer_size) == tx_out) {
+ }
+// Start Critical Section - don't interrupt while changing global buffer variables
+ NVIC_DisableIRQ(UART1_IRQn);
+ }
+ tx_buffer[tx_in] = cmdbuff[i];
+ i++;
+ tx_in = (tx_in + 1) % buffer_size;
+ }
+ if (esp.writeable() && (empty)) {
+ temp_char = tx_buffer[tx_out];
+ tx_out = (tx_out + 1) % buffer_size;
+// Send first character to start tx interrupts, if stopped
+ esp.putc(temp_char);
+ }
+// End Critical Section
+ NVIC_EnableIRQ(UART1_IRQn);
+ return;
}
-// Large WEB buffer data send
-void SendWEB()
-{
- int i=0;
- if(esp.writeable()) {
- while(webbuff[i]!='\0') {
- esp.putc(webbuff[i]);
- i++;
- }
- }
-}
+
// 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++;
+ read_line();
+ sscanf(rx_line,replybuff);
+}
+
+// Read a line from the large rx buffer from rx interrupt routine
+void read_line() {
+ int i;
+ i = 0;
+// Start Critical Section - don't interrupt while changing global buffer variables
+ NVIC_DisableIRQ(UART1_IRQn);
+// Loop reading rx buffer characters until end of line character
+ while ((i==0) || (rx_line[i-1] != '\r')) {
+// Wait if buffer empty
+ if (rx_in == rx_out) {
+// End Critical Section - need to allow rx interrupt to get new characters for buffer
+ NVIC_EnableIRQ(UART1_IRQn);
+ while (rx_in == rx_out) {
+ }
+// Start Critical Section - don't interrupt while changing global buffer variables
+ NVIC_DisableIRQ(UART1_IRQn);
}
+ rx_line[i] = rx_buffer[rx_out];
+ i++;
+ rx_out = (rx_out + 1) % buffer_size;
}
- t1.stop();
+// End Critical Section
+ NVIC_EnableIRQ(UART1_IRQn);
+ rx_line[i-1] = 0;
+ return;
+}
+
+
+// Interupt Routine to read in data from serial port
+void Rx_interrupt() {
+ DataRX=1;
+ //led3=1;
+// Loop just in case more than one character is in UART's receive FIFO buffer
+// Stop if buffer full
+ while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
+ rx_buffer[rx_in] = esp.getc();
+// Uncomment to Echo to USB serial to watch data flow
+ pc.putc(rx_buffer[rx_in]);
+ rx_in = (rx_in + 1) % buffer_size;
+ }
+ //led3=0;
+ return;
+}
+
+
+// Interupt Routine to write out data to serial port
+void Tx_interrupt() {
+ //led2=1;
+// Loop to fill more than one character in UART's transmit FIFO buffer
+// Stop if buffer empty
+ while ((esp.writeable()) && (tx_in != tx_out)) {
+ esp.putc(tx_buffer[tx_out]);
+ tx_out = (tx_out + 1) % buffer_size;
+ }
+ //led2=0;
+ return;
+}
+
+void gettime()
+{
+ time_t seconds = time(NULL);
+ strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
+}
+
+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
}
// Analog in example
void getbattery()
@@ -408,29 +396,4 @@
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
-}
+}
\ No newline at end of file
