Some time ago, I published my AmbientMonitor project: a thermometer and hygrometer based on Arduino and an AJAX web interface.
To connect Arduino to the network I chose a shield based on the ENC28J60 chip; Danny Kimbler sent me a sketch that instead uses the official shield (W5100 chip) and the DS18B20 temperature sensor:
I published his sketch in my Github’s repository, let’s describe some code:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x7D, 0x54 }; byte ip[] = { 10, 150, 41, 3 }; byte subnet[] = { 255, 255, 255, 0 }; byte gateway[] = { 10, 150, 41, 254 }; |
The network configuration (IP address, netmask…) is done at the beginning of the sketch and must be changed accordingly to the particular network you’re connecting the shield to.
#define TEMPERATURE_INDOOR 5 OneWire oneWireIndoor(TEMPERATURE_INDOOR); DallasTemperature sensorIndoor(&oneWireIndoor); |
Two libraries are needed to get data from the sensor:
[checklist]
- OneWire, by PJRC
- DallasTemperature, by Miles Burton
[/checklist]
In the example, the sensor is connected to Arduino’s PIN5.
temperatureIndoor = readTemperatureCelsius(sensorIndoor); client.print("myCB({'temperature':"); client.print(temperatureIndoor); client.print(",'humidity':"); client.print(temperatureIndoor); client.print("})"); |
The sketch sends the data (JSON format) to the AJAX webpage… the sensor chosen doesn’t provide humidity values, therefore Danny sends the temperature twice…
float readTemperatureCelsius(DallasTemperature sensor) { sensor.requestTemperatures(); float temperature = sensor.getTempFByIndex(0); return temperature; } |
The readTemperatureCelsius function leverages some methods of the DallasTemperature library to get the actual temperature from the sensor.
Many thanks to Danny for having sent me his sketch: now you can try my AmbientMonitor project with both the most used ethernet shields for Arduino!