Created
January 17, 2020 16:33
-
-
Save bykalim/a7d7c4647a88857c0a4b6634f558aa5f to your computer and use it in GitHub Desktop.
This file contains 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
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#include <Adafruit_Sensor.h> | |
#include <DHT.h> | |
#include <DHT_U.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define DHTTYPE DHT11 // DHT 11 | |
#define TILT_SENSOR_PIN 10 // SD3 | |
#define TILT_LED_PIN 16 // D6 | |
int value = 0; | |
const char* ssid = "Furkan"; | |
const char* password = "furkankurt"; | |
const char* mqtt_server = "192.168.137.107"; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
const int DHTPin = D1; | |
const int lamp = LED_BUILTIN; | |
DHT dht(DHTPin, DHTTYPE); | |
long now = millis(); | |
long lastMeasure = 0; | |
void setup_wifi() { | |
delay(10); | |
Serial.println(); | |
Serial.print("Connecting ?!: "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("WiFi connected - ESP IP adres: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void callback(String topic, byte* message, unsigned int length) { | |
Serial.print(topic); | |
String messageTemp; | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)message[i]); | |
messageTemp += (char)message[i]; | |
}/* | |
Serial.println(); | |
if(topic=="home/1/lamp"){ | |
Serial.print("Odadaki lampanın durumu: "); | |
if(messageTemp == "on"){ | |
digitalWrite(lamp, HIGH); | |
Serial.print("On"); | |
} | |
else if(messageTemp == "off"){ | |
digitalWrite(lamp, LOW); | |
Serial.print("Off"); | |
} | |
}*/ | |
Serial.println(); | |
} | |
void reconnect() { | |
while (!client.connected()) { | |
Serial.print("MQTT connecting..."); | |
// bağlanmamyı dene | |
if (client.connect("ESP8266Client","furkan","furkan")) { | |
Serial.println("MQTT connected"); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" trying in 5 sec"); | |
delay(5000); | |
} | |
} | |
} | |
void setup() { | |
pinMode(10, INPUT); // SD3 | |
pinMode(16, OUTPUT); // D6 | |
pinMode(LED_BUILTIN, OUTPUT); | |
Serial.begin(115200);setup_wifi(); | |
client.setServer(mqtt_server, 1883); | |
client.setCallback(callback); | |
} | |
void loop() { | |
Serial.print("Value: "); | |
Serial.println(analogRead(TILT_SENSOR_PIN)); | |
if(value != analogRead(TILT_SENSOR_PIN)){ | |
digitalWrite(16, HIGH); | |
} else { | |
digitalWrite(16, LOW); | |
} | |
if (!client.connected()) { | |
reconnect(); | |
} | |
if(!client.loop()) | |
client.connect("ESP8266Client","furkan","furkan"); | |
now = millis(); | |
if (now - lastMeasure > 5000) { | |
lastMeasure = now; | |
int h = analogRead(10); | |
if (isnan(h)) { | |
Serial.println("DHT Cannot read from sensor!"); | |
return; | |
} | |
char snum[20]; | |
itoa(analogRead(10), snum, 10); | |
// Publishes tilt values to mqtt. | |
client.publish("home/2/tilt",snum); | |
delay(500); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment