#include "Adafruit_NeoPixel.h"
#include "AdafruitIO_WiFi.h"
#define PIXEL_PIN 14
#define PIXEL_COUNT 16
#define PIXEL_TYPE NEO_GRB + NEO_KHZ800
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""
#define IO_USERNAME "nguyentiendat"
#define IO_KEY "aio_aTAv84rnDDhN4CKkjjijbOj4ExZd"
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
// cài đặt 'color' feed
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
AdafruitIO_Feed *color = io.feed("color");
void setup() {
Serial.begin(115200);
while(! Serial);
//kết nối tới io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();
// Thiết lập một bộ xử lý tin nhắn cho feed 'color'.
// Hàm handleMessage (được định nghĩa phía dưới) sẽ được gọi mỗi khi một tin nhắn được nhận từ Adafruit IO.
color->onMessage(handleMessage);
// đợi kết nối
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}
// đã kết nối tới Adafruit IO thành công
Serial.println();
Serial.println(io.statusText());
color->get();
// khởi tạo Led NeoPixel
pixels.begin();
pixels.show();
}
void loop() {
// io.run(); là bắt buộc cho tất cả các chương trình.
// Nó nên luôn được đặt ở đầu của hàm loop của bạn.
// Luôn kiểm tra tín hiệu điều khiển từ Adafruit IO
io.run();
}
/* Hàm này được gọi mỗi khi một tin nhắn 'color'
được nhận từ Adafruit IO. Nó đã được gán vào
feed 'color' trong hàm setup() ở trên.*/
void handleMessage(AdafruitIO_Data *data) {
// hiển thị màu và in giá trị HEX
Serial.println("Received HEX: ");
Serial.println(data->value());
long color = data->toNeoPixel();
for(int i=0; i<PIXEL_COUNT; ++i) {
pixels.setPixelColor(i, color);
}
pixels.show();
}