Advertisement
0crash0

esp8266_test

Mar 21st, 2015
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.43 KB | None | 0 0
  1. #include "ESP8266.h"
  2. #include "Arduino.h"
  3.  
  4. #define SSID        "ESP"
  5. #define PASSWORD    "12345678"
  6. #define CHANNEL     7
  7. #define ENCRYPTION  0 //0 - OPEN, 1 - WEP, 2 - WPA_PSK, 3 - WPA2_PSK, 4 - WPA_WPA2_PSK
  8. #define IPap        "192.168.1.1"
  9. ESP8266 wifi(Serial1, 115200);
  10.  
  11. char *hello_msg = "Hello, this is server!";
  12.  
  13. char *cm1 = "1tst";
  14. char *cm2 = "2tst";
  15. char *cm3 = "3tst";
  16. char *cm4 = "4tst";
  17. char *cm5 = "5tst";
  18.  
  19. String cmd1(cm1);
  20. String cmd2(cm2);
  21. String cmd3(cm3);
  22. String cmd4(cm4);
  23. String cmd5(cm5);
  24.  
  25. bool cm1st = false;
  26. bool cm2st = false;
  27. bool cm3st = false;
  28. bool cm4st = false;
  29. bool cm5st = false;
  30.  
  31. bool btn1 = false;
  32. bool btn2 = false;
  33. bool btn3 = false;
  34.  
  35. void setup(void)
  36. {
  37.     pinMode(4, OUTPUT);
  38.     pinMode(5, OUTPUT);
  39.     pinMode(6, OUTPUT);
  40.     pinMode(7, OUTPUT);
  41.     pinMode(8, OUTPUT);
  42.     pinMode(9, OUTPUT);
  43.  
  44.     pinMode(10, INPUT);
  45.     pinMode(11, INPUT);
  46.     pinMode(12, INPUT);
  47.  
  48.     for (int i = 4; i <= 9; i++){
  49.         delay(100);
  50.         digitalWrite(i, HIGH);
  51.         delay(900);
  52.         digitalWrite(i, LOW);
  53.     }
  54.     delay(5000);
  55.     Serial.begin(115200);
  56.     Serial.print("setup begin\r\n");
  57.  
  58.     Serial.print("FW Version:");
  59.     Serial.println(wifi.getVersion().c_str());
  60.  
  61.     if (wifi.setOprToSoftAP()) {
  62.         Serial.print("to station + softap ok\r\n");
  63.     }
  64.     else {
  65.         Serial.print("to station + softap err\r\n");
  66.     }
  67.  
  68.     if (wifi.setSoftAPParam(SSID, PASSWORD, CHANNEL, ENCRYPTION)) {
  69.         Serial.print("Start AP success\r\n");
  70.         Serial.print("IP: ");
  71.         Serial.println(wifi.getLocalIP().c_str());
  72.     }
  73.     else {
  74.         Serial.print("Start AP failure\r\n");
  75.     }
  76.  
  77.     if (wifi.setAPip(IPap)) {
  78.         Serial.print("set IP ok\r\n");
  79.     }
  80.     else {
  81.         Serial.print("set IP err\r\n");
  82.     }
  83.  
  84.     if (wifi.enableMUX()) {
  85.         Serial.print("multiple ok\r\n");
  86.     }
  87.     else {
  88.         Serial.print("multiple err\r\n");
  89.     }
  90.  
  91.     if (wifi.setTCPIPmode(0)) {
  92.         Serial.print("set tcp/ip mode ok\r\n");
  93.     }
  94.     else {
  95.         Serial.print("set tcp/ip mode err\r\n");
  96.     }
  97.  
  98.     if (wifi.startTCPServer(3000)) {
  99.         Serial.print("start tcp server ok\r\n");
  100.     }
  101.     else {
  102.         Serial.print("start tcp server err\r\n");
  103.     }
  104.  
  105.     if (wifi.setTCPServerTimeout(100)) {
  106.         Serial.print("set tcp server timout 10 seconds\r\n");
  107.     }
  108.     else {
  109.         Serial.print("set tcp server timout err\r\n");
  110.     }
  111.  
  112.     Serial.print("setup end\r\n");
  113. }
  114.  
  115. void loop(void)
  116. {
  117.     readBtns();
  118.     Btnlight();
  119.     digitalWrite(9, HIGH);
  120.  
  121.     uint8_t buffer[128] = { 0 };
  122.     uint8_t mux_id;
  123.     uint32_t len = wifi.recv(&mux_id, buffer, sizeof(buffer), 100);
  124.     if (len > 0) {
  125.         Serial.print("Status:[");
  126.         Serial.print(wifi.getIPStatus().c_str());
  127.         Serial.println("]");
  128.  
  129.         Serial.print("Received: ");
  130.  
  131.         for (uint32_t i = 0; i < len; i++){
  132.             Serial.print((char)buffer[i]);
  133.         }
  134.         String bff(reinterpret_cast<char*>(buffer));
  135.  
  136.         if (bff == cmd1){
  137.             digitalWrite(4, HIGH);
  138.         }
  139.  
  140.         if (bff == cmd1){
  141.             if (cm1st == false){
  142.                 digitalWrite(4, HIGH);
  143.                 cm1st = true;
  144.             }
  145.             else{
  146.                 digitalWrite(4, LOW);
  147.                 cm1st = false;
  148.             }
  149.         }
  150.         else if (bff == cmd2){
  151.             if (cm2st == false){
  152.                 digitalWrite(5, HIGH);
  153.                 cm2st = true;
  154.             }
  155.             else{
  156.                 digitalWrite(5, LOW);
  157.                 cm2st = false;
  158.             }
  159.         }
  160.         else if (bff == cmd3){
  161.             if (cm3st == false){
  162.                 digitalWrite(6, HIGH);
  163.                 cm3st = true;
  164.             }
  165.             else{
  166.                 digitalWrite(6, LOW);
  167.                 cm3st = false;
  168.             }
  169.         }
  170.         else if (bff == cmd4){
  171.             if (cm4st == false){
  172.                 digitalWrite(7, HIGH);
  173.                 cm4st = true;
  174.             }
  175.             else{
  176.                 digitalWrite(7, LOW);
  177.                 cm4st = false;
  178.             }
  179.         }
  180.         else if (bff == cmd5){
  181.             if (cm5st == false){
  182.                 for (int i = 4; i < 8; i++){
  183.                     digitalWrite(i, LOW);
  184.                 }
  185.                 cm5st = true;
  186.             }
  187.             else{
  188.                 for (int i = 4; i < 8; i++){
  189.                     digitalWrite(i, HIGH);
  190.                 }
  191.                 cm5st = false;
  192.             }
  193.         }
  194.  
  195.  
  196.         if (wifi.send(mux_id, (const uint8_t*)hello_msg, strlen(hello_msg))) {
  197.             Serial.print("send back ok\r\n");
  198.         }
  199.         else {
  200.             Serial.print("send back err\r\n");
  201.         }
  202.  
  203.         /*       if (wifi.releaseTCP(mux_id)) {
  204.         Serial.print("release tcp ");
  205.         Serial.print(mux_id);
  206.         Serial.println(" ok");
  207.         } else {
  208.         Serial.print("release tcp");
  209.         Serial.print(mux_id);
  210.         Serial.println(" err");
  211.         }
  212.         */
  213.         Serial.print("Status:[");
  214.         Serial.print(wifi.getIPStatus().c_str());
  215.         Serial.println("]");
  216.     }
  217. }
  218.  
  219. void readBtns(){
  220.     btn1 = digitalRead(10);
  221.     btn2 = digitalRead(11);
  222.     btn1 = digitalRead(12);
  223. }
  224.  
  225. void Btnlight(){
  226.     if (btn1){
  227.         digitalWrite(4, HIGH);
  228.     }
  229.     if (btn2){
  230.         digitalWrite(5, HIGH);
  231.     }
  232.     if (btn3){
  233.         digitalWrite(6, HIGH);
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement