esp8266-ws2801d/src/main.cpp

120 lines
2.2 KiB
C++
Raw Permalink Normal View History

/*
* This sketch demonstrates how to scan WiFi networks.
* The API is almost the same as with the WiFi Shield library,
* the most obvious difference being the different file you need to include:
*/
#include <Arduino.h>
#include "ESP8266WiFi.h"
#include "relais.h"
#include "wlan.h"
2017-03-01 23:56:34 +01:00
#include "fader.h"
#include "ws2801.h"
2017-03-04 21:22:48 +01:00
#include "ws2801_udp.h"
2017-03-04 21:22:48 +01:00
#define NWLANFAV 5
const char *favorite_wlans[2*NWLANFAV] = {
// SSID, password (NULL if unencrypted)
2017-03-04 21:22:48 +01:00
"Error-404", "XKZbEF0AiMNPAGf4EbHAAZ",
"Error-403", "2b4EWWpqAyCpgOQK1oeD",
2017-03-04 21:22:48 +01:00
"bytewerk", "bluemchenwiese",
"Freifunk", NULL,
"franken.freifunk.net", NULL
};
2017-03-01 23:56:34 +01:00
uint32_t fader_next_update = 0;
uint32_t fader_loop = 0;
void setup_wifi()
{
bool connected = false;
for(int i = 0; i < NWLANFAV; i++) {
connected = (0 == wlan_connectto(favorite_wlans[2*i + 0], favorite_wlans[2*i + 1]));
if(connected) {
break;
}
}
if(!connected) {
Serial.println("All predefined WLANs failed to connect. Trying any open network.");
connected = (0 == wlan_connect_any_open());
}
if(connected) {
wlan_print_conninfo();
} else {
Serial.println("All WLAN connections failed. Giving up for now.");
}
}
void setup()
{
Serial.begin(115200);
setup_wifi();
relais_setup();
2017-03-01 23:56:34 +01:00
ws2801_init();
fader_init();
2017-03-04 21:22:48 +01:00
ws2801_udp_setup();
2017-03-05 01:55:40 +01:00
for(int i = 0; i < MAX_NUM_MODULES; i++) {
2017-03-01 23:56:34 +01:00
fader_set_colour(i, 0, 0, 0);
fader_fade_colour(i, 0, 255, 0);
}
Serial.println("Setup done");
}
void loop()
{
char buf[512];
if(WiFi.status() != WL_CONNECTED) {
// wifi disconnected -> attempt reconnect
delay(5000);
setup_wifi();
}
2017-03-01 23:56:34 +01:00
uint32_t now = millis();
if(now > fader_next_update) {
fader_update();
2017-03-02 00:03:14 +01:00
fader_next_update += 10; // -> 100 FPS
2017-03-01 23:56:34 +01:00
2017-03-04 21:22:48 +01:00
/*
2017-03-02 00:03:14 +01:00
if((fader_loop % 200) == 0) {
int round = (fader_loop / 200) % 3;
if(round == 0) {
2017-03-05 01:55:40 +01:00
for(int i = 0; i < MAX_NUM_MODULES; i++) {
2017-03-02 00:03:14 +01:00
fader_fade_colour(i, 255, 0, 0);
}
} else if(round == 1) {
2017-03-05 01:55:40 +01:00
for(int i = 0; i < MAX_NUM_MODULES; i++) {
2017-03-01 23:56:34 +01:00
fader_fade_colour(i, 0, 255, 0);
}
} else {
2017-03-05 01:55:40 +01:00
for(int i = 0; i < MAX_NUM_MODULES; i++) {
2017-03-02 00:03:14 +01:00
fader_fade_colour(i, 0, 0, 255);
2017-03-01 23:56:34 +01:00
}
}
}
2017-03-04 21:22:48 +01:00
*/
2017-03-01 23:56:34 +01:00
fader_loop++;
}
if(ws2801_udp_loop()) {
relais_heartbeat();
}
relais_loop();
}