#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D3 // Data wire is plugged into port 2 on the Arduino
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
//DeviceAddress insideThermometer; // arrays to hold device address
//TEMPERATURE//
#include "SevenSegmentTM1637.h"
#include "TM1637Display.h"
#define CLK D6
#define DIO D5
TM1637Display display(CLK, DIO);
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "My WIFI"; // Connect to WIFI
const char* password = "WIFI password"; // Connect to WIFI
const char* mqtt_server = "m23.cloudmqtt.com"; // Connect to MQTT m23.cloudmqtt.com
const char* mqtt_Port = "19686"; // 19686
const char* mqtt_user = "user"; // Connect to MQTT
const char* mqtt_password = "password"; // Connect to MQTT
WiFiClient espClient; // Create an ESP8266 WifiClient class to connect to the MQTT server
PubSubClient client(espClient); //
const char* outTopic = "sensor/temperature";
const char* inTopic = "sensor/temperature";
//Connect to WIFI........................................................................................
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
} // We start by connecting to a WiFi network
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //Print the IP Address
} //Connect to WIFI
//Connect to WIFI........................................................................................
void callback(char* topic, byte* payload, unsigned int length) { //Subscribe to the Topic, Payload
Serial.print("Message arrived [");
Serial.print(topic); //Print the Topic
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload*);*
-
}*
-
// Switch on the LED if an 1 was received as first character*
-
if ((char)payload[0] == '1') {*
-
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level*
-
// but actually the LED is on; this is because*
-
// it is active low on the ESP-01)*
-
} else {*
-
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH*
-
}*
}
/*/
/*/
void reconnect() { // Loop until we're reconnected DO NOT TOUCH.........
-
while (!client.connected()) {*
-
Serial.print("Attempting MQTT connection...");*
-
// String clientId ="espClient"; // "ESP8266Client-"; // Create a random client ID*
-
//clientId += String(random(0xffff), HEX);*
-
if (client.connect("espClient")){ //(clientId.c_str())) { // Attempt to connect*
-
Serial.println("connected");*
*//// RECONNECTED..... *
-
client.publish("outTopic", "hello world"); // Once connected, publish an announcement...*
-
client.subscribe("inTopic"); // ... and resubscribe*
-
} else {*
-
Serial.print("failed, rc=");*
-
Serial.print(client.state());*
-
Serial.println(" try again in 5 seconds");*
-
// Wait 5 seconds before retrying*
-
delay(5000);*
-
}*
-
}//*
}
//
void setup() {
-
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output*
-
Serial.begin(115200); // Connect to WIFI*
WiFi.mode(WIFI_STA);
-
setup_wifi(); // Connect to WIFI*
-
client.setServer(mqtt_server, 19686); //Connect to MQTT*
client.setCallback(callback);
display.setBrightness(0x0f); // set brightness to the LED screen
-
digitalWrite(D3,HIGH); // Activate internal Pull Up resistor*
-
pinMode(D3,INPUT_PULLUP); // Activate internal Pull Up resistor*
// Serial.begin(115200); // start serial port
-
//sensors.setResolution(insideThermometer, 9); // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)*
-
digitalWrite(D3,HIGH); // Activate internal Pull Up resistor*
-
pinMode(D3,INPUT_PULLUP); // Activate internal Pull Up resistor*
-
sensors.begin(); // Start temp sensors*
}
//
void loop()
{
-
sensors.requestTemperatures(); // Send the command to get temperatures // call sensors.requestTemperatures() to issue a global temperature*
-
Serial.print("Temperature for the device 1 is: ");*
-
Serial.print(sensors.getTempCByIndex(0)); // printTemperature(insideThermometer); // Use a simple function to print out the data*
-
Serial.println();*
-
delay(2000);*
// Display temperature
display.clear();
display.showNumberDec (int (sensors.getTempCByIndex(0)), false); // LED Screen
delay(2000);
/////////
-
if (!client.connected()) {*
-
reconnect();*
-
}*
-
client.loop();*
long lastMsg = 0;
float temp = 0.0;
//float hum = 0.0;
float diff = 1.0;
-
long now = millis();*
-
if (now - lastMsg > 2000) {*
-
lastMsg = now;*
-
float newTemp = sensors.getTempCByIndex(0);*
-
// float newHum = dht.readHumidity();*
-
if (checkBound(newTemp, temp, diff)) {*
-
temp = newTemp;*
-
Serial.print("New temperature:");*
-
Serial.println(String(temp).c_str());*
-
client.publish(outTopic, String(temp).c_str(), true);*
-
}*
}//
}
bool checkBound(float newValue, float prevValue, float maxDiff) {
-
return !isnan(newValue) &&*
-
(newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);*
}