Hulp met samen fout vinden

Arduino specifieke Software
Berichten: 3
Geregistreerd: 06 Apr 2017, 20:36

Hulp met samen fout vinden

Berichtdoor Reneke » 06 Apr 2017, 20:44

Hallo,

Ik wou vragen of iemand me zou willen helpen, ben nog niet zo lang bezig met Arduino en Mysensors.
Had al een beetje met sketches iets op gang gekregen, en wou een DS1820 erin verwerken.
Ik heb het nog behoorlijk moeilijk ermee wanneer wel of geen braces te moeten gebruiken.
Waardoor mijn sketch niet meer aan de praat te krijgen is.

Code: Alles selecteren
// Enable debug prints to serial monitor
#define MY_DEBUG

// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69

#include <SPI.h>
#include <MySensors.h> 
#include <BH1750.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// arrays to hold device address
DeviceAddress insideThermometer;


unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
int SOLAR_SENSE_PIN = A1;  // select the input pin for the solar sense point
#define CHILD_ID_BATTERY 4
#define CHILD_ID_SOLAR 6
#define CHILD_ID_LIGHT 1
#define CHILD_ID_TEMP 2

BH1750 lightSensor;

int oldBatteryPcnt = 0;
MyMessage msgTemp1(0,V_TEMP);
MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE);
MyMessage msgsolar(CHILD_ID_SOLAR, V_VOLTAGE);
MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
uint16_t lastlux;  //lux

void setup(void)
{
// use the 1.1 V internal reference
  #if defined(__AVR_ATmega2560__)
  analogReference(INTERNAL1V1);
  #else
  analogReference(INTERNAL);
  #endif
{
// Send the sketch version information to the gateway and Controller
  sendSketchInfo("Battery Meter", "2.0");
  present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
 

  // start serial port
  Serial.begin(9600);
// locate devices on the bus
  Serial.print("Locating devices...");
  sensors.begin();
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");
// report parasite power requirements
  Serial.print("Parasite power is: ");
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");
// assign address manually.  the addresses below will beed to be changed
  // to valid device addresses on your bus.  device address can be retrieved
  // by using either oneWire.search(deviceAddress) or individually via
  // sensors.getAddress(deviceAddress, index)
  //insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };

  // Method 1:
  // search for devices on the bus and assign based on an index.  ideally,
  // you would do this to initially discover addresses on the bus and then
  // use those addresses and manually assign them (see above) once you know
  // the devices on your bus (and assuming they don't change).
  if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
 
  // method 2: search()
  // search() looks for the next device. Returns 1 if a new address has been
  // returned. A zero might mean that the bus is shorted, there are no devices,
  // or you have already retrieved all of them.  It might be a good idea to
  // check the CRC to make sure you didn't get garbage.  The order is
  // deterministic. You will always get the same devices in the same order
  //
  // Must be called before search()
  //oneWire.reset_search();
  // assigns the first address found to insideThermometer
  //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");

  // show the addresses we found on the bus
  Serial.print("Device 0 Address: ");
  printAddress(insideThermometer);
  Serial.println();

  // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
  sensors.setResolution(insideThermometer, 9);
 
  Serial.print("Device 0 Resolution: ");
  Serial.print(sensors.getResolution(insideThermometer), DEC);
  Serial.println(); 
}}

// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
  // method 1 - slower
  //Serial.print("Temp C: ");
  //Serial.print(sensors.getTempC(deviceAddress));
  //Serial.print(" Temp F: ");
  //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit

  // method 2 - faster
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Temp F: ");
  Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}



void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
 
  // It responds almost immediately. Let's print out the data
  printTemperature(insideThermometer); // Use a simple function to print out the data


 // get the battery Voltage
  int sensorValue2 = analogRead(BATTERY_SENSE_PIN);
  delay(1000);   
 
  int sensorValue1 = analogRead(BATTERY_SENSE_PIN);
  delay(100);
 
  int sensorValue = analogRead(BATTERY_SENSE_PIN);
  delay(1000);
   
  int sensorValueS = analogRead(SOLAR_SENSE_PIN);
  delay(1000);
 
