Skip to content

Instantly share code, notes, and snippets.

@prasertsakd
Last active March 16, 2016 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prasertsakd/9cae1f800ec2f9da3355 to your computer and use it in GitHub Desktop.
Save prasertsakd/9cae1f800ec2f9da3355 to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Adafruit_NeoPixel.h>
WiFiUDP Udp;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(5, 5, NEO_GRB + NEO_KHZ800);
const char* ssid = "...............";
const char* password = "........................";
//IPAddress local_ip = {192,168,1,200};
//IPAddress gateway = {192,168,1,1};
//IPAddress subnet = {255,255,255,0};
int relayPin = 5;
int ledPin = 16;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(10);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode(relayPin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(relayPin, LOW);
digitalWrite(ledPin, HIGH);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
//if not need fix ip please comment
//WiFi.config(local_ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
Udp.begin(9998);
Serial.println("UDP Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
Udp.parsePacket();
while(Udp.available()){
Serial.print(Udp.remoteIP());
Serial.print(" : ");
//String req = Udp.readStringUntil('\r');
char req = Udp.read();
Serial.println(req);
//if (req.indexOf("H") != -1) {
if (req=='H') {
digitalWrite(relayPin, HIGH);
digitalWrite(ledPin, LOW);
colorfill(strip.Color(0, 0, 127),10);
}
//if (req.indexOf("L") != -1) {
if (req=='L') {
digitalWrite(relayPin, LOW);
digitalWrite(ledPin, HIGH);
colorfill(0,10);
}
Udp.flush();
//delay(5);
}
}
void colorfill(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
strip.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment