Hello everybody!
Often we see tutorials on sending data from nodeMCU to to client(browser)...now what i want to do is "REVERSE IT!!" >:( I want to send data from browser to nodeMCU...like user types new ssid and password in the browser and i want my nodeMCU to find and use that particular wifi connection....or just any simple data link api key...please help me here also do consider this i donno HTML so if possible gimme code along with explanation
Thanks for reading this far
Thanks noiasca
i will see it now
Auf weidazein
Yo!
i think it does not take no data from client......if am right please see the code
/*
Copyright (c) 2015, Majenko Technologies
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* * Neither the name of Majenko Technologies nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char *ssid = "corph";
const char *password = "corph@786";
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();
}
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);
}
where does it take input from user via browser? it just draws graph randomly
Thanks noiasca for prompting reply
but how is
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
taking me closer to what I want......i want new ssid from browser....how is this piece of code doing anything related to that?
Thanks noiasca
You are such a nice person
I will check 'em out...
hello noiasca!
I was tryna get to example of wifimanager called configOnStartup.....it shows compilation error....when i try to upload any other code it goes fine .....what could be wrong with it heres the error message followed by the code i am using...do see if this code will gimme my answers....thank you
Arduino: 1.8.7 (Windows Store 1.8.15.0) (Windows 10), Board: "NodeMCU 0.9 (ESP-12 Module), 80 MHz, Flash, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"
libraries\WiFiManager-master\WiFiManager.cpp.o: In function `WiFiManager::waitForConnectResult()':
C:\Users\MEHARR\Documents\Arduino\libraries\WiFiManager-master/WiFiManager.cpp:891: undefined reference to `WPS_is_unavailable_in_this_configuration__Please_check_FAQ_or_board_generator_tool'
libraries\WiFiManager-master\WiFiManager.cpp.o: In function `String::operator+=(__FlashStringHelper const*)':
C:\Users\MEHARR\Documents\Arduino\libraries\WiFiManager-master/WiFiManager.cpp:891: undefined reference to `WPS_is_unavailable_in_this_configuration__Please_check_FAQ_or_board_generator_tool'
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board NodeMCU 0.9 (ESP-12 Module).
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
heres the code
/*
This example will open a configuration portal for 60 seconds when first powered up.
ConfigOnSwitch is a a bettter example for most situations but this has the advantage
that no pins or buttons are required on the ESP8266 device at the cost of delaying
the user sketch for the period that the configuration portal is open.
Also in this example a password is required to connect to the configuration portal
network. This is inconvenient but means that only those who know the password or those
already connected to the target WiFi network can access the configuration portal and
the WiFi network credentials will be sent from the browser over an encrypted connection and
can not be read by observers.
*/
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h> //https://github.com/kentaylor/WiFiManager
// Onboard LED I/O pin on NodeMCU board
const int PIN_LED = 2; // D4 on NodeMCU and WeMos. Controls the onboard LED.
void setup() {
// put your setup code here, to run once:
// initialize the LED digital pin as an output.
pinMode(PIN_LED, OUTPUT);
Serial.begin(115200);
Serial.println("\n Starting");
unsigned long startedAt = millis();
WiFi.printDiag(Serial); //Remove this line if you do not want to see WiFi password printed
Serial.println("Opening configuration portal");
digitalWrite(PIN_LED, LOW); // turn the LED on by making the voltage LOW to tell us we are in configuration mode.
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//sets timeout in seconds until configuration portal gets turned off.
//If not specified device will remain in configuration mode until
//switched off via webserver.
if (WiFi.SSID()!="") wifiManager.setConfigPortalTimeout(60); //If no access point name has been previously entered disable timeout.
//it starts an access point
//and goes into a blocking loop awaiting configuration
if (!wifiManager.startConfigPortal("ESP8266","password")) {//Delete these two parameters if you do not want a WiFi password on your configuration access point
Serial.println("Not connected to WiFi but continuing anyway.");
} else {
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
digitalWrite(PIN_LED, HIGH); // Turn led off as we are not in configuration mode.
// For some unknown reason webserver can only be started once per boot up
// so webserver can not be used again in the sketch.
Serial.print("After waiting ");
int connRes = WiFi.waitForConnectResult();
float waited = (millis()- startedAt);
Serial.print(waited/1000);
Serial.print(" secs in setup() connection result is ");
Serial.println(connRes);
if (WiFi.status()!=WL_CONNECTED){
Serial.println("failed to connect, finishing setup anyway");
} else{
Serial.print("local ip: ");
Serial.println(WiFi.localIP());
}
}
void loop() {
delay(10000);
// put your main code here, to run repeatedly:
}