SOLVED - ESP8266 - Sketches Arduino -command relay

Started by gerald_Dijon, January 04, 2016, 09:45:13 PM

Previous topic - Next topic

gerald_Dijon

hi here,

I found an example and adapt it to command a relay. COde is fully fonctional (except sometime...)

My problems is that 3~5 consecutive time i can write or read the OUTPUT that command the relay.
Sometime Firefox has no response from the module during a long time.
I can see that the processor works well because  char "." is printed from the loop() on the terminal but nothing regarding the WIFI communication.
I need to wait 3~4 minutes and ask again the IP adress of the WIFI module and I can have an answer from the module ESP8266.
Tested the module at several distances from my BOX -> same thing
Try to do action without activate relay ( prob of parasitics ...)  -> no actions

Another test that i've done is  to replace the ESP8266 module from Olimex with a cheap chinese module and now everything works well without loss of communication....
But the piece of wired are not beautiful on the EVB board !!!

Do you have any idea ?


gerald_Dijon

//my code ( sorry some part are in French language

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

// Constantes Wifi
const char* ssid = "mySSID";
const char* password = "myPASSWORD";

// Variables globales
int relayPin = 5;               // Pin de pilotage du Relay
ESP8266WebServer server(80);    // Instance du serveur Web
String message = "";
int cpt=0;



void handleRoot() {
  Serial.println("HTTP GET /");
  message = "<html><body>";
  message += "<h1>Bloc relais OLIMEX #1</h1>";
    message += "<ul>";
      message += "<li><a href='/on' >Set Relay = ON</a></li>";
      message += "<li><a href='/off'>Set Relay = OFF</a></li>";
      message += "<li><a href='/status'>Actual state of relay ?</a></li>";
    message += "</ul>";
  message += "</body></html>";
  server.send(200, "text/html", message);
}

void handleRelayOn() {
  Serial.println("Mise du Relais -> ON");
  digitalWrite(relayPin, HIGH);
   message = "<html><body>";
   message += "<h1>Relay is ON !</h1>";
    message += "<ul>";
      message += "<li><a href='./off'>Set Relay = OFF</a></li>";
      message += "<li><a href='./'>Root</a></li>";
    message += "</ul>";
  message += "</body></html>";
  server.send(200, "text/html", message);
}

void handleRelayOff() {
  Serial.println("Mise du Relais -> OFF");
  digitalWrite(relayPin, LOW);
   message = "<html><body>";
   message += "<h1>Relay is OFF !</h1>";
    message += "<ul>";
      message += "<li><a href='./on' >Set Relay = ON</a></li>";
      message += "<li><a href='./'>Root</a></li>";
    message += "</ul>";
  message += "</body></html>";
  server.send(200, "text/html", message);
}


void handleStatus() {
  Serial.println("Ask STATUS");
   message = "<html><body>";
   if ( digitalRead(relayPin) == 1 ) {
      message += "<h1>Status = ON</h1>"; 
      message += "<ul>";
        message += "<li><a href='/off'>Mettre relais = OFF</a></li>";
      }
   else {
      message += "<h1>Status = OFF</h1>"; 
      message += "<ul>";
        message += "<li><a href='/on' >Mettre Relais = ON</a></li>";
      }
        message += "<li><a href='./'>Accueil</a></li>";
        message += "</ul>";
  message += "</body></html>";
  server.send(200, "text/html", message);
}

void handleRelayMOn() {
  Serial.println("Mise du Relais -> ON");
  digitalWrite(relayPin, HIGH);
  message = "ON";
  server.send(200, "text/html", message);
}

void handleRelayMOff() {
  digitalWrite(relayPin, LOW);
  message = "OFF";
  server.send(200, "text/html", message);
}

void handleMStatus() {
   if ( digitalRead(relayPin) == 1 ) {
      message = "ON"; 
      }
   else {
      message = "OFF"; 
      }
  server.send(200, "text/html", message);
}

void handleNotFound() {
   message = "<html><body>";
   message += "<h1>Page not Found !</h1>";
   message += "</body></html>";
   server.send(200, "text/html", message);
 
}

//--------------------------
void setup() {
  // Initialisation du port série (pour débugguer)
  Serial.begin(115200);
  Serial.println();
  Serial.println("Starting ...");

  // Initialisation du GPIO5 en sortie (pin 2 du NodeMCU)
  pinMode(relayPin, OUTPUT);

  // Initialisation de la connexion Wifi
  WiFi.begin(ssid, password);
  Serial.println("");

  // Attente de la connexion
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connecté au SSID : ");
  Serial.println(ssid);
  Serial.print("Adresse IP : ");
  Serial.println(WiFi.localIP());

  // Routage des requêtes HTTP
  server.on("/", handleRoot);
  server.on("/on", handleRelayOn);
  server.on("/off", handleRelayOff);
  server.on("/status", handleStatus);
  server.on("/mon", handleRelayMOn);
  server.on("/moff", handleRelayMOff);
  server.on("/mstatus", handleMStatus); 
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("Server HTTP is running");
}

//--------------------------
void loop() {
  delay(10);
  cpt++;
  server.handleClient();
  if ( (cpt % 50) == 0 ) {
      Serial.print(".");
      if ( (cpt % 1000) == 0 ) {
          cpt = 0;
          Serial.println(".");
          }
      }
}
 

gerald_Dijon

Hello,

i've found what is my mistake !

I've changed the configuration of my BOX
Replaced WIFI BG and N by B+G only and everything works well.

I can command the Relay from the DEV CArd