Full layout 'HUB' with functioning navigation between all screens and active temp/state display, now with outbound signals to leds and buzzer. Final Project.

Dependencies:   ID12RFID SPI_TFT_ILI9341 TFT_fonts mbed

Fork of Welcome_Screen_TFT_2_point_oh by 'SmOuse'

Files at this revision

API Documentation at this revision

Comitter:
bendavis96
Date:
Fri Jun 17 12:38:37 2016 +0000
Parent:
8:95cd847648cd
Commit message:
wifi inbound signals and outbound signals to led etc working, all working!;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 95cd847648cd -r 0adb037a25a4 main.cpp
--- a/main.cpp	Thu Jun 16 20:56:50 2016 +0000
+++ b/main.cpp	Fri Jun 17 12:38:37 2016 +0000
@@ -18,9 +18,11 @@
 
 Serial pc (USBTX,USBRX);
 ID12RFID rfid(PTE23);
+Serial dev(PTE0,PTE1);  // for KL25Z... asuming one can't use the PTA1 version which is the stdio
 DigitalOut rled(LED1);
 DigitalOut gled(LED2);
 DigitalOut bled(LED3);
+DigitalOut rst(PTD7); // single digital pin to drive the esp8266 reset line
 
 int touching = 0;
 
@@ -38,6 +40,39 @@
 //   SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1, PTA16, PTA17, PTC16,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z
 
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+//%%%%%%%%%%%%%%%%%%%%%% WIFI %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+// subroutine to run anytime a serial interrupt arrives from the device
+// this basically passes everything thatthe device produces on to the pc terminal screen
+
+void dev_recv()
+{
+    while(dev.readable()) {
+        pc.putc(dev.getc());
+        wait_us(1);
+    }
+}
+// subroutine to service the serial interrupt on the pc connection
+// this is a bit more complex - it takes what the use sends on the pc and copies it on to the device
+// the esp should echo these straight back to the the pc if all is well
+// this also detects the end of command character which is ascii 13 (0x0d) adn adds a linefeed after it =asscii 10 (0x0a)
+void pc_recv()
+{
+    char c;
+    while(pc.readable()) {
+        c = pc.getc();
+        dev.putc(c);
+        //  pc.putc(c); // echo back
+        if(c==13) {
+            dev.putc(10); // send the linefeed to complement the carriage return generated by return key on the pc
+            pc.putc(10);
+        }
+    }
+}
+
+/*void client() {
+    
+     i = dev.getc(); */
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 //%%%%%%%%%%%  Subroutine to read the x location of the touch point  %%%%%%%%%%
 
 
@@ -262,6 +297,78 @@
     TFT.printf("X");
 }
 
+//%%%%%%%%% OUTPUTS %%%%%%%%%%%%%%%
+
+void alarm_off()
+{
+    dev.printf("AT+CIPSEND=0,1\r\n");
+    wait(0.1);
+    dev.printf("x");
+    wait(0.1);
+}
+
+void control_start(){
+    dev.printf("AT+CIPSEND=1,1\r\n");
+    wait(0.1);
+    dev.printf("G");
+    wait(0.1);
+}
+
+void lights_off()
+{
+    dev.printf("AT+CIPSEND=1,1\r\n");
+    wait(0.1);
+    dev.printf("Z");
+    wait(0.1);
+}
+
+void lights_1()
+{
+    dev.printf("AT+CIPSEND=1,1\r\n");
+    wait(0.1);
+    dev.printf("X");
+    wait(0.1);
+}
+
+void lights_2()
+{
+    dev.printf("AT+CIPSEND=1,1\r\n");
+    wait(0.1);
+    dev.printf("Y");
+    wait(0.1);
+}
+
+void lights_3()
+{
+    dev.printf("AT+CIPSEND=1,1\r\n");
+    wait(0.1);
+    dev.printf("W");
+    wait(0.1);
+}
+
+void fan_off()
+{
+    dev.printf("AT+CIPSEND=1,1\r\n");
+    wait(0.1);
+    dev.printf("L");
+    wait(0.1);
+}
+
+void fan_on()
+{
+    dev.printf("AT+CIPSEND=1,1\r\n");
+    wait(0.1);
+    dev.printf("K");
+    wait(0.1);
+}
+
+void autom_togg()
+{
+    dev.printf("AT+CIPSEND=1,1\r\n");
+    wait(0.1);
+    dev.printf("Q");
+    wait(0.1);
+}
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 //%%%%%  IDs  %%%%%%
 
@@ -272,11 +379,14 @@
     bled = 1;
     gled = 1;
 
+    alarm_off();
+    control_start();
     TFT.printf("B");
     wait(0.05);
     TFT.printf("e");
     wait(0.05);
     TFT.printf("n");
+
 }
 
 void joe()
@@ -285,6 +395,8 @@
     bled = 0;
     gled = 1;
 
