Codebeispiel für nano
//ISCH! Vorsicht, die Pins sind "digital pins", steht hier 8 dann wird D8 gemeint und nicht Pin 8
//simple wireless temperature tranmitter
// DS18S20 sensor is connected to pin 8
// Rf modules tranmit (data) pin is connected to pin 7 on the arduino
// More info: http://blog.riyas.org/2014/06/cheap-remote-wireless-temperature-sensor-with-arduino-uno-433mhz-rfmodule.html
#include <VirtualWire.h>
#include <OneWire.h>
int DS18S20_Pin = 8; //DS18S20 Signal pin on digital 2
OneWire ds(DS18S20_Pin); // on digital pin 2
char *controller;
char msg[6];
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
vw_set_ptt_inverted(true); //
vw_set_tx_pin(7);
vw_setup(4000);// speed of data transfer Kbps
}
void loop()
{
float temperature = getTemp();
Serial.println(temperature);
dtostrf(temperature, 6, 2, msg);
digitalWrite(13,0);
vw_send((uint8_t *)msg, strlen(msg)); // Send temperature.
vw_wait_tx();
delay(500);
digitalWrite(13,1); // blink the led on pin13
delay(500);
}
float getTemp()
{
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}