Created
January 10, 2020 22:19
-
-
Save thexeno/eaae44bf3bd3eba2e3e2ffefe9016ded to your computer and use it in GitHub Desktop.
Code for the ESP32 Arduino based Stranger Things Christmas project, documented on https://hackaday.io/project/28603
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* USAGE: | |
* After upload, will run a default sentence on APA102 string. To upload a new one, just send a new one from the serial line. | |
* | |
* stranger_lights.ino - StrangerThings lights control - Version 1.0 | |
* Created on 12/2017 | |
* by Enrico Sanino | |
* | |
* This library is free software; you can redistribute it and/or | |
* modify it under the terms of the GNU Lesser General Public | |
* License as published by the Free Software Foundation; either | |
* version 2.1 of the License, or (at your option) any later version. | |
* | |
* This library is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
* Lesser General Public License for more details. | |
* | |
* You should have received a copy of the GNU Lesser General Public | |
* License along with this library; if not, write to the Free Software | |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
#include <aREST.h> | |
#include <APA102.h> | |
#include <WiFi.h> | |
//#include "EEPROM.h" | |
#include <WiFiClient.h> | |
#include <WiFiServer.h> | |
#include <WiFiUdp.h> | |
#include <string.h> | |
#include <Preferences.h> | |
#include "SimpleBLE.h" | |
#define STRIP_SIZE 26 | |
#define INPUT_BUFF_SIZE 64 | |
#define ITERATIONS 10 | |
// Define which pins to use. | |
SimpleBLE ble; | |
Preferences preferences; | |
//int addr = 0; | |
//#define EEPROM_SIZE 64 | |
String beaconMsg = "Stranger Things V2.0"; | |
String value = ""; | |
const uint8_t dataPin = 17; | |
const uint8_t clockPin = 4; | |
char wifiDataFlag = 0; | |
void wifiGetText(void); | |
char wifiDataAvailable(void); | |
const char* ssid = "YOUR SSID"; | |
const char* password = "YOUR PASSWORD"; | |
char bufferLine[255]; //buffer to hold incoming packet | |
// Create an object for writing to the LED strip. | |
APA102<dataPin, clockPin> ledStrip; | |
int index1[STRIP_SIZE] = { 25, 24, 23, 22, 21, 20, 19, 18, 10, 11, 12, 13, 14, 15, 16, 17, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; | |
char input_buffer[INPUT_BUFF_SIZE] = "merry christmas"; | |
volatile int input_size = 15; | |
rgb_color colors[STRIP_SIZE]; | |
WiFiServer server(80); | |
String ipToString(IPAddress ip){ | |
String s=""; | |
for (int i=0; i<4; i++) | |
s += i ? "." + String(ip[i]) : String(ip[i]); | |
return s; | |
} | |
void setup() { | |
pinMode(13, OUTPUT); | |
preferences.begin("sthings", false); | |
Serial.begin(115200); | |
ledStrip.startFrame(); | |
Serial.print("a"); | |
for (int i = 0; i < STRIP_SIZE; i++) { | |
colors[i] = rgb_color(0,0,0); | |
} | |
ledStrip.write(colors, STRIP_SIZE, 31); | |
ledStrip.endFrame(STRIP_SIZE); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
int cnt = 10; | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
cnt--; | |
if (cnt <= 0) | |
{ | |
break; | |
} | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected."); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
// sprintf(value, "%s", WiFi.localIP()); | |
//sprintf(value, "%x.%x.%x.%x", value[0], value[1], value[2], value[3]); | |
value = ipToString(WiFi.localIP()); | |
Serial.println("VALORE "); | |
Serial.println(value); | |
Serial.println("\VALORE "); | |
ble.begin(value); | |
Serial.println("Gateway: "); | |
Serial.println(WiFi.gatewayIP()); | |
server.begin(); | |
String tempStr = preferences.getString("sentence", "none"); | |
//String tempStrLen = preferences.getString("len", "none"); | |
tempStr.toCharArray(input_buffer, tempStr.length()+1); | |
input_buffer[tempStr.length()+1] = '\0'; | |
preferences.end(); | |
Serial.println("Initial buffer: "); | |
Serial.println(input_buffer); | |
Serial.println(strlen(input_buffer)); | |
input_size = strlen(input_buffer); | |
Serial.println(tempStr.length()); | |
Serial.println(); | |
} | |
void loop(){ | |
wifiGetText(); | |
if (Serial.available() > 0) { | |
input_size = Serial.available(); | |
Serial.readBytes(input_buffer, input_size); | |
input_buffer[input_size] = '\0'; | |
Serial.print(input_buffer); | |
Serial.print(" "); | |
Serial.print(input_size+1); | |
preferences.begin("sthings", false); | |
preferences.remove("sentence"); | |
preferences.putString("sentence", input_buffer); | |
preferences.end(); | |
} | |
else if (wifiDataAvailable()) { | |
input_size = strlen(bufferLine); | |
strcpy(input_buffer, bufferLine); | |
Serial.readBytes(input_buffer, input_size); | |
input_buffer[input_size+1] = '\0'; | |
Serial.print(input_buffer); | |
Serial.print(" "); | |
Serial.print(input_size); | |
preferences.begin("sthings", false); | |
preferences.remove("sentence"); | |
preferences.putString("sentence", input_buffer); | |
preferences.end(); | |
} | |
ledStrip.startFrame(); | |
process(input_buffer, input_size); | |
ledStrip.endFrame(STRIP_SIZE); | |
delay(500); | |
for (int i = 0; i < STRIP_SIZE; i++) { | |
colors[i] = rgb_color(64,64,64); | |
} | |
for (int i = 0; i < 31; i++) { | |
ledStrip.startFrame(); | |
ledStrip.write(colors, STRIP_SIZE, i); | |
ledStrip.endFrame(STRIP_SIZE); | |
delay(10); | |
} | |
for (int i = 31; i >= 0; i--) { | |
ledStrip.startFrame(); | |
ledStrip.write(colors, STRIP_SIZE, i); | |
ledStrip.endFrame(STRIP_SIZE); | |
delay(10); | |
} | |
for (int i = 0; i < STRIP_SIZE; i++) { | |
colors[i] = rgb_color(0,0,0); | |
} | |
ledStrip.startFrame(); | |
ledStrip.write(colors, STRIP_SIZE, 31); | |
ledStrip.endFrame(STRIP_SIZE); | |
delay(500); | |
} | |
char wifiDataAvailable(void) | |
{ | |
char ret = 0; | |
ret = wifiDataFlag; | |
wifiDataFlag = 0; | |
return ret; | |
} | |
void wifiGetText(void) | |
{ | |
WiFiClient client = server.available(); // listen for incoming clients | |
if (client) | |
{ // if you get a client, | |
Serial.println("New Client."); // print a message out the serial port | |
String currentLine = ""; // make a String to hold incoming data from the client | |
while (client.connected()) | |
{ // loop while the client's connected | |
if (client.available()) | |
{ // if there's bytes to read from the client, | |
char c = client.read(); // read a byte, then | |
Serial.write(c); // print it out the serial monitor | |
if (c == '\n') | |
{ // if the byte is a newline character | |
// if the current line is blank, you got two newline characters in a row. | |
// that's the end of the client HTTP request, so send a response: | |
if (currentLine.length() == 0) { | |
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) | |
// and a content-type so the client knows what's coming, then a blank line: | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-type:text/html"); | |
client.println(); | |
// the content of the HTTP response follows the header: | |
client.print("<!DOCTYPE html>"); | |
client.print("<html>"); | |
client.print("<body>"); | |
client.print("<form action=\"/action_page.php\">"); | |
client.print("Text: <input type=\"text\" name=\"text\" value=\"Mickey\"><br>"); | |
client.print("<input type=\"submit\" value=\"Submit\">"); | |
client.print("</form>"); | |
client.print("</html>"); | |
client.print("</body>"); | |
// The HTTP response ends with another blank line: | |
client.println(); | |
// break out of the while loop: | |
break; | |
} | |
else | |
{ // if you got a newline, then clear currentLine: | |
currentLine = ""; | |
} | |
} | |
else if (c != '\r') | |
{ // if you got anything else but a carriage return character, | |
currentLine += c; // add it to the end of the currentLine | |
} | |
if (currentLine.endsWith("HTTP/1.1")) | |
{ | |
int len = currentLine.length(); | |
for (int i = len; i > 0; i--) | |
{ | |
if (currentLine[i] == '=') | |
{ | |
i++; | |
for (int j=0; j<255; j++) | |
{ | |
bufferLine[j] = currentLine[i+j]; | |
if (currentLine.substring(i+j) == "HTTP/1.1") | |
{ | |
bufferLine[j] = '\0'; | |
i = 0; | |
wifiDataFlag = 1; | |
break; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
// close the connection: | |
client.stop(); | |
Serial.println("Client Disconnected."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment