  * (D2)-----[ ]-------56-|IRQ         |
  *                       +------------+
  *
- *                       DS18B20/sensor
- *                       +------------+
- * (GND)---------------1-|GND         |
- * (D14)-----+---------2-|DQ          |
- *           |       +-3-|VDD         |
- *          4K7      |   +------------+
+ *                           DS18B20/3
+ *                       +------------+++
+ * (GND)---------------1-|GND         |||\
+ * (D7)------+---------2-|DQ          ||| |
+ *           |       +-3-|VDD         |||/
+ *          4K7      |   +------------+++
  *           |       | 
- * (VCC)-----+-------+
+ * (VCC)-----+       +---(VCC/GND)
  *
  * This file is part of the Arduino Che Cosa project.
  */

#include "Cosa/OWI/Driver/DS18B20.hh"
#include "Cosa/Socket/Driver/W5100.hh"

// One-wire pin and connected DS18B20 device
OWI owi(Board::D14);
DS18B20 sensor(&owi);

// Network configuration
#define MOSQUITTO "test.mosquitto.org"

  // Start MQTT client with socket and connect to server
  client.begin(ethernet.socket(Socket::TCP));
  client.connect(SERVER, CLIENT);
  client.publish_P(PSTR("public/cosa/client"), CLIENT, sizeof(CLIENT));
}

void loop()
{
  static uint8_t i = 0;

  // Select the topic path for the current thermometer
  str_P topic;
  switch (i) {
  case 0: topic = PSTR("public/cosa/temperature/outdoors"); break;
  case 1: topic = PSTR("public/cosa/temperature/indoors"); break;
  case 2: topic = PSTR("public/cosa/temperature/basement"); break;
  };
    
  // Request a conversion and read the temperature
  thermometer[i]->convert_request();
  thermometer[i]->read_scratchpad();

  // Use an iobuffer and iostream to convert temperature value to a string
  IOBuffer<32> buf;
  IOStream cout(&buf);
  cout << *thermometer[i] << PSTR(" C") << ends;

  // Publish the value to the MQTT server and take a nap
  client.publish(topic, buf, buf.available());
  i += 1;
  if (i == membersof(thermometer)) i = 0;

  // Take a nap
  sleep(5);
}
