Interrupteur connecté Itead SonOff

591d6968b0e599.02787060

Étapes pour programmer ce module :

1°) Souder des broches :

Ouvrir le module et souder 5 broches qui permettront d’accéder aux broches TX et RX de l’ESP et aussi à son alimentation 3v3 et GND

SonOfffJ1

SONOFF J1 USB TO SERIAL
1 : VCC-3V3 3V3
2 : E-RX TXD
3 : E-TX RXD
4 : GND GND
5 : GPIO14 Not Connected

36414614690_374cbaf4af_o

2°) Connectez un module RS232 à une carte Arduino pour le programmer :

Bien relier le 3.3v et appuyer sur le switch pour placer l’ESP en mode programmation.

  1. Ne pas brancher l’alimentation 220v
  2. Maintenir le switch appuyé
  3. Connecter le module USB to Serial au PC
  4. Relachez le switch
  5. Utilisez l’interface Arduino pour télécharger un sketch

Les connexions sont les suivantes :

Fonction GPIO
Button 0
Relay 12
Green LED 13
Spare (pin 5) 14

Tests :

 // digital pin 2 has a pushbutton attached to it. Give it a name:  
 #define pushButton 0  
 #define greenLed  13  
 #define relay   12  
 int bRelay;  
 // the setup routine runs once when you press reset:  
 void setup() {  
  // initialize serial communication at 9600 bits per second:  
  Serial.begin(9600);  
  pinMode(pushButton, INPUT);  
  digitalWrite(pushButton, 1);    
  pinMode(greenLed, OUTPUT);  
  pinMode(relay, OUTPUT);  
  digitalWrite(greenLed, HIGH);  
  digitalWrite(relay, LOW);  
  bRelay=0;  
 }  
 void loop() {  
  int buttonState = digitalRead(pushButton);  
  delay(1);    // delay in between reads for stability  
  buttonState = digitalRead(pushButton);  
  if (buttonState != 1) {  
   if (bRelay = 0)  
   { Serial.println("RELAY UP !");  
    digitalWrite(greenLed, LOW);  
    digitalWrite(relay, HIGH); bRelay = 1; delay(50); } else { Serial.println("RELAY DOWN !"); digitalWrite(greenLed, HIGH); digitalWrite(relay, LOW); bRelay = 0; delay(50); } } } 

Connexion et test d’un buzzer à la PIN 5 via un NE555 (cf. page buzzer)

