You are on page 1of 10

/*

Există anumiți pini care scot un semnal de 3,3 V atunci când ESP8266 pornește.
Acest lucru poate fi problematic dacă aveți relee sau alte periferice conectate
la acele GPIO.
Următoarele GPIO scot un semnal HIGH la pornire:

GPIO16
GPIO3
GPIO1
GPIO10
GPIO9
*/

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "LittleFS.h"
#include <Arduino_JSON.h>
#include <EEPROM.h>
// Replace with your network credentials
const char* ssid = "PRAM";
const char* password = "123456789";

//const char* ssid = "Ephesos2";


//const char* password = "alex!@#$ALEX";

// Create AsyncWebServer object on port 80


AsyncWebServer server(80);
// Create a WebSocket object

AsyncWebSocket ws("/ws");
// Set LED GPIO

/*
const int Releu1 = 5;
const int Releu2 = 4;
*/

const int ledPin1 = 5; //const int Releu1 = 5;


const int ledPin2 = 4; //const int Releu2 = 4;
const int ledPin3 = 14;
const int ledPin4 = 12;
const int ledPin5 = 13;
const int ledPin6 = 15;

const int buttonStanga = 2 ; // intrari buton


const int buttonDreapta = 0;
const int buttonOprit = 9;//nu este folosit

unsigned long previousMillisP = 0; //


unsigned long previousMillis2 = 0; //
unsigned long previousMillis3 = 0;
int tempS = 0; //temporizare stinga dreapta
int tempD = 0;
int tempT = 0;// TEMPORIZARE trepte
unsigned long previousMillisT =0;
int tempTest = 0;

int buttonS = LOW; // variable for reading the pushbutton status


int buttonD = LOW;
int buttonO = LOW;

static int timpP;


static int timpO;
static int timpT;

String message = "";


String sliderValue1 = "10";
String sliderValue2 = "10";
String sliderValue3 = "3";

int dutyCycle1;
int dutyCycle2;
int dutyCycle3;

bool ledState1 = 0;
bool ledState2 = 0;
bool ledState3 = 0;
bool ledState4 = 0;
bool ledState5 = 0;

//Json Variable to Hold Slider Values


JSONVar sliderValues;

//Get Slider Values


String getSliderValues() {
sliderValues["sliderValue1"] = String(sliderValue1);
sliderValues["sliderValue2"] = String(sliderValue2);
sliderValues["sliderValue3"] = String(sliderValue3);

String jsonString = JSON.stringify(sliderValues);


return jsonString;
}

// Initialize LittleFS
void initFS() {
if (!LittleFS.begin()) {
Serial.println("An error has occurred while mounting LittleFS");
}
else {
Serial.println("LittleFS mounted successfully");
}
}

// Initialize WiFi
void initWiFi() {
/*
WiFi.mode(WIFI_AP);
WiFi.softAP(APssid, APpassword);
//WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
*/

WiFi.softAP(ssid, password,13);

IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);

// Print ESP8266 Local IP Address


Serial.println(WiFi.localIP());
}

void notifyClients(String sliderValues) {


ws.textAll(sliderValues);
}

//.....................................
void notifyClients() {
ws.textAll(String(ledState1));
ws.textAll(String(ledState2));
ws.textAll(String(ledState3));
ws.textAll(String(ledState4));
ws.textAll(String(ledState5));
}

void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) {


AwsFrameInfo *info = (AwsFrameInfo*)arg;
if (info->final && info->index == 0 && info->len == len && info->opcode ==
WS_TEXT) {
data[len] = 0;
message = (char*)data;
if (message.indexOf("1s") >= 0) {
sliderValue1 = message.substring(2);
dutyCycle1 = sliderValue1.toInt();
EEPROM.write(0, sliderValue1.toInt());
EEPROM.commit();
Serial.println(dutyCycle1);
Serial.println(getSliderValues());
notifyClients(getSliderValues());
}
if (message.indexOf("2s") >= 0) {
sliderValue2 = message.substring(2);
dutyCycle2 = sliderValue2.toInt();

EEPROM.write(35, sliderValue2.toInt());
EEPROM.commit();

// EEPROM.update(35, sliderValue2);
Serial.println(dutyCycle2);
Serial.println(getSliderValues());
notifyClients(getSliderValues());
}
//..........................................

if (message.indexOf("3s") >= 0) {
sliderValue3 = message.substring(2);
dutyCycle3 = sliderValue3.toInt();

EEPROM.write(70, sliderValue3.toInt());
EEPROM.commit();

// EEPROM.update(70, sliderValue3);
Serial.println(dutyCycle3);
Serial.print(getSliderValues());
notifyClients(getSliderValues());
}

if (strcmp((char*)data, "getValues") == 0) {
notifyClients(getSliderValues());
}
//.............................................

}
}
void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType
type, void *arg, uint8_t *data, size_t len) {
switch (type) {
case WS_EVT_CONNECT:
Serial.printf("WebSocket client #%u connected from %s\n", client->id(),
client->remoteIP().toString().c_str());
break;
case WS_EVT_DISCONNECT:
Serial.printf("WebSocket client #%u disconnected\n", client->id());
break;
case WS_EVT_DATA:
handleWebSocketMessage(arg, data, len);
break;
case WS_EVT_PONG:
case WS_EVT_ERROR:
break;
}
}

void initWebSocket() {
ws.onEvent(onEvent);
server.addHandler(&ws);
}

//............................

