Combine sketches

Moderator: leecollings

Post Reply
User avatar
thecosmicgate
Posts: 188
Joined: Monday 06 April 2015 14:37
Target OS: Linux
Domoticz version: newest
Location: The Netherlands / Hoeven
Contact:

Combine sketches

Post by thecosmicgate »

At the moment I've got running the 3 in 1 sketch : motion , temperature, moisture .
But I want to combine the battery monitoring sketch .
Is there somebody who can help me with this ?
Thnx a lot

Sent from my MotoG3 using Tapatalk
It's nice to be important, but it's more important to be nice
Scooter ;)
sundberg84
Posts: 52
Joined: Sunday 17 May 2015 11:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sweden
Contact:

Re: Combine sketches

Post by sundberg84 »

Code: Select all

#define VBAT_PER_BITS 0.003363075  // Calculated volts per bit from the used battery montoring voltage divider.   Internal_ref=1.1V, res=10bit=2^10-1=1023, Eg for 3V (2AA): Vin/Vb=R1/(R1+R2)=470e3/(1e6+470e3),  Vlim=Vb/Vin*1.1=3.44V, Volts per bit = Vlim/1023= 0.003363075
#define VMIN 1.9  // Battery monitor lower level. Vmin_radio=1.9V
#define VMAX 3.3  //  " " " high level. Vmin<Vmax<=3.44

int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
int oldBatteryPcnt = 0;
--------------------------------------------------
In setup:

Code: Select all

  analogReference(INTERNAL);    // use the 1.1 V internal reference for battery level measuring
--------------------------------------------------
In loop:

Code: Select all

 int sensorValue = analogRead(BATTERY_SENSE_PIN);    // Battery monitoring reading
//  Serial.println(sensorValue);
  float Vbat  = sensorValue * VBAT_PER_BITS;
//  Serial.print("Battery Voltage: "); Serial.print(Vbat); Serial.println(" V");
 //int batteryPcnt = sensorValue / 10;
  int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);   
//  Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");
  if (oldBatteryPcnt != batteryPcnt) {
    gw.sendBatteryLevel(batteryPcnt);
    oldBatteryPcnt = batteryPcnt;
   }
Controller: Domoticz (Raspberry PI)
Gateways: MySensors (Ethernet W5100), RFLink
User avatar
thecosmicgate
Posts: 188
Joined: Monday 06 April 2015 14:37
Target OS: Linux
Domoticz version: newest
Location: The Netherlands / Hoeven
Contact:

Re: Combine sketches

Post by thecosmicgate »

sundberg84 wrote:#define VBAT_PER_BITS 0.003363075 // Calculated volts per bit from the used battery montoring voltage divider. Internal_ref=1.1V, res=10bit=2^10-1=1023, Eg for 3V (2AA): Vin/Vb=R1/(R1+R2)=470e3/(1e6+470e3), Vlim=Vb/Vin*1.1=3.44V, Volts per bit = Vlim/1023= 0.003363075
#define VMIN 1.9 // Battery monitor lower level. Vmin_radio=1.9V
#define VMAX 3.3 // " " " high level. Vmin<Vmax<=3.44

int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
int oldBatteryPcnt = 0;
--------------------------------------------------
In setup:
analogReference(INTERNAL); // use the 1.1 V internal reference for battery level measuring
--------------------------------------------------
In loop:

int sensorValue = analogRead(BATTERY_SENSE_PIN); // Battery monitoring reading
// Serial.println(sensorValue);
float Vbat = sensorValue * VBAT_PER_BITS;
// Serial.print("Battery Voltage: "); Serial.print(Vbat); Serial.println(" V");
//int batteryPcnt = sensorValue / 10;
int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
// Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");
if (oldBatteryPcnt != batteryPcnt) {
gw.sendBatteryLevel(batteryPcnt);
oldBatteryPcnt = batteryPcnt;
I can copy paste this in the 3in1 sketch ?

Sent from my MotoG3 using Tapatalk
It's nice to be important, but it's more important to be nice
Scooter ;)
sundberg84
Posts: 52
Joined: Sunday 17 May 2015 11:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sweden
Contact:

Re: Combine sketches

Post by sundberg84 »

Yes, this is the battery code if you have a voltage divider described in http://www.mysensors.org/build/battery# ... tery-level
Controller: Domoticz (Raspberry PI)
Gateways: MySensors (Ethernet W5100), RFLink
User avatar
thecosmicgate
Posts: 188
Joined: Monday 06 April 2015 14:37
Target OS: Linux
Domoticz version: newest
Location: The Netherlands / Hoeven
Contact:

Re: Combine sketches

Post by thecosmicgate »

sundberg84 wrote:Yes, this is the battery code if you have a voltage divider described in http://www.mysensors.org/build/battery# ... tery-level
I'am going to buy the resistors .
But its that easy to open the 3in1 sketch and copy paste the battery sketch below this 3in1 sketch ?

Sent from my MotoG3 using Tapatalk
It's nice to be important, but it's more important to be nice
Scooter ;)
User avatar
Domosapiens
Posts: 232
Joined: Wednesday 20 August 2014 12:08
Target OS: Windows
Domoticz version: V3.5981
Location: NL
Contact:

Re: Combine sketches

Post by Domosapiens »

Useless information detection:
Sent from my MotoG3 using Tapatalk
Who cares?

Domosapiens
Win Vista&7; 1#Aeon Z-Stick S2; 1#Aeotec Z-Sick Gen5, 6#Fib.FGBS001; 24#DS18B20; 8#Everspr.AN158-2; 3#Philio PAN04; 1#Philio PAN06, 1#YouLess El; 1#Fib.FGWPE; 1#ZME_RC2; 2#FAK_ZWS230, 2#Quib.ZMNHCDx, 1#Quib.ZMNHDD1, 7#EM6555
User avatar
thecosmicgate
Posts: 188
Joined: Monday 06 April 2015 14:37
Target OS: Linux
Domoticz version: newest
Location: The Netherlands / Hoeven
Contact:

Re: Combine sketches

Post by thecosmicgate »

Domosapiens wrote:Useless information detection:
Sent from my MotoG3 using Tapatalk
Who cares?

Domosapiens
Nobody :) but thats some default on the tapatalk app on the mobile phone, had tot uncheck this
It's nice to be important, but it's more important to be nice
Scooter ;)
sundberg84
Posts: 52
Joined: Sunday 17 May 2015 11:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sweden
Contact:

Re: Combine sketches

Post by sundberg84 »

thecosmicgate wrote: I'am going to buy the resistors .
But its that easy to open the 3in1 sketch and copy paste the battery sketch below this 3in1 sketch ?

Sent from my MotoG3 using Tapatalk
Yea, pretty much - copy and paste - try it out, run the sketch and debug on serial, thats how it is done.
You need to try instead, you will make it.
Controller: Domoticz (Raspberry PI)
Gateways: MySensors (Ethernet W5100), RFLink
User avatar
thecosmicgate
Posts: 188
Joined: Monday 06 April 2015 14:37
Target OS: Linux
Domoticz version: newest
Location: The Netherlands / Hoeven
Contact:

Re: Combine sketches

Post by thecosmicgate »

The sketch i created :

Code: Select all

#include <SPI.h>
#include <MySensor.h>  
#include <DHT.h>  

int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point

#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define CHILD_ID_MOT 2   // Id of the sensor child
#define HUMIDITY_SENSOR_DIGITAL_PIN 4

#define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
#define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)


unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
int oldBatteryPcnt = 0;

MySensor gw;
DHT dht;
float lastTemp;
float lastHum;
boolean metric = true; 
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);

void setup()  
{ 
   // use the 1.1 V internal reference
#if defined(__AVR_ATmega2560__)
   analogReference(INTERNAL1V1);
#else
   analogReference(INTERNAL);
#endif
  gw.begin();
  dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 

  // Send the Sketch Version Information to the Gateway
  gw.sendSketchInfo("Humidity/Motion", "1.0");


 pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
 
  // Register all sensors to gw (they will be created as child devices)
  gw.present(CHILD_ID_HUM, S_HUM);
  gw.present(CHILD_ID_TEMP, S_TEMP);
  gw.present(CHILD_ID_MOT, S_MOTION);
   
  metric = gw.getConfig().isMetric;
}

void loop()      

{  
  // get the battery Voltage
   int sensorValue = analogRead(BATTERY_SENSE_PIN);
   #ifdef DEBUG
   Serial.println(sensorValue);
   #endif
   
   // 1M, 470K divider across battery and using internal ADC ref of 1.1V
   // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
   // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
   // 3.44/1023 = Volts per bit = 0.003363075
   float batteryV  = sensorValue * 0.003363075;
   int batteryPcnt = sensorValue / 10;

   #ifdef DEBUG
   Serial.print("Battery Voltage: ");
   Serial.print(batteryV);
   Serial.println(" V");

   Serial.print("Battery percent: ");
   Serial.print(batteryPcnt);
   Serial.println(" %");
   #endif

   if (oldBatteryPcnt != batteryPcnt) {
     // Power up radio after sleep
     gw.sendBatteryLevel(batteryPcnt);
     oldBatteryPcnt = batteryPcnt;
   }
     
     
  // Read digital motion value
  boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
        
  Serial.println(tripped);
  gw.send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
  
  delay(dht.getMinimumSamplingPeriod());

  float temperature = dht.getTemperature();
  if (isnan(temperature)) {
      Serial.println("Failed reading temperature from DHT");
  } else if (temperature != lastTemp) {
    lastTemp = temperature;
    if (!metric) {
      temperature = dht.toFahrenheit(temperature);
    }
    gw.send(msgTemp.set(temperature, 1));
    Serial.print("T: ");
    Serial.println(temperature);
  }
  
  float humidity = dht.getHumidity();
  if (isnan(humidity)) {
      Serial.println("Failed reading humidity from DHT");
  } else if (humidity != lastHum) {
      lastHum = humidity;
      gw.send(msgHum.set(humidity, 1));
      Serial.print("H: ");
      Serial.println(humidity);
  }

  
 
  // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
  gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
}
It's nice to be important, but it's more important to be nice
Scooter ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest