Advertisement
mesmariusz

ESP_001

Jul 27th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #define DEBUG true //W tym stanie Arduino będzie wyświetlać informacje na konsoli
  3. SoftwareSerial esp8266(2, 3); // Pin 2 jest pinem Rx a pin 3 jest Tx
  4. void ESP8266_Init();
  5. void setup()
  6. {
  7. Serial.begin(9600);
  8. esp8266.begin(9600); // Ustawianie prędkosci uart. Wartość 9600 jest wartością optymalną
  9. ESP8266_Init();
  10. pinMode(13, OUTPUT);
  11. digitalWrite(13, HIGH);
  12.  
  13. }
  14. void loop()
  15. {
  16. if (esp8266.available()) //sprawdzanie czy ESP nadaje
  17. {
  18. if (esp8266.find("+IPD,")) //jeżeli ESP nadaje to czy wysyła pakiet
  19. {
  20. String html;
  21. delay(1000);
  22. int connectionId = esp8266.read() - 48; //Pierwszy znak po +IPD, jest nr połączenia w ASCII
  23. if (esp8266.find("update.html?LedState=")) //przeglądarka odeśle metodą GET stan diody LED
  24. {
  25. int State = esp8266.read() - 48; //Pierwszy znak po LedState= jest wartością dla diody LED
  26. if (!State)
  27. {
  28. digitalWrite(13, LOW); // Stan wysoki na pinie 13 spowoduje włączenie diody LED
  29. Serial.println("OFF");
  30. }
  31. else if (State)
  32. {
  33. Serial.println("ON");
  34. digitalWrite(13, HIGH); // Stan niski na pinie 13 spowoduje wyłączenie diody LED
  35. }
  36. html = "<html>"
  37. "<body>"
  38. "<h1> Data Update</h1>"
  39. "</body>"
  40. "</html>"; //dane które zostaną wysłane po udanej zmianie stanu diody
  41. }
  42. else
  43. {
  44. html = "<html>"
  45. "<body>"
  46. "<form action=\"update.html\" method=\"get\">"
  47. "<fieldset>"
  48. "<legend>LED State</legend>"
  49. "<input type=\"radio\" name=\"LedState\" value=\"1\"checked=\"checked\"> ON"
  50. "<input type=\"radio\" name=\"LedState\" value=\"0\"> OFF<br>"
  51. "</fieldset>"
  52. "<input type=\"submit\" value=\"Submit\">"
  53. "</form>"
  54. "</body>"
  55. "</html>"; //pakiet opisujący wygląd strony głównej + nagłówki HTTP
  56. }
  57. //przygotowywanie komendy wysyłającej pakiet TCP
  58. String SendCommand = "AT+CIPSEND=";
  59. SendCommand += connectionId;
  60. SendCommand += ",";
  61. SendCommand += html.length();
  62. SendCommand += "\r\n";
  63. sendData(SendCommand, 1000, DEBUG); //wysłanie komendy do ESP
  64. sendData(html, 1000, DEBUG); //wysłanie danych pakietu TCP
  65. delay(1000);
  66. String closeCommand = "AT+CIPCLOSE=";
  67. closeCommand += connectionId; //należy dodać ID połączenia do polecenia zamknięcia
  68. closeCommand += "\r\n";
  69. sendData(closeCommand, 3000, DEBUG); //zamknięcie połączenia TCP
  70. }
  71. }
  72. }
  73. void ESP8266_Init()
  74. {
  75. sendData("AT+RST\r\n", 1000, DEBUG); //Reset modułu ESP12
  76. delay(1000);
  77. sendData("AT+CWMODE_CUR=3\r\n", 1000, DEBUG); //Ustawienie modułu w tryb AP/CLIENT
  78. delay(1000);
  79. sendData("AT+CWJAP_CUR=\"SSID\",\"PASSWORD\"\r\n", 1000, DEBUG); // Podłączenie do sieci WiFi
  80. while (1)
  81. {
  82. if (esp8266.available() && esp8266.find("OK"))
  83. {
  84. Serial.print("CONNECTED\n\r");
  85. break;
  86. }
  87. }
  88. sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); //Pozwolenie na ustawianie wielu połączeń, wymagane dla
  89. serwera
  90. sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); //Start serwera na porcie 80
  91. delay(1000);
  92. sendData("AT+CIFSR\r\n", 1000, DEBUG); //Pobranie adresów MAC oraz IP }
  93. String sendData(String command, const int waitForResponse, boolean debug)
  94. {
  95. String response = "";
  96. esp8266.print(command); //Wysłanie komendy do ESP
  97. long int time = millis();
  98. while ( (time + waitForResponse) > millis())
  99. {
  100. while (esp8266.available())
  101. {
  102. char znak = esp8266.read();
  103. response += znak;
  104. }
  105. }
  106. if (debug)
  107. {
  108. Serial.print(response); //Wyświetlenie odpowiedzi na konsoli
  109. }
  110. return response;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement