Andres Santos
Published © Apache-2.0

Arduino NeoPixel Wifi

An application to rule the neopixels all.

AdvancedShowcase (no instructions)10 hours11,476
Arduino NeoPixel Wifi

Things used in this project

Story

Read more

Schematics

Neopixel Wiring

Wiring neopixel

Code

Arduino code for WIFI neopixels

Arduino
You need several libraries such as the latest wifi101
/*
 
 created 30 march 2016
 by andres santos
 

*/

#include <SPI.h>
#include <WiFi101.h>
#include <WiFiMDNSResponder.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

char ssid[] = "unPhone";      // your network SSID (name)
char pass[] = "internetme";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

char mdnsName[] = "wifi101"; // the MDNS name that the board will respond to
// Note that the actual MDNS name will have '.local' after
// the name above, so "wifi101" will be accessible on
// the MDNS name "wifi101.local".
String getContent="";
bool isGet=false;
String gotContent="";
char* buf="";
int status = WL_IDLE_STATUS;

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            6

// How many NeoPixels are attached to the Arduino?
int numPixels=30; 
// Create a MDNS responder to listen and respond to MDNS name requests.
WiFiMDNSResponder mdnsResponder;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(numPixels, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 500; // delay for half a second


WiFiServer server(80);

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  // you're connected now, so print out the status:
  printWifiStatus();

  server.begin();
 pixels.begin();
  // Setup the MDNS responder to listen to the configured name.
  // NOTE: You _must_ call this _after_ connecting to the WiFi network and
  // being assigned an IP address.
  if (!mdnsResponder.begin(mdnsName)) {
    Serial.println("Failed to start MDNS responder!");
    while(1);
  }

  Serial.print("Server listening at http://");
  Serial.print(mdnsName);
  Serial.println(".local/");
}
String pos1, pos2, id3;
int rc=0,gc=150,bc=0;
void loop() {
  // Call the update() function on the MDNS responder every loop iteration to
  // make sure it can detect and respond to name requests.
  mdnsResponder.poll();

  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          break;
        }
        if(getContent=="GET"){
          isGet=true;
        }
        
        if (c == '\r'){
          isGet=false;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          isGet=false;
          
        }
        else if (c != '\r') {
          getContent=getContent+c;
         
          if(isGet){
            gotContent=gotContent+c;
          }
          // you've gotten a character on the current line
          currentLineIsBlank = false;
          
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);

    // close the connection:
    client.stop();
    getContent="";
    //separating characters
    int firstEncounter=gotContent.indexOf(" H");
    String realString=gotContent.substring(0,firstEncounter);
    Serial.println(realString.substring(0,4));
    if(realString==" /on"){
      for(int i=0;i<numPixels;i++){
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
      pixels.setPixelColor(i, pixels.Color(rc,gc,bc)); // Moderately bright green color.
      pixels.show(); // This sends the updated pixel color to the hardware.
       // Delay for a period of time (in milliseconds).
  }
    } else if(realString==" /off"){
      Serial.println("OFFING");
      for(int i=0;i<numPixels;i++){
        // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
        pixels.setPixelColor(i, 0); // Moderately bright green color.
        pixels.show(); // This sends the updated pixel color to the hardware.
         // Delay for a period of time (in milliseconds).
      }
    } else if(realString.substring(0,4)==" /co"){
      rc=realString.substring(4,7).toInt();
      gc=realString.substring(7,10).toInt();
      bc=realString.substring(10,13).toInt();
      for(int i=0;i<numPixels;i++){
        // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
        pixels.setPixelColor(i, pixels.Color(rc,gc,bc)); // Moderately bright green color.
        pixels.show(); // This sends the updated pixel color to the hardware.
         // Delay for a period of time (in milliseconds).
      }
    } else if(realString.substring(0,4)==" /nu"){
      int changePixelNumber=realString.substring(4).toInt();
      numPixels=changePixelNumber;
    } else if(realString.substring(0,4)==" /le"){
      for(int i=0;i<numPixels;i++){
        // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
        pixels.setPixelColor(i, 0); // Moderately bright green color.
        pixels.show(); // This sends the updated pixel color to the hardware.
         // Delay for a period of time (in milliseconds).
      }
      int incomingPixel=realString.substring(4).toInt();
      pixels.setPixelColor(incomingPixel  , pixels.Color(rc,gc,bc));
    }
    Serial.println(realString);
    gotContent="";
    buf="";
    Serial.println("client disconnected");
  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Credits

Andres Santos

Andres Santos

12 projects • 42 followers
I am just a dude with some spare parts

Comments