Skip to content

Instantly share code, notes, and snippets.

@weicks
Created August 28, 2018 13:18
Show Gist options
  • Save weicks/1a7233b1ec0fd82bd97a7dae0edf84d7 to your computer and use it in GitHub Desktop.
Save weicks/1a7233b1ec0fd82bd97a7dae0edf84d7 to your computer and use it in GitHub Desktop.
artnetWifi+SDread-Blynk
//-------------------Set RTC-------------------------------------------------------
#include "time.h"
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
//------------------------------ WiFi details------------------------------
char ssid[] = "Weix";
char pass[] = "asdf1234!";
//const char *ssid = "Weix";
//const char *password = "asdf1234!";
IPAddress local_ip(172, 20, 10, 10);
IPAddress gateway_ip(172, 20, 10, 1);
IPAddress subnet_ip(255, 255, 255, 240);
//------------------------Wifi variables ------------------------------
bool isWifiConnected=false;
bool isBlynkConnected=false;
static TaskHandle_t checkWifiTaskHandle;
static TaskHandle_t userTaskHandle = 0;
int WAIT_WIFI_MAX=100;
int WAIT_WIFI = 0;
//----------------------------------BLYNK------------------------------------------
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp32.h>
/*
#define BLYNK_USE_DIRECT_CONNECT
#include <BlynkSimpleEsp32_BLE.h>
#include <BLEDevice.h>
#include <BLEServer.h>
*/
//char auth[] = "4ef690bc3df245299df2307ee2ea8b41";
char auth[] = "7a38277d88df4359ae5e62cc646e8105";
BlynkTimer timer;
//--------------------------------Clock display---------------------------------
#include <Adafruit_GFX.h>
#include <FastLED.h>
#include <FastLED_NeoMatrix.h>
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS1307.h>
RtcDS1307<TwoWire> Rtc(Wire);
#define mw 80
#define mh 32
#define NUMMATRIX (mw*mh)
#define LED_RUN_LENGTH 16//number of leds per column
CRGB matrixleds[NUMMATRIX];
FastLED_NeoMatrix *matrix = new FastLED_NeoMatrix(matrixleds,16,16,5,2,
NEO_TILE_TOP + NEO_TILE_LEFT + NEO_TILE_ROWS +NEO_TILE_ZIGZAG+
NEO_MATRIX_TOP + NEO_MATRIX_LEFT +
NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG );
const uint16_t colors[] = {
matrix->Color(255, 0, 0), matrix->Color(0, 255, 0), matrix->Color(0, 0, 255) };
//-------------------------------
#include <WiFi.h>
//#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Artnet.h>
#include "SD.h"
#include "SPI.h"
//#define FASTLED_ALLOW_INTERRUPTS 0
//#define INTERRUPT_THRESHOLD 1
#include "FastLED.h"
FASTLED_USING_NAMESPACE
#define FASTLED_SHOW_CORE 0
#define LED_WIDTH 80
#define LED_HEIGHT 32
#define NUM_LEDS LED_WIDTH*LED_HEIGHT
#define UNIVERSE_SIZE 170
#define PIXEL_PER_PIN NUM_LEDS/5
/*we will use 5 pins each pin will drive two 16x16 panel hence 512 pix it will take 15ms to refresh your entire panel instead of 76ms
hence having a refresh rate of 65fps instead of 13fps*/
#define PIN1 2
#define PIN2 4
#define PIN3 27
#define PIN4 12
#define PIN5 13
//CRGB leds[NUM_LEDS];
uint8_t readbuffer[NUM_LEDS*3];
char filename[256];
char READ_NAME[]="savedata"; //the name of your save
Artnet artnet;
File root;
File myFile;
#define BRIGHTNESS bright
char datestring[20];
char blank[20];
int MatrixTime=1;
int Screen=1;
int bright=100;
BLYNK_WRITE(V0)
{ Screen=param.asInt();
}
BLYNK_WRITE(V1) //
{ MatrixTime=param.asInt();
}
BLYNK_WRITE(V2) // Slider - Brightnes
{
bright = param.asLong();
}
void checkWifi()
{
if (userTaskHandle == 0) {
const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 200 );
// -- Store the handle of the current task, so that the show task can
// notify it when it's done
// noInterrupts();
userTaskHandle = xTaskGetCurrentTaskHandle();
// -- Trigger the show task
xTaskNotifyGive(checkWifiTaskHandle);
//to thge contrary to the other one we do not wait for the display task to come back
}
}
void checkWifiTask(void *pvParameters)
{
//Serial.printf("djdj\n");
const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 500 );
// -- Run forever...
for(;;) {
// -- Wait for the trigger
ulTaskNotifyTake(pdTRUE,portMAX_DELAY);
while(1)
{
if((WiFi.status() == WL_CONNECTED))
{
if(!Blynk.connected()){
isBlynkConnected=false;
Blynk.config(auth);
Blynk.connect();
delay(500);
while ( !Blynk.connect() )
{
delay(500);
Serial.print("trying Connect blynk");
}
if(Blynk.connected()){
Serial.print("Blynk Connected");
isBlynkConnected=true;
}
}
else
{
isBlynkConnected=true;
}
}
if(WiFi.status() != WL_CONNECTED)
{
Serial.print("trying to connect to the Wifi");
isWifiConnected=false;
isBlynkConnected=false;
Blynk.disconnect();
WiFi.begin(ssid, pass);
while ( (WiFi.status() != WL_CONNECTED) && (WAIT_WIFI<WAIT_WIFI_MAX) )
{
Serial.println(WiFi.status());
delay(500);
//Serial.print(".");
WAIT_WIFI++;
}
WAIT_WIFI=0;
if(WiFi.status()==WL_CONNECTED)
{
Serial.print("Wifi Connected");
isWifiConnected=true;
Blynk.config(auth);
Blynk.connect();
delay(500);
while ( !Blynk.connect())
{
delay(500);
Serial.print("trying Connect blynk");
}
if(Blynk.connected()){
Serial.print("Blynk Connected");
isBlynkConnected=true;
}
}
}
// memcpy(leds,Tpic,LED_WIDTH*LED_HEIGHT*sizeof(CRGB));
delay(1000 );
//yield();
}
}
}
//-----------------------------------------------SETUP---------------------------------------------
void setup() {
Serial.begin(115200);
//WiFi.config(local_ip, gateway_ip, subnet_ip);
delay(100);
WiFi.begin("Weix", "asdf1234!");
delay(1500);
Blynk.config(auth);
Blynk.connect();
//Blynk.connectWiFi(ssid, pass);
/*
if (WiFi.status() == WL_CONNECTED) {
//Blynk.connectWiFi(ssid, pass);
reconnect=false;
}
if (WiFi.status() != WL_CONNECTED)
{ Blynk.disconnect();
//reconnect=true;
}
*/
Rtc.Begin();
RTCtime();
artnet.begin(NUM_LEDS+160,UNIVERSE_SIZE,1);
//Blynk.begin(auth);
xTaskCreatePinnedToCore(checkWifiTask, "checkWifiTask", 3000, NULL,3, &checkWifiTaskHandle, FASTLED_SHOW_CORE);
FastLED.setBrightness(BRIGHTNESS);
if(!SD.begin(5,SPI,80000000)){
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
}
memset(filename, 0, 255);
sprintf(filename,"/%s.dat",READ_NAME);
myFile = SD.open(filename);
FastLED.addLeds<WS2812B, PIN1, GRB>(matrixleds, 0*PIXEL_PER_PIN, PIXEL_PER_PIN);
FastLED.addLeds<WS2812B, PIN2, GRB>(matrixleds, 1*PIXEL_PER_PIN, PIXEL_PER_PIN);
FastLED.addLeds<WS2812B, PIN3, GRB>(matrixleds, 2*PIXEL_PER_PIN, PIXEL_PER_PIN);
FastLED.addLeds<WS2812B, PIN4, GRB>(matrixleds, 3*PIXEL_PER_PIN, PIXEL_PER_PIN);
FastLED.addLeds<WS2812B, PIN5, GRB>(matrixleds, 4*PIXEL_PER_PIN, PIXEL_PER_PIN);
matrix->begin();
matrix->setTextWrap(true);
//matrix->setBrightness(100); //doesnt work here
matrix->setTextColor(colors[40]); //colors 0 is normal! donnt know what i did .. but colors[40] is disapeared fonts :)
checkWifi(); //launching the process to check the wifi on core 0
}
bool artnetdisplay=false;
///----------------------------------------LOOP---------------------------------------------
void loop() {
if(isBlynkConnected){
Blynk.run();
RTCtime();
}
FastLED.setBrightness(BRIGHTNESS);
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
if(Screen==1)
{
Serial.println("SD-Card");
if (myFile.available())
{
myFile.read(readbuffer,NUM_LEDS*sizeof(CRGB));
memcpy(matrixleds,readbuffer,NUM_LEDS*sizeof(CRGB));
}
else
{
myFile.seek(0);
}
}
else
{
Serial.println("Artnet-Wifi");
// if(isWifiConnected){
if(artnet.read());
memcpy(matrixleds,artnet.getframe(),NUM_LEDS*sizeof(CRGB));
artnet.resetsync();
// }
}
if(MatrixTime==1){
Serial.println("on");
matrix->print (datestring);
}
else{
Serial.println("off");
matrix->print (blank);
}
FastLED.show();
}
///----------------------------------------Print Time ---------------------------------------------
void printDateTime(const RtcDateTime& dt)
{
sprintf(datestring,"%02u:%02u:%02u", dt.Hour(), dt.Minute(), dt.Second());
sprintf(blank,"");
Serial.print(datestring);
matrix->clear();
matrix->setCursor(0, 0);
}
void RTCtime(){
///----------------------------------------RTC-Set---------------------------------------------
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
struct tm timeinfo;
Rtc.Begin();
RtcDateTime compiled;
if(getLocalTime(&timeinfo))
{
compiled =RtcDateTime(
(uint16_t )timeinfo.tm_year,
(uint8_t) timeinfo.tm_mon,
(uint8_t) timeinfo.tm_mday,
(uint8_t) timeinfo.tm_hour,
(uint8_t) timeinfo.tm_min,
(uint8_t) timeinfo.tm_sec) ;
}
else
{
Serial.println("error");
}
if (!Rtc.IsDateTimeValid())
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
Rtc.SetSquareWavePin(DS1307SquareWaveOut_Low);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment