Skip to content

Instantly share code, notes, and snippets.

@nothans
Created January 25, 2023 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nothans/cdd101ec75210c4ecb64c7240f6c9727 to your computer and use it in GitHub Desktop.
Save nothans/cdd101ec75210c4ecb64c7240f6c9727 to your computer and use it in GitHub Desktop.
Get Field1 of a ThingSpeak Channel and Set an LED Strip On or Off
/*
Get Field1 of a ThingSpeak Channel and Set State of
WS2812 NeoPixel Strip using ESP32
Requirements:
* ESP32 Wi-Fi Device
* Arduino 2.0.3+ IDE
* Additional Boards URL: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
* Library: ThingSpeak by MathWorks
* Library: Adafruit NeoPixel by Adafruit
Created: Jan 25, 2023 by Hans Scharler (https://nothans.com)
*/
#include <WiFi.h>
WiFiClient client;
const char* SSID = "YOUR_SSID";
const char* PASSWORD = "YOUR_PASSWORD";
#include <ThingSpeak.h>
unsigned long myChannelNumber = YOUR_CHANNEL_NUMBER;
const char * myReadAPIKey = "YOUR_READ_API_KEY";
#include <Adafruit_NeoPixel.h>
#define LED_COUNT 30
#define LED_PIN 13
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200);
delay(100);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
delay(100);
ThingSpeak.begin(client);
strip.begin();
strip.setBrightness(100);
delay(100);
}
void loop() {
int field1 = ThingSpeak.readIntField(myChannelNumber, 1, myReadAPIKey);
if (field1 == 1) {
//Turn on
for (int i = 0; i < LED_COUNT; i++) {
strip.setPixelColor(i, strip.Color(255,255,255));
}
strip.show();
}
else if (field1 == 0) {
//Turn off
for (int i = 0; i < LED_COUNT; i++) {
strip.setPixelColor(i, strip.Color(0,0,0));
}
strip.show();
}
delay(15000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment