Hi guys,
I'm trying to connect HX711 amplifier (connected to a scale sensors) and w5100 ethernet on arduino mega. The problem I am having is that when I try to connect through browser to read data, server usually hangs, not always but in 90% of the time, while serial output works fine showing correct data. I googled a lot and I found that it might be SPI conflict. Now, I don't know much about communication protocols, so I was wondering is someone can help me out.
This is the code:
// Arduino Mega + W5100 Ethernet + 8 ch relay
#include <SPI.h>
#include <Ethernet.h>
#include "HX711.h"
#define DOUT 3
#define CLK 2
HX711 scale(DOUT, CLK);
float calibration_factor = -23000; // kalibracija vage
float weight;
float weight_old = 1;
String readString;
String weight_str;
boolean flag = 1;
// Settings for ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // <------- PUT YOUR MAC Address Here
byte ip[] = { 192, 168, 1, 177}; // <------- PUT YOUR IP Address Here
byte gateway[] = { 192, 168, 1, 1 }; // <------- PUT YOUR ROUTERS IP Address to which your shield is connected Here
byte subnet[] = { 255, 255, 255, 0 }; // <------- It will be as it is in most of the cases
EthernetServer server(80); // <------- It's Defaulf Server Port for Ethernet Shield
void setup() {
// Sprinkle some magic pixie dust. (disable SD SPI)
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
scale.set_scale();
scale.tare(); //Reset the scale to 0
scale.set_scale(calibration_factor);
// disable your new SPI until the w5100 is set up
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
//pinMode(10,INPUT);
digitalWrite(10, LOW);
// set up w5100
Ethernet.begin(mac, ip, gateway, subnet);
// disable w5100 SPI
server.begin();
// Initialize scale
delay(1000);
//enable serial data print
Serial.begin(9600);
Serial.println("server LED test 1.0"); // so that we can know what is getting loaded
}
// Main loop of the program
void loop() {
// Create a client connection
EthernetClient client = server.available();
if (flag == 1) {
// Serial.println("1231231 ");
// pinMode(3,INPUT);
// pinMode(10,OUTPUT);
digitalWrite(10, HIGH);
digitalWrite(3, LOW);
float weight = scale.get_units();
Serial.println("tezina ");
Serial.println(weight);
weight_str = String(weight);
Serial.println("tezina string");
Serial.println(weight_str);
flag = 0;
// pinMode(3,OUTPUT);
// pinMode(10,INPUT);
digitalWrite(3, HIGH);
digitalWrite(10, LOW);
}
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 100) { //read char by char HTTP request
readString += c; //store characters to string
//Serial.print(c);
}
if (c == '\n') { //if HTTP request has ended
Serial.println(readString); //print to serial monitor for debuging
/* Start OF HTML Section. Here Keep everything as it is unless you understand how it working */
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
client.println("<HTML>");
client.println("<HEAD>");
client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
//client.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://arduino-autohome.googlecode.com/svn/trunk/autohome.css\" />");
client.println("</HEAD>");
client.println("<body bgcolor=\"#D0D0D0\">");
client.println("<hr/>");
client.println("<hr/>");
client.println("<h4><center><img border=\"0\" src=\"http://www.cowperandnewtonmuseum.org.uk/wp-content/uploads/2017/04/bee.svg_.med_.png\" /></center></h4>");
client.println("<hr/>");
client.println("<h4><center><font size=\"20\">Kosnica 1: " + weight_str + " Kg </font></center></h4>");
client.println("<hr/>");
client.println("</center>");
client.println("</body>");
client.println("</HTML>");
//clearing string for next read
readString = "";
delay(1);
client.stop(); //stopping client
flag = 1;
}
}
}
}
}