Initial commit: relais is switchable over UDP

This commit is contained in:
Thomas Kolb 2017-03-01 22:57:37 +01:00
commit 6a573d176a
7 changed files with 337 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
.pioenvs
.piolibdeps
.clang_complete
.gcc-flags.json

14
platformio.ini Normal file
View file

@ -0,0 +1,14 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; http://docs.platformio.org/page/projectconf.html
[env:modwifi]
platform = espressif8266
board = modwifi
framework = arduino

114
src/main.cpp Normal file
View file

@ -0,0 +1,114 @@
/*
* 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"
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
};
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();
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();
}
/*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();
}

76
src/relais.cpp Normal file
View file

@ -0,0 +1,76 @@
#include <WiFiUdp.h>
#include <Arduino.h>
#include "relais.h"
WiFiUDP relaisUDP;
#define RELAIS_PIN 5
#define RELAIS_UDP_PORT 2800
void relais_setup(void)
{
Serial.println("Relais: Setting up pin.");
digitalWrite(RELAIS_PIN, LOW);
pinMode(RELAIS_PIN, OUTPUT);
Serial.print("Relais: Starting UDP server on port ");
Serial.println(RELAIS_UDP_PORT);
if(!relaisUDP.begin(RELAIS_UDP_PORT)) {
Serial.println("Relais: UDP server start FAILED!");
}
Serial.println("Relais setup done");
}
void relais_loop(void)
{
unsigned char databyte;
unsigned char answer;
int packetSize = relaisUDP.parsePacket();
if(packetSize)
{
Serial.print("Relais: Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = relaisUDP.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(relaisUDP.remotePort());
if(packetSize == 1) {
int len = relaisUDP.read(&databyte,1);
switch(databyte) {
case 0: // switch relais off
Serial.println("Relais: switch off.");
digitalWrite(RELAIS_PIN, LOW);
answer = 0;
break;
case 1: // switch relais on
Serial.println("Relais: switch on.");
digitalWrite(RELAIS_PIN, HIGH);
answer = 1;
break;
default: // unknown code -> error
Serial.println("Relais: wrong code.");
answer = 0xFF;
break;
}
} else { // wrong length
Serial.println("Relais: wrong packet length.");
answer = 0xFF;
}
// send a reply, to the IP address and port that sent us the packet we received
relaisUDP.beginPacket(relaisUDP.remoteIP(), relaisUDP.remotePort());
relaisUDP.write(answer);
relaisUDP.endPacket();
}
}

7
src/relais.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef RELAIS_H
#define RELAIS_H
void relais_setup(void);
void relais_loop(void);
#endif // RELAIS_H

113
src/wlan.cpp Normal file
View file

@ -0,0 +1,113 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
void wlan_reset(void)
{
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
}
int wlan_connectto(const char *ssid, const char *passwd)
{
int timeout = 60; // half-seconds
Serial.print("WLAN: connecting to ");
Serial.print(ssid);
if(passwd) {
WiFi.begin(ssid, passwd);
} else {
WiFi.begin(ssid);
}
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
if(--timeout == 0) {
Serial.println(" timeout :-(");
return -1;
}
}
Serial.println(" connected.");
Serial.print("WLAN: IP address: ");
Serial.println(WiFi.localIP());
return 0;
}
int wlan_connect_any_open(void)
{
Serial.println("WLAN: scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("WLAN: scan done");
Serial.print("WLAN: ");
Serial.print(n);
Serial.println(" networks found");
String ssid = "";
for(int i = 0; i < n; i++)
{
if(WiFi.encryptionType(i) == ENC_TYPE_NONE) {
if(wlan_connectto(ssid.c_str(), NULL) == 0) {
return 0;
}
}
}
if(ssid == "") {
Serial.println("WLAN: No open networks found to connect to.");
return -2;
}
return -1;
}
void wlan_print_conninfo(void)
{
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
for(int i = 5; i > 0; i--) {
Serial.print(mac[i],HEX);
Serial.print(":");
}
Serial.println(mac[0],HEX);
// BSSID
byte *bssid = WiFi.BSSID();
Serial.print("BSSID: ");
for(int i = 5; i > 0; i--) {
Serial.print(bssid[i],HEX);
Serial.print(":");
}
Serial.println(bssid[0],HEX);
// SSID
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the received signal strength:
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());
/*
// print the encryption type:
Serial.print("Encryption: ");
Serial.println(WiFi.encryptionType(),HEX);
Serial.println();
*/
}

9
src/wlan.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef WLAN_H
#define WLAN_H
void wlan_reset(void);
int wlan_connectto(const char *ssid, const char *passwd);
int wlan_connect_any_open(void);
void wlan_print_conninfo(void);
#endif // WLAN_H