154 lines
3.1 KiB
C++
154 lines
3.1 KiB
C++
/*
|
|
* 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 <WiFiUdp.h>
|
|
#include "ESP8266WiFi.h"
|
|
|
|
#include "relais.h"
|
|
#include "wlan.h"
|
|
#include "fader.h"
|
|
#include "ws2801.h"
|
|
|
|
WiFiUDP udpServer;
|
|
|
|
#define UDP_PORT 2703
|
|
|
|
#define NWLANFAV 2
|
|
|
|
const char *favorite_wlans[2*NWLANFAV] = {
|
|
// SSID, password (NULL if open)
|
|
"Error-403", "2b4EWWpqAyCpgOQK1oeD",
|
|
"franken.freifunk.net", NULL
|
|
};
|
|
|
|
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();
|
|
|
|
ws2801_init();
|
|
fader_init();
|
|
|
|
for(int i = 0; i < NUM_MODULES; i++) {
|
|
fader_set_colour(i, 0, 0, 0);
|
|
fader_fade_colour(i, 0, 255, 0);
|
|
}
|
|
|
|
Serial.print("starting UDP server on port ");
|
|
Serial.println(UDP_PORT);
|
|
|
|
if(!udpServer.begin(UDP_PORT)) {
|
|
Serial.println("UDP server start FAILED!");
|
|
}
|
|
|
|
Serial.println("Setup done");
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
char buf[512];
|
|
|
|
if(WiFi.status() != WL_CONNECTED) {
|
|
// wifi disconnected -> attempt reconnect
|
|
delay(5000);
|
|
|
|
setup_wifi();
|
|
}
|
|
|
|
uint32_t now = millis();
|
|
if(now > fader_next_update) {
|
|
fader_update();
|
|
|
|
fader_next_update += 10; // -> 100 FPS
|
|
|
|
if((fader_loop % 200) == 0) {
|
|
int round = (fader_loop / 200) % 3;
|
|
if(round == 0) {
|
|
for(int i = 0; i < NUM_MODULES; i++) {
|
|
fader_fade_colour(i, 255, 0, 0);
|
|
}
|
|
} else if(round == 1) {
|
|
for(int i = 0; i < NUM_MODULES; i++) {
|
|
fader_fade_colour(i, 0, 255, 0);
|
|
}
|
|
} else {
|
|
for(int i = 0; i < NUM_MODULES; i++) {
|
|
fader_fade_colour(i, 0, 0, 255);
|
|
}
|
|
}
|
|
}
|
|
|
|
fader_loop++;
|
|
}
|
|
|
|
/*int n = udpServer.read(buf, 512);
|
|
|
|
if(n > 0) {
|
|
Serial.print("Received UDP packet with ");
|
|
Serial.print(n);
|
|
Serial.println(" bytes.");
|
|
}*/
|
|
|
|
// if there's data available, read a packet
|
|
int packetSize = udpServer.parsePacket();
|
|
if(packetSize)
|
|
{
|
|
Serial.print("Received packet of size ");
|
|
Serial.println(packetSize);
|
|
Serial.print("From ");
|
|
IPAddress remoteIp = udpServer.remoteIP();
|
|
Serial.print(remoteIp);
|
|
Serial.print(", port ");
|
|
Serial.println(udpServer.remotePort());
|
|
|
|
// read the packet into packetBufffer
|
|
int len = udpServer.read(buf,512);
|
|
if (len >0) buf[len]=0;
|
|
/*
|
|
Serial.println("Contents:");
|
|
Serial.println(buf);
|
|
*/
|
|
|
|
// send a reply, to the IP address and port that sent us the packet we received
|
|
udpServer.beginPacket(udpServer.remoteIP(), udpServer.remotePort());
|
|
udpServer.write("OK\n");
|
|
udpServer.endPacket();
|
|
}
|
|
|
|
relais_loop();
|
|
}
|