Nodemcu master/Slave

Saluti a tutta la board,
avendo intenzione di automatizzare la sole luci (per ora) della mia abitazione (visto che i ragazzi le lasciano abitualmente acese) ho installato, attualmente un pilota per una sola luce, un nodemcu che gestisce l’accensione sia tramite il pulsante fisico che tramite attivazione vocale con Alexa tramite sinric.
Prima di procedere con tutto il resto di casa, e visto che è sviluppata su tre piani con circa 25 luci da comandare, vorrei ottimizzare il tutto con un solo nodemcu master che abbia installato sinric, e gli altri 3 o 4 in modalità slave che vengono comandati dal master.
Premetto che ovviamente tutta la mia abitazione è cablata e ha un wifi diverso per ogni piano
Potreste cortesemente indicarmi se è fattibile e quali librerie potrei usare e come ? :smiley:
Grazie per l’attenzione

scusate per la richiesta veramente elementare, ma sono agli inizi con questa piattaforma e mi sembrava un problema insormontabile comunque, anche per non lasciare il post vuoto, pubblico il codice (trovato in rete ed adattato) di quanto avevo richiesto per chi come me si trovasse in questa situazione;

Lato Client;

#include <SPI.h>
#include <ESP8266WiFi.h>


char ssid[] = "xxxxxxxx";               // SSID of your home WiFi
char pass[] = "xxxxxxxxxxxxx";    // password of your home WiFi

unsigned long askTimer = 0;

IPAddress server(192,168,1,85);       // IP address del server
WiFiClient client;

void setup() {
  Serial.begin(115200);               // only for debug
  WiFi.begin(ssid, pass);             // connects to the WiFi router
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
 Serial.print("Connessione avvenuta");
}

void loop () {
  client.connect(server, 80);   // Connection to the server
  Serial.println(".");
  client.println("Ciao server!\r");  // sends the message to the server
  String answer = client.readStringUntil('\r');   // receives the answer from the sever
  Serial.println("Dal server: " + answer);
  client.flush();
  delay(2000);                  // client will trigger the communication after two seconds
}

Lato server;

#include <SPI.h>
#include <ESP8266WiFi.h>

char ssid[] = "xxxxxxxxxxxxxxxx";               // SSID of your home WiFi
char pass[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx";    // password of your home WiFi
WiFiServer server(80);                    

IPAddress ip(192, 168, 1, 85);            // IP address of the server
IPAddress gateway(192,168,1,254);           // gateway of your network
IPAddress subnet(255,255,255,0);          // subnet mask of your network

void setup() {
  Serial.begin(115200);                   // only for debug
  WiFi.config(ip, gateway, subnet);       // forces to use the fix IP
  WiFi.begin(ssid, pass);                 // connects to the WiFi router
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
   Serial.print("Connessione avvenuta");
  server.begin();                        

}

void loop () {
  WiFiClient client = server.available();
  if (client) {
    if (client.connected()) {
      Serial.println(".");
      // ricezione messaggio dal client
      String request = client.readStringUntil('\r');    
      Serial.print("Dal client: "); Serial.println(request);
      client.flush();
      client.println("Risposta verso client\r"); // sends the answer to the client
    }
    client.stop();                // tarminates the connection with the client
  }
}

Saluti

ATTILA666999:
Premetto che ovviamente tutta la mia abitazione è cablata ...

... prima che io possa permettere il proseguire di questa discussione, devi chiarire molto bene il punto che è sopra.

Cosa intendi per "cablata" (spiega in dettaglio)? Che tipo di contatti hai dove ti puoi collegare? A che tensione devi lavorare?

Attendo la tua risposta prima di decidere, grazie.

Guglielmo

Per cablata intendevo che ho un switch su tutti i livelli (oltre anche alla wifi che dicevo) quindi volevo far capire che non avevo problemi di portata wifi e dove mancherebbe potrei mettere un access point.
Nel dettaglio, cercando di spiegare al meglio, volevo automatizzare tutte le luci di casa, e gestire vari scenari con alexa tramite sinric, quindi mandando un comando vocale ad alexa del tipo "buonanotte" lei(lui ?) invia tramite sinric il comando di spengere tutte le luci, accendere per 5 minuti solo quelle che occorrono per raggiungere il letto e poi spengere anche quelle , inserire il perimetrale e .. buonanotte :slight_smile: (non dite che sono un maniaco perchè ne sono consapevole :slight_smile:
Ad oggi questo è il punto;

  • Ho installato un nodemcu di test per la gestione di una sola luce sia tramite pulsante sul muro che tramite sinric
  • Devo sostituire tutti i frutti (interruttori) cambiandoli con quelli a pulsante, e rifare l'infilaggio dalla scatola di derivazione per portare i contatti (a 5 Volt) dei pulsanti sulle entrate del nodemcu e ovviamente le luci sui rele dei moduli che ho gia acquistato
  • Ho integrato i codici che ho pubblicato in questo modo;
    Lato server;
#include <SPI.h>
#include <ESP8266WiFi.h>

char ssid[] = "xxxxxxxxxx";               // SSID of your home WiFi
char pass[] = "xxxxxxxxxx";               // password of your home WiFi
WiFiServer server(80);                    

IPAddress ip(192, 168, 1, 33);            // IP address of the server
IPAddress gateway(192,168,1,254);           // gateway of your network
IPAddress subnet(255,255,255,0);          // subnet mask of your network

void setup() {
  Serial.begin(115200);                   // only for debug
  WiFi.config(ip, gateway, subnet);       // forces to use the fix IP
  WiFi.begin(ssid, pass);                 // connects to the WiFi router
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
   Serial.print("Connessione avvenuta");
  server.begin();                        
  // Print the IP address
  Serial.print(WiFi.localIP());
}

void loop () {
  WiFiClient client = server.available();
  if (client) {
    if (client.connected()) {
      Serial.println(".");
      // ricezione messaggio dal client
      String request = client.readStringUntil('\r');  
      String n_rele=request.substring(0,1);
      String n_attiva=request.substring(1.1);
      //Serial.println(n_rele);
      //Serial.println(n_attiva);


         if (n_rele == "1") {Serial.println("ok attivo rele n° 1");
            if (n_attiva == "1") Serial.println("accendo rele n° 1");
            if (n_attiva == "0") Serial.println("spengo rele n° 1");
            client.println("stato rele n° 1 = stato");
            }
            
         if (n_rele == "2") {Serial.println("ok attivo rele n° 2");
            if (n_attiva == "1") Serial.println("accendo rele n° 2");
            if (n_attiva == "0") Serial.println("spengo rele n° 2");
            client.println("stato rele n° 2 = stato");
            }
            
        if (n_rele == "3") {Serial.println("ok attivo rele n° 3");
            if (n_attiva == "1") Serial.println("accendo rele n° 3");
            if (n_attiva == "0") Serial.println("spengo rele n° 3");
            client.println("stato rele n° 3 = stato");
            }
            
         if (n_rele == "4") {Serial.println("ok attivo rele n° 4");
            if (n_attiva == "1") Serial.println("accendo rele n° 4");
            if (n_attiva == "0") Serial.println("spengo rele n° 4");
            client.println("stato rele n° 4 = stato");
            }


       
      client.stop();  
      client.flush();      
          

  
  
}

Lato client;

#include <SPI.h>
#include <ESP8266WiFi.h>

int buttonPin1 = 16;    
int buttonPin2 =  5;
int buttonPin3 =  4;
int buttonPin4 =  14;
int buttonPin5 =  12;
int buttonPin6 =  13;
int buttonPin7 =  15;



int buttonState1 = 0;         
int buttonState2 = 0;
int buttonState3 = 0; 
int buttonState4 = 0; 
int buttonState5 = 0; 
int buttonState6 = 0; 
int buttonState7 = 0;  


char ssid[] = "xxxxxxxx";               // SSID of your home WiFi
char pass[] = "xxxxxxxx";    // password of your home WiFi

unsigned long askTimer = 0;

IPAddress server(192,168,1,33);       // IP address del server
WiFiClient client;

void setup() {
  Serial.begin(115200);               // only for debug
  WiFi.begin(ssid, pass);             // connects to the WiFi router
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
 Serial.print("Connessione avvenuta");
   // Print the IP address
  Serial.print(WiFi.localIP());
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  pinMode(buttonPin7, INPUT);
  
}

void loop () {
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);
  buttonState6 = digitalRead(buttonPin6);
  buttonState7 = digitalRead(buttonPin7);




  
  if (buttonState1 == HIGH) {
    client.connect(server, 80);   // Connection to the server
    delay(100);
    client.println("11");
    Serial.println("11");
    String answer = client.readStringUntil('\r');   // receives the answer from the sever
    Serial.println("Dal server: " + answer);
   } 
  if (buttonState2 == HIGH) {
    client.connect(server, 80);   // Connection to the server
    delay(100);
    client.println("21");
    Serial.println("21");
    String answer = client.readStringUntil('\r');   // receives the answer from the sever
    Serial.println("Dal server: " + answer);
  } 
    if (buttonState3 == HIGH) {
    client.connect(server, 80);   // Connection to the server
    delay(100);
    client.println("31");
    Serial.println("31");
    String answer = client.readStringUntil('\r');   // receives the answer from the sever
    Serial.println("Dal server: " + answer);
  } 
    if (buttonState4 == HIGH) {
    client.connect(server, 80);   // Connection to the server
    delay(100);
    client.println("41");
    Serial.println("41");
    String answer = client.readStringUntil('\r');   // receives the answer from the sever
    Serial.println("Dal server: " + answer);
   } 
    if (buttonState5 == HIGH) {
    client.connect(server, 80);   // Connection to the server
    delay(100);
    client.println("10");
    Serial.println("10");
    String answer = client.readStringUntil('\r');   // receives the answer from the sever
    Serial.println("Dal server: " + answer);
  } 
    if (buttonState6 == HIGH) {
    client.connect(server, 80);   // Connection to the server
    delay(100);
    client.println("20");
    Serial.println("20");
    String answer = client.readStringUntil('\r');   // receives the answer from the sever
    Serial.println("Dal server: " + answer);
  } 
    if (buttonState7 == HIGH) {
    client.connect(server, 80);   // Connection to the server
    delay(100);
    client.println("30");
    Serial.println("30");
     String answer = client.readStringUntil('\r');   // receives the answer from the sever
    Serial.println("Dal server: " + answer);
  } 

}

Ovviamente il codice è solo un "prototipo" per il test di tutti i componenti che occorreranno alla realizzazione, ma credo che ora sono pronto cominciare la realizzazione.
l'antifurto lo inserisco sfruttando un modulo internet (questo Modulo wifiche avevo già installato e oltre a vari oggetti gestivo anche l'allarme tramite una stringa http

A seguire vorrei poi realizzare una pagina web per riassumere lo stato di tutti il sistema (luci e altro)
Scusate la prolissità e la sicura non chiarezza di quanto esposto
Saluti

p.s. scusate ma va prima client.stop(); e poi client.flush(); o viceversa ? in rete ci sono opinioni contrastanti

Purtroppo, come dovresti sapere, qui NON è permesso parlare di modifiche all'iimpianto elettrico (cosa che tu stai praticamente realizzando), per questo ti chiedevo cosa intendessi per "cablato".

In pratica, se un tuo elettricista di fiducia ti avesse fatto lui tutto l'impianto domotico "certificato" a norme e ti avesse dato dei punti di controllo in "bassissima tensione" da cui tu potevi effettuare alcune operazioni, allora ne avremmo potuto parlare (... non avresti violato la sua "certificazione" dell'impianto e non avresti messo le mani nella rete a 230V), mentre, dato che tu stai mettendo da solo relè e quant'altro nel tuo impianto ... la cosa NON è permessa (vd. REGOLAMENTO al punto 15 e suoi sottopunti) per cui ... debbo purtroppo chiudere il thread. :confused:

Guglielmo