Hello all!
I have a problem with ESP32 with ENC28J60 with fixed IP.
I have a code like this:
#include <SPI.h>
#include <EthernetENC.h>
#include "PubSubClient.h"
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//char server[] = "www.google.com"; // name address for Google (using DNS)
#define CLIENT_ID "MQTT"
#define INTERVAL 3000 // 3 sec delay between publishing
// Set the static IP address to use if the DHCP fails to assign
#define MYIPADDR 192,168,1,28
#define MYIPMASK 255,255,255,0
//#define MYDNS 192,168,1,1
//#define MYGW 192,168,1,1
EthernetClient client;
PubSubClient mqttClient;
long previousMillis;
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; // set to false for better speed measurement
const int SensorPin = 4; // the number of the pushbutton pin
const int ledPin = 2; // the number of the LED pin
long Czas_1 =0, Czas_2 =0;
long czas = 1000;
int SensorState = 0;
int lastSensorState = 0;
const char* startMessageTopic = "esp32/message"; //temat do wyslania wiadomosci
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
IPAddress ip(MYIPADDR);
//IPAddress dns(MYDNS);
//IPAddress gw(MYGW);
IPAddress sn(MYIPMASK);
//Ethernet.begin(mac, ip, dns, gw, sn);
Serial.println("Initialize Ethernet with DHCP:");
Ethernet.begin(mac, ip, sn);
delay(1000);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
// Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
Ethernet.begin(mac, ip, sn);
delay(1000);
Serial.println(Ethernet.localIP());
//mqttClient.setClient(client);
//mqttClient.setServer("192.168.1.9", 1883);
Serial.println(F("MQTT client configured"));
//previousMillis = millis();
// give the Ethernet shield a second to initialize:
// if you get a connection, report back via serial:
// beginMicros = micros();
}
void loop() {
SensorState= digitalRead(SensorPin);
if (SensorState != lastSensorState){
if (SensorState==HIGH){
digitalWrite(ledPin, LOW);
mqttClient.publish(startMessageTopic, "Hello from ESP32");
} else {
// turn LED off
digitalWrite(ledPin, HIGH);
mqttClient.publish(startMessageTopic, "Hello from ESP32");
}
}
lastSensorState=SensorState;
delay(100);
}
I wan to have fixed ip address, but, if i take DHCP i have a address from DHCP, but if i want to have fixed IP, I have IP address when program is in void setup() - in serial port monitor i see string from program.
When program goes to Void loop(), in i lost IP address

what is wrong?
can any one help me?