Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Krauser98
#36354 Hi all! I'm fairly new to Arduino, so I'm looking for a little help...

I've got a sketch written to pull some data from the internet, then update some WS2812 LEDs accordingly. Everything works really well, except that the light patterns have to stop whenever I pull new data from the internet. I can set an interval to only check for new data every X seconds, but that results in a loss of responsiveness.

Does anyone know how to keep the code moving forward while the ESP8266 is pulling the data?

Here's the important parts of my code:

Code: Select all//Include WiFi libraries
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
//Include NeoPixel libraries
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

//Define the Pin # the NeoPixel data is being sent on
#define PIN 5

// Define WiFi variables
const char* ssid     = "MYSSID";
const char* password = "Passwords";
 
//Set location to obtain current setting
char server[]="192.168.1.XXX";
WiFiClient client;
String justState = "";
String currState = "";
String currentState = "";
boolean readingState = false;
uint32_t numColors = 0;
uint8_t stateStyle = 0;
uint8_t stateColor = 0;
int *colorArray;

unsigned long currentMillis = 0;
unsigned long previousMillis = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(50, PIN, NEO_GRB + NEO_KHZ800);

void setup(void)
{
  Serial.begin(115200);

  // Connect to WiFi network
  WiFi.begin(ssid, password);
  Serial.print("\n\r \n\rWorking to connect");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  strip.begin();
  strip.show();
}

void loop()
{
   
   if (millis() > previousMillis + 30000 || previousMillis == 0) {
      previousMillis = millis();
      stateStyle = getState("Lights_Christmas_Selection");
      stateColor = getState("Lights_Christmas_Color");
   }
   
   displayLights(stateStyle, stateColor);  // This function runs the patterns / colors
}

int getState(String item) {
   currentState = "";
 
   if (client.connect(server, 8080)) {            
      Serial.println("Connected");
      client.println("GET /rest/items/" + item + "/ HTTP/1.0");
      client.println();
      if (client.available()) {
         Serial.println("Making HTTP request");
         while (client.available()) {
            char c = client.read();
            currentState += c;
            if (currentState.endsWith("<state>")) {
               readingState = true;
            } else if (readingState) {
               if (!currentState.endsWith("</state>")) {
                  currState += c;
               } else {
                  readingState = false;
                  justState = currState.substring(0, currState.length()-7);
                  client.stop();
                  currState = "";
               }
            }
         }
         Serial.print(item);
         Serial.print(" = ");
         Serial.println(justState);
      } else {
         Serial.println("Not Available");   
      }
   } else {
      Serial.println("Not Connected");   
   }
  // Send the output back as an integer
  return justState.toInt();
}