148 lines
3.2 KiB
C++
148 lines
3.2 KiB
C++
#include <ESP8266WiFi.h>
|
|
|
|
#include <Adafruit_BMP085.h>
|
|
#include <Wire.h>
|
|
|
|
// WIFI
|
|
const char* ssid = "Livebox-37cc"; // Le nom de votre réseau Wifi
|
|
const char* password = "8A6060920A8A86896F770F2C47";
|
|
|
|
const int relay_pin = 13;
|
|
const int buttonPin = 12; // the number of the pushbutton pin
|
|
int end_filament = 0; // variable for reading the pushbutton status
|
|
|
|
WiFiServer server(80);
|
|
|
|
|
|
#define DEBUG true
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
delay(10);
|
|
|
|
pinMode(relay_pin, OUTPUT);
|
|
digitalWrite(relay_pin, HIGH);
|
|
// initialize the pushbutton pin as an input:
|
|
pinMode(buttonPin, INPUT);
|
|
|
|
// Connect to WiFi network
|
|
Serial.println();
|
|
Serial.println();
|
|
Serial.print("Connecting to ");
|
|
Serial.println(ssid);
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.println("WiFi connected");
|
|
|
|
// Start the server
|
|
server.begin();
|
|
Serial.println("Server started");
|
|
|
|
// Print the IP address
|
|
Serial.print("Use this URL to connect: ");
|
|
Serial.print("http://");
|
|
Serial.print(WiFi.localIP());
|
|
Serial.println("/");
|
|
|
|
Wire.begin(4, 5);
|
|
|
|
}
|
|
|
|
void loop() {
|
|
|
|
// read the state of the pushbutton value:
|
|
end_filament = digitalRead(buttonPin);
|
|
|
|
// // check if the pushbutton is pressed. If it is, the end_filament is HIGH:
|
|
// if (end_filament == HIGH) {
|
|
// // turn LED on:
|
|
// digitalWrite(ledPin, HIGH);
|
|
// } else {
|
|
// // turn LED off:
|
|
// digitalWrite(ledPin, LOW);
|
|
// }
|
|
|
|
|
|
// Check if a client has connected
|
|
WiFiClient client = server.available();
|
|
if (!client) {
|
|
return;
|
|
}
|
|
|
|
// Wait until the client sends some data
|
|
Serial.println("new client");
|
|
while(!client.available()){
|
|
delay(1);
|
|
}
|
|
|
|
// Read the first line of the request
|
|
String request = client.readStringUntil('\r');
|
|
Serial.println(request);
|
|
client.flush();
|
|
|
|
// Match the request
|
|
|
|
int value = LOW;
|
|
if (request.indexOf("/LED=ON") != -1) {
|
|
digitalWrite(relay_pin, HIGH);
|
|
value = HIGH;
|
|
}
|
|
if (request.indexOf("/LED=OFF") != -1) {
|
|
digitalWrite(relay_pin, LOW);
|
|
value = LOW;
|
|
}
|
|
|
|
// Set relay_pin according to the request
|
|
//digitalWrite(relay_pin, value);
|
|
|
|
// Return the response
|
|
client.println("HTTP/1.1 200 OK");
|
|
client.println("Content-Type: text/html");
|
|
client.println(""); // do not forget this one
|
|
client.println("<!DOCTYPE HTML>");
|
|
client.println("<html>");
|
|
|
|
client.print("Led pin is now: ");
|
|
|
|
if(value == HIGH) {
|
|
client.print("On");
|
|
} else {
|
|
client.print("Off");
|
|
}
|
|
|
|
/////////////////////////////
|
|
//
|
|
////////////////////////////
|
|
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
|
|
delay(10); // wait for a second
|
|
digitalWrite(2, LOW);
|
|
|
|
|
|
#ifdef DEBUG
|
|
Serial.print("Send");
|
|
#endif
|
|
|
|
|
|
client.println("<TABLE>");
|
|
|
|
client.println("<TR><TD>Click <a href=\"/LED=ON\">here</a> turn the LED on pin 2 ON</TD></TR>");
|
|
client.println("<TR><TD>Click <a href=\"/LED=OFF\">here</a> turn the LED on pin 2 OFF</TD></TR>");
|
|
client.print("<TR><TD>");
|
|
client.print(end_filament);
|
|
client.println("</TD></TR>");
|
|
|
|
client.println("</TABLE></html>");
|
|
|
|
delay(1);
|
|
Serial.println("Client disconnected");
|
|
Serial.println("");
|
|
}
|
|
|
|
|