کد:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ESP8266WebServer.h>
#include <Adafruit_NeoPixel.h>
#define PIN 0
#define NUM_LEDS 16
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128,64,&Wire,-1);
String red = "0";
String green = "0";
String blue = "0";
int S1 = 0;
int S2 = 0;
int S3 = 0;
int speed_light = 500;
bool check_auto = false;
String R = "0";
String G = "0";
String B = "0";
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
char* ssid = "NodeMCU";
char* password = "1234567890";
ESP8266WebServer server;
unsigned int localPort = 2000;
IPAddress DestinationIP(192,168,4,2);
WiFiUDP udp;
char packetBuffer[50];
String inString;
char replyBuffer[50];
String strCon;
void setup()
{
strip.begin();
Serial.begin(115200);
delay(500);
WiFi.softAP(ssid,password);
Serial.println("");
IPAddress apip = WiFi.softAPIP();
Serial.print("Connect Your wifi mobile to this Nodemcu Access Point :");
Serial.println(ssid);
Serial.print("NodeMCU IP : ");
Serial.println(apip);
server.begin();
Serial.println("HTTP Server Started");
udp.begin(localPort);
Serial.print("Local Port : ");
Serial.println(udp.localPort());
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
led_show(red,green,blue);
}
void loop()
{
/*while (Serial.available())
{
red = Serial.parseInt();
green = Serial.parseInt();
blue = Serial.parseInt();
//fake = Serial.parseInt();
color_neo(red,green,blue);
}*/
receive_packet();
while(Serial.available() > 0)
{
char inChar = Serial.read();
inString += inChar;
if (inChar == '\n')
{
inString.toCharArray(replyBuffer, 50);
udp.beginPacket(DestinationIP,2000);\
udp.write(replyBuffer);
udp.endPacket();
inString="";
}
}
}
void receive_packet()
{
int packetSize = udp.parsePacket();
if (packetSize)
{
IPAddress remoteIp = udp.remoteIP();
int len = udp.read(packetBuffer, 255);
if (len > 0) packetBuffer[len] = 0;
//Serial.print("From ");
//Serial.print(remoteIp);
//Serial.print(" : ");
strCon = packetBuffer;
Serial.println(packetBuffer);
if (strCon.length()>=5)
{
red = strCon.substring(0,strCon.indexOf(','));
//Serial.println(red);
green = strCon.substring(red.length()+1,strCon.indexOf(',',red.length()+1));
//Serial.println(green);
blue = strCon.substring(red.length()+green.length()+2,strCon.indexOf(',',red.length()+green.length()+2));
//Serial.println(blue);
color_neo(red.toInt(),green.toInt(),blue.toInt());
led_show(red,green,blue);
}
else if (strCon.length()==4)
{
Serial.println(strCon);
speed_light = strCon.toInt();
speed_show(speed_light);
}
else if (strCon == "1")
{
state_show("STATE 1");
check_auto = false;
colorWipe(strip.Color(red.toInt(),green.toInt(),blue.toInt()),speed_light);
}
else if (strCon == "2")
{
state_show("STATE 2");
check_auto = false;
theaterChase(strip.Color(red.toInt(),green.toInt(),blue.toInt()),speed_light);
}
else if (strCon == "3")
{
state_show("STATE 3");
check_auto = false;
RunningLights(red.toInt(),green.toInt(),blue.toInt(),speed_light);
}
else if (strCon == "4")
{
state_show("STATE 4");
FadeInOut(red.toInt(),green.toInt(),blue.toInt());
}
else if (strCon == "5")
{
state_show("STATE 5");
SnowSparkle(red.toInt(),green.toInt(),blue.toInt(),speed_light,speed_light);
}
else if (strCon == "6")
{
state_show("STATE 6");
Sparkle(red.toInt(),green.toInt(),blue.toInt(),speed_light);
}
else if (strCon == "7")
{
state_show("STATE 7");
Strobe(red.toInt(),green.toInt(),blue.toInt(),10,speed_light,1000);
}
else if (strCon == "8")
{
state_show("STATE 8");
theaterChaseRainbow(speed_light);
}
else if (strCon == "9")
{
state_show("STATE 9");
Twinkle(red.toInt(),green.toInt(),blue.toInt(),10,speed_light,false);
}
else if (strCon == "st1")
{
state_show("AUTO 1");
check_auto = true;
colorWipe(strip.Color(red.toInt(),green.toInt(),blue.toInt()),speed_light);
}
else if (strCon == "st2")
{
state_show("AUTO 2");
check_auto = true;
theaterChase(strip.Color(red.toInt(),green.toInt(),blue.toInt()),speed_light);
}
else if (strCon == "st3")
{
state_show("AUTO 3");
check_auto = true;
RunningLights(red.toInt(),green.toInt(),blue.toInt(),speed_light);
}
else if (strCon == "st4")
{
S1 = 0;
S2 = 0;
S3 = 0;
check_auto = false;
state_show("AUTO OFF");
}
}
}
void color_neo(int redd,int greenn,int bluee)
{
for (int i = 0; i<=NUM_LEDS;i++)
{
strip.setPixelColor(i,redd,greenn,bluee);
strip.show();
}
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait)
{
for(uint16_t i=0; i<strip.numPixels(); i++)
{
strip.setPixelColor(i, c);
strip.show();
delay(wait);
strip.clear();
}
if (check_auto)
{
S1=S1+1;
inString = 100000+S1;
inString.toCharArray(replyBuffer, 50);
udp.beginPacket(DestinationIP,2000);
udp.write(replyBuffer);
udp.endPacket();
Serial.println(replyBuffer);
}
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait)
{
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
if (check_auto)
{
S2=S2+1;
//s2 = S2;
inString = 200000 +S2;
inString.toCharArray(replyBuffer, 50);
udp.beginPacket(DestinationIP,2000);
udp.write(replyBuffer);
udp.endPacket();
Serial.println(replyBuffer);
}
}
void RunningLights(int red, int green, int blue, int WaveDelay)
{
int Position=0;
for(int i=0; i<NUM_LEDS*2; i++)
{
Position++; // = 0; //Position + Rate;
for(int i=0; i<NUM_LEDS; i++) {
// sine wave, 3 offset waves make a rainbow!
//float level = sin(i+Position) * 127 + 128;
//setPixel(i,level,0,0);
//float level = sin(i+Position) * 127 + 128;
strip.setPixelColor(i,((sin(i+Position) * 127 + 128)/255)*red,
((sin(i+Position) * 127 + 128)/255)*green,
((sin(i+Position) * 127 + 128)/255)*blue);
}
strip.show();
delay(WaveDelay);
}
if (check_auto)
{
S3=S3+1;
//s3 = S3;
inString = 300000 +S3;
inString.toCharArray(replyBuffer, 50);
udp.beginPacket(DestinationIP,2000);
udp.write(replyBuffer);
udp.endPacket();
Serial.println(replyBuffer);
}
}
void FadeInOut(int red, int green, int blue)
{
float r, g, b;
for(int k = 0; k < 256; k=k+1) {
r = (k/256.0)*red;
g = (k/256.0)*green;
b = (k/256.0)*blue;
setAll(r,g,b);
strip.show();
}
for(int k = 255; k >= 0; k=k-2) {
r = (k/256.0)*red;
g = (k/256.0)*green;
b = (k/256.0)*blue;
setAll(r,g,b);
strip.show();
}
}
void setAll(int red, int green, int blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
//setPixel(i, red, green, blue);
strip.setPixelColor(i, strip.Color(red, green, blue));
}
strip.show();
}
void SnowSparkle(int red, int green, int blue, int SparkleDelay, int SpeedDelay) {
setAll(red,green,blue);
int Pixel = random(NUM_LEDS);
strip.setPixelColor(Pixel,0xff,0xff,0xff);
strip.show();
delay(SparkleDelay);
strip.setPixelColor(Pixel,red,green,blue);
strip.show();
delay(SpeedDelay);
}
void Sparkle(int red, int green, int blue, int SpeedDelay) {
int Pixel = random(NUM_LEDS);
strip.setPixelColor(Pixel,red,green,blue);
strip.show();
delay(SpeedDelay);
strip.setPixelColor(Pixel,0,0,0);
}
void Strobe(int red, int green, int blue, int StrobeCount, int FlashDelay, int EndPause){
for(int j = 0; j < StrobeCount; j++) {
setAll(red,green,blue);
strip.show();
delay(FlashDelay);
setAll(0,0,0);
strip.show();
delay(FlashDelay);
}
delay(EndPause);
}
void theaterChaseRainbow(int SpeedDelay) {
byte *c;
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q=0; q < 3; q++) {
for (int i=0; i < NUM_LEDS; i=i+3) {
c = Wheell( (i+j) % 255);
strip.setPixelColor(i+q, *c, *(c+1), *(c+2)); //turn every third pixel on
}
strip.show();
delay(SpeedDelay);
for (int i=0; i < NUM_LEDS; i=i+3) {
strip.setPixelColor(i+q, 0,0,0); //turn every third pixel off
}
}
}
}
byte * Wheell(int WheelPos) {
static byte c[3];
if(WheelPos < 85) {
c[0]=WheelPos * 3;
c[1]=255 - WheelPos * 3;
c[2]=0;
} else if(WheelPos < 170) {
WheelPos -= 85;
c[0]=255 - WheelPos * 3;
c[1]=0;
c[2]=WheelPos * 3;
} else {
WheelPos -= 170;
c[0]=0;
c[1]=WheelPos * 3;
c[2]=255 - WheelPos * 3;
}
return c;
}
void Twinkle(int red, int green, int blue, int Count, int SpeedDelay, boolean OnlyOne) {
setAll(0,0,0);
for (int i=0; i<Count; i++) {
strip.setPixelColor(random(NUM_LEDS),red,green,blue);
strip.show();
delay(SpeedDelay);
if(OnlyOne) {
setAll(0,0,0);
}
}
delay(SpeedDelay);
}
//----------------------
void led_show(String l1,String l2,String l3)
{
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0,5);
display.print("LED1 = ");
display.print(l1);
display.setCursor(0,27);
display.print("LED2 = ");
display.print(l2);
display.setCursor(0,50);
display.print("LED3 = ");
display.print(l3);
display.display();
}
void speed_show(int val)
{
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0,27);
display.print("SPEED=");
display.print(val);
display.display();
}
void state_show(String str)
{
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0,27);
display.print(str);
display.display();
}
این کد آردوینو برای nodemcu هست