Connect the Raspberry Pi to the Arduino directly via a LAN cable

i have mosquito broker on my raspberry pi and i want connect with arduino with cable lan using mqtt protocol

i use : arduino nano with enc28j60 module

"I don't want to use a router or switch"

Here is my code:

#include <SPI.h>
#include <UIPEthernet.h> // for the module ENC28J60 
#include <PubSubClient.h>  

const char* mqtt_server = "192.168.1.18"; // Raspberry
const int mqtt_port = 1883; 

EthernetClient ethClient;
PubSubClient client(ethClient);
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
#define LED_PIN 6
unsigned long lastMsg = 0;

int sensor1 = 0;   
int sensor2 = 0; 
int sensor3 = 0;  
int command1 =0;    

const char* sensor1_topic= "Temp";  
const char*  sensor2_topic="Humid"; 
const char*  sensor3_topic="CO2";    
const char* command1_topic="Room/led";  

void setup() {
  Serial.begin(9600);
  while (!Serial) ;
  Ethernet.begin(mac);
  pinMode(LED_PIN, OUTPUT);    

  client.setServer(mqtt_server,mqtt_port);
  client.setCallback(callback);
  randomSeed(micros());
  String clientId = "EthernetClient-"; 
  clientId += String(random(0xffff), HEX);
  if (client.connect(clientId.c_str())) {
    Serial.println("connected");

    client.subscribe(command1_topic);   
  } 
  else {
    Serial.print("\nfailed, rc=");
    Serial.println(client.state());
    }
}

//================================================ loop
//================================================
void loop() {
  client.loop();

  //---- example: how to publish sensor values every 5 sec
  unsigned long now = millis();
  if (now - lastMsg > 5000 ) {
    lastMsg = now;
    sensor1= 4+random(50);      
    sensor2= 20+random(80);   
    sensor3= 400+random(80);   
    publishMessage(sensor1_topic,String(sensor1),true); 
    publishMessage(sensor2_topic,String(sensor2),true);
    publishMessage(sensor3_topic,String(sensor3),true);
  } 
}

//=======================================  

void callback(char* topic, byte* payload, unsigned int length) {
  String incommingMessage = "";
  for (int i = 0; i < length; i++) incommingMessage+=(char)payload[i];
  
  Serial.println("Message arrived ["+String(topic)+"]"+incommingMessage);
    if( strcmp(topic,command1_topic) == 0){
     if (incommingMessage.equals("1")) digitalWrite(LED_PIN, LOW);  
     else digitalWrite(LED_PIN, HIGH);  
  }
 
}



//======================================= publising as string
void publishMessage(const char* topic, String payload , boolean retained){
  if (client.publish(topic, payload.c_str(), true))
      Serial.println("Message publised ["+String(topic)+"]: "+payload);
}

Your Ethernet wire is a separate subnet. If 192.168.1.18 is the address your RPi uses on the WAN then it can't use the same address on the new subnet. You have to assign a separate address range (like 192.168.2.x) to the Ethernet interface and assigned your RPi and Arduino addresses from that range.

If you want the Arduino to have Internet access you can set up your RPi as a bridge between the Ethernet and WiFi:

Thank you it's working

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