Created
February 8, 2019 06:26
-
-
Save bauerjj/55ea59a2418834f1bda93b59e3335477 to your computer and use it in GitHub Desktop.
wemos D1 occupancy sensor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/ | |
* https://codebender.cc/sketch:356078#HC-SR04%20Ultrasonic%20Sensor%20Example.ino | |
* https://einstronic.com/store/controller/esp8266/wemos/wemos-d1-mini-esp8266-wifi-development-board/ | |
* | |
* Uses: https://github.com/gamo256/dweet-esp | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
#include <ESP8266HTTPClient.h> | |
#define THING_NAME "jjb-garage-test" // Put here your thing name | |
#define WIFI_SSID "8FF71E" | |
#define WIFI_PASS "62C26C2C68912" | |
String occupied; | |
// defines pins numbers | |
//D4 (PIN 2) is the onBoard LED!! | |
const int trigPin = 5; //D1 | |
const int echoPin = 4; //D2 | |
const int sensorPWR = 14; //D5 | |
const int led = 2; // onboard | |
// defines variables | |
long duration; | |
int cm, in; | |
const char serverAddress[] = "dweet.io"; // server address | |
int port = 80; | |
void setup() { | |
pinMode(led, OUTPUT); // on-board LED | |
digitalWrite(led, LOW); | |
pinMode(sensorPWR, OUTPUT); | |
//pinMode(16, OUTPUT); | |
pinMode(14, OUTPUT); | |
pinMode(12, OUTPUT); | |
pinMode(13, OUTPUT); | |
pinMode(15, OUTPUT); | |
pinMode(1, OUTPUT); | |
pinMode(3, OUTPUT); | |
pinMode(2, OUTPUT); | |
Serial.begin(115200); | |
Serial.println(""); | |
Serial.println("--- Entered Setup ---"); | |
Serial.println(""); | |
WiFi.disconnect(); | |
WiFi.begin(WIFI_SSID, WIFI_PASS); | |
while(WiFi.status()!=WL_CONNECTED) | |
{ | |
Serial.print("."); | |
delay(500); | |
} | |
Serial.println(""); | |
Serial.print("IP Address: "); | |
Serial.print(WiFi.localIP()); | |
Serial.println(""); | |
// sensor | |
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output | |
pinMode(echoPin, INPUT); // Sets the echoPin as an Input | |
} | |
int i; | |
void loop() { | |
// Power on the sensor. Must wait | |
digitalWrite(sensorPWR, HIGH); | |
delay(1000); | |
// Clears the trigPin | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
// Sets the trigPin on HIGH state for 10 micro seconds | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
// Reads the echoPin, returns the sound wave travel time in microseconds | |
duration = pulseIn(echoPin, HIGH); | |
// Calculating the distance | |
cm = duration / 58.0; | |
in = duration / 148.0; | |
// Prints the distance on the Serial Monitor | |
Serial.print("Distance: "); | |
Serial.println(cm); | |
Serial.println(in); | |
String testme = "jjbgarage"; | |
if(i++ %2 == 0 ) | |
occupied = "1"; | |
else | |
occupied = "0"; | |
// Post data to dweet.io | |
HTTPClient http; | |
String URL = "http://dweet.io/dweet/for/" + testme + "?occupied=" + occupied + "&test=123"; // Works with HTTP | |
http.begin(URL); // Works with HTTP | |
int httpCode = http.GET(); | |
if (httpCode > 0) { | |
String payload = http.getString(); | |
Serial.println(payload); // Print response | |
} | |
http.end(); | |
pinMode(14, OUTPUT); | |
pinMode(12, OUTPUT); | |
pinMode(13, OUTPUT); | |
pinMode(15, OUTPUT); | |
pinMode(1, OUTPUT); | |
pinMode(3, OUTPUT); | |
pinMode(2, OUTPUT); | |
pinMode(5, OUTPUT); | |
pinMode(4, OUTPUT); | |
pinMode(0, OUTPUT); | |
// Drive all pins LOW | |
//digitalWrite(16, LOW); | |
digitalWrite(14, LOW); | |
digitalWrite(12, LOW); | |
digitalWrite(13, LOW); | |
digitalWrite(15, LOW); | |
digitalWrite(1, LOW); | |
digitalWrite(3, LOW); | |
digitalWrite(2, LOW); | |
digitalWrite(5, LOW); | |
digitalWrite(4, LOW); | |
digitalWrite(0, LOW); | |
Serial.println("Deep Sleep: "); | |
ESP.deepSleep(20e6); | |
Serial.println("Awake!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment