Using Ethernet and SD card together in SPI of arduino - arduino

I am trying to use Ethernet module ENC28J60 (http://www.amazon.com/ENC28J60-Ethernet-Network-Module-STM32/dp/B008B4QIV2) and SD Card module in single project. I have to read data from the network and write them into the micro sd card.
How can I achieve this? I am getting following output from by sketch below
initialization done.
DONE Ethernet as well with IP: 255.255.255.255
SD card is initialized correctly, but Ethernet is not.
I have used pin 10,4 for CS of Ethernet, SD card respectively.
Program is as below
#include "SD.h"
#include "SPI.h"
#include "Ethernet.h"
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,200);
IPAddress gateway(192,168,1,99);
IPAddress mask(255,255,255,0);
//File myFile;
EthernetServer server(80);
void setup()
{
Serial.begin(9600);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}else {
Serial.println("initialization done.");
}
Ethernet.begin(mac, ip,gateway,gateway,mask);
server.begin();
delay(5000);
Serial.print("DONE Ethernet as well with IP: ");
Serial.println(Ethernet.localIP());
}
void loop(){
//todo
}

Just declare both CS pins as output. It will work.

Related

Simple Arduino Client Server communication via Ethernet, IP

Simple Ethernet communication between two Arduino boards. I used two Arduino UNO boards, Two Arduino Ethernet Shields.
Here is my Server Code
//Server
#include <SPI.h>
#include <Ethernet.h>
// network configuration. gateway and subnet are optional.
// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//the IP address for the shield:
byte ip[] = { 192,168,1,180 };
// the router's gateway address:
byte gateway[] = { 192, 168, 1, 1 };
// the subnet:
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server = EthernetServer(10001);
void setup()
{
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
}
void loop()
{
// if an incoming client connects, there will be bytes available to read:
EthernetClient client = server.available();
if (client == true) {
// read bytes from the incoming client and write them back
// to any clients connected to the server:
server.write(client.read());
}
}
Here is my Client Code
//Client
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xEC};
IPAddress ip(192,168,1,177);
IPAddress server(192,168,1,180);
EthernetClient client(10001);
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 10001)) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
I input those two codes to two Arduino boards. Wiring and other connection are correct and reliable.
But I got this output from client serial monitor
connecting...
connection failed
disconnecting.
According to my view, error may happen in client code. Can you help me to find the error?

Arduino DNS on local

How do I reference the address of the DNS serverIP, subnetIP, gatewayIP ?
As you can see from teh code below, I have tried but all I geet is the following error,
"class EthernetClass has no member called subnet".
#include <SPI.h>
#include <Ethernet.h>
// Network configuration
// Note : DNS server, gateway and subnet are optional.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// The IP address of Arduino Ethernet shield.
IPAddress myLocalIP(192, 168, 100, 2);
// The DNS server IP.
IPAddress dnsServer(192, 168, 100, 1);
// The router's gateway IP address.
IPAddress gateway(192, 168, 0, 1);
// the subnet:
IPAddress subnet(255, 255, 255, 0);
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, myLocalIP, subnet); // Initialize the ethernet device
// Ethernet.begin(mac, ip, dnServer, gateway, subnet);
Serial.print("Local IP = "); // Print out the IP address of the shield.
Serial.println(Ethernet.localIP());
Serial.print("DNS Address = ");
Serial.println(Ethernet.IPAddress.subnet() );
} // End setup.
void loop()
{
} // End loop.
Wallek876, Thank you for the info.
I have added the others as well, namely subnetMask, gatewayIP however when I used the documented method (as per your link) to print the MAC address - namely - Ethernet.MACAddress(macBuffer); I got an error.
The documented code - (copied from the documentation) is as follows;
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Ethernet.begin(mac, ip);
byte macBuffer[6]; // create a buffer to hold the MAC address
Ethernet.MACAddress(macBuffer); // fill the buffer
Serial.print("The MAC address is: ");
for (byte octet = 0; octet < 6; octet++) {
Serial.print(macBuffer[octet], HEX);
if (octet < 5) {
Serial.print('-');
}
}
}
void loop () {}
The error being on line Ethernet.MACAddress(macBuffer);
MACAddress not defined.
I tried several other solutions all to no avail - so some suggetions would be greatefully received.

Blink Led using MQTT in arduino

