Hello,
Total newbie when it comes to C++ and Arduino programing. I have been able to adapt this code to work on a NodeMCU ESP8266, but I would like to make some improvements upon it, and that's where my skills really die. The first code snippet is an abbreviated version of the full code so that you can have an idea of what is going on(if you want), but I really want to focus on snippet 2 and 3.
// LED variables
const int LEDPin = D7;
const int LEDCount = 172;
//Adafruit_NeoPixel strip(LEDCount, LEDPin, NEO_GRB + NEO_KHZ800);
// Wifi and Server Variables
char ssid[] = ""; // your network SSID (name)
char pass[] = ""; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the Wifi radio's status
char url[] = "";
WiFiClient client;
bool printWebData = false;
//Four Hour Interval between calls to API
const unsigned long postingInterval = 1000000;
// Used to tell whether to check API again
unsigned long lastConnectionTime = 0;
// initialize LED strip and some commonly used colors
Adafruit_NeoPixel strip(LEDCount, LEDPin, NEO_GRB + NEO_KHZ800);
uint32_t red = strip.Color(127, 0, 0);
uint32_t green = strip.Color(0, 127, 0);
uint32_t blue = strip.Color(0, 0, 128);
uint32_t orange = strip.Color(255, 165, 0);
uint32_t yellow = strip.Color(255, 255, 0);
//variables for parsing the API response and handling which LEDs to light up
boolean foundStart = false;
unsigned int dataCount = 0;
unsigned int pixelNum = 0;
void setup() {
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(125); // Set BRIGHTNESS to about 1/5 (max = 255)
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
//connect to wifi network
wifiConnect();
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
}
void loop() {
if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
// if there are incoming bytes available
// from the server, read them and print them:
int len = client.available();
if (len > 0) {
unsigned char buffer[len];
if (len > 80) len = 80;
client.read(buffer, len);
if (printWebData) {
Serial.write(buffer, len);
}
// if headers are still being read
int i = 0;
if (!foundStart) {
for (i; i < len; i++) {
if (buffer[i] == 2) {
Serial.println("FOUND WHERE START OF MESSAGE IS");
foundStart = true;
i++;
break;
}
}
}
//once past headers, read the useful data
if (foundStart) {
for (int j = i; j < len; j++) {
Serial.print("j: ");
Serial.println(j);
Serial.print("buffer[j]: ");
Serial.println((char)buffer[j]);
yield();
Serial.print("pixelNum: ");
Serial.println(pixelNum);
Serial.print("dataCount: ");
Serial.println(dataCount);
if (buffer[j] == 48) {
Serial.println("closed trail");
// data point for a lift
if (dataCount == 87 || dataCount == 88 || dataCount == 89 || dataCount == 90 || dataCount == 91 || dataCount == 92 || dataCount == 93) {
Serial.println("found a lift");
colorLift(red);
}
else {
strip.setPixelColor(pixelNum, red);
pixelNum++;
}
}
// trail is open
else if (buffer[j] == 49) {
Serial.println("open trail");
// data point for a lift
if (dataCount == 87 || dataCount == 88 || dataCount == 89 || dataCount == 90 || dataCount == 91 || dataCount == 92 || dataCount == 93) {
Serial.println("found a lift");
colorLift(green);
}
else {
strip.setPixelColor(pixelNum, green);
pixelNum++;
}
}
else if (buffer[j] == 50) {
Serial.println("groomed trail");
// data point for a lift
if (dataCount == 87 || dataCount == 88 || dataCount == 89 || dataCount == 90 || dataCount == 91 || dataCount == 92 || dataCount == 93) {
Serial.println("found a lift");
colorLift(blue);
}
else {
strip.setPixelColor(pixelNum, blue);
pixelNum++;
}
}
else if (buffer[j] == 52) {
Serial.println("closed lift");
// data point for a lift
if (dataCount == 87 || dataCount == 88 || dataCount == 89 || dataCount == 90 || dataCount == 91 || dataCount == 92 || dataCount == 93) {
Serial.println("found a lift");
colorLift(red);
}
else {
strip.setPixelColor(pixelNum, red);
pixelNum++;
}
}
else if (buffer[j] == 53) {
Serial.println("open lift");
// data point for a lift
if (dataCount == 87 || dataCount == 88 || dataCount == 89 || dataCount == 90 || dataCount == 91 || dataCount == 92 || dataCount == 93) {
Serial.println("found a lift");
colorLift(green);
}
else {
strip.setPixelColor(pixelNum, green);
pixelNum++;
}
}
dataCount++;
strip.show();
}
}
}
if (millis() - lastConnectionTime > postingInterval || lastConnectionTime == 0) {
//reset the variables needed for parsing a response
foundStart = false;
pixelNum = 0;
dataCount = 0;
if (serverConnect()) {
strip.clear();
}
else {
Serial.println("Server could not be reached");
}
}
}
else {
Serial.print("You were disconnected from the network");
wifiConnect();
printCurrentNet();
printWifiData();
}
}
void colorLift(uint32_t color) {
switch (dataCount) {
case 89:
Serial.println("89th data point");
for (pixelNum; pixelNum < 91; pixelNum++) {
strip.setPixelColor(pixelNum, color);
}
break;
case 90:
Serial.println("90th data point");
for (pixelNum; pixelNum < 93; pixelNum++) {
strip.setPixelColor(pixelNum, color);
}
break;
}
Serial.print("pixelNum after lift: ");
Serial.println(pixelNum);
}
Ideally I would love for this section within the code to have a chasing effect for however many cells are defined. In order to avoid have to rewrite the entire strip each time, I was hoping there was a way to just run these specific cells on some sort of a loop.
case 90:
Serial.println("90th data point");
for (pixelNum; pixelNum < 93; pixelNum++) {
strip.setPixelColor(pixelNum, color);
}
break;
Similar to the above one, I would like this section to have the strip.setPixelColor(pixelNum, blue) blink blue rather than just be statically on
else if (buffer[j] == 50) {
Serial.println("groomed trail");
// data point for a lift
if (dataCount == 87 || dataCount == 88 || dataCount == 89 || dataCount == 90 || dataCount == 91 || dataCount == 92 || dataCount == 93) {
Serial.println("found a lift");
colorLift(blue);
}
else {
strip.setPixelColor(pixelNum, blue);
pixelNum++;
}
}
Any assistance with this would be greatly appreciated!
Thanks