first commit
This commit is contained in:
160
W1D1_AUTONOME/W1D1_AUTONOME.ino
Normal file
160
W1D1_AUTONOME/W1D1_AUTONOME.ino
Normal file
@@ -0,0 +1,160 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
//////////////////////
|
||||
// WiFi Definitions //
|
||||
//////////////////////
|
||||
const char WiFiAPPSK[] = "sparkfun";
|
||||
|
||||
// Avant la connexion au réseau local
|
||||
// Se connecter au réseau ESP8266 Thing **** Qui apparait dans
|
||||
// la liste des réseau WIFI
|
||||
// sur l'adresse IP http://192.168.4.1/
|
||||
|
||||
char* ssid = "Livebox-37cc"; // Le nom de votre réseau Wifi
|
||||
char* pass = "8A6060920A8A86896F770F2C47";
|
||||
|
||||
|
||||
|
||||
WiFiServer server(80);
|
||||
|
||||
void setup()
|
||||
{
|
||||
initHardware();
|
||||
setupWiFi();
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Check if a client has connected
|
||||
WiFiClient client = server.available();
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Read the first line of the request
|
||||
String req = client.readStringUntil('\r');
|
||||
Serial.println(req);
|
||||
// client.flush();
|
||||
|
||||
// Match the request
|
||||
int val = -1; // We'll use 'val' to keep track of both the
|
||||
// request type (read/set) and value if set.
|
||||
if (req.indexOf("/led/0") != -1)
|
||||
val = 0; // Will write LED low
|
||||
else if (req.indexOf("/led/1") != -1)
|
||||
val = 1; // Will write LED high
|
||||
else if (req.indexOf("/read") != -1)
|
||||
val = -2; // Will print pin reads
|
||||
else if (req.indexOf("/admin") != -1)
|
||||
val = -3;
|
||||
else if (req.indexOf("/ident") != -1) {
|
||||
val = -4;
|
||||
// Return : ident?name=jjjjj&pass=kkkk&H=
|
||||
int Pos_r = req.indexOf("name");
|
||||
int Pos_g = req.indexOf("pass", Pos_r); //finds a location of a string "g", after the position of "r"
|
||||
int End = req.indexOf("H", Pos_g);
|
||||
if(End < 0){
|
||||
End = req.length() + 1;
|
||||
}
|
||||
//ssid
|
||||
ssid = getValueFrom(req, Pos_r, Pos_g);
|
||||
|
||||
//Password
|
||||
pass = getValueFrom(req, Pos_g, End);
|
||||
}
|
||||
|
||||
// Otherwise req will be invalid. We'll say as much in HTML
|
||||
|
||||
client.flush();
|
||||
|
||||
// Prepare the response. Start with the common header:
|
||||
String s = "HTTP/1.1 200 OK\r\n";
|
||||
s += "Content-Type: text/html\r\n\r\n";
|
||||
s += "<!DOCTYPE HTML>\r\n<html>\r\n";
|
||||
|
||||
if (val == -3) {
|
||||
|
||||
// client.println("<body><center>Set the color and then choose an animation:");
|
||||
// client.println("<form method=get><br>Red: <input type='range' min='1' max='100' name=redVal value=redTemp oninput='showValue(this.value)'>"); //was onchange event
|
||||
// client.println("<span id='range'>0</span>");
|
||||
// client.println("<script type='text/javascript'>\r\nfunction showValue(newValue)\r\n{\r\ndocument.getElementById('range').innerHTML=newValue;\r\n}\r\n</script>\r\n");
|
||||
// client.print("<br><br>Green: <input type='range' min='1' max='100' name=greVal value=>");
|
||||
// client.print("<br><br>Blue: <input type='range' min='1' max='100' name=bluVal value=>");
|
||||
// client.println(" <br><br><input name=H type=submit value=submit><br><br>"); //</form>
|
||||
// client.println("</form>");
|
||||
// client.println("<script type='text/javascript'></script>\r\n");
|
||||
// client.println("</center>");
|
||||
// client.println("</BODY>");
|
||||
|
||||
client.println("<body>");
|
||||
client.println("<form action='/ident' method='get'>");
|
||||
client.println("<script type='text/javascript'>\r\nfunction showValue(newValue)\r\n{\r\ndocument.getElementById('text').innerHTML=newValue;\r\n}\r\n</script>\r\n");
|
||||
client.println("<div>");
|
||||
client.println("<label for='name'>SSID:</label>");
|
||||
client.println("<input type='text' id='name' name='name' />");
|
||||
client.println("</div>");
|
||||
client.println("<div>");
|
||||
client.println(" <label for='pass'>Password:</label>");
|
||||
client.println(" <input type='password' id='pass' name='pass' />");
|
||||
client.println("</div>");
|
||||
client.println("<div class='button'>");
|
||||
client.println(" <button name=H type='submit'>Ok</button>");
|
||||
client.println("</div>");
|
||||
client.println("</form>");
|
||||
client.println("</body>");
|
||||
|
||||
} else {
|
||||
s += "Invalid Request.<br> Try /led/1, /led/0, or /read /admin.";
|
||||
}
|
||||
s += "</html>\n";
|
||||
|
||||
// Send the response to the client
|
||||
client.print(s);
|
||||
delay(1);
|
||||
Serial.println("Client disonnected");
|
||||
|
||||
// The client will actually be disconnected
|
||||
// when the function returns and 'client' object is detroyed
|
||||
}
|
||||
|
||||
void setupWiFi()
|
||||
{
|
||||
WiFi.mode(WIFI_AP);
|
||||
|
||||
// Do a little work to get a unique-ish name. Append the
|
||||
// last two bytes of the MAC (HEX'd) to "Thing-":
|
||||
uint8_t mac[WL_MAC_ADDR_LENGTH];
|
||||
WiFi.softAPmacAddress(mac);
|
||||
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
|
||||
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
|
||||
macID.toUpperCase();
|
||||
String AP_NameString = "ESP8266 Thing " + macID;
|
||||
|
||||
char AP_NameChar[AP_NameString.length() + 1];
|
||||
memset(AP_NameChar, 0, AP_NameString.length() + 1);
|
||||
|
||||
for (int i=0; i<AP_NameString.length(); i++)
|
||||
AP_NameChar[i] = AP_NameString.charAt(i);
|
||||
|
||||
WiFi.softAP(AP_NameChar, WiFiAPPSK);
|
||||
}
|
||||
|
||||
void initHardware()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
}
|
||||
char* getValueFrom(String request, int deb, int fin) {
|
||||
// String value = 0;
|
||||
char* colorBuff;
|
||||
|
||||
int bufLength = ((fin) - (deb+5)); //the 5 is for the "name=" string
|
||||
|
||||
if(bufLength > 4){ //dont overflow the buffer (we only want 3 digits)
|
||||
bufLength = 4;
|
||||
}
|
||||
request.substring((deb+5), (fin-1)).toCharArray(colorBuff, bufLength); //transfer substring to buffer (cuts from past the "r=" to just before the "g", stores the length into a buffer)
|
||||
// value = atoi(colorBuff); //converts the array (3 digits) into an interger, this will be the red value
|
||||
|
||||
return colorBuff;
|
||||
}
|
||||
Reference in New Issue
Block a user