String processor(const String& var) {


Serial.println(var);
if (var == "STATE1") {
if (ledState1) {
return "on";
}
else {
return "off";
}
notifyClients("LED1on");
}
if (var == "STATE2") {
if (ledState2) {
return "on";
}
else {
return "off";
}
notifyClients("LED2on");
}
if (var == "STATE3") {
if (ledState3) {
return "on";
}
else {
return "off";
}
notifyClients("LED3on");
}
if (var == "STATE4") {
if (ledState4) {
return "on";
}
else {
return "off";
}
notifyClients("LED4on");
}

if (var == "STATE5") {
if (ledState5) {
return "on";
}
else {
return "off";
}
notifyClients("LED5on");
}

return String();

/*
return String(var == "STATE1" && ledState1 ? "on" : "off");
return String(var == "STATE2" && ledState2 ? "on" : "off");

*/
}

void setup() {
Serial.begin(115200);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);

digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);

pinMode(buttonStanga, INPUT);
pinMode(buttonDreapta, INPUT);
pinMode(buttonOprit, INPUT);

EEPROM.begin(100);
sliderValue1 = EEPROM.read(0);
sliderValue2 = EEPROM.read(35);
sliderValue3 = EEPROM.read(70);
initWiFi();
initFS();

initWebSocket();

// Web Server Root URL


server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(LittleFS, "/index.html", "text/html", false, processor);
});

server.serveStatic("/", LittleFS, "/");

// Start server
server.begin();
}

void loop() {

timpP = sliderValue1.toInt();
timpO = sliderValue2.toInt();
timpT = sliderValue3.toInt();

// read the state of the pushbutton value:


buttonS = digitalRead(buttonStanga);
buttonD = digitalRead(buttonDreapta);
buttonO = digitalRead(buttonOprit);
//Serial.print("butoane");
//Serial.print(buttonS);
//Serial.print(buttonD);
//Serial.println(buttonO);

unsigned long currentMillis = millis();


if (currentMillis - previousMillisT >= 100) {
previousMillisT = currentMillis;
//if (tempTest <= 280){
// tempTest= tempTest+1;}
//.......................................
if (ledState1 == LOW && tempS <= 51){
tempS = tempS + 1;
}
if (ledState2 == LOW && tempD <= 51){
tempD = tempD + 1;
}

if (ledState1 == HIGH || ledState2 == HIGH ){

//320
if (tempT <= 80*timpT){
tempT =tempT + 70 - timpP;

}
}
else{
tempT =tempT -(70- timpO);
if (tempT <= 9){
tempT = 0;
}
}

ws.cleanupClients();
}

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:

if (buttonS == LOW && tempD >= 50 ) {


//if (tempTest >=100 && tempD >= 40 ) {

// turn LED on:


ledState1 = HIGH;
digitalWrite(ledPin1, ledState1);
notifyClients("LED1on");
tempS = 0;
delay(4);
// Serial.print("ledPin1 ");
}
else {
// turn LED off:
ledState1 = LOW;
if (tempT <= 9){
digitalWrite(ledPin1, ledState1);
notifyClients("LED1off");
delay(4);
}
}
/* if (tempTest >=239 ) {
tempTest = 0;

} */
if (buttonD == LOW && tempS >= 50 ) {
// turn LED on:
ledState2 = HIGH;
digitalWrite(ledPin2, ledState2);
tempD = 0;
notifyClients("LED2on");
// Serial.print("ledPin2");
delay(4);
}
else {
// turn LED off:
ledState2 = LOW;
if (tempT <= 9){

digitalWrite(ledPin2, ledState2);
notifyClients("LED2off");
delay(4);
}
}

if (tempT >= 122){


ledState3 = HIGH;
digitalWrite(ledPin3, ledState3);
notifyClients("LED3on");
delay(4);
}
if (tempT >= 242){
ledState4 = HIGH;
digitalWrite(ledPin4, ledState4);
notifyClients("LED4on");
delay(4);
}
if (tempT >= 320){
ledState5 = HIGH;
digitalWrite(ledPin5, ledState5);
notifyClients("LED5on");
delay(4);
}

if (tempT <= 239){


ledState5 = LOW;
digitalWrite(ledPin5, ledState5);
notifyClients("LED5off");
delay(4);
}
if (tempT <= 218){
ledState4 = LOW;
digitalWrite(ledPin4, ledState4);
notifyClients("LED4off");
delay(4);
}
if (tempT <= 108){
ledState3 = LOW;
digitalWrite(ledPin3, ledState3);
notifyClients("LED3off");
delay(4);
}
if (buttonD == HIGH || buttonD == HIGH ){
digitalWrite(ledPin6, HIGH);
}

else {
if (tempT <= 9){
digitalWrite(ledPin6, HIGH);
}
}
/*
if(ledState3 == true){
notifyClients("LED3on");
}
else{
notifyClients("LED3off");}

if(ledState4 == true){
notifyClients("LED4on");
}
else{
notifyClients("LED4off");}

if(ledState5 == true){
notifyClients("LED5on");
}
else{
notifyClients("LED5off");}

*/

Serial.print("LED 3,4,5 = ");


Serial.print(ledState3);
Serial.print(ledState4);
Serial.println(ledState5);

Serial.print("TIMP timpP,timpO,tempT = ");


Serial.print(timpP);
Serial.print(" = ");
Serial.print(timpO);
Serial.print(" = ");
Serial.println(tempT);

//Serial.println(sliderValue1);
//Serial.println(sliderValue2);
}

You might also like