mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
120 lines
3.1 KiB
C++
120 lines
3.1 KiB
C++
#include <ESP8266WiFi.h>
|
|
#include <PubSubClient.h>
|
|
|
|
#include "DHT.h"
|
|
|
|
#define DHTPIN 2 // what digital pin we're connected to
|
|
|
|
#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
|
|
|
|
/*const char* ssid = "ESP8266";
|
|
const char* password = "password";*/
|
|
/*const char* ssid = "bigChief2";
|
|
const char* password = "987654321";*/
|
|
const char* ssid = "VOO-375468";
|
|
const char* password = "UYQQMTHF";
|
|
const char* mqtt_server = "m21.cloudmqtt.com";
|
|
|
|
char message_buff[100];
|
|
|
|
WiFiClient espClient;
|
|
PubSubClient client(espClient);
|
|
|
|
const int Led = 14;
|
|
|
|
// Connect pin 1 (on the left) of the sensor to +5V
|
|
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
|
|
// to 3.3V instead of 5V!
|
|
// Connect pin 2 of the sensor to whatever your DHTPIN is
|
|
// Connect pin 4 (on the right) of the sensor to GROUND
|
|
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
|
|
|
|
// Initialize DHT sensor.
|
|
// Note that older versions of this library took an optional third parameter to
|
|
// tweak the timings for faster processors. This parameter is no longer needed
|
|
// as the current DHT reading algorithm adjusts itself to work on faster procs.
|
|
DHT dht(DHTPIN, DHTTYPE);
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial.println("DHT22 test!");
|
|
WiFi.softAPdisconnect(true);
|
|
dht.begin();
|
|
|
|
pinMode(Led, OUTPUT);
|
|
|
|
WiFi.begin(ssid, password);
|
|
Serial.println("");
|
|
digitalWrite(Led, LOW);
|
|
|
|
// 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());
|
|
digitalWrite(Led, HIGH);
|
|
client.setServer(mqtt_server, 18932);
|
|
|
|
/*server.on("/temp", []() {
|
|
String page = "{ \"temperature\":" + String((int)dht.readTemperature()) + ", \"humidity\":" + String((int)dht.readHumidity())+"}";
|
|
server.send(200, "text/plain", page);
|
|
});*/
|
|
|
|
//client = PubSubClient(mqtt_server, 18932, callback);
|
|
|
|
}
|
|
|
|
void reconnect() {
|
|
// Loop until we're reconnected
|
|
while (!client.connected()) {
|
|
Serial.print("Attempting MQTT connection...");
|
|
// Attempt to connect
|
|
if (client.connect("ESP8266Client-TempSensor", "oilkfgjy", "lEyZb90q49Rf")) {
|
|
Serial.println("connected");
|
|
} else {
|
|
Serial.print("failed, rc=");
|
|
Serial.print(client.state());
|
|
Serial.println(" try again in 5 seconds");
|
|
delay(5000);
|
|
}
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
if(WiFi.status() != WL_CONNECTED){
|
|
digitalWrite(Led, LOW);
|
|
Serial.println("Trying to reconnect !");
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.println("Connected !");
|
|
}else{
|
|
if(digitalRead(Led)==LOW){
|
|
digitalWrite(Led, HIGH);
|
|
}
|
|
}
|
|
|
|
if (!client.connected()) {
|
|
digitalWrite(Led, LOW);
|
|
reconnect();
|
|
}else{
|
|
digitalWrite(Led, HIGH);
|
|
}
|
|
client.loop();
|
|
|
|
String pubString = String((int)dht.readTemperature());
|
|
String pubStringPost = String("Degree="+pubString);
|
|
pubStringPost.toCharArray(message_buff, pubStringPost.length()+1);
|
|
|
|
client.publish("DegreeHome",message_buff);
|
|
|
|
delay(5000);
|
|
}
|