#ifdef MY_DEBUG
  Serial.print("Battery Voltage2: ");
  Serial.println(sensorValue2);
  Serial.print("Battery Voltage1: ");
  Serial.println(sensorValue1);
  Serial.print("Battery Voltage: ");
  Serial.println(sensorValue);
  Serial.print("Solar Voltage: ");
  Serial.println(sensorValueS);
#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
  const float vRef = 4.200/ 1.05/ 1023 ; 
  int batteryPcnt = sensorValue / 10;
  float batteryV  = sensorValue * 0.0042598 ; // 0.0038952294568380753114792412093 max 4,2 volt
  float batteryS  = sensorValueS * 0.0102459 ; // 0.0038952294568380753114792412093 max 10 volt
 
#ifdef MY_DEBUG
  Serial.print("Battery Voltage: ");
  Serial.print(batteryV);
  Serial.println(" V");
send(msgbatt.set(batteryV ,2));

  Serial.print("Solar Voltage: ");
  Serial.print(batteryS);
  Serial.println(" V");
send(msgsolar.set(batteryS ,2));

  Serial.print("Battery percent: ");
  Serial.print(batteryPcnt);
  Serial.println(" %");
#endif
   if (oldBatteryPcnt != batteryPcnt) {
    // Power up radio after sleep
    sendBatteryLevel(batteryPcnt);
    oldBatteryPcnt = batteryPcnt;
  }
     
  uint16_t lux = lightSensor.readLightLevel();// Get Lux value
  Serial.println(lux);
  if (lux != lastlux) {
      send(msg.set(lux));
      lastlux = lux;
  }
 
}
     sleep(SLEEP_TIME);
}



Hopelijk heb ik de code goed geplaatst.

alle hulp / uitleg is welkom.
p.s. Uiteindelijk wil ik dus ook straks de temperatuur van de ds1820 verzenden via Mysensors naar Domotica.

Advertisement

Berichten: 3
Geregistreerd: 06 Apr 2017, 20:36

Re: Hulp met samen fout vinden

Berichtdoor Reneke » 06 Apr 2017, 22:27

Vergeet de foutmelding die ik krijg...
'printAddress' was not declared in this scope

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: Hulp met samen fout vinden

Berichtdoor Koepel » 07 Apr 2017, 06:38

Je bedoelt van die { } curly braces of brackets : https://www.arduino.cc/en/reference/braces
Accolades: https://nl.wikipedia.org/wiki/Accolade_(leesteken)

Problemen met die curley braces kun je voorkomen door het inspringen altijd heel consequent te doen, en door iedere spatie goed te zetten. Geen spatie te veel en geen spatie te weinig.
In de Arduino IDE zit in het menu: Hulpmiddelen / Automatische opmaak.
Dan wordt het de tekst layout opnieuw gemaakt. Helaas is dat met een stijl die ik niet leuk vind. Het is echter wel mogelijk om de manier van automatische opmaak in een configuratiebestand te wijzigen.

Met die automatische opmaak zie je meteen dat de onderste twee regels niet kloppen. Die hangen er maar een beetje bij buiten de loop() functie.

De functie "printAddress" zit niet in de sketch, dus de compiler kan die functie niet vinden.

Berichten: 3
Geregistreerd: 06 Apr 2017, 20:36

Re: Hulp met samen fout vinden

Berichtdoor Reneke » 08 Apr 2017, 09:48

met moeite is het me wel gelukt.
Nu nog uitzoeken hoe ik de temperatuur via mysensors naar domotica stuur.

Code: Alles selecteren
// Enable debug prints to serial monitor
#define MY_DEBUG

// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69

#include <SPI.h>
#include <MySensors.h> 
#include <BH1750.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// arrays to hold device address
DeviceAddress insideThermometer;


unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
int SOLAR_SENSE_PIN = A1;  // select the input pin for the solar sense point
#define CHILD_ID_BATTERY 4
#define CHILD_ID_SOLAR 6
#define CHILD_ID_LIGHT 1
#define CHILD_ID_TEMP 2

BH1750 lightSensor;

int oldBatteryPcnt = 0;
MyMessage msgTemp1(0,V_TEMP);
MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE);
MyMessage msgsolar(CHILD_ID_SOLAR, V_VOLTAGE);
MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
uint16_t lastlux;  //lux

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

 
// Send the sketch version information to the gateway and Controller
  sendSketchInfo("Battery Meter", "2.0");
  present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
 
  //Serial.begin(9600); // start serial port
  Serial.print("Locating devices..."); // locate devices on the bus
  sensors.begin(); // start reading sensor
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");
  Serial.print("Parasite power is: "); // report parasite power requirements
 
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");
// assign address manually.  the addresses below will beed to be changed
  // to valid device addresses on your bus.  device address can be retrieved
  // by using either oneWire.search(deviceAddress) or individually via
  // sensors.getAddress(deviceAddress, index)
  //insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };

  // Method 1:
  // search for devices on the bus and assign based on an index.  ideally,
  // you would do this to initially discover addresses on the bus and then
  // use those addresses and manually assign them (see above) once you know
  // the devices on your bus (and assuming they don't change).
  if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
 
  // method 2: search()
  // search() looks for the next device. Returns 1 if a new address has been
  // returned. A zero might mean that the bus is shorted, there are no devices,
  // or you have already retrieved all of them.  It might be a good idea to
  // check the CRC to make sure you didn't get garbage.  The order is
  // deterministic. You will always get the same devices in the same order
  //
  // Must be called before search()
  //oneWire.reset_search();
  // assigns the first address found to insideThermometer
  //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");

  // show the addresses we found on the bus
  Serial.print("Device 0 Address: ");
  printAddress(insideThermometer);
  Serial.println();

  // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
  sensors.setResolution(insideThermometer, 9);
 
  Serial.print("Device 0 Resolution: ");
  Serial.print(sensors.getResolution(insideThermometer), DEC);
  Serial.println(); 
}

// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
  // method 1 - slower
  //Serial.print("Temp C: ");
  //Serial.print(sensors.getTempC(deviceAddress));
  //Serial.print(" Temp F: ");
  //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit

  // method 2 - faster
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Temp F: ");
  Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}



void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
 
  // It responds almost immediately. Let's print out the data
  printTemperature(insideThermometer); // Use a simple function to print out the data
{

 // get the battery Voltage
  int sensorValue2 = analogRead(BATTERY_SENSE_PIN);
  delay(1000);   
 
  int sensorValue1 = analogRead(BATTERY_SENSE_PIN);
  delay(100);
 
  int sensorValue = analogRead(BATTERY_SENSE_PIN);
  delay(1000);
   
  int sensorValueS = analogRead(SOLAR_SENSE_PIN);
  delay(1000);
 
#ifdef MY_DEBUG
  Serial.print("Battery Voltage2: ");
  Serial.println(sensorValue2);
  Serial.print("Battery Voltage1: ");
  Serial.println(sensorValue1);
  Serial.print("Battery Voltage: ");
  Serial.println(sensorValue);
  Serial.print("Solar Voltage: ");
  Serial.println(sensorValueS);
#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
  const float vRef = 4.200/ 1.05/ 1023 ; 
  int batteryPcnt = sensorValue / 10;
  float batteryV  = sensorValue * 0.0042598 ; // 0.0038952294568380753114792412093 max 4,2 volt
  float batteryS  = sensorValueS * 0.0102459 ; // 0.0038952294568380753114792412093 max 10 volt
 
#ifdef MY_DEBUG
  Serial.print("Battery Voltage: ");
  Serial.print(batteryV);
  Serial.println(" V");
send(msgbatt.set(batteryV ,2));

  Serial.print("Solar Voltage: ");
  Serial.print(batteryS);
  Serial.println(" V");
send(msgsolar.set(batteryS ,2));

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

   if (oldBatteryPcnt != batteryPcnt) {
    // Power up radio after sleep
    sendBatteryLevel(batteryPcnt);
    oldBatteryPcnt = batteryPcnt;
  }
     
  uint16_t lux = lightSensor.readLightLevel();// Get Lux value
  Serial.println(lux);
  if (lux != lastlux) {
      send(msg.set(lux));
      lastlux = lux;
  }}
  sleep(SLEEP_TIME);
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}
 



Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Bing [Bot], Donaldglors en 22 gasten