+    alarm_off();
+    control_start();
     TFT.printf("J");
     wait(0.05);
     TFT.printf("o");
@@ -299,6 +411,8 @@
     bled = 0;
     gled = 1;
 
+    alarm_off();
+    control_start();
     TFT.printf("A");
     wait(0.05);
     TFT.printf("r");
@@ -356,6 +470,33 @@
 
 int main()
 {
+//%%%%%%%%%%%%%%%% WIFI %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+    pc.printf("start");
+    pc.baud(115200);
+    dev.baud(115200);
+    rst = 0;
+    wait(1);
+    rst = 1; // send the esp8266 reset
+    wait(1);
+
+    rled = 0, gled = 1;         //Red LED indicates startup in progress
+
+    pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services
+    dev.attach(&dev_recv, Serial::RxIrq);
+    dev.printf("AT+RST\r\n");
+    wait(10);
+    dev.printf("AT+CWJAP=\"BTHub3-WXWX\",\"fdd6f7c682\"\r\n");
+    wait(10);
+    dev.printf("AT+CIPMUX=1\r\n");
+    wait(2);
+    dev.printf("AT+CIPSERVER=1,333\r\n");
+    wait(2);
+    dev.printf("AT+CIPSTO=3000\r\n");
+
+    rled = 1, gled = 0;     //Green LED indicates startup complete
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
     int display = 0;
     int reading = 0;
     int id = 0;
@@ -416,8 +557,15 @@
                     break;
                 }
                 while (display == 1) {
+                    //%%%%%% RFID Override %%%%%%%%
+                    reading = rfid.readable();
+                    if (reading) {
+                        id = rfid.read();
+                        TFT.set_orientation(1);
+                        break;
+                    }
                     //%%%%% INCOMING DATA %%%%%%
-                    TFT.set_orientation(1);
+                    /*TFT.set_orientation(1);
                     TFT.foreground(White);
                     TFT.locate(145,0);
                     TFT.set_font((unsigned char*) Arial12x12);
@@ -426,7 +574,7 @@
                     TFT.printf("Fan: On");
                     TFT.locate(145, 30);
                     TFT.printf("Lights: Low");
-                    TFT.foreground(Cyan);
+                    TFT.foreground(Cyan); */
 
                     //%%%%% TOUCH %%%%%%
                     TFT.set_orientation(0);
@@ -453,14 +601,6 @@
                         display = 4;            //jump to automatic control
                         TFT.set_orientation(1);
                     }
-
-                    //%%%%%% RFID Override %%%%%%%%
-                    reading = rfid.readable();
-                    if (reading) {
-                        id = rfid.read();
-                        TFT.set_orientation(1);
-                        break;
-                    }
                 }
                 break;
 
@@ -481,22 +621,22 @@
 
                     if (yp<120 && xp<120 && touching==1) { //button for LIGHTS OFF
 
-                        //LIGHTS OFF ACTION
+                        lights_off();
                     }
 
-                    if (yp>230 && xp<60 && touching==1) { //button for LIGHTS 1
+                    if (yp>250 && xp<60 && touching==1) { //button for LIGHTS 1
 
-                        //LIGHTS 1 ACTION
+                        lights_1();
                     }
 
-                    if (yp>230 && 70<xp<110 && touching==1) { //button for LIGHTS 2
+                    if (yp>250 && 70<xp<110 && touching==1) { //button for LIGHTS 2
 
-                        //LIGHTS 2 ACTION
+                        lights_2();
                     }
 
-                    if (yp>230 && xp>115 && touching==1) { //button for LIGHTS 3
+                    if (yp>250 && xp>115 && touching==1) { //button for LIGHTS 3
 
-                        //LIGHTS 3 ACTION
+                        lights_3();
                     }
 
                     if (yp<30 && xp>210 && touching==1) { //RETURN TO MAIN SCREEN
@@ -533,12 +673,12 @@
 
                     if (yp<120 && xp<120 && touching==1) { //button for FAN OFF
 
-                        //FAN OFF ACTION
+                        fan_off();
                     }
 
                     if (yp>230 && xp<120 && touching==1) { //button for FAN ON
 
-                        //FAN ON ACTION
+                        fan_on();
                     }
 
                     if (yp<30 && xp>210 && touching==1) { //RETURN TO MAIN SCREEN
@@ -575,12 +715,12 @@
 
                     if (yp<120 && xp<120 && touching==1) { //button for AUTO OFF
 
-                        //AUTO OFF ACTION
+                        autom_togg();
                     }
 
                     if (yp>230 && xp<120 && touching==1) { //button for AUTO ON
 
-                        //AUTO ON ACTION
+                        autom_togg();
                     }
 
                     if (yp<30 && xp>210 && touching==1) { //RETURN TO MAIN SCREEN