Advertisement
AtomSoft

Untitled

Jan 10th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.26 KB | None | 0 0
  1. /*********************************************************************
  2. This is an example for our Monochrome OLEDs based on SSD1306 drivers
  3.  
  4.   Pick one up today in the adafruit shop!
  5.   ------> http://www.adafruit.com/category/63_98
  6.  
  7. This example is for a 128x64 size display using SPI to communicate
  8. 4 or 5 pins are required to interface
  9.  
  10. Adafruit invests time and resources providing this open source code,
  11. please support Adafruit and open-source hardware by purchasing
  12. products from Adafruit!
  13.  
  14. Written by Limor Fried/Ladyada  for Adafruit Industries.  
  15. BSD license, check license.txt for more information
  16. All text above, and the splash screen must be included in any redistribution
  17. *********************************************************************/
  18.  
  19. #include <SPI.h>
  20. #include <Wire.h>
  21. #include <Adafruit_GFX.h>
  22. #include <Adafruit_SSD1306.h>
  23.  
  24. //Adafruit Default  If using software SPI (the default case):
  25. //#define OLED_MOSI   9
  26. //#define OLED_CLK   10
  27. //#define OLED_DC    11
  28. //#define OLED_CS    12
  29. //#define OLED_RESET 13
  30. //Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
  31.  
  32. //My Arduino Nano Test Board
  33. //#define OLED_DC 4
  34. //#define OLED_CS 5
  35. //#define OLED_CLK 2
  36. //#define OLED_MOSI 3
  37. //#define OLED_RESET 6
  38. //Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
  39.  
  40.  
  41.  
  42. //* Uncomment this block to use hardware SPI
  43. #define OLED_DC     4
  44. #define OLED_CS   17  //NEW D17  
  45. #define OLED_RESET  12
  46. Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);
  47. //*/
  48.  
  49. #define NUMFLAKES 10
  50. #define XPOS 0
  51. #define YPOS 1
  52. #define DELTAY 2
  53.  
  54. #define LOGO16_GLCD_HEIGHT 16
  55. #define LOGO16_GLCD_WIDTH  16
  56.  
  57. #if (SSD1306_LCDHEIGHT != 64)
  58. #error("Height incorrect, please fix Adafruit_SSD1306.h!");
  59. #endif
  60.  
  61. const int up_btn = 6;
  62. const int lt_btn = 8;
  63. const int dn_btn = 9;
  64. const int rt_btn = 5;
  65. const int ok_btn = 3;
  66. const int back_btn = 2;
  67.  
  68.  
  69. String inputString = "";         // a string to hold incoming data
  70. boolean stringComplete = false;  // whether the string is complete
  71. String inputString2 = "";         // a string to hold incoming data
  72. boolean stringComplete2 = false;  // whether the string is complete
  73. int rxlen = 0;
  74.  
  75. boolean isDone = false;  
  76. char menuSelection = 0;
  77.  
  78. int lineCount = 0;
  79.  
  80. void setup()   {                
  81.   Serial.begin(9600);
  82.   Serial1.begin(9600);
  83.  
  84.   pinMode(up_btn, INPUT);
  85.   pinMode(dn_btn, INPUT);
  86.   pinMode(lt_btn, INPUT);
  87.   pinMode(rt_btn, INPUT);
  88.   pinMode(back_btn, INPUT);
  89.  
  90.   // reserve 200 bytes for the inputString:
  91.   inputString.reserve(400);
  92.   inputString2.reserve(200);
  93.   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  94.   display.begin(SSD1306_SWITCHCAPVCC);
  95.   // init done
  96.  
  97.   // Clear the buffer.
  98.   display.clearDisplay();
  99.  
  100.   display.setTextSize(1);
  101.   display.setTextColor(WHITE);
  102.   display.setCursor(0,0);
  103.   display.println("AtomSoftTech");
  104.   display.setCursor(0,20);
  105.   display.println("Mike Rankin");
  106.   display.setCursor(0,50);
  107.   display.println("EPS8266 Mini Terminal");
  108.   display.display();
  109.  
  110.   delay(3000);
  111.  
  112.   display.clearDisplay();
  113.   display.display();
  114.  
  115.   display.setCursor(0,0);
  116. }
  117.  
  118. void MainMenu()
  119. {
  120.   char menuItem = 0;
  121.   boolean isenter = false;
  122.   boolean isnew = false;
  123.    
  124.   while(isenter == false)
  125.   {
  126.     display.clearDisplay();
  127.     display.setCursor(0,0);
  128.     display.println("Select Function\n");
  129.     isnew = false;
  130.            
  131.     if(menuItem == 0)
  132.     {
  133.       display.println(">UART Terminal");
  134.       display.println(" ESP8266 Control");
  135.     }
  136.     else
  137.     {
  138.       display.println(" UART Terminal");
  139.       display.println(">ESP8266 Control");
  140.     }
  141.     display.display();
  142.     delay(200);
  143.    
  144.     while(isnew == false)
  145.     {
  146.       if(digitalRead(dn_btn) == HIGH)
  147.       {
  148.         if(menuItem<1)
  149.           menuItem++;
  150.          
  151.         isnew = true;
  152.       }
  153.       if(digitalRead(up_btn) == HIGH)
  154.       {
  155.         if(menuItem>0)
  156.           menuItem--;
  157.          
  158.         isnew = true;
  159.       }
  160.       if(digitalRead(ok_btn) == HIGH)
  161.       {
  162.         isnew = true;
  163.         isenter = true;
  164.         menuSelection = menuItem+1;
  165.         delay(500);
  166.       }
  167.     }
  168.   }
  169. }
  170.  
  171. void loop() {
  172.   MainMenu();
  173.  
  174.   switch(menuSelection)
  175.   {
  176.     case 1:
  177.       UartTerminal();
  178.       break;
  179.     case 2:
  180.       ESPTerminal();
  181.       break;
  182.     default:
  183.     break;
  184.   }
  185. }
  186. void ESPTerminal ()
  187. {
  188.   char menuItem = 0;
  189.   boolean isenter = false;
  190.   boolean isnew = false;
  191.   int x;
  192.   char inChar;
  193.   char rxc = 0;
  194.  
  195.   char myItems[4][25] = {
  196.   " Connect WiFi\0",
  197.   " Status \0",
  198.   " Function 3\0",
  199.   " Function 4\0"
  200.   };
  201.  
  202.   while(1)
  203.   {
  204.     display.clearDisplay();
  205.     display.setCursor(0,0);
  206.     display.println("ESP8266 Commands\n");
  207.     isnew = false;
  208.            
  209.     for(x=0;x<4;x++)
  210.     {
  211.       if(menuItem == x)
  212.         myItems[x][0] = '>';
  213.       else
  214.         myItems[x][0] = ' ';
  215.        
  216.       display.println(myItems[x]);
  217.     }
  218.    
  219.     display.display();
  220.     delay(200);
  221.     isnew = false;
  222.    
  223.     while(isnew == false)
  224.     {
  225.       if(digitalRead(dn_btn) == HIGH)
  226.       {
  227.         if(menuItem<3)
  228.           menuItem++;
  229.          
  230.         isnew = true;
  231.       }
  232.      
  233.       if(digitalRead(up_btn) == HIGH)
  234.       {
  235.         if(menuItem>0)
  236.           menuItem--;
  237.          
  238.         isnew = true;
  239.       }
  240.      
  241.       if(digitalRead(ok_btn) == HIGH)
  242.       {
  243.         isenter = true;
  244.  
  245.         switch(menuItem)
  246.         {
  247.          case 0:
  248.            connectWiFi();
  249.            break;
  250.          case 1:
  251.            Serial1.println("AT+CIPSTATUS");
  252.            break;
  253.          case 2:
  254.  
  255.            break;
  256.          case 3:
  257.  
  258.            break;
  259.          default:
  260.            break;
  261.         }
  262.         delay(300);
  263.    
  264.         display.clearDisplay();
  265.         display.setCursor(0,0);
  266.  
  267.         isDone = false;          
  268.         while(digitalRead(ok_btn) == LOW)
  269.         {
  270.           while(Serial1.available()==0)
  271.           {
  272.             if(digitalRead(ok_btn) == HIGH)
  273.             {
  274.               isDone = true;
  275.               break;
  276.             }
  277.           }
  278.          
  279.           rxlen = Serial1.available();
  280.          
  281.           if(isDone == false)
  282.           {
  283.             inputString = "";
  284.  
  285.             while(rxlen--)
  286.             {
  287.               inChar = (char)Serial1.read();
  288.               inputString+= inChar;
  289.  
  290.               rxc++;
  291.            
  292.               if ((inChar == '\n') || (rxc == 20)) {
  293.                 lineCount++;
  294.                 rxc = 0;
  295.                 if(lineCount ==9)
  296.                 {                
  297.                   lineCount = 0;
  298.                   display.setCursor(0,0);
  299.                   display.clearDisplay();
  300.                 }
  301.               }
  302.             }
  303.            
  304.             Serial.print(inputString);
  305.             display.print(inputString);
  306.             display.display();            
  307.           }
  308.         }
  309.  
  310.         isDone = false;        
  311.         isnew = true;
  312.       }//END of OK
  313.      
  314.       if(digitalRead(lt_btn) == HIGH)
  315.       {
  316.         menuSelection = 0;
  317.         return;
  318.       }
  319.     }
  320.   }
  321. }
  322.  
  323. void connectWiFi()
  324. {
  325.   int x;
  326.  
  327.   Serial1.println("AT+RST");  //Reset wifi
  328.  
  329.   display.clearDisplay();
  330.   display.setCursor(0,0);
  331.   display.display();    
  332.   delay(1500); //1 sec is good
  333.    
  334.   if(Serial1.find("eady"))
  335.   {
  336.    Serial.println("Module Found!");
  337.    display.println("Module Found!");
  338.    display.display();        
  339.  
  340.    Serial.println("Trying to Set up Wifi");
  341.    display.println("Trying to connect");
  342.    display.display();        
  343.  
  344.    Serial1.println("AT+CWMODE=3");  //mode 1 gives error
  345.  
  346.    delay(2000);
  347.    Serial.println("Trying to set Mode 3");
  348.    display.println("Trying to set Mode 3");
  349.    display.display();        
  350.  
  351.    char cmd[40] = "AT+CWJAP=\"AtomSoft\",\"PASS\"\r\n\0";
  352.    Serial.print(cmd);
  353.    Serial1.print(cmd);
  354.    
  355.    for(x=1;x<16;x++)
  356.    {
  357.      delay(1000);
  358.      if(Serial1.find("OK"))
  359.      {
  360.        Serial.println("Wifi Connected");
  361.        display.println("Wifi Connected");
  362.        x = 15;
  363.      }else
  364.      {
  365.        Serial.print("Time: ");
  366.        Serial.print(x);
  367.        Serial.println(" sec(s)");
  368.  
  369.        if((x==6)||(x==14))
  370.        {
  371.          display.clearDisplay();
  372.          display.setCursor(0,0);
  373.        }
  374.        
  375.        display.print("Time: ");
  376.        display.print(x);
  377.        display.println(" sec(s)");
  378.  
  379.        if(x == 15)
  380.        {
  381.          Serial.println("Connect Failed!");
  382.          display.println("Connect Failed!");
  383.        }
  384.          
  385.        display.display();
  386.      }
  387.    }
  388.    //   String cmd = "AT+CWJAP=\"MANUALSSID\",\"MANUALPASS\"";
  389.      
  390.   }
  391.   else
  392.   {
  393.    Serial.println("Module Not Found!");
  394.    display.println("Module Not Found!");
  395.    display.display();        
  396.   }
  397. }
  398. void UartTerminal()
  399. {
  400.   display.clearDisplay();
  401.   display.setCursor(0,0);
  402.   display.println("UART Terminal");
  403.   display.display();
  404.  
  405.   while(isDone == false)
  406.   {
  407.     if(digitalRead(lt_btn) == HIGH)
  408.     {
  409.       menuSelection = 0;
  410.       isDone = true;
  411.     }
  412.    
  413.     CheckSerial();
  414.    
  415.     if (stringComplete2) {
  416.         Serial1.println(inputString2);
  417.         inputString2 = "";
  418.         stringComplete2 = false;
  419.     }
  420.    
  421.     if (stringComplete) {
  422.       Serial.println(inputString);
  423.       display.println(inputString);
  424.       display.display();
  425.       // clear the string:
  426.       inputString = "";
  427.       stringComplete = false;
  428.     }
  429.   }
  430. }
  431. void CheckSerial() {
  432.   while (Serial1.available()) {
  433.     // get the new byte:
  434.     char inChar = (char)Serial1.read();
  435.     // add it to the inputString:
  436.     inputString += inChar;
  437.     // if the incoming character is a newline, set a flag
  438.     // so the main loop can do something about it:
  439.     if (inChar == '\n') {
  440.       stringComplete = true;
  441.       lineCount++;
  442.       if(lineCount ==9)
  443.       {
  444.         lineCount = 0;
  445.         display.setCursor(0,0);
  446.         display.clearDisplay();
  447.       }
  448.     }
  449.   }
  450.     while (Serial.available()) {
  451.     // get the new byte:
  452.     char inChar2 = (char)Serial.read();
  453.     // add it to the inputString:
  454.     inputString2 += inChar2;
  455.     // if the incoming character is a newline, set a flag
  456.     // so the main loop can do something about it:
  457.     if (inChar2 == '\n') {
  458.       stringComplete2 = true;
  459.     }
  460.   }
  461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement