Leggere il mac del dispositivo che si collega via wifi al sito dell'arduino

volevo sapere come far sapere all'arduino il mac del dispositivo che si collega al suo sito, per far variare le risposte... (l'arduino è: arduino uno wifi rev 2)...
grazie

(tramite wifi)

arduino uno rev 2 non ha WiFi....

1 Like

scusa, arduino uno wifi rev 2

sistemato

com'è la rete wireless? l'arduino funge da punto di accesso WiFi?

1 Like

è una rete privata, credo di sì (alla seconda domanda), nel senso che una volta collegato, l'arduino risponde alle chiamate che arrivano da altri dispositivi... il codice che ho adesso funziona, ma volevo sostituire l'ip con il mac, in modo che non cambiasse mai... (anche in caso di cambi di rete!)... codice:

/*

  WiFi Web Server LED Blink

  A simple web server that lets you blink an LED via the web.

  This sketch will create a new access point (with no password).

  It will then launch a new server and print out the IP address

  to the Serial monitor. From there, you can open that address in a web browser

  to turn on and off the LED on pin 13.

  If the IP address of your board is yourAddress:

    http://yourAddress/H turns the LED on

    http://yourAddress/L turns it off

  created 25 Nov 2012

  by Tom Igoe

  adapted to WiFi AP by Adafruit

 */
 int comanda_arduino=1;
 int comandabile=1;
 int mostra_info=1;
 int giri=0;
 int potenza=0;
 int valore_ventilatore=0;
 int ventilatore;
#include <CapacitiveSensor.h>
CapacitiveSensor capSensor = CapacitiveSensor(4,2);
#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
int connessi =0;
WiFiServer server(80);
void setup() {

  //Initialize serial and wait for port to open:

  Serial.begin(9600);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for native USB port only

  }

  Serial.println("Access Point Web Server");

  pinMode(5, OUTPUT);      // set the LED pin mode

  // check for the WiFi module:

  if (WiFi.status() == WL_NO_MODULE) {

    Serial.println("Communication with WiFi module failed!");

    // don't continue

    while (true);

  }

  String fv = WiFi.firmwareVersion();

  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {

    Serial.println("Please upgrade the firmware");

  }

  // by default the local IP address of will be 192.168.4.1

  // you can override it with the following:

  // WiFi.config(IPAddress(10, 0, 0, 1));

  // print the network name (SSID);

  Serial.print("Creating access point named: ");

  Serial.println(ssid);

  // Create open network. Change this line if you want to create an WEP network:

  status = WiFi.beginAP(ssid, pass);

  if (status != WL_AP_LISTENING) {

    Serial.println("Creating access point failed");

    // don't continue

    while (true);

  }

  // wait 10 seconds for connection:

  delay(10000);
status = WiFi.beginAP(ssid, pass);
while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
 
  // wait 10 seconds for connection:


  // start the web server on port 80

  server.begin();

  // you're connected now, so print out the status

  printWiFiStatus();
}
IPAddress myIP1 (192,168,1,145);
IPAddress myIParduino (192,168,1,147);  //l'ip dell'arduino, per non essere bloccato in nessun modo...


void loop() {

    

  if(ventilatore==0){
    digitalWrite(5, LOW);
  }
    if(ventilatore==1){
    digitalWrite(5, HIGH);
  }




  
  //analogWrite(5, ventilatore);
  // compare the previous status to the current status

  if (status != WiFi.status()) {

    // it has changed update the variable

    status = WiFi.status();

    if (status == WL_AP_CONNECTED) {

      // a device has connected to the AP

      Serial.println("Device connected to AP");

    } else {

      // a device has disconnected from the AP, and we are back in listening mode

      Serial.println("Device disconnected from AP");

    }

  }



  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,

    Serial.println("new client");           // print a message out the serial port

    String currentLine = "";                // make a String to hold incoming data from the client

    while (client.connected()) {            // loop while the client's connected

      if (client.available()) {             // if there's bytes to read from the client,

        char c = client.read();             // read a byte, then

        Serial.write(c);                    // print it out the serial monitor

        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.

          // that's the end of the client HTTP request, so send a response:
int connessi=client.available();
          if (currentLine.length() == 0) {

            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)

            // and a content-type so the client knows what's coming, then a blank line:

            client.println("HTTP/1.1 200 OK");

            client.println("Content-type:text/html");


            // the content of the HTTP response follows the header:
              //client.println("Refresh: 2");
              client.println();
              
                            
                            //IPAddress myIP1 (192,168,1,145);

if( client.remoteIP() == myIP1 ) {
  client.print("<a href=\"/on\">on</a>-<a href=\"/off\">off</a> valore corrente: ");
                if(ventilatore==1){
  client.print("on");
}
if(ventilatore==0){
  client.print("off");
}
  client.print("<br>");

client.print("comandi avanzati:");
  client.print("<br>");

client.print("   <a href=\"/mostra_info\">mostra_info</a>-<a href=\"/nascondi_info\">nascondi_info</a>");

client.print(" stato corrente: ");

if(mostra_info==1){
  client.print("visibili");
}
if(mostra_info==0){
  client.print("nascoste");
}

  client.print("<br>");


client.print("   <a href=\"/comandabile\">comandabile</a>-<a href=\"/non_comandabile\">non_comandabile</a>");

client.print(" stato corrente: ");

if(comandabile==1){
  client.print("comandabile");
}
if(comandabile==0){
  client.print("non comandabile");
}

  client.print("<br>");

client.print("   <a href=\"/arduino\">arduino</a>-<a href=\"/no_arduino\">no_arduino</a>");

client.print(" stato corrente: ");

if(comanda_arduino==1){
  client.print("comandabile");
}
if(comanda_arduino==0){
  client.print("non comandabile");
}
  

              }
              else{
                             if(mostra_info==1){
              client.print("<a href=\"/on\">on</a>-<a href=\"/off\">off</a> valore corrente: ");
             
//<br>
if(ventilatore==1){
  client.print("on");
}
if(ventilatore==0){
  client.print("off");
}
              }
              }
 


                       client.println();

            // break out of the while loop:

            break;

          }

          else {      // if you got a newline, then clear currentLine:

            currentLine = "";

          }

        }

        else if (c != '\r') {    // if you got anything else but a carriage return character,

          currentLine += c;      // add it to the end of the currentLine

        }

        // Check to see if the client request was "GET /H" or "GET /L":

if( client.remoteIP() == myIParduino ) {
  if(comanda_arduino==1){
              if (currentLine.endsWith("GET /on")) {
ventilatore=1;        
}
        if (currentLine.endsWith("GET /off")) {
ventilatore=0;        
}
  }

}

if( client.remoteIP() == myIP1 ) {
        if (currentLine.endsWith("GET /on")) {
ventilatore=1;        
}
        if (currentLine.endsWith("GET /off")) {
ventilatore=0;        
}
         if (currentLine.endsWith("GET /mostra_info")) {
mostra_info=1;        
}       
        if (currentLine.endsWith("GET /nascondi_info")) {
mostra_info=0;        
}
if (currentLine.endsWith("GET /comandabile")) {
comandabile=1;        
}
if (currentLine.endsWith("GET /non_comandabile")) {
comandabile=0;        
}
if (currentLine.endsWith("GET /arduino")) {
comanda_arduino=1;        
}
if (currentLine.endsWith("GET /no_arduino")) {
comanda_arduino=0;        
}

}
else{
  if(comandabile==1){
    
        if (currentLine.endsWith("GET /on")) {
ventilatore=1;        
}
        if (currentLine.endsWith("GET /off")) {
ventilatore=0;        
}
  }
}




      }

    }

    // close the connection:

    client.stop();

    Serial.println("client disconnected");

  }


}

void printWiFiStatus() {

  // print the SSID of the network you're attached to:

  Serial.print("SSID: ");

  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:

  IPAddress ip = WiFi.localIP();

  Serial.print("IP Address: ");

  Serial.println(ip);

  // print where to go in a browser:

  Serial.print("To see this page in action, open a browser to http://");

  Serial.println(ip);

}

credo di aver risposto anche alla prima domanda, ma non ne sono sicuro...

se hai un router tra i tuoi due arduino, vede gli indirizzi Mac e organizza i frame in base agli indirizzi IP.

non è possibile impostare l'indirizzo IP del server perché dipende dalla rete in cui si sta entrando.

la maggior parte dei router supporta mDNS (bonjour, zeroConf), quindi è probabilmente quello che dovresti guardare. ➜ ESP8266 Webserver: Resolving an address with mDNS - techtutorialsx

in alternativa se gli arduino sono vicini, guarda espnow

1 Like

lo so, per quello volevo cambiare l'IP con il mac...

Non è possibile prelevare il MAC address direttamente dai pacchetti dati TCP o UDP perché è un'informazione che non è presente (si ferma al livello del router).

Dovresti implementare un protocollo tuo che prevede la registrazione del client in modo univoco.

1 Like

ok, ma sull'arduino vero?

Un protocollo di comunicazione è per definizione multidirezionale, quindi sia lato server (Arduino) che lato client.

1 Like

ok! cerco su internet, se ci sono degli esempi!

Guarda che fai molto prima a NON usare il DHCP e mettere un IP fisso per i device che vuoi identificare così quello non cambia mai.

Normalmente, nei router, puoi scegliere il range di IP che vengono usati dal DHCP e quelli che non vengono usati. Metti tutti gli IP fissi nell'intervallo che è fuori dal range del DHCP e così potrai avere sia i tuoi devices sempre identificabili che altri oggetti che si collegano saltuariamente e che usano il DHCP.

Guglielmo

1 Like

Dubito ne troverai pronti all'uso...
Forse ti conviene seguire il consiglio di Guglielmo, ammesso che il client sia sempre noto a priori.

1 Like

ok, appena riesco provo

ok :grinning:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.