У меня есть приложение Android, которое я создал с помощью MIT App Inventor, которое работает со следующим файлом ino в Arduino IDE:
#include <ESP8266WiFi.h>
const char* ssid = "my ssid";
const char* password = "my password";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
// Assign output variables to GPIO pins
int tool_box_lights = 0;
int pass_side_lights = 0;
int drv_side_lights = 0;
int rear_lights = 0;
int winch = 0;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
// Serial.println("New Client"); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
currentTime = millis();
previousTime = currentTime;
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
// Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
//Serial.println(header);
// turns the GPIOs on and off
if (header.indexOf("/tb/on") >= 0)
{
Serial.println("toolbox on");
client.println("toolbox on");
tool_box_lights = 1;
} else if (header.indexOf("/tb/off") >= 0)
{
Serial.println("toolbox off");
client.println("toolbox off");
tool_box_lights = 0;
} else if (header.indexOf("/ps/on") >= 0)
{
Serial.println("pass side on");
client.println("pass side on");
pass_side_lights = 1;
} else if (header.indexOf("/ps/off") >= 0)
{
Serial.println("pass side off");
client.println("pass side off");
pass_side_lights = 0;
} else if (header.indexOf("/ds/on") >= 0)
{
Serial.println("drv side on");
client.println("drv side on");
drv_side_lights = 1;
} else if (header.indexOf("/ds/off") >= 0)
{
Serial.println("drv side off");
client.println("drv side off");
drv_side_lights = 0;
} else if (header.indexOf("/rr/on") >= 0)
{
Serial.println("rear lights on");
client.println("rear lights on");
rear_lights = 1;
} else if (header.indexOf("/rr/off") >= 0)
{
Serial.println("rear lights off");
client.println("rear lights off");
rear_lights = 0;
} else if (header.indexOf("/wc/in") >= 0)
{
Serial.println("winch in");
client.println("winch in");
winch = 1;
} else if (header.indexOf("/wc/out") >= 0)
{
Serial.println("winch out");
client.println("winch out");
winch = 2;
} else if (header.indexOf("/wc/off") >= 0)
{
Serial.println("winch off");
client.println("winch off");
winch = 0;
}
// Display the HTML web page
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
// Serial.println("Client disconnected.");
// Serial.println("");
}
}
, но когда я пытаюсь заставить его работать с программными точками доступа:
IPAddress local_IP1(192,168,0,22);
IPAddress gateway1(192,168,0,1);
IPAddress subnet(255,255,255,0);
...
Serial.print("\nSetting soft-AP configuration ... ");
Serial.println(WiFi.softAPConfig(local_IP1, gateway1, subnet) ? "Ready" : "Failed!");
Serial.println(WiFi.softAP(ssid, password, false, 4) ? "Ready" : "Failed!");
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
/*
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
*/
// Print local IP address and start web server
Serial.println("");
// Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.softAPIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client"); // print a message out in the serial port
...
Он никогда не соединяется. Я попытался пропустить, где я вручную устанавливал IP-адрес:
IPAddress local_IP1(192,168,0,22);
IPAddress gateway1(192,168,0,1);
IPAddress subnet(255,255,255,0);
, и он оказался 192.168.1.4
, поэтому я установил его в качестве IP-адреса в MIT App, и я получил ошибку 1103. Вот часть кода блока для этого:
![MIT App Inventor Block Diagram](https://i.stack.imgur.com/3gqF7.jpg)
Единственное, что я изменяю, - это IP-адрес. Он отлично работает, если я использую IP-адрес маршрутизатора, но я хочу использовать его в своем грузовике и не хочу носить с собой маршрутизатор.