The main problem, as far as I realise, is that the check if(Client.connected()) returns 0 and I cannot discover the cause. I use different libraries (not the usuall libraries) because the command Ethernet.begin(mac); would fail and nothing else would run after this command.
The code is a known program and is given:
//#include <SPI.h>
//#include <Ethernet.h>
#include <Dhcp.h>
#include <Dns.h>
#include <ethernet_comp.h>
#include <UIPClient.h>
#include <UIPEthernet.h>
#include <UIPServer.h>
#include <UIPUdp.h>
/*
Simple Web Data Client
This sketch connects to a website using an Arduino Wiznet Ethernet shield,
downloads a simple dataset, and does some LED fades based on the data.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created december 2014
by Nathan Yau, based on work by David A. Mellis, Tom Igoe, and Adrian McEwen
This example code is in the public domain.
*/
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
char server[] = "projects.flowingdata.com";
String dataLocation = "/holidays/current-rates.txt HTTP/1.1";
// 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;
String currentLine = ""; // string for incoming serial data
String currRates = "";
boolean readingRates = false; // is reading?
const int requestInterval = 600000; // milliseconds delay between requests
boolean requested; // whether you've made a request since connecting
long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
IPAddress ip(11,164,2,156);
// LED Stuff //
// Variables for first LED
// int led1 = 3; // the pin that the LED is attached to
// int brightness1 = 0; // how bright the LED is
// int maxBrightness1 = 100; // how bright before turning fade
// int fadeAmount1 = 1; // how many points to fade the LED by
// int delayTime1 = 40;
// float tweetsPerMin1 = 0;
// long lastFadeTime1 = 0;
// Variables for second LED
// int led2 = 5;
// int brightness2 = 0;
// int maxBrightness2 = 100;
// int fadeAmount2 = 1;
// int delayTime2 = 40;
// float tweetsPerMin2 = 0;
// long lastFadeTime2 = 0;
// Variables for third LED
// int led3 = 9;
// int brightness3 = 0;
// int maxBrightness3 = 100;
// int fadeAmount3 = 1;
// int delayTime3 = 40;
// float tweetsPerMin3 = 0;
// long lastFadeTime3 = 0;
// Data baselines
float baselineRate = 500; // Tweets per minute
int baselineDelay = 10; // Milliseconds between fade increments
int minBrightness = 10; // Minimum top brightness when fading
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// start the Ethernet connection:
Ethernet.begin(mac);
Serial.println(Ethernet.localIP());
// Connect to server
connectToServer();
// LEDs
// pinMode(led1, OUTPUT);
// pinMode(led2, OUTPUT);
// pinMode(led3, OUTPUT);
}
void loop() {
// Set LED fade rates and brightness
// analogWrite(led1, brightness1);
// analogWrite(led2, brightness2);
// analogWrite(led3, brightness3);
// Fade accordingly for next iteration
// if (millis() - lastFadeTime1 > delayTime1) {
// fadeByRate(tweetsPerMin1, brightness1, fadeAmount1, delayTime1, maxBrightness1, lastFadeTime1);
// }
// if (millis() - lastFadeTime2 > delayTime2) {
// fadeByRate(tweetsPerMin2, brightness2, fadeAmount2, delayTime2, maxBrightness2, lastFadeTime2);
// }
// if (millis() - lastFadeTime3 > delayTime3) {
// fadeByRate(tweetsPerMin3, brightness3, fadeAmount3, delayTime3, maxBrightness3, lastFadeTime3);
// }
// Checking for new data
if (client.connected()) {
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
if (client.available()) { //[color=red]PROBLEM!!![/color]
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
// read incoming bytes:
char inChar = client.read();
// add incoming byte to end of line:
currentLine += inChar;
// if you get a newline, clear the line:
if (inChar == '\n') {
currentLine = "";
}
if (currentLine.endsWith("<rates>")) {
readingRates = true;
}
else if (readingRates) {
if (!currentLine.endsWith("</rates>")) { //'>' is our ending character
currRates += inChar;
}
else {
readingRates = false;
String justRates = currRates.substring(0, currRates.length() - 8);
Serial.println(justRates);
// Split justRates
int firstSpaceIndex = justRates.indexOf(" ");
int secondSpaceIndex = justRates.indexOf(" ", firstSpaceIndex + 1);
String firstVal = justRates.substring(0, firstSpaceIndex);
String secondVal = justRates.substring(firstSpaceIndex + 1, secondSpaceIndex);
String thirdVal = justRates.substring(secondSpaceIndex);
// Convert strings to floats and store
int tweetsPerMin1 = strToFloat(firstVal);
int tweetsPerMin2 = strToFloat(secondVal);
int tweetsPerMin3 = strToFloat(thirdVal);
// Reset for next reading
currRates = "";
}
}
}
client.stop();
}
else if (millis() - lastAttemptTime > requestInterval) {
// if you're not connected, and two minutes have passed since
// your last connection, then attempt to connect again:
connectToServer();
}
}
void connectToServer() {
// attempt to connect, and wait a millisecond:
Serial.println("connecting to server...");
client.connect(server, 80); // Dummy call
client.stop();
delay(1000);
if (client.connect(server, 80)) {
Serial.println("making HTTP request...");
// make HTTP GET request to dataLocation:
client.println("GET " + dataLocation);delay(1000);
Serial.println("GET " + dataLocation);
client.println("Host: projects.flowingdata.com");delay(1000);
Serial.println("Host: projects.flowingdata.com");
client.println();delay(5000);
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}
// Calculate brightness and maxBrightness based on rate.
void fadeByRate(float& tweetsPerMin, int& brightness, int& fadeAmount, int& delayTime, int& maxBrightness, long& lastFadeTime) {
// maxBrightness between 80 and 255
maxBrightness = min( max(round(tweetsPerMin / baselineRate * 255), minBrightness), 255 );
// Blink lightly if low rate
if (maxBrightness == minBrightness) {
if (brightness == 0) {
brightness = minBrightness;
delayTime = round(5000 * tweetsPerMin / 100);
}
else {
brightness = 0;
delayTime = 5000;
}
}
// Fade otherwise
else {
delayTime = 40;
// Reverse direction when faded out or at maxBrightness
if (brightness == 0) {
fadeAmount = abs(fadeAmount);
}
else if (brightness >= maxBrightness) {
fadeAmount = -abs(fadeAmount);
delayTime = round(10000 * tweetsPerMin / baselineRate);
Serial.println(delayTime);
}
// Adjust brightness.
brightness += fadeAmount;
}
lastFadeTime = millis();
}
float strToFloat(String str) {
char carray[str.length() + 1]; // determine size of array
str.toCharArray(carray, sizeof(carray)); // put str into an array
return atof(carray);
}
Any help or indication would be appreciated