I have to implement a prototyping scenario that blink LED in the arduino with MQTT protocol. I already tried with several MQTT libraries but non of them not work perfectly. Connection to the MQTT broker working successfully but when I publish the message with topic which I set in arduino not blink the LED. Arduno have to publish a message when it successfully connect but this publishing part also not working
this is my code
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Set the MAC address
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 100);
IPAddress server(192, 168, 1, 20);
// Set what PINs our Led's are connected to
int redPin = 13;
//int greenPin = 6;
//int bluePin = 7;
// Set a generic code that will trigger our Blue Led
// think of this as a set of codes for automation you might write
byte triggerRed[13] = "12345";
// handles messages that are returned from the broker on our subscribed channel
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("New message from broker on topic:");
Serial.println(topic);
Serial.print("Payload:");
Serial.write(payload, length);
// Check and see if our payload matches our simple trigger test
if ((length == 5) & (memcmp(payload, triggerRed, 5) == 0) )
{
//blink(redPin);
}
}
EthernetClient ethClient;
PubSubClient client(ethClient);
// Fire up our PubSub client
//PubSubClient client(server, 1883, callback);
void setup()
{
// Open serial communications
Serial.begin(9600);
client.setServer(server, 1883);
client.setCallback(callback);
// Setup our Leds
pinMode(redPin, OUTPUT);
// pinMode(greenPin, OUTPUT);
// pinMode(bluePin, OUTPUT);
// attempt a DHCP connection
Serial.println("Attempting to get an IP address using DHCP:");
if (!Ethernet.begin(mac))
{
// if DHCP fails, start with a hard-coded address:
Serial.println("failed to get an IP address using DHCP, trying manually");
Ethernet.begin(mac, ip);
}
Serial.print("My address:");
Serial.println(Ethernet.localIP());
// Connect to Broker, give it arduino as the name
if (client.connect("arduino")) {
// Good, we connected turn on the red led
digitalWrite(redPin, HIGH);
// Publish a message to the status topic
client.publish("status","Arduino is now online");
// Listen for messages on the control topic
client.subscribe("ultra");
}
}
void loop()
{
client.loop();
}
// Anything with flashing lights.
void blink(int targetLed)
{
digitalWrite(redPin, HIGH);
}
how can I fix this ?
Put connection routine in a loop and try with test.mosquitto.org first.
Here is code that works for me (Ethernet shield hardware):
definitions:
#define CLIENT_NAME "myclientname"
#define TOPIC "mytopic"
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 105);
IPAddress server(85, 119, 83, 194);// test.mosquitto.org
#define MQTT_PORT 1883
IPAddress myDns(8, 8, 8, 8);
EthernetClient ethClient;
PubSubClient client(ethClient);
setup:
client.setServer(server, MQTT_PORT);
client.setCallback(callback);
Ethernet.begin(mac);
loop:
if (!client.connected()) {
reconnect();
}
client.loop();
reconnect routine
void reconnect() {
if (millis() - reconnectionTimer >reconnection_period){// Loop until we're reconnected
reconnectionTimer = millis();
if (!client.connected()) {
// Attempt to connect
if (client.connect(CLIENT_NAME)) {
client.subscribe(TOPIC);
}
else {
Serial.print(client.state());
Serial3.print(client.state());
}
}
}
}
update for blink:
void blink(){
digitalWrite(led, LOW);
delay(500);
digitalWrite(led, HIGH);
}
I lost a lot of time because i had the problem with the mqtt server. ensure that your server is using mqtt protocol, because i was using ws protocol and any library that I tried doesn't works with this protocol

Ethernet Shield does not supplying the correct IP

Good Day Everyone.
I am having a hard time configuring the ethernet shield.
I mounted the ENC28J60 Ethernet Shield on the Arduino Mega 2560.
I connected the USB port of the Arduino to the USB port of my PC,
I connected the ethernet port of the Ethernet Shield to a LAN Port of a TP-Link Router with an IP Address of 192.168.0.1 and has a DHCP starting address from .100 to .200
I put uploaded the code that I get from to the Arduino Module: https://www.arduino.cc/en/Reference/EthernetClient
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 100 };
byte server[] = { 64, 233, 187, 99 }; // Google
EthernetClient client;
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 80)) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
And then, from the serial window of Arduino IDE, I am getting
connecting...
connection failed
disconnecting...
I also tried some similar codes from above, some are outputting the shields IP, but still, I get the same problem, the Ethernet Shield does not output an IP Address or says "connected".
I also tried both straight-through and cross-over cables.
Please Help.
Thanks.
In your code:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
Must match your Ethernet Shield mac address.
Maybe try letting your router provide an IP address via DHCP. To do that, just change the line in your setup routine to Ethernet.begin(mac);

Arduino Ethernet Shield 2 not working

I am currently trying to get the Ethernet Shield working on my Mega. I was trying to run the Webserver example but the program seems to stuck at one point, so I tried to start from scratch.
This is my test code:
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0F, 0xF6, 0x3D
};
byte subnet[] = { 255,0,0,0 };
byte gateway[] = { 2,0,0,1 };
IPAddress ip(2, 0, 0, 1);
EthernetServer server(80);
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet);
Serial.println("Ethernet started");
server.begin();
Serial.println("Server started");
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Loop");
}
The output I get from the serial console is:
Etrted
Ethernet started
So I think the program gets stuck inside the EthernetServer::begin() function.
I am aware that there are earlier versions of ethernet shields which are not compatible to the mega, but the vendor of my shield says it is.
Also I don't understand, why it outputs the first line.
Thanks for your hints!
Arduino.cc and Arduino.org are not the same... Arduino.org, who is selling the ethernet shield 2, has their own IDE with the correct library! You can download it at http://www.arduino.org/downloads and the source can be found at https://github.com/arduino-org/Arduino/tree/1.7.4/libraries
Try this code form (http://www.arduino.cc/en/Tutorial/DhcpAddressPrinter):
#include <SPI.h>
#include <Ethernet.h>
// 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
}
// 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() {
}
And post what the serial monitor output is.
Try this:
NB. You can reuse your code written for Arduino Ethernet Shield, simply replacing
#include <Ethernet.h> --> #include <Ethernet2.h>
#include <EthernetUdp.h> --> #include <EthernetUdp2.h>
See this: http://labs.arduino.org/Arduino+Ethernet+Shield+2

Resources