Hello,
I am building a controller with a small OLED display that is controlled over ethernet.
Hardware:
- Arduino Nano v3
- ENC28J60, PINS:D10,D11,D12,D13
- Generic I2C OLED128x32, PINS:A4,A5
My code works with either the ENC28J60 or the I2C OLED but not with both at the same time.
I can use the W5500 shield c/w Ethernet.h and EthernetUdp.h libraries to solve this problem but ENC8J60 is more suited for my purpose since it fits my pcb and the arduino nano fits on top of it.
Hopefully someone can help sort this out.
The following code has been edited to keep things simple. Basically, if I comment out the 8x8lib items I get ethernet comms.
FYI I also tried adafruit SSD_1306 and got the same results.
#include <UIPEthernet.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#define UDP_TX_PACKET_MAX_SIZE 24
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xA0 };
IPAddress ip(192,168,2,4);
unsigned int localPort = 1244; // local port to listen on
IPAddress OutIP(192,168,2,1);
int OutPort = 1242;
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
#include <U8x8lib.h>
U8X8_SSD1306_128X32_UNIVISION_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
char Line1[11];
char Line2[11];
void setup() {
// start the Ethernet and UDP
Ethernet.begin(mac,ip);
Udp.begin(localPort);
u8x8.begin();
u8x8.setFont(u8x8_font_chroma48medium8_r);
u8x8.clear();
u8x8.setFlipMode(1);
LCDRefreshTimer = millis(); //must be put at the end of the setup to start the refresh timer
}
void loop() {
ReadNetwork();
SendString();
updateDisplay();
}
//This procedure updates the OLED text
void updateDisplay() {
if ((millis()-LCDRefreshTimer) > 2000) {
LCDRefreshTimer = millis();
u8x8.clear();
u8x8.drawString(0,0,Line1);
u8x8.drawString(1,0,Line2);
}
}
void ReadNetwork() {
String MyString;
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if(packetSize)
{
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
MyString = GetHashtag("#STOP");
if (MyString != "Null"){
//DoSomething...
}
} //if(packetSize)
}
void SendString() {
if ((millis()-LCDRefreshTimer) > 50) {
String MyString;
LCDRefreshTimer = millis();
lMotorPosition = MotorEncoder.read();
MyString = "{,#SEND,SOMETHING,}";
char ReplyBuffer[MyString.length()+1];
MyString.toCharArray(ReplyBuffer, MyString.length()+1);
Udp.beginPacket(OutIP, OutPort);
Udp.write(ReplyBuffer);
Udp.endPacket();
}
}
String GetHashtag(String Hashtag) { // Get the string next to the hashtag
char MyChars[numChars];
char * myPointer; // this is used by strtok() as an index
char cHashtag[sizeof(Hashtag)]; //this char is used to pass the Hashtag to Strstr function
strcpy(MyChars, packetBuffer); //copy the received chars to allow manipulation
Hashtag.toCharArray(cHashtag, sizeof(cHashtag)); //load the hashtag into chashtag
myPointer = 0; //ensures the pointer is at 0
myPointer = strstr(MyChars,cHashtag); //look for hashtag
if (myPointer != 0) { //if the strstr finds something proceed
myPointer = strtok(myPointer,","); //load the pointer with strtok
myPointer = strtok(NULL, ","); //skip to the next value past the comma
strcpy(MyChars, myPointer); // copy the pointer to a char array
return MyChars; //return the char array and convert it to a string
}
else {return "Null";} //if all fails return null
}