Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <Streaming.h>
- #define SLEEP_DELAY_IN_SECONDS 30
- #define ONE_WIRE_BUS D5 // DS18B20 pin
- const char* ssid = "xxxxx";
- const char* password = "xxxxxx";
- const char* mqtt_server = "192.168.2.113";
- const char* mqtt_username = "xxx";
- const char* mqtt_password = "xxx";
- const char* mqtt_topic = "ha/test/test";
- float temperature;
- WiFiClient espClient;
- PubSubClient client(espClient);
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature DS18B20(&oneWire);
- char temperatureString[6];
- void setup() {
- // setup serial port
- Serial.begin(115200);
- // setup WiFi
- setup_wifi();
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- // setup OneWire bus
- DS18B20.begin();
- }
- void setup_wifi() {
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println();
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
- Serial.println("connected");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- float getTemperature() {
- Serial << "Hämtar temperatur.." << endl;
- float temp;
- DS18B20.requestTemperatures();
- temp = DS18B20.getTempCByIndex(0);
- Serial.print(temp);
- return temp;
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- client.publish(mqtt_topic, "temp2");
- temperature = getTemperature();
- client.publish(mqtt_topic, "temp3");
- client.publish(mqtt_topic, "temp4");
- // konvertera till rätt format
- dtostrf(temperature, 2, 2, temperatureString);
- Serial << "Skickar temperatur: " << temperatureString << endl;
- client.publish(mqtt_topic, temperatureString);
- // Serial << "Closing MQTT connection..." << endl;
- client.disconnect();
- // Serial << "Closing WiFi connection..." << endl;
- WiFi.disconnect();
- delay(100);
- Serial << "Entering deep sleep mode for " << SLEEP_DELAY_IN_SECONDS << " seconds..." << endl;
- ESP.deepSleep(SLEEP_DELAY_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
- //ESP.deepSleep(10 * 1000, WAKE_NO_RFCAL);
- delay(500); // wait for deep sleep to happen
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement