Hello, currently I am working on some small project which i want to unite in one thing and I have spend too much time lurking throuh the internet so I want to ask here what the problem is and how to solve it maybe someone will help.
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x7B
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void temperatura_wewnetrzna (void);
void temperatura_zewnetrzna (void);
uint8_t temp_wewn;
uint8_t temp_zewn;
#define Pin_D1 5 //d5/gpio14
#define Pin_D2 6 // d6/gpio12
// Enter your wifi network name and Wifi Password
const char* ssid = "Grzechuu";
const char* password = "grzegorz2137";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Assign output variables to GPIO pins
#define swiatlo_1 0//d0 gpio16
#define swiatlo_2 3//d3 gpio0
// These variables store current output state of LED
String swiatlo1_stan = "Wyl";
String swiatlo2_stan = "Wyl";
void setup() {
//while (1){ESP.wdtFeed();};
//ESP.wdtDisable();
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(swiatlo_1, OUTPUT);
pinMode(swiatlo_2, OUTPUT);
pinMode(Pin_D1, OUTPUT);
pinMode(Pin_D2, OUTPUT);
pinMode(A0, INPUT);
// Set outputs to LOW
digitalWrite(swiatlo_1, LOW);
digitalWrite(swiatlo_2, LOW);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Connect to Wi-Fi network with SSID and password
Serial.print("Łączenie z : ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(200);
Serial.println("Łączę...");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("Połączono");
Serial.println("Lokalny adres IP: : ");
Serial.println(WiFi.localIP());
server.begin();
}
void temperatura_wewnetrzna() {
temp_wewn=(analogRead(A0) / 1024.0) * 100;
digitalWrite(Pin_D1,HIGH);
digitalWrite(Pin_D2,LOW);
}
void temperatura_zewnetrzna() {
temp_wewn=(analogRead(A0) / 1024.0) * 100;
digitalWrite(Pin_D1,LOW);
digitalWrite(Pin_D2,HIGH);
}
void loop() {
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("Nowy użytkownik"); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
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();
// turns the GPIOs on and off
if (header.indexOf("GET /0/on") >= 0) {
Serial.println("Światło dla ptaków włączone");
swiatlo1_stan = "on";
digitalWrite(swiatlo_1, HIGH);
} else if (header.indexOf("GET /0/off") >= 0) {
Serial.println("Światło dla ptaków wyłączone");
swiatlo1_stan = "off";
digitalWrite(swiatlo_1, LOW);
} else if (header.indexOf("GET /3/on") >= 0) {
Serial.println("Green LED is on");
swiatlo2_stan = "on";
digitalWrite(swiatlo_2, HIGH);
} else if (header.indexOf("GET /3/off") >= 0) {
Serial.println("Green LED is off");
swiatlo2_stan = "off";
digitalWrite(swiatlo_2, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".buttonRed { background-color: #ff0000; border: none; color: white; padding: 16px 40px; border-radius: 60%;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".buttonGreen { background-color: #00ff00; border: none; color: white; padding: 16px 40px; border-radius: 60%;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".buttonYellow { background-color: #feeb36; border: none; color: white; padding: 16px 40px; border-radius: 60%;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".buttonOff { background-color: #77878A; border: none; color: white; padding: 16px 40px; border-radius: 70%;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}</style></head>");
// Web Page Heading
client.println("<body><h1>Grzeszne IoT Prot.v.1.2</h1>");
// Display current state, and ON/OFF buttons for GPIO 2 Red LED
client.println("<p>Swiatlo1 " + swiatlo1_stan + "</p>");
// If the outputRedState is off, it displays the OFF button
if (swiatlo1_stan == "off") {
client.println("<p><a href=\"/0/on\"><button class=\"button buttonOff\">Wyl</button></a></p>");
} else {
client.println("<p><a href=\"/0/off\"><button class=\"button buttonRed\">Wlaczone</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 4 Green LED
client.println("<p>Swiatlo2 " + swiatlo2_stan + "</p>");
// If the outputGreenState is off, it displays the OFF button
if (swiatlo2_stan == "off") {
client.println("<p><a href=\"/3/on\"><button class=\"button buttonOff\">Wyl</button></a></p>");
} else {
client.println("<p><a href=\"/3/off\"><button class=\"button buttonGreen\">Wlaczone</button></a></p>");
}
client.println("</body></html>");
// 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("");
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(4, 4);
temperatura_wewnetrzna();
Serial.println("temp.w.: ");
Serial.println(temp_wewn);
delay(300);
temperatura_zewnetrzna();
Serial.println("temp.z.: ");
Serial.println(temp_zewn);
delay(300);
}
And the serial monitor:
Thank you in advance