Hi,
I have been working on two small programs for a few days now. One of them runs on a Arduino Uno with a W5100 Ethernet Shield and is working as intended. The other one runs on a Arduino Nano (ATMega 328) and controls a 21x16 flip-disc display (Wikipedia) via a library (came with the display). The display controlling also works as intended. Now to the problem: the arduino with the ethernet shield is running a http server and sends a part of the request data to the arduino controlling the flip-disc display via I2C. Both arduinos share a common ground and there are 2K pull-up resistors between the I2C cables and 5V (internal resistors are disabled), the cables also have a makeshift shield (also connected to ground). In many cases sending data will work perfectly, but sometimes the receiving arduino (flip-disc controller) will crash while receiving and/or parsing the received data (I have got no idea where that actually is/which line of code is causing the crash, crash means the arduino will do nothing before you reset it, even watchdog stops working!). Maybe I am just not reading everything correctly, but I have not been able to find the mistake (5+ hours of debugging), so I wanted to ask you to look over the code and tell me if you have found a problem.
See more recent post at the bottom for updated code!
--- Code for arduino with ethernet shield ---
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#define RESET 2
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
EthernetServer server(80);
void setup() {
pinMode(RESET, OUTPUT);
digitalWrite(RESET, HIGH);
Serial.begin(9600);
Wire.begin();
Ethernet.begin(mac);
server.begin();
Serial.print("Flipdot Server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
if (client) {
Serial.println();
char arg[350] = "";
boolean currentLineIsBlank = true;
byte commandPos = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if(c == '/' && commandPos == 0) {
commandPos = 1;
}
else if(c == ' ' && commandPos == 1) {
commandPos = 2;
}
else if(commandPos == 1) {
char str[2] = {c, '\0'};
strcat(arg, str);
}
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Access-Control-Allow-Origin: *");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Request answered");
client.println("</html>");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
if(arg[0] == 'x') {
digitalWrite(RESET, LOW);
delay(10);
digitalWrite(RESET, HIGH);
return;
}
Serial.println("Sending received data to flipdot...");
Wire.beginTransmission(105);
Wire.write(arg[0]);
char len[11] = "";
for(byte i = 2; i < 12; i++) {
len[i - 2] = arg[i];
}
len[10] = '\0';
long lenL = atol(len);
{
byte multiplier = 0;
byte buf = 0b00000000;
for(byte i = 0; i < 32; i++) {
if(i % 8 == 0 && i != 0) {
multiplier++;
Wire.write(buf);
buf = 0b00000000;
}
if(((lenL >> i) & 1) == 1) {
buf |= 1 << (i - multiplier * 8);
}
}
Wire.write(buf);
Wire.write('\0');
}
Serial.println("Sending header data...");
byte state = Wire.endTransmission();
Wire.beginTransmission(105);
if(arg[0] == 'i') {
byte multiplier = 0;
byte buf = 0b00000000;
for(int i = 0; i < strlen(arg) - 12; i++) {
if(i % 8 == 0 && i != 0) {
multiplier++;
Wire.write(buf);
buf = 0b00000000;
if(multiplier % 32 == 0 && multiplier != 0) {
Wire.endTransmission();
Wire.beginTransmission(105);
}
if(multiplier == 42) {
break;
}
}
if(arg[i + 13] == '1') {
buf |= 1 << (i - multiplier * 8);
}
}
Wire.write('\0');
Serial.println("Sending 42 bytes of image data...");
}
else if(arg[0] == 'c') {
char unix[11] = "";
for(byte i = 13; i < 23; i++) {
unix[i - 13] = arg[i];
}
unix[10] = '\0';
long unixL = atol(unix);
byte multiplier = 0;
byte buf = 0b00000000;
for(byte i = 0; i < 32; i++) {
if(i % 8 == 0 && i != 0) {
multiplier++;
Wire.write(buf);
buf = 0b00000000;
}
if(((unixL >> i) & 1) == 1) {
buf |= 1 << (i - multiplier * 8);
}
}
Wire.write(buf);
Wire.write('\0');
}
else if(arg[0] == 'r') {
Wire.write('\0');
}
if(Wire.endTransmission() == 0 && state == 0) {
Serial.println("All data successfully sent!");
}
else {
Serial.println("An error occurred while sending the data!");
}
}
}