/* * Send temperature from ESP8266 with multiple DS18B20 to MQTT server. * A simple Sketch to read the Temperature from multiple DS18B20 and publish them to a MQTT-Server using a ESP8266. * Compiles in the Arduino IDE for the ESP8266 * * For deep sleep support uncomment 'deep sleep' part * * */ #include #include #include #include /* deep sleep #define SLEEP_DELAY_IN_SECONDS 30 */ // data cable connected to D2 pin #define ONE_WIRE_BUS D2 #define TEMPERATURE_PRECISION 9 // Lower resolution //wifi const char* ssid = "ssid"; const char* password = "password"; //mqtt const char* mqtt_server = "X.X.X.X"; const char* mqtt_username = "loxberry"; const char* mqtt_password = "password"; const char* host = "esp1"; WiFiClient espClient; PubSubClient client(espClient); OneWire oneWire(ONE_WIRE_BUS); // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); int numberOfDevices; // Number of temperature devices found DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address 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 setup() { // setup serial port Serial.begin(115200); // setup WiFi setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); // setup OneWire bus // Start up the library sensors.begin(); // Grab a count of devices on the wire numberOfDevices = sensors.getDeviceCount(); // locate devices on the bus Serial.print("Locating devices..."); Serial.print("Found "); Serial.print(numberOfDevices, DEC); Serial.println(" devices."); // report parasite power requirements Serial.print("Parasite power is: "); if (sensors.isParasitePowerMode()) Serial.println("ON"); else Serial.println("OFF"); // Loop through each device, print out address for(int i=0;i