Go Down

Topic: Ethernet W5100 + HX711 = SPI problem? (Read 218 times) previous topic - next topic

BMaster23

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:
Code: [Select]
// 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;
        }

      }

    }
  }

}

PaulS

Code: [Select]
  //pinMode(10,INPUT);
Get your delete key fixed!

Code: [Select]
  digitalWrite(10, LOW);
  // set up w5100

Don't you suppose that the Ethernet library already does that?

Quote
I googled a lot and I found that it might be SPI conflict.
I guess you missed all the pages about not using Strings and about using the F() macro to keep string literals from being copied to SRAM. You need to go back to see Mr. Google.
The art of getting good answers lies in asking good questions.

BMaster23

Hi PaulS,

Can you please be more specific with your answers?

So you are saying that Strings causes all of my problems? 

PaulS

Quote
So you are saying that Strings causes all of my problems?
No. What I'm saying is that you are using a lot of Strings, uselessly, and they cause some people problems when used in the limited memory of an Arduino. You may, or may not, be one of the unlucky ones.

There is no need to use Strings, though. ALL of the functionality of Strings is provided, in the end, by the string functions that operate on the string that the String class wraps.

There is no reason, either, to copy all of those string literals into SRAM at run time. Use the F() macro:
  Serial.println(F("server LED test 1.0")); // so that we can know what is getting loaded
and keep them out.
The art of getting good answers lies in asking good questions.

DaveEvans


BMaster23

No. What I'm saying is that you are using a lot of Strings, uselessly, and they cause some people problems when used in the limited memory of an Arduino. You may, or may not, be one of the unlucky ones.

There is no need to use Strings, though. ALL of the functionality of Strings is provided, in the end, by the string functions that operate on the string that the String class wraps.

There is no reason, either, to copy all of those string literals into SRAM at run time. Use the F() macro:
  Serial.println(F("server LED test 1.0")); // so that we can know what is getting loaded
and keep them out.
I've done this, but since I am using MEGA all I did is reduce the use of SRAM from 12% to 6%. Thanks for the tip.
In order to solve this problem is to use function power_up and power_down after finishing measurements.

Go Up