Skip to content

Instantly share code, notes, and snippets.

@alkalinecoffee
Created January 17, 2017 00:05
Show Gist options
  • Save alkalinecoffee/ac4e8d87704b307e85425c94b47f7a2c to your computer and use it in GitHub Desktop.
Save alkalinecoffee/ac4e8d87704b307e85425c94b47f7a2c to your computer and use it in GitHub Desktop.
echo fireplace
// simple script to send RF code out to transmitter
// https://github.com/alkalinecoffee/echo_fireplace
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
const int ledPin = 16;
const int transmitPin = 12; // D6
const int pulseLength = 430;
void setup() {
pinMode(ledPin, OUTPUT);
mySwitch.enableTransmit(transmitPin);
mySwitch.setPulseLength(430);
}
void loop() {
digitalWrite(ledPin, false);
mySwitch.send("1111011110100011101100110");
delay(1000);
digitalWrite(ledPin, true);
delay(1000);
}
// https://github.com/alkalinecoffee/echo_fireplace
/*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a GPIO pin depending on the request
* http://server_ip/gpio/0 will set the GPIO2 low,
* http://server_ip/gpio/1 will set the GPIO2 high
* server_ip is the IP address of the ESP8266 module, will be
* printed to Serial when the module is connected.
*/
#include <ESP8266WiFi.h>
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
const int ledPin = 16;
const int transmitPin = 12; // D6
const int pulseLength = 430;
const char* onCode = "1111011110100011101100010";
const char* offCode = "1111011110100011101100110";
const char* ssid = "***";
const char* password = "***";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
mySwitch.enableTransmit(transmitPin);
mySwitch.setPulseLength(pulseLength);
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
// 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 a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val;
if (req.indexOf("/gpio/0") != -1) {
Serial.println("Sending OFF signal");
digitalWrite(ledPin, HIGH);
mySwitch.send(offCode);
} else if (req.indexOf("/gpio/1") != -1) {
Serial.println("Sending ON signal");
digitalWrite(ledPin, LOW);
mySwitch.send(onCode);
} else {
Serial.println("invalid request");
client.stop();
return;
}
// Set GPIO2 according to the request
// digitalWrite(2, val);
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
s += (val)?"high":"low";
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment