Browse Source

test code

main
Hendrik Langer 7 years ago
parent
commit
9796344e76
  1. 124
      arduino/push_test/push_test.ino

124
arduino/push_test/push_test.ino

@ -16,132 +16,54 @@
*/ */
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiClient.h>
#include "esp32-hal-ledc.h" #include "esp32-hal-ledc.h"
const char* ssid = "ssid"; const char* ssid = "ssid";
const char* password = "password"; const char* password = "password";
#define SERVO_LOW 4000 #define SERVO_LOW 8000
#define SERVO_HIGH 8000 #define SERVO_HIGH 3000
#define SERVO_CHANNEL 5 #define SERVO_CHANNEL_0 5
#define SERVO_CHANNEL_1 18
const int buttonPin = 0;
int servoChannel=SERVO_CHANNEL_0;
#define TIMER_WIDTH 16 #define TIMER_WIDTH 16
// TCP server at port 80 will respond to HTTP requests
WiFiServer server(80);
void setup(void) void setup(void)
{ {
Serial.begin(115200); Serial.begin(115200);
// Connect to WiFi network pinMode(buttonPin, INPUT_PULLUP);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Set up mDNS responder:
// - first argument is the domain name, in this example
// the fully-qualified domain name is "esp8266.local"
// - second argument is the IP address to advertise
// we send our IP address on the WiFi network
if (!MDNS.begin("esp32")) {
Serial.println("Error setting up MDNS responder!");
while(1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
// Start TCP (HTTP) server
server.begin();
Serial.println("TCP server started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
ledcSetup(1, 50, TIMER_WIDTH); ledcSetup(1, 50, TIMER_WIDTH);
ledcAttachPin(SERVO_CHANNEL,1); ledcAttachPin(SERVO_CHANNEL_0,1);
ledcWrite(1, SERVO_LOW); ledcWrite(1, SERVO_LOW);
delay(1000); delay(1000);
ledcDetachPin(SERVO_CHANNEL); ledcDetachPin(SERVO_CHANNEL_0);
delay(100);
ledcSetup(1, 50, TIMER_WIDTH);
ledcAttachPin(SERVO_CHANNEL_1,1);
ledcWrite(1, SERVO_LOW);
delay(1000);
ledcDetachPin(SERVO_CHANNEL_1);
} }
void loop(void) void loop(void)
{ {
// Check if a client has connected if (digitalRead(buttonPin) == LOW) {
WiFiClient client = server.available(); if (servoChannel==SERVO_CHANNEL_0) servoChannel=SERVO_CHANNEL_1;
if (!client) { else servoChannel=SERVO_CHANNEL_0;
return; ledcAttachPin(servoChannel,1);
}
Serial.println("");
Serial.println("New client");
// Wait for data from client to become available
while(client.connected() && !client.available()){
delay(1);
}
// Read the first line of HTTP request
String req = client.readStringUntil('\r');
// First line of HTTP request looks like "GET /path HTTP/1.1"
// Retrieve the "/path" part by finding the spaces
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return;
}
req = req.substring(addr_start + 1, addr_end);
Serial.print("Request: ");
Serial.println(req);
client.flush();
String s;
if (req == "/")
{
IPAddress ip = WiFi.localIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP32 at ";
s += ipStr;
s += "</html>\r\n\r\n";
Serial.println("Sending 200");
}
else if (req == "/push") {
IPAddress ip = WiFi.localIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello, pushing!";
s += "</html>\r\n\r\n";
Serial.println("/push");
ledcAttachPin(SERVO_CHANNEL,1);
ledcWrite(1, SERVO_HIGH); ledcWrite(1, SERVO_HIGH);
delay(1500); delay(1500);
ledcWrite(1, SERVO_LOW); ledcWrite(1, SERVO_LOW);
delay(1500); delay(1500);
ledcDetachPin(SERVO_CHANNEL); ledcDetachPin(servoChannel);
}
else
{
s = "HTTP/1.1 404 Not Found\r\n\r\n";
Serial.println("Sending 404");
}
client.print(s);
Serial.println("Done with client"); Serial.println("Done");
}
delay(50);
} }

Loading…
Cancel
Save