Test de serveur web

 #include   
  //-- Envoi commandes à l'ESP : http://Adresse_ip_esp/RON ou ROFF  
 //-- Ou appui sur le bouton  
 //-- Définitions paramétres Wifi et serveur  
 const char* ssid = "Votre_SSID";  
 const char* password = "xxxxxxxxxxxx";  
 #define pushButton 0  
 #define greenLed  13  
 #define relay   12  
 //--- WEB SETTINGS  
 WiFiServer serverHTTP(80);  
 WiFiClient clientHTTP;  
 unsigned int cpt = 0, bRelay;  
 boolean takeRqte = false;  
 String requete = "", ResRequete = "", sMessage = "";  
 //*** Setup Général ***  
 void setup() {  
  ///////////  
  pinMode(pushButton, INPUT);  
  digitalWrite(pushButton, 1);  
  pinMode(greenLed, OUTPUT);  
  pinMode(relay, OUTPUT);  
  digitalWrite(greenLed, HIGH);  
  digitalWrite(relay, LOW);  
  bRelay = 0;  
  ////////////  
  Serial.begin(9600);  
  ///////////////////  
  delay(10);  
  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");  
  Serial.println("IP address: ");  
  Serial.println(WiFi.localIP());  
  delay(1000);  
  //-- Start the HTTP server  
  serverHTTP.begin();  
  //-- Blink  
  digitalWrite(greenLed, LOW);  
  delay(1000);  
  digitalWrite(greenLed, HIGH);  
  delay(1000);  
  digitalWrite(greenLed, LOW);  
  delay(1000);  
  digitalWrite(greenLed, HIGH);  
 }  
 //** Programme ***  
 void loop() {  
  /////////////////////  
  int buttonState = digitalRead(pushButton);  
  delay(1);    // delay in between reads for stability  
  buttonState = digitalRead(pushButton);  
  if (buttonState != 1) {  
   if (bRelay = 0)  
   { Serial.println("RELAY UP !");  
    digitalWrite(greenLed, LOW);  
    digitalWrite(relay, HIGH);  
    bRelay = 1;  
   }  
   else  
   { Serial.println("RELAY DOWN !");  
    digitalWrite(greenLed, HIGH);  
    digitalWrite(relay, LOW);  
    bRelay = 0;  
   }  
  }  
  //-- Test si WIFI toujours disponible  
  if (WiFi.status() != WL_CONNECTED) {  
   Serial.println("...WIFI LOST...");  
   delay(1000);  
   ESP.reset(); //-- On reboot  
  }  
  ///////////////////////////////  
  /// ANALYSE WEB ///////////////  
  clientHTTP = serverHTTP.available();  
  if (clientHTTP) {  
   //-- Lecture commande reçue à partir du WEB  
   boolean currentLineIsBlank = true;  
   while (clientHTTP.connected()) {  
    if (clientHTTP.available()) {  
     char c = clientHTTP.read();  
     if (cpt == 4 && c == '/') {  
      takeRqte = true;  
     }  
     if (c == ' ') {  
      takeRqte = false;  
     }  
     if (takeRqte == true && cpt != 4)  
     {  
      requete = requete + c;  
     }  
     cpt++;  
     if (c == '\n' && currentLineIsBlank) {  
      //// Traitement requete  
      requete.trim();  
      requete.toUpperCase();  
      Serial.println("Commande recue : " + requete);  
      sMessage = "Commande inconnue";  
      //-- Actions en fonction de la requete  
      if (requete.equals("RON"))  
      { digitalWrite(greenLed, LOW);  
       digitalWrite(relay, HIGH);  
       sMessage = "RELAY UP !";  
       bRelay = 1;  
      }  
      else if (requete.equals("ROFF"))  
      { sMessage = "RELAY DOWN !";  
       digitalWrite(greenLed, HIGH);  
       digitalWrite(relay, LOW);  
       bRelay = 0;  
      }  
      //////--- Retour HTTP  
      Serial.println(sMessage);  
      /////  
      // send a standard http response header  
      clientHTTP.println("HTTP / 1.1 200 OK");  
      clientHTTP.println("Content - Type: text / html");  
      clientHTTP.println("Connnection: close");  
      clientHTTP.println();  
      clientHTTP.println("");  
      clientHTTP.println("");  
      //http://www.startyourdev.com/html/tag-html-balise-b  
      clientHTTP.println("" + sMessage + "");  
      clientHTTP.println("");  
      break;  
     }  
     if (c == '\n') {  
      currentLineIsBlank = true;  
     }  
     else if (c != '\r') {  
      currentLineIsBlank = false;  
     }  
    }  
   }  
   // give the web browser time to receive the data  
   delay(1);  
   requete = "";  
   takeRqte = false;  
   cpt = 0;  
  }  
 }  

