W5500 Wiznet Ethernet Shield

MQTT Test / DHCP
ORIGINAL POST
By Louis
components
Hardware Components
details

W5500eth.png

DHCP

DHCP Ethernet connecting Test

  • DHCP-based IP printer

This sketch uses the DHCP extensions to the Ethernet library to get an IP address via DHCP and print the address obtained. using an Arduino Wiznet Ethernet shield.

  • Circuit

Ethernet shield attached to pins 10, 11, 12, 13

Source Code


#include <SPI.h>
#include <Ethernet3.h>

#define SS 10 //W5500 CS
#define RST 7 //W5500 RST
#define CS 4 //SD CS pin
const int LED = 13;

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// this check is only needed on the Leonardo:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
pinMode(SS, OUTPUT);
pinMode(RST, OUTPUT);
pinMode(CS, OUTPUT);
digitalWrite(SS, LOW);
digitalWrite(CS, HIGH);
/* If you want to control Reset function of W5500 Ethernet controller */
digitalWrite(RST,HIGH);

pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
Serial.print(“START”);

// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
// no point in carrying on, so do nothing forevermore:
for (;;)
;
}
// print your local IP address:
Serial.print(“My IP address: “);
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(“.”);
}
Serial.println();
}

void loop() {

}


MQTT

 

Data Upload Check                                                            Server Data Check





Source Code

#include <SPI.h>
#include <Ethernet3.h>
#include “PubSubClient.h”
//#include “DHT.h”
#define CLIENT_ID “NANO_UNO”
#define TOKEN “”
#define PUBLISH_DELAY 10* 1000
//#define DHTPIN 3
//#define DHTTYPE DHT11
bool statusD4 = HIGH;
bool statusD5 = HIGH;
bool statusD6 = HIGH;
bool statusD7 = HIGH;
bool statusD8 =HIGH;
bool statusD9 = HIGH;
bool statusD10 =LOW;
bool statusD11=LOW;
bool statusD12=LOW;
int valueA0;
int valueA1;
float valueA2; //made a float to store a battery value
int valueA3;
int upTime = 0;
byte i;
String ip = “192.168”;
String MAC = “”;
uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};

EthernetClient ethClient;
PubSubClient mqttClient;
//DHT dht(DHTPIN, DHTTYPE);

long previousMillis;

#define SS 10 //W5500 CS
#define RST 7 //W5500 RST

void setup() {

// setup serial communication
Serial.begin(9600);

while(!Serial) {};
Serial.println(F(“MQTT Arduino Demo”));
Serial.println();

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
pinMode(SS, OUTPUT);
pinMode(RST, OUTPUT);
//pinMode(CS, OUTPUT);
digitalWrite(SS, LOW);
//digitalWrite(CS, HIGH);
/* If you want to control Reset function of W5500 Ethernet controller */
digitalWrite(RST,HIGH);

// setup ethernet communication using DHCP
if (Ethernet.begin(mac) == 0) {
//Serial.println(F(“Unable to configure Ethernet using DHCP”));
for (;;);
}

// ip=”192.168″;
for (i = 2; i < 4; i++) {
ip = ip + “.”;
ip = ip + String (Ethernet.localIP()[i]);
}

//—mac
for (i = 0; i < 6; i++) {
if ((mac[i]) <= 0x0F) {
MAC = MAC + “0”; //zet er zonodig een ‘0’ voor
}
MAC = MAC + String ((mac[i]), HEX);
MAC = MAC + “:”;
}
MAC[(MAC.length()) – 1] = ‘\0’; //verwijder laatst toegevoegde “:”

Serial.println(F(“Ethernet configured via DHCP”));
Serial.print(“IP address: “);
Serial.println(Ethernet.localIP());
Serial.println();

// setup mqtt client
mqttClient.setClient(ethClient);
// mqttClient.setServer( “raspberrypi.local”,1883);

mqttClient.setServer(“farm1st.ddns.net”, 1883);
Serial.println(F(“MQTT client configured”));

// setup DHT sensor
// dht.begin();
//Serial.println(F(“DHT sensor initialized”));

//Serial.println();
// Serial.println(F(“Ready to send data”));
previousMillis = millis();
}

void loop() {
/*
statusD4 = digitalRead(4);//
statusD5 = digitalRead(5);//
statusD6 = digitalRead(6);//
statusD7 = digitalRead(7);
statusD8 = digitalRead(8);
statusD9 = digitalRead(9);
statusD10=digitalRead(10);
statusD11=digitalRead(11);
statusD12=digitalRead(12);
upTime = millis() / 60000;
valueA0 = analogRead(A0);//
valueA1 = analogRead(A1);
valueA2 = analogRead(A2);
valueA2 = (valueA2 * 5) / 1023.0;
valueA3 = analogRead(A3); //garage
// it’s time to send new data? */
if (millis() – previousMillis > PUBLISH_DELAY) {
sendData();
previousMillis = millis();
}

mqttClient.loop();
}

long randomNumber = 0;

void sendData() {

char msgBuffer[7];//was 20

randomSeed(analogRead(A0));

float h = random(100); // dht.readHumidity();
float t = random(100)-50; // dht.readTemperature();

Serial.println(“sendData”);

if (mqttClient.connect(CLIENT_ID, TOKEN, NULL)) {

char data[64] = { 0x00, };
sprintf(data, “{\”humidity\” : %d, \”temperature\” : %d }”, (int)h, (int)t);
Serial.print(“publish=”);
Serial.println(data);

mqttClient.publish(“v1/devices/me/telemetry”, data);

//mqttClient.publish(“temp”, deblank(dtostrf(t, 4, 1, msgBuffer)));//
//mqttClient.publish(“humid”, deblank(dtostrf(h, 3, 0, msgBuffer)));

/*
mqttClient.publish(“home/d4”, (statusD4 == HIGH) ? “OPEN” : “CLOSED”);//
mqttClient.publish(“home/d5”, (statusD5 == HIGH) ? “OPEN” : “CLOSED”);//
mqttClient.publish(“home/d6”, (statusD6 == HIGH) ? “OPEN” : “CLOSED”);//
mqttClient.publish(“home/d7”, (statusD7 == HIGH) ? “OPEN” : “CLOSED”);//
mqttClient.publish(“home/d8”, (statusD8 == HIGH) ? “OPEN” : “CLOSED”); //
mqttClient.publish(“home/d9”, (statusD9 == HIGH) ? “OPEN” : “CLOSED”); //
mqttClient.publish(“home/d10”,(statusD10 == HIGH) ? “OPEN” : “CLOSED”); //
mqttClient.publish(“home/d11”,(statusD11== HIGH) ? “OPEN” : “CLOSED”); //
mqttClient.publish(“home/d12”,(statusD12== HIGH) ? “OPEN” : “CLOSED”); //
mqttClient.publish(“home/A0”, deblank(dtostrf(valueA0, 4, 0, msgBuffer)));
mqttClient.publish(“home/A1”, deblank(dtostrf(valueA1, 4, 0, msgBuffer)));
mqttClient.publish(“home/A2”, deblank(dtostrf(valueA2, 4, 2, msgBuffer)));
mqttClient.publish(“home/A3”, deblank(dtostrf(valueA1, 4, 0, msgBuffer)));
mqttClient.publish(“home/versie”, “enc28j60MQTT_uni”);
mqttClient.publish(“home/ip”, ip.c_str());
mqttClient.publish(“home/u”, deblank(dtostrf(upTime, 6, 0, msgBuffer)));//uptime
mqttClient.publish(“home/mac”, MAC.c_str()); */

mqttClient.disconnect();

}
}

char * deblank(char *str) {
char *out = str;
char *put = str;

for (; *str != ‘\0’; ++str) {

if (*str != ‘ ‘) {
*put++ = *str;
}
}
*put = ‘\0’;
return out;
}

