Hi there.
I am hoping to control a linear actuator via wifi to open and close a farm gate. I have made my own linear actuator using a motor from a cordless drill and a worm drive.
The gate is beyond the reach of my wifi network so it will be an access point which hopefully my phone will automatically connect to as I approach the gate. I then open a browser and control the gate via a web interface.
I intend to use an ESP8266-12 as the processor which will read the state of two limit switches and operate two relays via 8050 transistors. One relay is a spdt to connect power to operate the dc motor and the other is DPDT to change direction of rotation of the motor.
I intend to supply power via a 12v gel battery and use a solar panel to maintain charge. I will probably use an existing solar charge controller for this as I already have one.
I often make glaringly obvious mistakes:unamused: and so I would appreciate anyone casting an eye over the schematic and commenting or suggesting changes.
Here's some code for the controller.
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsServer.h>
#include <Hash.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#define USE_SERIAL Serial
static const char ssid[] = "front_gate";
static const char password[] = "";
MDNSResponder mdns;
static void writeRelay(bool);
ESP8266WiFiMulti WiFiMulti;
ESP8266WebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);
static const char PROGMEM INDEX_HTML[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0">
<title>Front Gate Controller</title>
<style>
"body { background-color: #74A5EE; font-family: Arial, Helvetica, Sans-Serif; Color: #FF6F00; }"
"h1 {text-align:center; Color: #FF6F00;}"
p {text-align:center;}
</style>
<script>
var websock;
function start() {
websock = new WebSocket('ws://' + window.location.hostname + ':81/');
websock.onopen = function(evt) { console.log('websock open'); };
websock.onclose = function(evt) { console.log('websock close'); };
websock.onerror = function(evt) { console.log(evt); };
websock.onmessage = function(evt) {
console.log(evt);
var e = document.getElementById('ledstatus');
if (evt.data === 'gateOpen') {
e.style.color = 'red';
}
else if (evt.data === 'gateClose') {
e.style.color = 'green';
}
else {
console.log('unknown event');
}
};
}
function buttonclick(e) {
websock.send(e.id);
}
</script>
</head>
<body onload="javascript:start();">
<table cellpadding="10%" cellspacing="10%" align="center" bgcolor="#74A5EE" width="100%" style="text-align: center; ">
<tr>
<td><span align="centre"><h1>Front Gate Controller</h1></span></td>
</tr>
<tr>
<td><div class="centre" id="ledstatus"><h1>Gate</h1></div></td>
</tr>
<tr>
<td><div class="centre"><button id="gateOpen" type="button" onclick="buttonclick(this);"><h1>-OPEN-</h1></button></div></td>
</tr>
<tr>
<td><centre><button id="gateClose" type="button" onclick="buttonclick(this);"><h1>-CLOSE-</h1></button></centre></td>
</tr>
</table>
</body>
</html>
)rawliteral";
const int relay = 4;//gate open/close relay pin
const int powerOn = 5;//power to motor relay pin
const int closedLimitSwitch = 14;// limit switch for closed position
const int OpenLimitSwitch = 12;//Limit switch for open position
bool gateStatus;// Current gate status
// Commands sent through Web Socket
const char RELAYON[] = "gateOpen";
const char RELAYOFF[] = "gateClose";
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length)
{
USE_SERIAL.printf("webSocketEvent(%d, %d, ...)\r\n", num, type);
switch (type) {
case WStype_DISCONNECTED:
USE_SERIAL.printf("[%u] Disconnected!\r\n", num);
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\r\n", num, ip[0], ip[1], ip[2], ip[3], payload);
// Send the current LED status
if (gateStatus) {
webSocket.sendTXT(num, RELAYON, strlen(RELAYON));
}
else {
webSocket.sendTXT(num, RELAYOFF, strlen(RELAYOFF));
}
}
break;
case WStype_TEXT:
USE_SERIAL.printf("[%u] get Text: %s\r\n", num, payload);
if (strcmp(RELAYON, (const char *)payload) == 0) {
writeRelay(true);
}
else if (strcmp(RELAYOFF, (const char *)payload) == 0) {
writeRelay(false);
}
else {
USE_SERIAL.println("Unknown command");
}
// send data to all connected clients
webSocket.broadcastTXT(payload, length);
break;
case WStype_BIN:
USE_SERIAL.printf("[%u] get binary length: %u\r\n", num, length);
hexdump(payload, length);
// echo data back to browser
webSocket.sendBIN(num, payload, length);
break;
default:
USE_SERIAL.printf("Invalid WStype [%d]\r\n", type);
break;
}
}
void handleRoot()
{
server.send_P(200, "text/html", INDEX_HTML);
}
void handleNotFound()
{
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
static void writeRelay(bool relayOn)
{
gateStatus = relayOn;
if (relayOn) {
if (digitalRead(openLimitSwitch)==1){
digitalWrita(powerOn, 1);
digitalWrite(relay, 1);
}
else {
digitalWrite(powerOn, 0);
digitalWrite(relay, 0);
}
}
else {
if (digitalRead(closedLimitSwitch)==1){
digitalWrite(powerOn, 1);
digitalWrite(relay, 0);
}
else {
digitalWrite(powerOn, 0);
digitalWrite(relay, 0);
}
}
}
void setup()
{
pinMode(relay, OUTPUT);
writeRelay(false);
pinMode(powerOn, OUTPUT);
digitalWrite(powerOn, LOW);
pinMode(closedLimitSwitch, INPUT_PULLUP);
pinMode(openLimitSwitch, INPUT_PULLUP);
USE_SERIAL.begin(115200);
//Serial.setDebugOutput(true);
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for (uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\r\n", t);
USE_SERIAL.flush();
delay(1000);
}
// WiFiMulti.addAP(ssid, password);
//
// while (WiFiMulti.run() != WL_CONNECTED) {
// Serial.print(".");
// delay(100);
// }
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
USE_SERIAL.print("AP IP address: ");
USE_SERIAL.println(myIP);
USE_SERIAL.println("");
USE_SERIAL.print("Connected to ");
USE_SERIAL.println(ssid);
USE_SERIAL.print("IP address: ");
USE_SERIAL.println(WiFi.localIP());
if (mdns.begin("espWebSock", WiFi.localIP())) {
USE_SERIAL.println("MDNS responder started");
mdns.addService("http", "tcp", 80);
mdns.addService("ws", "tcp", 81);
}
else {
USE_SERIAL.println("MDNS.begin failed");
}
USE_SERIAL.print("Connect to http://espWebSock.local or http://");
USE_SERIAL.println(WiFi.localIP());
server.on("/", handleRoot);
server.onNotFound(handleNotFound);
server.begin();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
void loop()
{
webSocket.loop();
server.handleClient();
}