Wifi IoT Project

Dependencies:   4DGL-uLCD-SE mbed

Files at this revision

API Documentation at this revision

Comitter:
yscho529
Date:
Tue Nov 01 19:51:07 2016 +0000
Commit message:
Wifi IoT

Changed in this revision

4DGL-uLCD-SE.lib Show annotated file Show diff for this revision Revisions of this file
Speaker.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
song.cpp Show annotated file Show diff for this revision Revisions of this file
song.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 6e8326358c6c 4DGL-uLCD-SE.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/4DGL-uLCD-SE.lib	Tue Nov 01 19:51:07 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/4180_1/code/4DGL-uLCD-SE/#2cb1845d7681
diff -r 000000000000 -r 6e8326358c6c Speaker.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Speaker.h	Tue Nov 01 19:51:07 2016 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+// new class to play a note on Speaker based on PwmOut class
+class Speaker
+{
+public:
+    Speaker(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the Speaker Constructor
+    }
+// class method to play a note based on PwmOut class
+    void PlayNote(float frequency, float duration, float volume) {
+        _pin.period(1.0/frequency);
+        _pin = volume/2.0;
+        wait(duration);
+        _pin = 0.0;
+    }
+
+private:
+    PwmOut _pin;
+};
\ No newline at end of file
diff -r 000000000000 -r 6e8326358c6c main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Nov 01 19:51:07 2016 +0000
@@ -0,0 +1,333 @@
+// ESP8266 Static page WEB server to control Mbed
+
+#include "mbed.h"
+#include "song.h"
+
+Serial pc(USBTX, USBRX);
+Serial esp(p28, p27); // tx, rx
+
+
+// Standard Mbed LED definitions
+DigitalOut  led1(LED1);
+DigitalOut  led2(LED2);
+DigitalOut  led3(LED3);
+DigitalOut  led4(LED4);
+
+char ssid[32] = "Mia";     // enter WiFi router ssid inside the quotes
+char pwd [32] = "myhotspot"; // 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];
+
+// 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];
+void SendCMD(),getreply(),ReadWebData(),startserver();
+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
+int dayofmonth  =26;    // 1-31
+int month       =8;     // 1-12
+int year        =15;    // last 2 digits
+
+int main()
+{
+    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);
+    startserver();
+    DataRX=0;
+    count=0;
+    while(1) {
+        if(DataRX==1) {
+            ReadWebData();
+            esp.attach(&Rx_interrupt, Serial::RxIrq);
+        }
+    }
+}
+
+// Reads and processes GET and POST web data
+void ReadWebData()
+{
+    wait_ms(200);
+    esp.attach(NULL,Serial::RxIrq);
+    DataRX=0;
+    memset(webdata, '\0', sizeof(webdata));
+    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;
+        playsong1();
+    }
+    else if( strstr(webdata, "check=led2v") != NULL ) {
+        led2=!led2;
+        playsong2();
+    }
+    else if( strstr(webdata, "check=led3v") != NULL ) {
+        led3=!led3;
+        playsong3();
+    }
+    else if( strstr(webdata, "check=led4v") != NULL ) {
+        led4=!led4;
+        playsong4();
+    }
+    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 webserver
+void startserver()
+{
+    pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
+    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();
+    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>ECE 4180 Lab 4</h1>')\r\n");
+        SendCMD();
+        getreply();
+        wait(0.4);
+        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\"> Play Rambling Wreck')\r\n");
+        SendCMD();
+        getreply();
+        wait(0.3);
+        strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led2v\"> Play Mary had a little lamb')\r\n");
+        SendCMD();
+        getreply();
+        wait(0.3);
+        strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led3v\"> Play Bingo')\r\n");
+        SendCMD();
+        getreply();
+        wait(0.3);
+        strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led4v\"> Play Are you sleeping?')\r\n");
+        SendCMD();
+        getreply();
+        wait(0.3);
+        strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"Play\"></form>')\r\n");
+        SendCMD();
+        getreply();
+        wait(0.3);
+        strcpy(cmdbuff, "conn:send('<p><h2>How to use:</h2><ul><li>Select a checkbox to choose song</li><li>Click Play to play songs</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()
+{
+    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;
+}
+
+// Get Command and ESP status replies
+void getreply()
+{
+    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;
+    }
+// 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;
+}
\ No newline at end of file
diff -r 000000000000 -r 6e8326358c6c mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Nov 01 19:51:07 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9bcdf88f62b0
\ No newline at end of file
diff -r 000000000000 -r 6e8326358c6c song.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/song.cpp	Tue Nov 01 19:51:07 2016 +0000
@@ -0,0 +1,83 @@
+#include "mbed.h"
+#include "uLCD_4DGL.h"
+#include "Speaker.h"
+#include "song.h"
+
+uLCD_4DGL uLCD(p9,p10,p11);
+Speaker mySpeaker(p26);
+
+void playsong1(void) {
+    uLCD.cls();
+    uLCD.printf("Oh! If I had a daughter, sir, I'd dress her in White and Gold!");
+    mySpeaker.PlayNote(392, 0.5, 0.3); //1 G quarter
+    mySpeaker.PlayNote(349.23, 0.25, 0.3); //2 F eighth 
+    mySpeaker.PlayNote(311.3, 0.5, 0.3); //3 E flat quarter
+    mySpeaker.PlayNote(311.3, 0.25, 0.3); //4 E flat eighth
+    mySpeaker.PlayNote(311.3, 0.5, 0.3); //5 E flat quarter
+    mySpeaker.PlayNote(349.23, 0.25, 0.3); //6 F eighth
+    mySpeaker.PlayNote(392, 0.5, 0.3); //7 G quarter
+    mySpeaker.PlayNote(392, 0.25, 0.3); //8 G eighth
+    mySpeaker.PlayNote(392, 0.25, 0.3); //9 G eighth
+    mySpeaker.PlayNote(349.23, 0.25, 0.3); //10 F eighth
+    mySpeaker.PlayNote(311.3, 0.25, 0.3); //11 E flat eighth
+    mySpeaker.PlayNote(349.23, 0.25, 0.3); //12 F eighth
+    mySpeaker.PlayNote(392, 0.25, 0.3); //13 G eighth
+    mySpeaker.PlayNote(349.23, 0.25, 0.3); //14 F eighth
+    mySpeaker.PlayNote(311.3, 0.5, 0.3); //15 E flat quarter
+    mySpeaker.PlayNote(293.66, 0.25, 0.3); //16 D eighth
+    mySpeaker.PlayNote(311.3, 0.6, 0.3); //17 E flat extended quarter
+}
+
+void playsong2(void) {
+    uLCD.cls();
+    uLCD.printf("Mary had a little lamb. Its fleece was white as snow!");
+    mySpeaker.PlayNote(493.88, 0.5, 0.3); //1 B quarter
+    mySpeaker.PlayNote(440, 0.5, 0.3); //2 A quarter
+    mySpeaker.PlayNote(392, 0.5, 0.3); //3 G quarter
+    mySpeaker.PlayNote(440, 0.5, 0.3); //4 A quarter
+    mySpeaker.PlayNote(493.88, 0.5, 0.3); //5 B quarter
+    mySpeaker.PlayNote(493.88, 0.5, 0.3); //6 B quarter
+    mySpeaker.PlayNote(493.88, 0.5, 0.3); //7 B quarter
+    mySpeaker.PlayNote(493.88, 0.5, 0.3); //8 B quarter
+    mySpeaker.PlayNote(440, 0.5, 0.3); //9 A quarter
+    mySpeaker.PlayNote(440, 0.5, 0.3); //10 A quarter
+    mySpeaker.PlayNote(493.88, 0.5, 0.3); //11 B quarter
+    mySpeaker.PlayNote(440, 0.5, 0.3); //12 A quarter
+    mySpeaker.PlayNote(392, 2, 0.3); //13 G whole
+}
+    
+void playsong3(void) {
+    uLCD.cls();
+    uLCD.printf("B-I-N-G-O! And Bingo was his name-o!");
+    mySpeaker.PlayNote(261.63, 2, 0.3); //1 C whole
+    mySpeaker.PlayNote(261.63, 2, 0.3); //2 C whole
+    mySpeaker.PlayNote(293.66, 0.5, 0.3); //3 D quarter
+    mySpeaker.PlayNote(293.66, 0.5, 0.3); //4 D quarter
+    mySpeaker.PlayNote(293.66, 0.5, 0.3); //5 D quarter
+    mySpeaker.PlayNote(261.63, 0.5, 0.3); //6 C quarter
+    mySpeaker.PlayNote(264.94, 0.5, 0.3); //7 B quarter
+    mySpeaker.PlayNote(196, 0.5, 0.3); //8 G quarter
+    mySpeaker.PlayNote(220, 0.5, 0.3); //9 A quarter
+    mySpeaker.PlayNote(249.94, 0.5, 0.3); //10 B quarter
+    mySpeaker.PlayNote(261.03, 0.5, 0.3); //11 C whole
+    mySpeaker.PlayNote(261.03, 0.5, 0.3); //12 C whole
+}
+    
+void playsong4(void) {
+    uLCD.cls();
+    uLCD.printf("Are you sleeping? Are you sleeping? Brother John? Brother John?");
+    mySpeaker.PlayNote(261.63, 0.5, 0.3); //1 C quarter
+    mySpeaker.PlayNote(293.66, 0.5, 0.3); //2 D quarter
+    mySpeaker.PlayNote(329.63, 0.5, 0.3); //3 E quarter
+    mySpeaker.PlayNote(261.63, 0.5, 0.3); //4 C quarter
+    mySpeaker.PlayNote(261.63, 0.5, 0.3); //5 C quarter
+    mySpeaker.PlayNote(293.66, 0.5, 0.3); //6 D quarter
+    mySpeaker.PlayNote(329.63, 0.5, 0.3); //7 E quarter
+    mySpeaker.PlayNote(261.63, 0.5, 0.3); //8 C quarter
+    mySpeaker.PlayNote(329.63, 0.5, 0.3); //9 E quarter
+    mySpeaker.PlayNote(349.23, 0.5, 0.3); //10 F quarter
+    mySpeaker.PlayNote(392, 1, 0.3); //11 G half
+    mySpeaker.PlayNote(329.63, 0.5, 0.3); //12 E quarter
+    mySpeaker.PlayNote(349.23, 0.5, 0.3); //13 F quarter
+    mySpeaker.PlayNote(392, 1, 0.3); //14 G half
+}
\ No newline at end of file
diff -r 000000000000 -r 6e8326358c6c song.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/song.h	Tue Nov 01 19:51:07 2016 +0000
@@ -0,0 +1,12 @@
+#ifndef SONG_H
+#define SONG_H
+
+void playsong1(void);
+
+void playsong2(void);
+
+void playsong3(void);
+
+void playsong4(void);
+
+#endif //PLAYER_H
\ No newline at end of file