Garage mit RasPi, Versuche mit BLE
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,193 @@
|
|||||||
|
#include <SPI.h>
|
||||||
|
#include <Ethernet.h>
|
||||||
|
|
||||||
|
// Enter a MAC address and IP address for your controller below.
|
||||||
|
byte mac[] = {0xA4, 0xDA, 0x49, 0xB8, 0x19, 0xA5};
|
||||||
|
IPAddress ip(192, 168, 178, 52);
|
||||||
|
EthernetServer server(80);
|
||||||
|
|
||||||
|
const int remotePin = 2;
|
||||||
|
const int motorPin = 3;
|
||||||
|
|
||||||
|
const int ledPin = 5;
|
||||||
|
|
||||||
|
const int remoteButton = 6;
|
||||||
|
const int motorButton = 7;
|
||||||
|
|
||||||
|
const int sensorPin = A0;
|
||||||
|
|
||||||
|
const int clickDelay = 500;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(9600);
|
||||||
|
|
||||||
|
//Turn off WiFi
|
||||||
|
WiFi.mode(WIFI_OFF);
|
||||||
|
//WiFi.forceSleepBegin(); //This also works
|
||||||
|
|
||||||
|
|
||||||
|
HIER GEHT'S WEITER
|
||||||
|
|
||||||
|
/* // Check PINs
|
||||||
|
Serial.print("MISO: ");
|
||||||
|
Serial.println(PIN_SPI_MISO);
|
||||||
|
Serial.print("MOSI: ");
|
||||||
|
Serial.println(PIN_SPI_MOSI);
|
||||||
|
Serial.print("SCK: ");
|
||||||
|
Serial.println(PIN_SPI_SCK);
|
||||||
|
Serial.print("SS: ");
|
||||||
|
Serial.println(PIN_SPI_SS);*/
|
||||||
|
|
||||||
|
pinMode(remotePin, OUTPUT);
|
||||||
|
pinMode(motorPin, OUTPUT);
|
||||||
|
pinMode(ledPin, OUTPUT);
|
||||||
|
pinMode(remoteButton, INPUT);
|
||||||
|
pinMode(motorButton, INPUT);
|
||||||
|
|
||||||
|
// Serial.println("Starting ethernet");
|
||||||
|
Ethernet.begin(mac, ip);
|
||||||
|
|
||||||
|
// Serial.println(Ethernet.localIP());
|
||||||
|
|
||||||
|
// Serial.println("init complete");
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
if (digitalRead(remoteButton) == HIGH) {
|
||||||
|
// Serial.println("Remote button pressed");
|
||||||
|
triggerRemote();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (digitalRead(motorButton) == HIGH) {
|
||||||
|
// Serial.println("Motor button pressed");
|
||||||
|
triggerMotor();
|
||||||
|
}
|
||||||
|
|
||||||
|
// listen for incoming clients
|
||||||
|
EthernetClient client = server.available();
|
||||||
|
if (client) {
|
||||||
|
// Serial.println("new client");
|
||||||
|
// an http request ends with a blank line
|
||||||
|
bool currentLineIsBlank = true;
|
||||||
|
|
||||||
|
int sensorValue = 0;
|
||||||
|
char uri[64];
|
||||||
|
byte idx = 0;
|
||||||
|
boolean uriLine = false;
|
||||||
|
|
||||||
|
while (client.connected()) {
|
||||||
|
if (client.available()) {
|
||||||
|
char c = client.read();
|
||||||
|
|
||||||
|
if (currentLineIsBlank && c == 'G') {
|
||||||
|
uriLine = true;
|
||||||
|
uri[idx] = c;
|
||||||
|
idx++;
|
||||||
|
}
|
||||||
|
else if (uriLine) {
|
||||||
|
if (c == '\n') {
|
||||||
|
uriLine = false;
|
||||||
|
uri[idx] = '\0';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
uri[idx] = c;
|
||||||
|
idx++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serial.write(c);
|
||||||
|
// if you've gotten to the end of the line (received a newline
|
||||||
|
// character) and the line is blank, the http request has ended,
|
||||||
|
// so you can send a reply
|
||||||
|
if (c == '\n' && currentLineIsBlank) {
|
||||||
|
// send a standard http response header
|
||||||
|
client.println("HTTP/1.1 200 OK");
|
||||||
|
client.println("Content-Type: text/plain");
|
||||||
|
client.println("Connection: close"); // the connection will be closed after completion of the response
|
||||||
|
client.println();
|
||||||
|
if (uri[4] == '/' && uri[5] == 'r' && uri[6] == ' ') {
|
||||||
|
client.println("trigger remote");
|
||||||
|
triggerRemote();
|
||||||
|
}
|
||||||
|
else if (uri[4] == '/' && uri[5] == 'm' && uri[6] == ' ') {
|
||||||
|
client.println("trigger motor");
|
||||||
|
triggerMotor();
|
||||||
|
}
|
||||||
|
else if (uri[4] == '/' && uri[5] == 'a' && uri[6] == ' ') {
|
||||||
|
client.println("trigger all");
|
||||||
|
triggerRemote();
|
||||||
|
triggerMotor();
|
||||||
|
}
|
||||||
|
else if (uri[4] == '/' && uri[5] == 'o' && uri[6] == ' ') {
|
||||||
|
// check if open
|
||||||
|
if (sensorValue > 512) {
|
||||||
|
client.println("closed, opening now");
|
||||||
|
triggerMotor();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
client.println("already open");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (uri[4] == '/' && uri[5] == 'c' && uri[6] == ' ') {
|
||||||
|
// check if open
|
||||||
|
if (sensorValue > 512) {
|
||||||
|
client.println("already closed");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
client.println("open, closing now");
|
||||||
|
triggerMotor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (uri[4] == '/' && uri[5] == 's' && uri[6] == ' ') {
|
||||||
|
sensorValue = analogRead(sensorPin);
|
||||||
|
|
||||||
|
if (sensorValue > 512) {
|
||||||
|
client.println("true");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
client.println("false");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (uri[4] == '/' && uri[5] == 'p' && uri[6] == ' ') {
|
||||||
|
client.println("ping");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
client.println("not found");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (c == '\n') {
|
||||||
|
// you're starting a new line
|
||||||
|
currentLineIsBlank = true;
|
||||||
|
} else if (c != '\r') {
|
||||||
|
// you've gotten a character on the current line
|
||||||
|
currentLineIsBlank = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// give the web browser time to receive the data
|
||||||
|
delay(1);
|
||||||
|
// close the connection:
|
||||||
|
client.stop();
|
||||||
|
// Serial.println("client disconnected");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void triggerRemote() {
|
||||||
|
// Serial.println("triggerRemote");
|
||||||
|
digitalWrite(ledPin, HIGH);
|
||||||
|
digitalWrite(remotePin, HIGH);
|
||||||
|
delay(clickDelay);
|
||||||
|
digitalWrite(remotePin, LOW);
|
||||||
|
digitalWrite(ledPin, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
void triggerMotor() {
|
||||||
|
// Serial.println("triggerMotor");
|
||||||
|
digitalWrite(ledPin, HIGH);
|
||||||
|
digitalWrite(motorPin, HIGH);
|
||||||
|
delay(clickDelay);
|
||||||
|
digitalWrite(motorPin, LOW);
|
||||||
|
digitalWrite(ledPin, LOW);
|
||||||
|
}
|
||||||
@@ -0,0 +1,245 @@
|
|||||||
|
/*--------------------------------------------------
|
||||||
|
HTTP 1.1 Webserver for ESP8266
|
||||||
|
for ESP8266 adapted Arduino IDE
|
||||||
|
|
||||||
|
Stefan Thesen 04/2015
|
||||||
|
|
||||||
|
Running stable for days
|
||||||
|
(in difference to all samples I tried)
|
||||||
|
|
||||||
|
Does HTTP 1.1 with defined connection closing.
|
||||||
|
Reconnects in case of lost WiFi.
|
||||||
|
Handles empty requests in a defined manner.
|
||||||
|
Handle requests for non-exisiting pages correctly.
|
||||||
|
|
||||||
|
This demo allows to switch two functions:
|
||||||
|
Function 1 creates serial output and toggels GPIO2
|
||||||
|
Function 2 just creates serial output.
|
||||||
|
|
||||||
|
Serial output can e.g. be used to steer an attached
|
||||||
|
Arduino, Raspberry etc.
|
||||||
|
--------------------------------------------------*/
|
||||||
|
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
|
||||||
|
const char* ssid = "LeWe-mobil";
|
||||||
|
const char* password = "XXXXXXXXX";
|
||||||
|
|
||||||
|
unsigned long ulReqcount;
|
||||||
|
unsigned long ulReconncount;
|
||||||
|
|
||||||
|
|
||||||
|
// Create an instance of the server on Port 80
|
||||||
|
WiFiServer server(80);
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
// setup globals
|
||||||
|
ulReqcount=0;
|
||||||
|
ulReconncount=0;
|
||||||
|
|
||||||
|
// prepare GPIO2
|
||||||
|
pinMode(2, OUTPUT);
|
||||||
|
digitalWrite(2, 0);
|
||||||
|
|
||||||
|
// start serial
|
||||||
|
Serial.begin(9600);
|
||||||
|
delay(1);
|
||||||
|
|
||||||
|
// inital connect
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFiStart();
|
||||||
|
}
|
||||||
|
|
||||||
|
void WiFiStart()
|
||||||
|
{
|
||||||
|
ulReconncount++;
|
||||||
|
|
||||||
|
// Connect to WiFi network
|
||||||
|
Serial.println();
|
||||||
|
Serial.println();
|
||||||
|
Serial.print("Connecting to ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
Serial.println("");
|
||||||
|
Serial.println("WiFi connected");
|
||||||
|
|
||||||
|
// Start the server
|
||||||
|
server.begin();
|
||||||
|
Serial.println("Server started");
|
||||||
|
|
||||||
|
// Print the IP address
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
// check if WLAN is connected
|
||||||
|
if (WiFi.status() != WL_CONNECTED)
|
||||||
|
{
|
||||||
|
WiFiStart();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if a client has connected
|
||||||
|
WiFiClient client = server.available();
|
||||||
|
if (!client)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait until the client sends some data
|
||||||
|
Serial.println("new client");
|
||||||
|
unsigned long ultimeout = millis()+250;
|
||||||
|
while(!client.available() && (millis()<ultimeout) )
|
||||||
|
{
|
||||||
|
delay(1);
|
||||||
|
}
|
||||||
|
if(millis()>ultimeout)
|
||||||
|
{
|
||||||
|
Serial.println("client connection time-out!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the first line of the request
|
||||||
|
String sRequest = client.readStringUntil('\r');
|
||||||
|
//Serial.println(sRequest);
|
||||||
|
client.flush();
|
||||||
|
|
||||||
|
// stop client, if request is empty
|
||||||
|
if(sRequest=="")
|
||||||
|
{
|
||||||
|
Serial.println("empty request! - stopping client");
|
||||||
|
client.stop();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get path; end of path is either space or ?
|
||||||
|
// Syntax is e.g. GET /?pin=MOTOR1STOP HTTP/1.1
|
||||||
|
String sPath="",sParam="", sCmd="";
|
||||||
|
String sGetstart="GET ";
|
||||||
|
int iStart,iEndSpace,iEndQuest;
|
||||||
|
iStart = sRequest.indexOf(sGetstart);
|
||||||
|
if (iStart>=0)
|
||||||
|
{
|
||||||
|
iStart+=+sGetstart.length();
|
||||||
|
iEndSpace = sRequest.indexOf(" ",iStart);
|
||||||
|
iEndQuest = sRequest.indexOf("?",iStart);
|
||||||
|
|
||||||
|
// are there parameters?
|
||||||
|
if(iEndSpace>0)
|
||||||
|
{
|
||||||
|
if(iEndQuest>0)
|
||||||
|
{
|
||||||
|
// there are parameters
|
||||||
|
sPath = sRequest.substring(iStart,iEndQuest);
|
||||||
|
sParam = sRequest.substring(iEndQuest,iEndSpace);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// NO parameters
|
||||||
|
sPath = sRequest.substring(iStart,iEndSpace);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// output parameters to serial, you may connect e.g. an Arduino and react on it
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
if(sParam.length()>0)
|
||||||
|
{
|
||||||
|
int iEqu=sParam.indexOf("=");
|
||||||
|
if(iEqu>=0)
|
||||||
|
{
|
||||||
|
sCmd = sParam.substring(iEqu+1,sParam.length());
|
||||||
|
Serial.println(sCmd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////
|
||||||
|
// format the html response
|
||||||
|
///////////////////////////
|
||||||
|
String sResponse,sHeader;
|
||||||
|
|
||||||
|
////////////////////////////
|
||||||
|
// 404 for non-matching path
|
||||||
|
////////////////////////////
|
||||||
|
if(sPath!="/")
|
||||||
|
{
|
||||||
|
sResponse="<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>";
|
||||||
|
|
||||||
|
sHeader = "HTTP/1.1 404 Not found\r\n";
|
||||||
|
sHeader += "Content-Length: ";
|
||||||
|
sHeader += sResponse.length();
|
||||||
|
sHeader += "\r\n";
|
||||||
|
sHeader += "Content-Type: text/html\r\n";
|
||||||
|
sHeader += "Connection: close\r\n";
|
||||||
|
sHeader += "\r\n";
|
||||||
|
}
|
||||||
|
///////////////////////
|
||||||
|
// format the html page
|
||||||
|
///////////////////////
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ulReqcount++;
|
||||||
|
sResponse = "<html><head><title>Demo für ESP8266 Steuerung</title></head><body>";
|
||||||
|
sResponse += "<font color=\"#000000\"><body bgcolor=\"#d0d0f0\">";
|
||||||
|
sResponse += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">";
|
||||||
|
sResponse += "<h1>Demo für ESP8266 Steuerung</h1>";
|
||||||
|
sResponse += "Funktion 1 schaltet GPIO2 und erzeugt eine serielle Ausgabe.<BR>";
|
||||||
|
sResponse += "Funktion 2 erzeugt nur eine serielle Ausgabe.<BR>";
|
||||||
|
sResponse += "<FONT SIZE=+1>";
|
||||||
|
sResponse += "<p>Funktion 1 <a href=\"?pin=FUNCTION1ON\"><button>einschalten</button></a> <a href=\"?pin=FUNCTION1OFF\"><button>ausschalten</button></a></p>";
|
||||||
|
sResponse += "<p>Funktion 2 <a href=\"?pin=FUNCTION2ON\"><button>einschalten</button></a> <a href=\"?pin=FUNCTION2OFF\"><button>ausschalten</button></a></p>";
|
||||||
|
|
||||||
|
//////////////////////
|
||||||
|
// react on parameters
|
||||||
|
//////////////////////
|
||||||
|
if (sCmd.length()>0)
|
||||||
|
{
|
||||||
|
// write received command to html page
|
||||||
|
sResponse += "Kommando:" + sCmd + "<BR>";
|
||||||
|
|
||||||
|
// switch GPIO
|
||||||
|
if(sCmd.indexOf("FUNCTION1ON")>=0)
|
||||||
|
{
|
||||||
|
digitalWrite(2, 1);
|
||||||
|
}
|
||||||
|
else if(sCmd.indexOf("FUNCTION1OFF")>=0)
|
||||||
|
{
|
||||||
|
digitalWrite(2, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sResponse += "<FONT SIZE=-2>";
|
||||||
|
sResponse += "<BR>Aufrufzähler=";
|
||||||
|
sResponse += ulReqcount;
|
||||||
|
sResponse += " - Verbindungszähler=";
|
||||||
|
sResponse += ulReconncount;
|
||||||
|
sResponse += "<BR>";
|
||||||
|
sResponse += "Stefan Thesen 04/2015<BR>";
|
||||||
|
sResponse += "</body></html>";
|
||||||
|
|
||||||
|
sHeader = "HTTP/1.1 200 OK\r\n";
|
||||||
|
sHeader += "Content-Length: ";
|
||||||
|
sHeader += sResponse.length();
|
||||||
|
sHeader += "\r\n";
|
||||||
|
sHeader += "Content-Type: text/html\r\n";
|
||||||
|
sHeader += "Connection: close\r\n";
|
||||||
|
sHeader += "\r\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the response to the client
|
||||||
|
client.print(sHeader);
|
||||||
|
client.print(sResponse);
|
||||||
|
|
||||||
|
// and stop the client
|
||||||
|
client.stop();
|
||||||
|
Serial.println("Client disonnected");
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
#include <BLEDevice.h>
|
||||||
|
#include <BLEServer.h>
|
||||||
|
#include <BLEUtils.h>
|
||||||
|
#include <BLE2902.h>
|
||||||
|
|
||||||
|
#define SERVICE_UUID "7cddf5af-453f-40fa-808e-37ae6ad8facd"
|
||||||
|
BLECharacteristic temphumidSense (BLEUUID("3383a686-a53c-42e4-a88c-e0e1de4c4bda"), BLECharacteristic::PROPERTY_READ
|
||||||
|
| BLECharacteristic::PROPERTY_NOTIFY
|
||||||
|
| BLECharacteristic::PROPERTY_INDICATE);
|
||||||
|
BLEDescriptor temphumidDescriptor(BLEUUID((uint16_t)0x2901));
|
||||||
|
|
||||||
|
bool clientConnected = false;
|
||||||
|
|
||||||
|
|
||||||
|
class MyServerCallbacks: public BLEServerCallbacks {
|
||||||
|
void onConnect (BLEServer * pServer) {
|
||||||
|
clientConnected = true;
|
||||||
|
};
|
||||||
|
void onDisconnect (BLEServer * pServer) {
|
||||||
|
clientConnected = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void setup () {
|
||||||
|
Serial.begin(115200);
|
||||||
|
BLEDevice::init("esp32-Server");
|
||||||
|
BLEServer *pServer = BLEDevice::createServer();
|
||||||
|
pServer->setCallbacks(new MyServerCallbacks());
|
||||||
|
|
||||||
|
BLEService *pSensor = pServer-> createService(SERVICE_UUID);
|
||||||
|
|
||||||
|
pSensor-> addCharacteristic(&temphumidSense);
|
||||||
|
temphumidDescriptor.setValue("In Degree Celcius");
|
||||||
|
temphumidSense.addDescriptor(&temphumidDescriptor);
|
||||||
|
temphumidSense.addDescriptor(new BLE2902());
|
||||||
|
|
||||||
|
pServer->getAdvertising()->addServiceUUID(SERVICE_UUID);
|
||||||
|
pSensor-> start(); // Start the service
|
||||||
|
pServer-> getAdvertising()->start ();// Starts the discovery of ESP32
|
||||||
|
|
||||||
|
Serial.println("Waiting for a client to connect ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop () {
|
||||||
|
if (clientConnected) {
|
||||||
|
temphumidSense.setValue("1234");
|
||||||
|
temphumidSense.notify(); // Send the Temperature reading to the application!
|
||||||
|
Serial.println("Sent string");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("Not connected");
|
||||||
|
}
|
||||||
|
delay(2000);
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
#include <BLEDevice.h>
|
||||||
|
#include <BLEUtils.h>
|
||||||
|
#include <BLEServer.h>
|
||||||
|
|
||||||
|
#define ONBOARD_LED 2
|
||||||
|
|
||||||
|
#define SERVICE_UUID "a17881b1-519a-45e5-85c3-ae3acf763e19"
|
||||||
|
#define CHARACTERISTIC_UUID "05f4798d-3165-455a-931d-e0aceb723423"
|
||||||
|
|
||||||
|
|
||||||
|
bool clientConnected = false;
|
||||||
|
|
||||||
|
class MyServerCallbacks: public BLEServerCallbacks {
|
||||||
|
void onConnect (BLEServer * pServer) {
|
||||||
|
clientConnected = true;
|
||||||
|
};
|
||||||
|
void onDisconnect (BLEServer * pServer) {
|
||||||
|
clientConnected = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void print_wakeup_reason(){
|
||||||
|
esp_sleep_wakeup_cause_t wakeup_reason;
|
||||||
|
|
||||||
|
wakeup_reason = esp_sleep_get_wakeup_cause();
|
||||||
|
|
||||||
|
switch(wakeup_reason)
|
||||||
|
{
|
||||||
|
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
|
||||||
|
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
|
||||||
|
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
|
||||||
|
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
|
||||||
|
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
|
||||||
|
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
pinMode(ONBOARD_LED,OUTPUT);
|
||||||
|
|
||||||
|
/* Bluetooth */
|
||||||
|
Serial.println("Starting BLE");
|
||||||
|
BLEDevice::init("LeWe Garage Door Impulse");
|
||||||
|
BLEServer *pServer = BLEDevice::createServer();
|
||||||
|
|
||||||
|
pServer->setCallbacks(new MyServerCallbacks());
|
||||||
|
|
||||||
|
BLEService *pService = pServer->createService(SERVICE_UUID);
|
||||||
|
BLECharacteristic *pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID,BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
|
||||||
|
pCharacteristic->setValue("Hallo");
|
||||||
|
pService->start();
|
||||||
|
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
||||||
|
pAdvertising->addServiceUUID(SERVICE_UUID);
|
||||||
|
pAdvertising->setScanResponse(true);
|
||||||
|
// pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
|
||||||
|
// pAdvertising->setMinPreferred(0x12);
|
||||||
|
|
||||||
|
// BLEDevice::startAdvertising();
|
||||||
|
pAdvertising->start();
|
||||||
|
|
||||||
|
Serial.println("Bluetooth characteristic now active");
|
||||||
|
|
||||||
|
|
||||||
|
/* Wait 10 seconds for the clients to find us */
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
delay(900);
|
||||||
|
digitalWrite(ONBOARD_LED,HIGH);
|
||||||
|
delay(100);
|
||||||
|
digitalWrite(ONBOARD_LED,LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Enter deep sleep */
|
||||||
|
/* print_wakeup_reason();
|
||||||
|
|
||||||
|
// configure wakeup source
|
||||||
|
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33,1); //1 = High, 0 = Low
|
||||||
|
|
||||||
|
esp_deep_sleep_start();*/
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
if (clientConnected) {
|
||||||
|
Serial.println("Client connected");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
delay(400);
|
||||||
|
digitalWrite(ONBOARD_LED,HIGH);
|
||||||
|
delay(100);
|
||||||
|
digitalWrite(ONBOARD_LED,LOW);
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
@@ -0,0 +1,193 @@
|
|||||||
|
#include <SPI.h>
|
||||||
|
#include <Ethernet.h>
|
||||||
|
|
||||||
|
// Enter a MAC address and IP address for your controller below.
|
||||||
|
byte mac[] = {0xA4, 0xDA, 0x49, 0xB8, 0x19, 0xA5};
|
||||||
|
IPAddress ip(192, 168, 178, 52);
|
||||||
|
EthernetServer server(80);
|
||||||
|
|
||||||
|
const int remotePin = 2;
|
||||||
|
const int motorPin = 3;
|
||||||
|
|
||||||
|
const int ledPin = 5;
|
||||||
|
|
||||||
|
const int remoteButton = 6;
|
||||||
|
const int motorButton = 7;
|
||||||
|
|
||||||
|
const int sensorPin = A0;
|
||||||
|
|
||||||
|
const int clickDelay = 500;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(9600);
|
||||||
|
|
||||||
|
//Turn off WiFi
|
||||||
|
WiFi.mode(WIFI_OFF);
|
||||||
|
//WiFi.forceSleepBegin(); //This also works
|
||||||
|
|
||||||
|
|
||||||
|
HIER GEHT'S WEITER
|
||||||
|
|
||||||
|
/* // Check PINs
|
||||||
|
Serial.print("MISO: ");
|
||||||
|
Serial.println(PIN_SPI_MISO);
|
||||||
|
Serial.print("MOSI: ");
|
||||||
|
Serial.println(PIN_SPI_MOSI);
|
||||||
|
Serial.print("SCK: ");
|
||||||
|
Serial.println(PIN_SPI_SCK);
|
||||||
|
Serial.print("SS: ");
|
||||||
|
Serial.println(PIN_SPI_SS);*/
|
||||||
|
|
||||||
|
pinMode(remotePin, OUTPUT);
|
||||||
|
pinMode(motorPin, OUTPUT);
|
||||||
|
pinMode(ledPin, OUTPUT);
|
||||||
|
pinMode(remoteButton, INPUT);
|
||||||
|
pinMode(motorButton, INPUT);
|
||||||
|
|
||||||
|
// Serial.println("Starting ethernet");
|
||||||
|
Ethernet.begin(mac, ip);
|
||||||
|
|
||||||
|
// Serial.println(Ethernet.localIP());
|
||||||
|
|
||||||
|
// Serial.println("init complete");
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
if (digitalRead(remoteButton) == HIGH) {
|
||||||
|
// Serial.println("Remote button pressed");
|
||||||
|
triggerRemote();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (digitalRead(motorButton) == HIGH) {
|
||||||
|
// Serial.println("Motor button pressed");
|
||||||
|
triggerMotor();
|
||||||
|
}
|
||||||
|
|
||||||
|
// listen for incoming clients
|
||||||
|
EthernetClient client = server.available();
|
||||||
|
if (client) {
|
||||||
|
// Serial.println("new client");
|
||||||
|
// an http request ends with a blank line
|
||||||
|
bool currentLineIsBlank = true;
|
||||||
|
|
||||||
|
int sensorValue = 0;
|
||||||
|
char uri[64];
|
||||||
|
byte idx = 0;
|
||||||
|
boolean uriLine = false;
|
||||||
|
|
||||||
|
while (client.connected()) {
|
||||||
|
if (client.available()) {
|
||||||
|
char c = client.read();
|
||||||
|
|
||||||
|
if (currentLineIsBlank && c == 'G') {
|
||||||
|
uriLine = true;
|
||||||
|
uri[idx] = c;
|
||||||
|
idx++;
|
||||||
|
}
|
||||||
|
else if (uriLine) {
|
||||||
|
if (c == '\n') {
|
||||||
|
uriLine = false;
|
||||||
|
uri[idx] = '\0';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
uri[idx] = c;
|
||||||
|
idx++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serial.write(c);
|
||||||
|
// if you've gotten to the end of the line (received a newline
|
||||||
|
// character) and the line is blank, the http request has ended,
|
||||||
|
// so you can send a reply
|
||||||
|
if (c == '\n' && currentLineIsBlank) {
|
||||||
|
// send a standard http response header
|
||||||
|
client.println("HTTP/1.1 200 OK");
|
||||||
|
client.println("Content-Type: text/plain");
|
||||||
|
client.println("Connection: close"); // the connection will be closed after completion of the response
|
||||||
|
client.println();
|
||||||
|
if (uri[4] == '/' && uri[5] == 'r' && uri[6] == ' ') {
|
||||||
|
client.println("trigger remote");
|
||||||
|
triggerRemote();
|
||||||
|
}
|
||||||
|
else if (uri[4] == '/' && uri[5] == 'm' && uri[6] == ' ') {
|
||||||
|
client.println("trigger motor");
|
||||||
|
triggerMotor();
|
||||||
|
}
|
||||||
|
else if (uri[4] == '/' && uri[5] == 'a' && uri[6] == ' ') {
|
||||||
|
client.println("trigger all");
|
||||||
|
triggerRemote();
|
||||||
|
triggerMotor();
|
||||||
|
}
|
||||||
|
else if (uri[4] == '/' && uri[5] == 'o' && uri[6] == ' ') {
|
||||||
|
// check if open
|
||||||
|
if (sensorValue > 512) {
|
||||||
|
client.println("closed, opening now");
|
||||||
|
triggerMotor();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
client.println("already open");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (uri[4] == '/' && uri[5] == 'c' && uri[6] == ' ') {
|
||||||
|
// check if open
|
||||||
|
if (sensorValue > 512) {
|
||||||
|
client.println("already closed");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
client.println("open, closing now");
|
||||||
|
triggerMotor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (uri[4] == '/' && uri[5] == 's' && uri[6] == ' ') {
|
||||||
|
sensorValue = analogRead(sensorPin);
|
||||||
|
|
||||||
|
if (sensorValue > 512) {
|
||||||
|
client.println("true");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
client.println("false");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (uri[4] == '/' && uri[5] == 'p' && uri[6] == ' ') {
|
||||||
|
client.println("ping");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
client.println("not found");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (c == '\n') {
|
||||||
|
// you're starting a new line
|
||||||
|
currentLineIsBlank = true;
|
||||||
|
} else if (c != '\r') {
|
||||||
|
// you've gotten a character on the current line
|
||||||
|
currentLineIsBlank = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// give the web browser time to receive the data
|
||||||
|
delay(1);
|
||||||
|
// close the connection:
|
||||||
|
client.stop();
|
||||||
|
// Serial.println("client disconnected");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void triggerRemote() {
|
||||||
|
// Serial.println("triggerRemote");
|
||||||
|
digitalWrite(ledPin, HIGH);
|
||||||
|
digitalWrite(remotePin, HIGH);
|
||||||
|
delay(clickDelay);
|
||||||
|
digitalWrite(remotePin, LOW);
|
||||||
|
digitalWrite(ledPin, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
void triggerMotor() {
|
||||||
|
// Serial.println("triggerMotor");
|
||||||
|
digitalWrite(ledPin, HIGH);
|
||||||
|
digitalWrite(motorPin, HIGH);
|
||||||
|
delay(clickDelay);
|
||||||
|
digitalWrite(motorPin, LOW);
|
||||||
|
digitalWrite(ledPin, LOW);
|
||||||
|
}
|
||||||
Binary file not shown.
Reference in New Issue
Block a user