Client MQTT

 #include <ESP8266WiFi.h>  
 #include <PubSubClient.h>  
 //-- Définitions paramétres Wifi et serveur  
 const char* ssid = "xxxx";  
 const char* password = "zzzz";  
 const char* mqtt_server = "192.168.0.20";  
 //-- Constantes Domoticz  
 #define idxActivLight  "100"  
 //-- Pour Led ESP  
 #define pushButton 0  
 #define greenLed  13  
 #define relay   12  
 int bRelay;  
 //-- Définition Client Wifi et MQTT  
 WiFiClient espClient;  
 PubSubClient client(espClient);  
 char msgToDomoticZ[120];  
 const char *topicin = "domoticz/in";  
 const char *topicout = "domoticz/out";  
 const char *msgContact  = "{\"command\": \"switchlight\", \"idx\": ";  
 const char *msgContactOn = ", \"switchcmd\": \"On\", \"level\": 100}";  
 const char *msgContactOff = ", \"switchcmd\": \"Off\", \"level\": 100}";  
 //"{\"command\": \"switchlight\", \"idx\": 19, \"switchcmd\": \"On\", \"level\": 100}";  
 String s_cmd;  
 //** MQTT CALLBACK ***  
 //-- idx 100 = Capteur Domoticz AA_ACTIV_LIGHT  
 void callback(char* topic, byte* payload, unsigned int length) {  
  String sMsg;  
  for (int i = 0; i < length; i++) {  
   sMsg = sMsg + (char)payload[i];  
  }  
  //Serial.println(sMsg);  
  //--  
  if (sMsg.indexOf("AA_ACTIV_LIGHT") > 0)  
  {  
   //Serial.println(sMsg);  
   if (sMsg.indexOf("nvalue\" : 1") > 0)  
   {  
    Serial.println("RELAY UP !");  
    digitalWrite(greenLed, LOW);  
    digitalWrite(relay, HIGH);  
    bRelay = 1;  
    delay(50);  
    return;  
   }  
   else  
   {  
    Serial.println("RELAY DOWN !");  
    digitalWrite(greenLed, HIGH);  
    digitalWrite(relay, LOW);  
    bRelay = 0;  
    delay(50);  
    return;  
   }  
  }  
  //  
  else if (sMsg.indexOf("NodeRedDate") > 0)  
  { //Serial.println(sMsg);  
   //-- Extraction de la zone Time_Date  
   int nValTemps = sMsg.indexOf("svalue1") + 12;  
   int nFinValTemps = sMsg.indexOf('\"', nValTemps);  
   String sValTemps = sMsg.substring(nValTemps, nFinValTemps);  
   Serial.println("TIME :" + sValTemps);  
   return;  
  }  
 }  
 ///////////////////  
 void setup_wifi() {  
  delay(10);  
  // We start by connecting to a WiFi network  
  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");  
  Serial.println("IP address: ");  
  Serial.println(WiFi.localIP());  
  delay(1000);  
 }  
 //*** Gestion perte serveur MQTT ****  
 void reconnect() {  
  // Loop until we're reconnected  
  while (!client.connected()) {  
   Serial.print("Attempting MQTT connection...");  
   // Attempt to connect  
   if (client.connect("ESP8266Client")) {  
    Serial.println("connected");  
    //--Resubscribe  
    client.subscribe("domoticz\/in");  
   } else {  
    Serial.print("failed, rc=");  
    Serial.print(client.state());  
    Serial.println(" try again in 5 seconds");  
    // Wait 5 seconds before retrying  
    delay(5000);  
   }  
  }  
 }  
 //*** Setup Général ***  
 void setup() {  
  ///////////  
  pinMode(pushButton, INPUT);  
  digitalWrite(pushButton, 1);  
  pinMode(greenLed, OUTPUT);  
  pinMode(relay, OUTPUT);  
  digitalWrite(greenLed, HIGH);  
  digitalWrite(relay, LOW);  
  bRelay = 0;  
  ////////////  
  Serial.begin(9600);  
  setup_wifi();  
  client.setServer(mqtt_server, 1883);  
  client.setCallback(callback);  
  if (client.connect("ESP8266Client")) {  
   Serial.println("connected");  
   // ... Subscribe  
   client.subscribe("domoticz\/out");  
   //-- Blink  
   digitalWrite(greenLed, LOW);  
   delay(1000);  
   digitalWrite(greenLed, HIGH);  
   delay(1000);  
   digitalWrite(greenLed, LOW);  
   delay(1000);  
   digitalWrite(greenLed, HIGH);  
  }  
  bRelay = 0;  
 }  
 //** Programme ***  
 void loop() {  
  //-- Test si WIFI toujours disponible  
  if (WiFi.status() != WL_CONNECTED) {  
   Serial.println("...WIFI LOST...");  
   delay(1000);  
   ESP.reset(); //-- On reboot  
  }  
  //-- Test si MQTT est toujours disponible  
  if (!client.connected()) {  
   reconnect();  
  }  
  //-- Lecture MQTT  
  client.loop();  
  /////////////////////  
  int buttonState = digitalRead(pushButton);  
  delay(1);    // delay in between reads for stability  
  buttonState = digitalRead(pushButton);  
  if (buttonState != 1) {  
   if (bRelay == 0)  
   { Serial.println("RELAY UP !");  
    digitalWrite(greenLed, LOW);  
    digitalWrite(relay, HIGH);  
    bRelay = 1;  
    strcpy(msgToDomoticZ, msgContact);  
    strcat(msgToDomoticZ, idxActivLight);  
    strcat(msgToDomoticZ, msgContactOn);  
    client.publish("domoticz\/in", msgToDomoticZ);  
    delay(50);  
   }  
   else  
   { Serial.println("RELAY DOWN !");  
    digitalWrite(greenLed, HIGH);  
    digitalWrite(relay, LOW);  
    bRelay = 0;  
    strcpy(msgToDomoticZ, msgContact);  
    strcat(msgToDomoticZ, idxActivLight);  
    strcat(msgToDomoticZ, msgContactOff);  
    client.publish("domoticz\/in", msgToDomoticZ);  
    delay(50);  
   }  
  }  
  ///////////////////  
 }  

Serveur WEB et MQTT

 #include <ESP8266WiFi.h>  
 #include <PubSubClient.h>  
 //-- Définitions paramétres Wifi et serveur  
 const char* ssid = "xxx";  
 const char* password = "zzz";  
 const char* mqtt_server = "192.168.0.20";  
 //--- WEB SETTINGS  
 WiFiServer serverHTTP(80);  
 WiFiClient clientHTTP;  
 //-- Constantes Domoticz  
 #define idxActivLight  "100"  
 //-- Pour Led ESP  
 #define pushButton 0  
 #define greenLed  13  
 #define relay   12  
 unsigned int cpt = 0, bRelay;  
 boolean takeRqte = false;  
 String requete = "", ResRequete = "", sMessage = "";  
 //-- Définition Client Wifi et MQTT  
 WiFiClient espClient;  
 PubSubClient client(espClient);  
 char msgToDomoticZ[120];  
 const char *topicin = "domoticz/in";  
 const char *topicout = "domoticz/out";  
 const char *msgContact  = "{\"command\": \"switchlight\", \"idx\": ";  
 const char *msgContactOn = ", \"switchcmd\": \"On\", \"level\": 100}";  
 const char *msgContactOff = ", \"switchcmd\": \"Off\", \"level\": 100}";  
 //"{\"command\": \"switchlight\", \"idx\": 19, \"switchcmd\": \"On\", \"level\": 100}";  
 String s_cmd;  
 //** MQTT CALLBACK ***  
 //-- idx 100 = Capteur Domoticz AA_ACTIV_LIGHT  
 void callback(char* topic, byte* payload, unsigned int length) {  
  String sMsg;  
  for (int i = 0; i < length; i++) {  
   sMsg = sMsg + (char)payload[i];  
  }  
  //Serial.println(sMsg);  
  //--  
  if (sMsg.indexOf("AA_ACTIV_LIGHT") > 0)  
  {  
   //Serial.println(sMsg);  
   if (sMsg.indexOf("nvalue\" : 1") > 0)  
   {  
    Serial.println("RELAY UP !");  
    digitalWrite(greenLed, LOW);  
    digitalWrite(relay, HIGH);  
    bRelay = 1;  
    delay(50);  
    return;  
   }  
   else  
   {  
    Serial.println("RELAY DOWN !");  
    digitalWrite(greenLed, HIGH);  
    digitalWrite(relay, LOW);  
    bRelay = 0;  
    delay(50);  
    return;  
   }  
  }  
  //  
  else if (sMsg.indexOf("NodeRedDate") > 0)  
  { //Serial.println(sMsg);  
   //-- Extraction de la zone Time_Date  
   int nValTemps = sMsg.indexOf("svalue1") + 12;  
   int nFinValTemps = sMsg.indexOf('\"', nValTemps);  
   String sValTemps = sMsg.substring(nValTemps, nFinValTemps);  
   Serial.println("TIME :" + sValTemps);  
   return;  
  }  
 }  
 ////////////////  
 //** Setup Wifi ****  
 void setup_wifi() {  
  delay(10);  
  // We start by connecting to a WiFi network  
  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");  
  Serial.println("IP address: ");  
  Serial.println(WiFi.localIP());  
  delay(1000);  
 }  
 //*** Gestion perte serveuir MQTT ****  
 void reconnect() {  
  // Loop until we're reconnected  
  while (!client.connected()) {  
   Serial.print("Attempting MQTT connection...");  
   // Attempt to connect  
   if (client.connect("ESP8266Client")) {  
    Serial.println("connected");  
    //--Resubscribe  
    // ... Subscribe  
    client.subscribe("domoticz\/out");  
   } else {  
    Serial.print("failed, rc=");  
    Serial.print(client.state());  
    Serial.println(" try again in 5 seconds");  
    // Wait 5 seconds before retrying  
    delay(5000);  
   }  
  }  
 }  
 //*** Setup Général ***  
 void setup() {  
  ///////////  
  pinMode(pushButton, INPUT);  
  digitalWrite(pushButton, 1);  
  pinMode(greenLed, OUTPUT);  
  pinMode(relay, OUTPUT);  
  digitalWrite(greenLed, HIGH);  
  digitalWrite(relay, LOW);  
  bRelay = 0;  
  ////////////  
  Serial.begin(9600);  
  setup_wifi();  
  client.setServer(mqtt_server, 1883);  
  client.setCallback(callback);  
  if (client.connect("ESP8266Client")) {  
   Serial.println("connected");  
   // ... Subscribe  
   client.subscribe("domoticz\/out");  
   //-- Blink  
   digitalWrite(greenLed, LOW);  
   delay(1000);  
   digitalWrite(greenLed, HIGH);  
   delay(1000);  
   digitalWrite(greenLed, LOW);  
   delay(1000);  
   digitalWrite(greenLed, HIGH);  
  }  
  bRelay = 0;  
  //-- Start the HTTP server  
  serverHTTP.begin();  
 }  
 //** Programme ***  
 void loop() {  
  //-- Test si WIFI toujours disponible  
  if (WiFi.status() != WL_CONNECTED) {  
   Serial.println("...WIFI LOST...");  
   delay(1000);  
   ESP.reset(); //-- On reboot  
  }  
  //-- Test si MQTT est toujours disponible  
  if (!client.connected()) {  
   reconnect();  
  }  
  //-- Lecture MQTT  
  client.loop();  
  /////////////////////  
  int buttonState = digitalRead(pushButton);  
  delay(1);    // delay in between reads for stability  
  buttonState = digitalRead(pushButton);  
  if (buttonState != 1) {  
   if (bRelay == 0)  
   { Serial.println("RELAY UP !");  
    digitalWrite(greenLed, LOW);  
    digitalWrite(relay, HIGH);  
    bRelay = 1;  
    strcpy(msgToDomoticZ, msgContact);  
    strcat(msgToDomoticZ, idxActivLight);  
    strcat(msgToDomoticZ, msgContactOn);  
    client.publish("domoticz\/in", msgToDomoticZ);  
    delay(50);  
   }  
   else  
   { Serial.println("RELAY DOWN !");  
    digitalWrite(greenLed, HIGH);  
    digitalWrite(relay, LOW);  
    bRelay = 0;  
    strcpy(msgToDomoticZ, msgContact);  
    strcat(msgToDomoticZ, idxActivLight);  
    strcat(msgToDomoticZ, msgContactOff);  
    client.publish("domoticz\/in", msgToDomoticZ);  
    delay(50);  
   }  
  }  
  ///////////////////  
  ///////////////////////////////  
  /// ANALYSE WEB ///////////////  
  clientHTTP = serverHTTP.available();  
  if (clientHTTP) {  
   //-- Lecture commande reçue à partir du WEB  
   boolean currentLineIsBlank = true;  
   while (clientHTTP.connected()) {  
    if (clientHTTP.available()) {  
     char c = clientHTTP.read();  
     if (cpt == 4 && c == '/') {  
      takeRqte = true;  
     }  
     if (c == ' ') {  
      takeRqte = false;  
     }  
     if (takeRqte == true && cpt != 4)  
     {  
      requete = requete + c;  
     }  
     cpt++;  
     if (c == '\n' && currentLineIsBlank) {  
      //// Traitement requete  
      requete.trim();  
      requete.toUpperCase();  
      Serial.println("Commande recue : " + requete);  
      sMessage = "Commande inconnue";  
      //-- Actions en fonction de la requete  
      if (requete.equals("RON"))  
      { sMessage = "RELAY UP !";  
       digitalWrite(greenLed, LOW);  
       digitalWrite(relay, HIGH);  
       bRelay = 1;  
       strcpy(msgToDomoticZ, msgContact);  
       strcat(msgToDomoticZ, idxActivLight);  
       strcat(msgToDomoticZ, msgContactOn);  
       client.publish("domoticz\/in", msgToDomoticZ);  
       delay(50);  
      }  
      else if (requete.equals("ROFF"))  
      { sMessage = "RELAY DOWN !";  
       digitalWrite(greenLed, HIGH);  
       digitalWrite(relay, LOW);  
       bRelay = 0;  
       strcpy(msgToDomoticZ, msgContact);  
       strcat(msgToDomoticZ, idxActivLight);  
       strcat(msgToDomoticZ, msgContactOff);  
       client.publish("domoticz\/in", msgToDomoticZ);  
       delay(50);  
      }  
      //////--- Retour HTTP  
      Serial.println(sMessage);  
      /////  
      // send a standard http response header  
      clientHTTP.println("HTTP / 1.1 200 OK");  
      clientHTTP.println("Content - Type: text / html");  
      clientHTTP.println("Connnection: close");  
      clientHTTP.println();  
      clientHTTP.println("<!DOCTYPE HTML>");  
      clientHTTP.println("<html>");  
      //http://www.startyourdev.com/html/tag-html-balise-b  
      clientHTTP.println("<b>" + sMessage + "</b>");  
      clientHTTP.println("</html>");  
      break;  
     }  
     if (c == '\n') {  
      currentLineIsBlank = true;  
     }  
     else if (c != '\r') {  
      currentLineIsBlank = false;  
     }  
    }  
   }  
   // give the web browser time to receive the data  
   delay(1);  
   requete = "";  
   takeRqte = false;  
   cpt = 0;  
  }  
  ////////////////////  
 }  

Références :

http://www.esp8266.com/viewtopic.php?f=29&t=2153

https://www.fais-le-toi-meme.fr/fr/electronique/tutoriel/programmes-arduino-executes-sur-esp8266-arduino-ide)

https://www.itead.cc/sonoff-wifi-wireless-switch.htmlhttps://www.itead.cc/sonoff-wifi-wireless-switch.html

Manuel : https://www.itead.cc/blog/user-guide-for-sonoff-slampher

Wiki : https://www.itead.cc/wiki/Sonoff_TH_10/16

Publicités
Articles récents
Commentaires récents
fatima sur Bienvenue !
AdminDroid sur Bienvenue !
fatima sur Bienvenue !
Archives
Catégories
%d blogueurs aiment cette page :