Some background on what I’m trying to accomplish. I’m building a sketch that displays the weather forecast using ambient design. Information is pulled from a PHP script that builds a page that displays a unique character indicating the weather forecast (S=Same C=Cold W=Warm). The LEDs display the forecast temperature based on that.
Using the TwitterClient ethernet example, I’ve been able to retrieve the data from my site and turn on the correct LED. In the process of building this sketch I’ve also learned the difference between delay() and millis(), which is where I’m stumbling now. In the example sketch below, I’d like the LEDs (for now) to blink if the forecast matches, which does not occur, and the LED simply turns on.
Ideally the LED should blink in the duration between server calls replacing digitalWrite(whitePin, HIGH) in the sketch. I haven’t included a blink example (having used the Blink Without Delay as a guide) in the sketch below, as I’m unable to make this work properly. My understanding is that the sketch is cycling through connectToServer() but the timing won’t properly work with another timing routine inside that. It has been suggested that global variables may solve this issue.
I’ll be honest, I’m new at this and I’m not a programmer. While the code is most likely bloated and perhaps inefficient, it does mostly work. I look forward to any advice you may impart.
Using Uno with ethernet shield.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char serverName[] = "lessismorecast.com";
EthernetClient client;
const int whitePin = 9;
const int redPin = 6;
const int bluePin = 5;
const unsigned long requestInterval = 14400000; // delay between requests
unsigned long lastAttemptTime = 0; // last time connected to the server
String currentLine = ""; // string to hold the text from server
String forecast = ""; // string to hold the forecast
boolean readingforecast = false; // if you're currently reading the forecast
boolean requested; // whether you've made a request since connecting
void setup() {
pinMode(whitePin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// currentTime = millis();
// loopTime = currentTime;
currentLine.reserve(256); // reserve space for the strings
forecast.reserve(150); // reserve space for forecast strings
// Start up the ethernet shield
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) { // start ethernet using mac & IP address
Serial.println("Failed to configure Ethernet using DHCP");
while(true); // no point in carrying on, so stay in endless loop
}
connectToServer();
}
void loop() {
if (client.connected()) {
if (client.available()) { // read incoming bytes
char inChar = client.read();
currentLine += inChar; // add incoming byte to end of line
if (inChar == '\n') { // if you get a newline, clear the line
currentLine = "";
}
if ( currentLine.endsWith("<text>")) { // if the current line ends with <text>, it will be followed by the forecast
readingforecast = true; // Clear the forecast string
forecast = "";
}
if (readingforecast) { // Add bytes to the forecast String
if (inChar == 'S') {
digitalWrite(whitePin, HIGH); // white
Serial.println("tomorrow is the same");
}
}
if (readingforecast) {
if (inChar == 'W') {
digitalWrite(redPin, HIGH); // red
Serial.println("tomorrow is warmer");
}
}
if (readingforecast) {
if (inChar == 'C') {
digitalWrite(bluePin, HIGH); // blue
Serial.println("tomorrow is colder");
}
}
if (readingforecast) {
if (inChar != '<') {
forecast += inChar;
}
else {
readingforecast = false; // End of forecast if "<" character is reached
Serial.println(forecast);
Serial.println("disconnecting...");
client.stop(); // close the connection to the server
}
}
}
}
else if (millis() - lastAttemptTime > requestInterval) {
connectToServer(); // if you're not connected, and two minutes have passed since your last connection, then attempt to connect again
}
}
void connectToServer() {
digitalWrite(whitePin, LOW); // Reset LED to off
digitalWrite(redPin, LOW); // Reset LED to off
digitalWrite(bluePin, LOW); // Reset LED to off
// attempt to connect, and wait a millisecond:
delay(1000); // give the Ethernet shield a second to initialize
Serial.println("connecting...");
if (client.connect(serverName, 80)) { // Connect to server
Serial.println("connected...");
// make HTTP GET request to twitter:
client.println("GET /beacon.php HTTP/1.0"); // Make a HTTP request
client.println("HOST: lessismorecast.com");
client.println("User-Agent: Arduino 1.0");
client.println();
}
lastAttemptTime = millis(); // note the time of this connect attempt
}