WeMos D1 статический ip адрес при подключении по wi-fi
- Войдите на сайт для отправки комментариев
Пт, 11/01/2019 - 16:19
Есть скетч простого вебсервера.
Подскажите плиз как правильно прописать статический айпи для сети а то с динамическим не подключается.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif
const char *ssid = STASSID;
const char *password = STAPSK;
ESP8266WebServer server(80);
const int led = 13;
void handleRoot() {
digitalWrite(led, 1);
char temp[400];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
snprintf(temp, 400,
"<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>ESP8266 Demo</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>Hello from ESP8266!</h1>\
<p>Uptime: %02d:%02d:%02d</p>\
<img src=\"/test.svg\" />\
</body>\
</html>",
hr, min % 60, sec % 60
);
server.send(200, "text/html", temp);
digitalWrite(led, 0);
}
void handleNotFound() {
digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
void setup(void) {
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/test.svg", drawGraph);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
MDNS.update();
}
void drawGraph() {
String out = "";
char temp[100];
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n";
out += "<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
out += "<g stroke=\"black\">\n";
int y = rand() % 130;
for (int x = 10; x < 390; x += 10) {
int y2 = rand() % 130;
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2);
out += temp;
y = y2;
}
out += "</g>\n</svg>\n";
server.send(200, "image/svg+xml", out);
}
хм... странно, как раз со статикой проблем не было,
вот пример как динамически получается адрес
http://arduino.ru/forum/proekty/ethernet-vyklyuchatel-nagruzki-v-lokalno...
Так не..., проблема в том что сеть в офисе так настроена что подключает только с кокнретными айпишниками. А я не могу найти как прописать в скетче чтобы задавался статический айпишник свой для платы.
вот пример как динамически получается адрес
ой, виноват, не тот пример выложил
А я не могу найти как прописать в скетче чтобы задавался статический айпишник свой для платы.
в 14 строке, там же где и порт
согласно исходников:
А я не могу найти как прописать в скетче чтобы задавался статический айпишник свой для платы.
в 14 строке, там же где и порт
согласно исходников:
Айпишник написать вместо addr?
соответсвенно. Примеры уже откройте.
Поставил из примера выдает пачку ошибок
warning: espcomm_sync failed
Пробывал вписать в месте где стратует сервер теже ошибки.
Пробывал задать строкой аналогичной выводу айпишника через WiFi.localIP и с точкой и без точки, и без WiFi.
В итоге выдает "does not name a type"
#include <ESP8266WiFi.h> #ifndef STASSID #define STASSID "*****" #define STAPSK "***********" #endif const char* ssid = STASSID; const char* password = STAPSK; // Create an instance of the server // specify the port to listen on as an argument WiFiServer server(80); IPAddress ip(192, 168, 0, 111); void setup() { Serial.begin(115200); // prepare LED pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, 0); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print(F("Connecting to ")); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(F(".")); } Serial.println(); Serial.println(F("WiFi connected")); // Start the server server.begin(); Serial.println(F("Server started")); // Print the IP address Serial.println(WiFi.localIP()); } void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } Serial.println(F("new client")); client.setTimeout(5000); // default is 1000 // Read the first line of the request String req = client.readStringUntil('\r'); Serial.println(F("request: ")); Serial.println(req); // Match the request int val; if (req.indexOf(F("/gpio/0")) != -1) { val = 0; } else if (req.indexOf(F("/gpio/1")) != -1) { val = 1; } else { Serial.println(F("invalid request")); val = digitalRead(LED_BUILTIN); } // Set LED according to the request digitalWrite(LED_BUILTIN, val); // read/ignore the rest of the request // do not client.flush(): it is for output only, see below while (client.available()) { // byte by byte is not very efficient client.read(); } // Send the response to the client // it is OK for multiple small client.print/write, // because nagle algorithm will group them into one single packet client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ")); client.print((val) ? F("high") : F("low")); client.print(F("<br><br>Click <a href='http://")); client.print(WiFi.localIP()); client.print(F("/gpio/1'>here</a> to switch LED GPIO on, or <a href='http://")); client.print(WiFi.localIP()); client.print(F("/gpio/0'>here</a> to switch LED GPIO off.</html>")); // The client will actually be *flushed* then disconnected // when the function returns and 'client' object is destroyed (out-of-scope) // flush = ensure written data are received by the other side Serial.println(F("Disconnecting from client")); }