W5500eth.png

DHCP

DHCP Ethernet connecting Test

  • DHCP-based IP printer

This sketch uses the DHCP extensions to the Ethernet library to get an IP address via DHCP and print the address obtained. using an Arduino Wiznet Ethernet shield.

  • Circuit

Ethernet shield attached to pins 10, 11, 12, 13

Source Code


#include <SPI.h>
#include <Ethernet3.h>

#define SS 10 //W5500 CS
#define RST 7 //W5500 RST
#define CS 4 //SD CS pin
const int LED = 13;

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// this check is only needed on the Leonardo:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
pinMode(SS, OUTPUT);
pinMode(RST, OUTPUT);
pinMode(CS, OUTPUT);
digitalWrite(SS, LOW);
digitalWrite(CS, HIGH);
/* If you want to control Reset function of W5500 Ethernet controller */
digitalWrite(RST,HIGH);

pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
Serial.print(“START”);

// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
// no point in carrying on, so do nothing forevermore:
for (;;)
;
}
// print your local IP address:
Serial.print(“My IP address: “);
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(“.”);
}
Serial.println();
}

void loop() {

}


MQTT

 

Data Upload Check                                                            Server Data Check





Source Code

#include <SPI.h>
#include <Ethernet3.h>
#include “PubSubClient.h”
//#include “DHT.h”
#define CLIENT_ID “NANO_UNO”
#define TOKEN “”
#define PUBLISH_DELAY 10* 1000
//#define DHTPIN 3
//#define DHTTYPE DHT11
bool statusD4 = HIGH;
bool statusD5 = HIGH;
bool statusD6 = HIGH;
bool statusD7 = HIGH;
bool statusD8 =HIGH;
bool statusD9 = HIGH;
bool statusD10 =LOW;
bool statusD11=LOW;
bool statusD12=LOW;
int valueA0;
int valueA1;
float valueA2; //made a float to store a battery value
int valueA3;
int upTime = 0;
byte i;
String ip = “192.168”;
String MAC = “”;
uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};

EthernetClient ethClient;
PubSubClient mqttClient;
//DHT dht(DHTPIN, DHTTYPE);

long previousMillis;

#define SS 10 //W5500 CS
#define RST 7 //W5500 RST

void setup() {

// setup serial communication
Serial.begin(9600);

while(!Serial) {};
Serial.println(F(“MQTT Arduino Demo”));
Serial.println();

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
pinMode(SS, OUTPUT);
pinMode(RST, OUTPUT);
//pinMode(CS, OUTPUT);
digitalWrite(SS, LOW);
//digitalWrite(CS, HIGH);
/* If you want to control Reset function of W5500 Ethernet controller */
digitalWrite(RST,HIGH);

// setup ethernet communication using DHCP
if (Ethernet.begin(mac) == 0) {
//Serial.println(F(“Unable to configure Ethernet using DHCP”));
for (;;);
}

// ip=”192.168″;
for (i = 2; i < 4; i++) {
ip = ip + “.”;
ip = ip + String (Ethernet.localIP()[i]);
}

//—mac
for (i = 0; i < 6; i++) {
if ((mac[i]) <= 0x0F) {
MAC = MAC + “0”; //zet er zonodig een ‘0’ voor
}
MAC = MAC + String ((mac[i]), HEX);
MAC = MAC + “:”;
}
MAC[(MAC.length()) – 1] = ‘\0’; //verwijder laatst toegevoegde “:”

Serial.println(F(“Ethernet configured via DHCP”));
Serial.print(“IP address: “);
Serial.println(Ethernet.localIP());
Serial.println();

// setup mqtt client
mqttClient.setClient(ethClient);
// mqttClient.setServer( “raspberrypi.local”,1883);

mqttClient.setServer(“farm1st.ddns.net”, 1883);
Serial.println(F(“MQTT client configured”));

// setup DHT sensor
// dht.begin();
//Serial.println(F(“DHT sensor initialized”));

//Serial.println();
// Serial.println(F(“Ready to send data”));
previousMillis = millis();
}

