Skip to content

Instantly share code, notes, and snippets.

@tonyjoanes
Created July 17, 2019 08:06
Show Gist options
  • Save tonyjoanes/07fe944cd5ce50a2f85f7f14b8aa19cf to your computer and use it in GitHub Desktop.
Save tonyjoanes/07fe944cd5ce50a2f85f7f14b8aa19cf to your computer and use it in GitHub Desktop.
// working d. bodnar 12-21-2017
/*
static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;
static const uint8_t D5 = 14;
static const uint8_t D6 = 12;
static const uint8_t D7 = 13;
static const uint8_t D8 = 15;
static const uint8_t RX = 3;
static const uint8_t TX = 1;
*/
#include <Servo.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>
#include "switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"
unsigned long previousMillis = 0;
const long interval = 1000;
boolean connectWifi();
void shiftkeyOn();
void shiftkeyOff();
// Change this before you flash
const char* ssid = "main2";
const char* password = "pppppppp";
boolean wifiConnected = false;
UpnpBroadcastResponder upnpBroadcastResponder;
Switch *shiftkey = NULL;
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup()
{
pinMode(BUILTIN_LED, OUTPUT); // set onboard LED as output
Serial.begin(9600);
wifiConnected = connectWifi();
if (wifiConnected) {
upnpBroadcastResponder.beginUdpMulticast();
shiftkey = new Switch("shiftkey ", 80, shiftkeyOn, shiftkeyOff);
Serial.println("Adding switches upnp broadcast responder");
upnpBroadcastResponder.addDevice(*shiftkey);
}
}
void loop()
{
if (wifiConnected) {
upnpBroadcastResponder.serverLoop();
shiftkey->serverLoop();
}
// unsigned long currentMillis = millis();
if (millis() - previousMillis >= interval) {
previousMillis = millis();
digitalWrite(BUILTIN_LED, !digitalRead(BUILTIN_LED));
}
}
void shiftkeyOn() {
myservo.attach(14); // attaches the servo on pin 12 to the servo object
Serial.print("Switch 1 turn on ...");
for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.println(pos);
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.println(pos);
delay(15); // waits 15ms for the servo to reach the position
}
myservo.detach(); // attaches the servo on pin 12 to the servo object
}
void shiftkeyOff() {
Serial.print("Switch 1 turn off ...");
}
// connect to wifi – returns true if successful or false if not
boolean connectWifi() {
boolean state = true;
int i = 0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");
// Wait for connection
Serial.print("Connecting ...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (i > 10) {
state = false;
break;
}
i++;
}
if (state) {
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("");
Serial.println("Connection failed.");
}
return state;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment