
делюсь скетчем по работе с модулем передачи noolite через ардуино. после заливки и подключения контроллер понимает следующие команды (передачей по usb serial-порту):
Подача сигнала привязки ячейки 0:
nlpair0;
Подача сигнала включения ячейки 0:
nlon0;
Подача сигнала выключения ячейки 0:
nloff0;
Подача сигнала переключения ячейки 0:
nlswitch0;
Подача сигнала установки яркости на заданную величину (50) ячейки 0:
nldim0:50;
Подача сигнала установки яркости RGB-контроллера на заданные величины (R-G-B) ячейки 0:
nlrgb0:50-60-70;
Код ниже можно адаптировать для собственного ethernet-гейта или использовать как есть с универсальным узлом автоматики или при прямом подключении через USB-порт.
Код: Выделить всё
#include <SoftwareSerial.h>
#include <EEPROM.h>
#define PIN_RX (10) // RX PIN (connect to TX on noolite controller)
#define PIN_TX (11) // TX PIN (connect to RX on noolite controller)
#define PIN_LED (13)
#define NOO_BUF_LEN (12)
SoftwareSerial mySerial(PIN_RX, PIN_TX); // RX, TX
unsigned int unique_device_id = 0;
unsigned int uptime = 0;
unsigned int old_uptime = 0;
String inData;
void setup()
{
pinMode(PIN_LED,OUTPUT);
Serial.begin(9600);
// set the data rate for the SoftwareSerial port
pinMode(PIN_RX, INPUT);
pinMode(PIN_TX, OUTPUT);
mySerial.begin(9600);
// Device ID
Serial.print("Getting Device ID... ");
unique_device_id=EEPROMReadInt(0);
if (unique_device_id<10000 || unique_device_id>60000 || unique_device_id==26807) {
Serial.print("N/A, updating... ");
randomSeed(analogRead(0));
unique_device_id=random(10000, 60000);
EEPROMWriteInt(0, unique_device_id);
}
Serial.println(unique_device_id);
}
void nooSend(byte channel, byte buf[NOO_BUF_LEN]) {
buf[0]=85;
buf[1]=B01010000; //
buf[4]=0;
buf[5]=channel;
buf[9]=0;
int checkSum;
for(byte i=0;i<(NOO_BUF_LEN-2);i++) {
checkSum+=buf[i];
}
buf[10]=lowByte(checkSum);
buf[11]=170;
Serial.print("Sending: ");
for(byte i=0;i<(NOO_BUF_LEN);i++) {
Serial.print(buf[i]);
if (i!=(NOO_BUF_LEN-1)) { Serial.print('-'); }
}
Serial.println("");
for(byte i=0;i<(NOO_BUF_LEN);i++) {
mySerial.write(buf[i]);
}
}
//This function will write a 2 byte integer to the eeprom at the specified address and address + 1
void EEPROMWriteInt(int p_address, unsigned int p_value)
{
byte lowByte = ((p_value >> 0) & 0xFF);
byte highByte = ((p_value >> 8) & 0xFF);
EEPROM.write(p_address, lowByte);
EEPROM.write(p_address + 1, highByte);
}
//This function will read a 2 byte integer from the eeprom at the specified address and address + 1
unsigned int EEPROMReadInt(int p_address)
{
byte lowByte = EEPROM.read(p_address);
byte highByte = EEPROM.read(p_address + 1);
return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
}
void noolitePair(byte channel) {
byte buf[NOO_BUF_LEN];
for(byte i=0;i<(NOO_BUF_LEN);i++) {
buf[i]=0;
}
buf[2]=15;
buf[3]=0;
nooSend(channel,buf);
}
void nooliteTurnOn(byte channel) {
byte buf[NOO_BUF_LEN];
for(byte i=0;i<(NOO_BUF_LEN);i++) {
buf[i]=0;
}
buf[2]=2;
buf[3]=0;
nooSend(channel,buf);
}
void nooliteTurnOff(byte channel) {
byte buf[NOO_BUF_LEN];
for(byte i=0;i<(NOO_BUF_LEN);i++) {
buf[i]=0;
}
buf[2]=0;
buf[3]=0;
nooSend(channel,buf);
}
void nooliteSwitch(byte channel) {
byte buf[NOO_BUF_LEN];
for(byte i=0;i<(NOO_BUF_LEN);i++) {
buf[i]=0;
}
buf[2]=4;
buf[3]=0;
nooSend(channel,buf);
}
void nooliteDim(byte channel, byte level) {
byte buf[NOO_BUF_LEN];
for(byte i=0;i<(NOO_BUF_LEN);i++) {
buf[i]=0;
}
buf[2]=6;
buf[3]=0;
buf[6]=level;
nooSend(channel,buf);
}
void nooliteRGB(byte channel, byte levelR, byte levelG, byte levelB) {
byte buf[NOO_BUF_LEN];
for(byte i=0;i<(NOO_BUF_LEN);i++) {
buf[i]=0;
}
buf[2]=6;
buf[3]=0;
buf[6]=levelR;
buf[7]=levelG;
buf[8]=levelB;
nooSend(channel,buf);
}
void loop() // run over and over
{
uptime=round(millis()/1000);
if (uptime!=old_uptime) {
Serial.print("Uptime: ");
Serial.println(uptime);
old_uptime=uptime;
}
if (Serial.available()) {
char c=Serial.read();
if (c == '\n' || c == ';')
{
Serial.println(inData);
int commandProcessed=0;
if (inData.startsWith("nlpair")) {
commandProcessed=1;
inData.replace("nlpair","");
noolitePair(inData.toInt());
}
if (inData.startsWith("nlon")) {
commandProcessed=1;
inData.replace("nlon","");
nooliteTurnOn(inData.toInt());
}
if (inData.startsWith("nloff")) {
commandProcessed=1;
inData.replace("nloff","");
nooliteTurnOff(inData.toInt());
}
if (inData.startsWith("nlswitch")) {
commandProcessed=1;
inData.replace("nlswitch","");
nooliteSwitch(inData.toInt());
}
if (inData.startsWith("nldim")) {
commandProcessed=1;
inData.replace("nldim","");
int firstSep = inData.indexOf(':');
String firstPart=inData.substring(0, firstSep);
String secondPart=inData.substring(firstSep+1);
nooliteDim(firstPart.toInt(),secondPart.toInt());
}
if (inData.startsWith("nlrgb")) {
commandProcessed=1;
inData.replace("nlrgb","");
int firstSep = inData.indexOf(':');
String chPart=inData.substring(0, firstSep);
String secondPart=inData.substring(firstSep+1);
int chSep1 = secondPart.indexOf('-');
int chSep2 = secondPart.lastIndexOf('-');
String RPart=secondPart.substring(0,chSep1);
String GPart=secondPart.substring(chSep1+1,chSep2);
String BPart=secondPart.substring(chSep2+1);
nooliteRGB(chPart.toInt(),RPart.toInt(),GPart.toInt(),BPart.toInt());
}
if (commandProcessed==0) {
Serial.print("Unknown command: ");
Serial.println(inData);
}
inData="";
Serial.flush();
} else {
inData += (c);
}
}
if (mySerial.available())
Serial.write(mySerial.read());
}