How Can I Add the NRF24L01 ratio to the 2-wire Temp Probe Sketch
-
Hi,
How do I add the radio sketch to this simple sketch?
#include <MySensor.h>
#include <SPI.h>// SPI Pins
#define CE_PIN 9
#define CS_PIN 10// Wait times
#define LONG_WAIT 500
#define SHORT_WAIT 50#define SKETCH_NAME "2 Wire Temp Probe"
#define SKETCH_VERSION "v0.1"// which analog pin to connect
#define ID_S_TEMP 007#define THERMISTORPIN A0
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 10000// Global Vars
unsigned long SLEEP_TIME = 12000; // Sleep time between reads (in milliseconds)
boolean metric = true;
long randNumber;// Instanciate MySersors Gateway
MySensor gw;#ifdef ID_S_TEMP
MyMessage msg_S_TEMP(ID_S_TEMP,V_TEMP);
#endif#ifdef ID_S_DISTANCE
MyMessage msg_S_DISTANCE(ID_S_DISTANCE,V_DISTANCE);
#endifint samples[NUMSAMPLES];
void setup(void) {
Serial.begin(9600);
analogReference(EXTERNAL);
}void loop(void) {
uint8_t i;
float average;// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;Serial.print("Average analog reading ");
Serial.println(average);// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
Serial.print("Thermistor resistance ");
Serial.println(average);float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 258.95; // convert to CSerial.print("Temperature ");
Serial.print(steinhart);
Serial.println(" *C");delay(1000);
} -
To initialize the radio and the sensor (in setup):
gw.begin(); gw.sendSketchInfo("2-Wire Temperature Sensor", "1.0"); gw.present(ID_S_TEMP, S_TEMP);//child ID 0to send the temperature (in loop):
gw.send(msg.setSensor(ID_S_TEMP).set(temperature, 1));to save energy use
gw.sleep(sleeptime)instead ofdelay(sleeptime).How do you wire the sensors? What do you wire on the external reference pin?
-
Hi Fleinze, thanks for the help. I am using A0 for the pin and I decided to rebuild the Dallas temp sketch, I can compile it and the radio looks like it is communicating, I just can get it to register as a sensor in vera. Any ideas?
NEW Sketch
#include <MySensor.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
#define ONE_WIRE_BUS A0 // Pin where sensor is connected
#define MAX_ATTACHED_DS18B20 16
#define LONG_WAIT 500
#define SHORT_WAIT 50
#define ID_S_TEMP 97
#define THERMISTORPIN A0
#define THERMISTORNOMINAL 10000
#define TEMPERATURENOMINAL 25
#define NUMSAMPLES 5
#define BCOEFFICIENT 3950
#define SERIESRESISTOR 10000unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature.MySensor gw;
float lastTemperature[MAX_ATTACHED_DS18B20];
int numSensors=A0;
boolean receivedConfig = false;
boolean metric = true;
// Initialize temperature message
MyMessage msg(A0,V_TEMP);int samples[NUMSAMPLES];
void setup(void) {
Serial.begin(115200);
analogReference(EXTERNAL);
// Startup up the OneWire library
sensors.begin();
// requestTemperatures() will not block current thread
sensors.setWaitForConversion(false);// Startup and initialize MySensors library. Set callback for incoming messages.
gw.begin();// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Temperature Sensor", "1.1");// Fetch the number of attached temperature sensors
numSensors = sensors.getDeviceCount();// Present all sensors to controller
for (int i=A0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
gw.present(i, S_TEMP);
}
}
void loop(void) {
// Process incoming messages (like config from server)
gw.process();
// Fetch temperatures from sensor
sensors.requestTemperatures();// query conversion time and sleep until conversion completed
int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
// sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
gw.sleep(conversionTime);uint8_t i;
float average;// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;Serial.print("Average analog reading ");
Serial.println(average);// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
Serial.print("Thermistor resistance ");
Serial.println(average);float temperature;
temperature = average / THERMISTORNOMINAL; // (R/Ro)
temperature = log(temperature); // ln(R/Ro)
temperature /= BCOEFFICIENT; // 1/B * ln(R/Ro)
temperature += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
temperature = 1.0 / temperature; // Invert
temperature -= 258.95; // convert to CSerial.print("Temperature ");
Serial.print(temperature);
Serial.println(" *C");
// Send in the new temperature
gw.send(msg.setSensor(i).set(temperature,1));
// Save new temperatures for next compare
lastTemperature[i]=temperature;
// delay(1000);
gw.sleep(SLEEP_TIME);
}
send: 254-254-0-0 s=255,c=0,t=17,pt=0,l=3,sg=0,st=ok:1.5
send: 254-254-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
read: 0-0-254 s=255,c=3,t=6,pt=0,l=1,sg=0:M
read: 0-0-254 s=255,c=3,t=1,pt=0,l=10,sg=0:1442404204
sensor started, id=254, parent=0, distance=1
send: 254-254-0-0 s=255,c=3,t=11,pt=0,l=18,sg=0,st=ok:Temperature Sensor
send: 254-254-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.1
Average analog reading 706.00
Thermistor resistance 22271.30
Temperature 22.21 *C
send: 254-254-0-0 s=5,c=1,t=0,pt=7,l=5,sg=0,st=ok:22.2
Average analog reading 707.00
Thermistor resistance 22373.42
Temperature 22.12 *C
send: 254-254-0-0 s=5,c=1,t=0,pt=7,l=5,sg=0,st=ok:22.1
Average analog reading 707.20
Thermistor resistance 22393.92
Temperature 22.10 *C
send: 254-254-0-0 s=5,c=1,t=0,pt=7,l=5,sg=0,st=ok:22.1
Average analog reading 708.00
Thermistor resistance 22476.19
Temperature 22.02 *C
send: 254-254-0-0 s=5,c=1,t=0,pt=7,l=5,sg=0,st=ok:22.0
Average analog reading 709.00
Thermistor resistance 22579.62
Temperature 21.93 *CSo the value that sends is rounded 21.93C = 22 and it looks like it sent that. I just cant figure out how the vera will pick it up.
Any ideas?
-
Hi @Newzwaver!
please consider using using the code-feature of the forum, because it makes the code much more readable.
You still had a lot of code from the dallastemp-example in your code which is not needed in you case. I tried to clean it up for you, but I did#include <MySensor.h> #include <SPI.h> #define THERMISTORPIN A0 #define THERMISTORNOMINAL 10000 #define TEMPERATURENOMINAL 25 #define NUMSAMPLES 5 #define BCOEFFICIENT 3950 #define SERIESRESISTOR 10000 const unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) MySensor gw; // Initialize temperature message MyMessage msg(0,V_TEMP); int samples[NUMSAMPLES]; void setup(void) { pinMode(THERMISTORPIN, INPUT); analogReference(EXTERNAL); // Startup and initialize MySensors library. Set callback for incoming messages. gw.begin(); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("Temperature Sensor", "1.1"); // Present all sensors to controller gw.present(0, S_TEMP); } void loop(void) { // Process incoming messages (like config from server) gw.process(); float average; // take N samples in a row, with a slight delay for (uint8_t i=0; i< NUMSAMPLES; i++) { samples[i] = analogRead(THERMISTORPIN); delay(10); } // average all the samples out average = 0; for (uint8_t i=0; i< NUMSAMPLES; i++) { average += samples[i]; } average /= NUMSAMPLES; Serial.print("Average analog reading "); Serial.println(average); // convert the value to resistance average = 1023 / average - 1; average = SERIESRESISTOR / average; Serial.print("Thermistor resistance "); Serial.println(average); float temperature; temperature = average / THERMISTORNOMINAL; // (R/Ro) temperature = log(temperature); // ln(R/Ro) temperature /= BCOEFFICIENT; // 1/B * ln(R/Ro) temperature += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To) temperature = 1.0 / temperature; // Invert temperature -= 258.95; // convert to C Serial.print("Temperature "); Serial.print(temperature); Serial.println(" *C"); // Send in the new temperature gw.send(msg.setSensor(0).set(temperature,1)); // delay(1000); gw.sleep(SLEEP_TIME); }The original sketch is programmed to present a number of sensors, hence the for loop before
gw.present(). You need just one sensor so you can present it at any child-ID (i.e. child-ID 0). The way the for loop was written,gw.present()was never executed. Also when sending the data you sent it as child-IDi.iwas set in the last for-loop to 5.
Try this, it should work.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login