Editing
ESP8266
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== IoT X'ample == '''Breadboarded multi-function device using ESP-12 module, PIR sensor, buzzer, indicator LEDs and light sensor.''' See additional PIR info [[ESP8266/PIR]] [[File:BoardRoom_bb.png|800px]] The following code can be used on the above diagrammed hardware. Using a web browser the ESP8266 will respond the following url requests * http://xxx.xxx.xxx.xxx/ (Displays current status, motion and light level) * http://xxx.xxx.xxx.xxx/buzz (Triggers the buzzer to make a noise) * http://xxx.xxx.xxx.xxx/hello (Returns a simple hello message) <pre> /* AS IS NO GUARANTEE NO WARRANTY */ #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> const char *ssid = "YourWiFi"; const char *password = "WiFiPassword"; ESP8266WebServer server ( 80 ); const int pir = 4; const int led = 14; const int buzzer = 12; void handleRoot() { char temp[400]; int sec = millis() / 1000; int min = sec / 60; int hr = min / 60; snprintf ( temp, 400, "<html>\ <head>\ <title>PIR Demo</title>\ <style>\ body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\ </style>\ </head>\ <body>\ <h1>Hello from ESP8266 PIR!</h1>\ <p>Uptime: %02d:%02d:%02d</p>\ <p>Motion: %s</p>\ <p>Ambient: %d%</p>\ </body>\ </html>", hr, min % 60, sec % 60, digitalRead(pir)?"true":"false", round(analogRead(A0)/10.24) ); server.send ( 200, "text/html", temp ); } void handleBuzz() { char temp[400]; int sec = millis() / 1000; int min = sec / 60; int hr = min / 60; snprintf ( temp, 400, "<html>\ <head>\ <title>BUZZ!!!</title>\ <style>\ body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #ff0000; }\ </style>\ </head>\ <body>\ <h1>BUZZ!!!</h1>\ <p>Uptime: %02d:%02d:%02d</p>\ <p>Motion: %s</p>\ </body>\ </html>", hr, min % 60, sec % 60, digitalRead(pir)?"true":"false" ); server.send ( 200, "text/html", temp ); digitalWrite(buzzer, HIGH); delay(300); digitalWrite(buzzer, LOW); } 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 setup ( void ) { pinMode ( led, OUTPUT ); digitalWrite ( led, 0 ); pinMode(buzzer, OUTPUT); digitalWrite(buzzer, 0); pinMode(pir, INPUT); Serial.begin ( 115200 ); 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() ); server.on ( "/", handleRoot ); server.on("/buzz", handleBuzz); server.on ( "/hello", []() { server.send ( 200, "text/plain", "hi, how ya doin?" ); } ); server.onNotFound ( handleNotFound ); server.begin(); Serial.println ( "HTTP server started" ); } void loop ( void ) { if (digitalRead(pir)) { // Do something interesting on motion } else { // Nothing new here } // LED on if motion == true digitalWrite(led, digitalRead(pir)); server.handleClient(); } </pre>
Summary:
Please note that all contributions to Noisebridge are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
Noisebridge:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Request account
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Dig in!
Noisebridge
- Status: MOVED
- Donate
- ABOUT
- Accessibility
- Vision
- Blog
Manual
MANUAL
Visitors
Participation
Community Standards
Channels
Operations
Events
EVENTS
Guilds
GUILDS
- Meta
- Electronics
- Fabrication
- Games
- Music
- Library
- Neuro
- Philosophy
- Funding
- Art
- Crypto
- Documentation/Wiki
Wiki
Recent Changes
Random Page
Help
Categories
(Edit)
Tools
What links here
Related changes
Special pages
Page information