Advertisement
mesmariusz

esp_001_poprawny

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