Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #define DEBUG true //W tym stanie Arduino będzie wyświetlać informacje na konsoli
- SoftwareSerial esp8266(2, 3); // Pin 2 jest pinem Rx a pin 3 jest Tx
- void ESP8266_Init();
- void setup()
- {
- Serial.begin(9600);
- esp8266.begin(9600); // Ustawianie prędkosci uart. Wartość 9600 jest wartością optymalną
- ESP8266_Init();
- pinMode(13, OUTPUT);
- digitalWrite(13, HIGH);
- }
- void loop()
- {
- if (esp8266.available()) //sprawdzanie czy ESP nadaje
- {
- if (esp8266.find("+IPD,")) //jeżeli ESP nadaje to czy wysyła pakiet
- {
- String html;
- delay(1000);
- int connectionId = esp8266.read() - 48; //Pierwszy znak po +IPD, jest nr połączenia w ASCII
- if (esp8266.find("update.html?LedState=")) //przeglądarka odeśle metodą GET stan diody LED
- {
- int State = esp8266.read() - 48; //Pierwszy znak po LedState= jest wartością dla diody LED
- if (!State)
- {
- digitalWrite(13, LOW); // Stan wysoki na pinie 13 spowoduje włączenie diody LED
- Serial.println("OFF");
- }
- else if (State)
- {
- Serial.println("ON");
- digitalWrite(13, HIGH); // Stan niski na pinie 13 spowoduje wyłączenie diody LED
- }
- html = "<html>"
- "<body>"
- "<h1> Data Update</h1>"
- "</body>"
- "</html>"; //dane które zostaną wysłane po udanej zmianie stanu diody
- }
- else
- {
- html = "<html>"
- "<body>"
- "<form action=\"update.html\" method=\"get\">"
- "<fieldset>"
- "<legend>LED State</legend>"
- "<input type=\"radio\" name=\"LedState\" value=\"1\"checked=\"checked\"> ON"
- "<input type=\"radio\" name=\"LedState\" value=\"0\"> OFF<br>"
- "</fieldset>"
- "<input type=\"submit\" value=\"Submit\">"
- "</form>"
- "</body>"
- "</html>"; //pakiet opisujący wygląd strony głównej + nagłówki HTTP
- }
- //przygotowywanie komendy wysyłającej pakiet TCP
- String SendCommand = "AT+CIPSEND=";
- SendCommand += connectionId;
- SendCommand += ",";
- SendCommand += html.length();
- SendCommand += "\r\n";
- sendData(SendCommand, 1000, DEBUG); //wysłanie komendy do ESP
- sendData(html, 1000, DEBUG); //wysłanie danych pakietu TCP
- delay(1000);
- String closeCommand = "AT+CIPCLOSE=";
- closeCommand += connectionId; //należy dodać ID połączenia do polecenia zamknięcia
- closeCommand += "\r\n";
- sendData(closeCommand, 3000, DEBUG); //zamknięcie połączenia TCP
- }
- }
- }
- void ESP8266_Init()
- {
- sendData("AT+RST\r\n", 1000, DEBUG); //Reset modułu ESP12
- delay(1000);
- sendData("AT+CWMODE_CUR=3\r\n", 1000, DEBUG); //Ustawienie modułu w tryb AP/CLIENT
- delay(1000);
- sendData("AT+CWJAP_CUR=\"SSID\",\"PASSWORD\"\r\n", 1000, DEBUG); // Podłączenie do sieci WiFi
- while (1)
- {
- if (esp8266.available() && esp8266.find("OK"))
- {
- Serial.print("CONNECTED\n\r");
- break;
- }
- }
- sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); //Pozwolenie na ustawianie wielu połączeń, wymagane dla
- serwera
- sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); //Start serwera na porcie 80
- delay(1000);
- sendData("AT+CIFSR\r\n", 1000, DEBUG); //Pobranie adresów MAC oraz IP }
- String sendData(String command, const int waitForResponse, boolean debug)
- {
- String response = "";
- esp8266.print(command); //Wysłanie komendy do ESP
- long int time = millis();
- while ( (time + waitForResponse) > millis())
- {
- while (esp8266.available())
- {
- char znak = esp8266.read();
- response += znak;
- }
- }
- if (debug)
- {
- Serial.print(response); //Wyświetlenie odpowiedzi na konsoli
- }
- return response;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement