notes/ESP8266 Arduino IDE Code-LLwnsgTM.sh
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "<SSID>";
const char* password = "<PASSWORD>";
ESP8266WebServer server(80);
int p = 0;
int dataPin = 13;
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 0; // the number of the pushbutton pin
const int ledPin = 2; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void handleRoot() {
if (server.args() > 0 ) {
for ( uint8_t i = 0; i < server.args(); i++ ) {
if (server.argName(i) == "switch") {
server.send(200, "text/plain", server.arg(i));
int code = server.arg(i).toInt();
/* Same switch as above, but using decimal code */
mySwitch.send(code, 24);
delay(1000);
}
}
}else{
server.send(200, "text/plain", "hello from esp8266!");
}
}
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);
}
void ring(){
Serial.println("Ringing Bell");
HTTPClient http;
http.begin("http://<server IP>/cgi-bin/doorbell.cgi");
int httpCode = http.GET();
http.end();
delay(5000);
}
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
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());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
// Transmitter is connected to Arduino Pin 'dataPin'
mySwitch.enableTransmit(dataPin);
// Optional set pulse length.
mySwitch.setPulseLength(185);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
p=0;
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
p++;
Serial.println(p);
if(p > 50){
Serial.println("Ring!!!");
p=0;
ring();
}
}
//webserver
server.handleClient();
}
syntax highlighted by Code2HTML, v. 0.9.1