void loop() {
/*
statusD4 = digitalRead(4);//
statusD5 = digitalRead(5);//
statusD6 = digitalRead(6);//
statusD7 = digitalRead(7);
statusD8 = digitalRead(8);
statusD9 = digitalRead(9);
statusD10=digitalRead(10);
statusD11=digitalRead(11);
statusD12=digitalRead(12);
upTime = millis() / 60000;
valueA0 = analogRead(A0);//
valueA1 = analogRead(A1);
valueA2 = analogRead(A2);
valueA2 = (valueA2 * 5) / 1023.0;
valueA3 = analogRead(A3); //garage
// it’s time to send new data? */
if (millis() – previousMillis > PUBLISH_DELAY) {
sendData();
previousMillis = millis();
}

mqttClient.loop();
}

long randomNumber = 0;

void sendData() {

char msgBuffer[7];//was 20

randomSeed(analogRead(A0));

float h = random(100); // dht.readHumidity();
float t = random(100)-50; // dht.readTemperature();

Serial.println(“sendData”);

if (mqttClient.connect(CLIENT_ID, TOKEN, NULL)) {

char data[64] = { 0x00, };
sprintf(data, “{\”humidity\” : %d, \”temperature\” : %d }”, (int)h, (int)t);
Serial.print(“publish=”);
Serial.println(data);

mqttClient.publish(“v1/devices/me/telemetry”, data);

//mqttClient.publish(“temp”, deblank(dtostrf(t, 4, 1, msgBuffer)));//
//mqttClient.publish(“humid”, deblank(dtostrf(h, 3, 0, msgBuffer)));

/*
mqttClient.publish(“home/d4”, (statusD4 == HIGH) ? “OPEN” : “CLOSED”);//
mqttClient.publish(“home/d5”, (statusD5 == HIGH) ? “OPEN” : “CLOSED”);//
mqttClient.publish(“home/d6”, (statusD6 == HIGH) ? “OPEN” : “CLOSED”);//
mqttClient.publish(“home/d7”, (statusD7 == HIGH) ? “OPEN” : “CLOSED”);//
mqttClient.publish(“home/d8”, (statusD8 == HIGH) ? “OPEN” : “CLOSED”); //
mqttClient.publish(“home/d9”, (statusD9 == HIGH) ? “OPEN” : “CLOSED”); //
mqttClient.publish(“home/d10”,(statusD10 == HIGH) ? “OPEN” : “CLOSED”); //
mqttClient.publish(“home/d11”,(statusD11== HIGH) ? “OPEN” : “CLOSED”); //
mqttClient.publish(“home/d12”,(statusD12== HIGH) ? “OPEN” : “CLOSED”); //
mqttClient.publish(“home/A0”, deblank(dtostrf(valueA0, 4, 0, msgBuffer)));
mqttClient.publish(“home/A1”, deblank(dtostrf(valueA1, 4, 0, msgBuffer)));
mqttClient.publish(“home/A2”, deblank(dtostrf(valueA2, 4, 2, msgBuffer)));
mqttClient.publish(“home/A3”, deblank(dtostrf(valueA1, 4, 0, msgBuffer)));
mqttClient.publish(“home/versie”, “enc28j60MQTT_uni”);
mqttClient.publish(“home/ip”, ip.c_str());
mqttClient.publish(“home/u”, deblank(dtostrf(upTime, 6, 0, msgBuffer)));//uptime
mqttClient.publish(“home/mac”, MAC.c_str()); */

mqttClient.disconnect();

}
}

char * deblank(char *str) {
char *out = str;
char *put = str;

for (; *str != ‘\0’; ++str) {

if (*str != ‘ ‘) {
*put++ = *str;
}
}
*put = ‘\0’;
return out;
}

COMMENTS

Please Login to comment
  Subscribe  
Notify of
POSTED BY