Relais WIFI & ESP8266 version serveur


L'idée est de créer une page Web sur l'ESP8266 avec les informations d'un relais allumé ou eteind. Nous utilisons le module relais WIFI 5V & un ESP8266 version ESP-01.
Pour la programmation de l'ESP8266, il faut un dongle USB comme celui-ci. Prenez soin d'enlever l'ESP et le brancher sur le dongle USB cf page ICI.

L'ESP8266 est un module wifi qui possède une mémoire interne et un microcontrôleur il sera utilisé en fonction serveur.

Des précautions sont à prendre avant d'utiliser ce module.

D'abord le tester vous pouvez avoir les éléments ICI.

La liste des commandes est ICI.

Puis le flasher pour y injecter le scripte. Pour le flasher vous aurez tout ce qu'il faut ICI.

Relais & ESP-01
plus
Relais & ESP-01
plus
Relais & ESP-01

Le scripte ci-dessous est a injecter dans l'ESP8266 à partir du logiciel arduino.

La configuration de cet ESP-01 doit être ainsi.

Configuration de l'ESP8266


L'Arduino doit être alimenté en 3,3 volts minimum.

Le scripte est à télécharger en version serveur ICI



  /*--------------------------------------------------
HTTP 1.1 Webserver for ESP8266 
for ESP8266 adapted Arduino IDE
http://www.esp8266.com/viewtopic.php?p=65572

--------------------------------------------------*/

#include 

const char* ssid     = "FREE";
const char* password = "WIFI";
int ledState = false;
unsigned long ulReqcount;
unsigned long ulReconncount;

byte relON[] = {0xA0, 0x01, 0x01, 0xA2};  //Hex command to send to serial for open relay
byte relOFF[] = {0xA0, 0x01, 0x00, 0xA1}; //Hex command to send to serial for close relay

// Create an instance of the server on Port 80
WiFiServer server(80);

void setup() 
{
  // setup globals
  ulReqcount=0; 
  ulReconncount=0;
 
  // start serial
  Serial.begin(9600);
  delay(1);
  
  // inital connect
  WiFi.mode(WIFI_STA);
  WiFiStart();
}

void WiFiStart()
{
  ulReconncount++;
  
  // 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.println(WiFi.localIP());
}

void loop() 
{
  // check if WLAN is connected
  if (WiFi.status() != WL_CONNECTED)
  {
    WiFiStart();
  }
  
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) 
  {
    return;
  }
  
  // Wait until the client sends some data
  Serial.println("new client");
  unsigned long ultimeout = millis()+250;
  while(!client.available() && (millis()ultimeout) 
  { 
    Serial.println("client connection time-out!");
    return; 
  }
  
  // Read the first line of the request
  String sRequest = client.readStringUntil('\r');
  //Serial.println(sRequest);
  client.flush();
  
  // stop client, if request is empty
  if(sRequest=="")
  {
    Serial.println("empty request! - stopping client");
    client.stop();
    return;
  }
  
  // get path; end of path is either space or ?
  // Syntax is e.g. GET /?pin=MOTOR1STOP HTTP/1.1
  String sPath="",sParam="", sCmd="";
  String sGetstart="GET ";
  int iStart,iEndSpace,iEndQuest;
  iStart = sRequest.indexOf(sGetstart);
  if (iStart>=0)
  {
    iStart+=+sGetstart.length();
    iEndSpace = sRequest.indexOf(" ",iStart);
    iEndQuest = sRequest.indexOf("?",iStart);
    
    // are there parameters?
    if(iEndSpace>0)
    {
      if(iEndQuest>0)
      {
        // there are parameters
        sPath  = sRequest.substring(iStart,iEndQuest);
        sParam = sRequest.substring(iEndQuest,iEndSpace);
      }
      else
      {
        // NO parameters
        sPath  = sRequest.substring(iStart,iEndSpace);
      }
    }
  }
  
  ///////////////////////////////////////////////////////////////////////////////
  // output parameters to serial, you may connect e.g. an Arduino and react on it
  ///////////////////////////////////////////////////////////////////////////////
  if(sParam.length()>0)
  {
    int iEqu=sParam.indexOf("=");
    if(iEqu>=0)
    {
      sCmd = sParam.substring(iEqu+1,sParam.length());
      Serial.println(sCmd);
    }
  }
  
  
  ///////////////////////////
  // format the html response
  ///////////////////////////
  String sResponse,sHeader;
  
  ////////////////////////////
  // 404 for non-matching path
  ////////////////////////////
  if(sPath!="/")
  {
    sResponse="404 Not Found

Not Found

The requested URL was not found on this server.

"; sHeader = "HTTP/1.1 404 Not found\r\n"; sHeader += "Content-Length: "; sHeader += sResponse.length(); sHeader += "\r\n"; sHeader += "Content-Type: text/html\r\n"; sHeader += "Connection: close\r\n"; sHeader += "\r\n"; } /////////////////////// // format the html page /////////////////////// else { ulReqcount++; sResponse = "Demo pour ESP8266 version ESP-01"; sResponse += ""; sResponse += ""; sResponse += "

Demo pour ESP8266 version ESP-01

"; sResponse += "Allumez en cliquant sur le bouton.
"; sResponse += ""; sResponse += "

Funktion 1  

"; ////////////////////// // react on parameters ////////////////////// if (sCmd.length()>0) { // write received command to html page sResponse += "Kommando:" + sCmd + "
"; // switch GPIO if(sCmd.indexOf("FUNCTION1ON")>=0) { Serial.write(relON, sizeof(relON)); // turns the relay ON ledState = false; } else if(sCmd.indexOf("FUNCTION1OFF")>=0) { Serial.write(relOFF, sizeof(relOFF)); // turns the relay OFF ledState = true; } } sResponse += ""; sResponse += "
Aufrufzähler="; sResponse += ulReqcount; sResponse += " - Verbindungszähler="; sResponse += ulReconncount; sResponse += "
"; sResponse += ""; sHeader = "HTTP/1.1 200 OK\r\n"; sHeader += "Content-Length: "; sHeader += sResponse.length(); sHeader += "\r\n"; sHeader += "Content-Type: text/html\r\n"; sHeader += "Connection: close\r\n"; sHeader += "\r\n"; } // Send the response to the client client.print(sHeader); client.print(sResponse); // and stop the client client.stop(); Serial.println("Client disonnected"); } /* test a faire #define RX_PIN 3 // GPIO3 #define TX_PIN 1 // GPIO1 void setup() { // dont Serial.begin(74880)!!!!!!!!!!!!!!!!!!! pinMode(RX_PIN, INPUT); pinMode(TX_PIN, INPUT); } */