Help with XivelyLib

I’m trying to combine the library example with my code to read the temp. I think I understand enough to alter it but when it comes to changing the function sensorUpdate(). I’m lost espically since I don;t have the original context of the code.

Here is my sensor code:

double temperature = 0.0;
int tempf = 0;

void setup()
{
  // Register a Spark variable here
  Spark.variable("temperature", &temperature, DOUBLE);
  Spark.variable("tempf", &tempf, INT);

  // Connect the temperature sensor to A7 and configure it to be an input
  pinMode(A7, INPUT);
}

void loop() {
  int reading = 0;
  double voltage = 0.0;
        
  // Keep reading the sensor value so when we make an API
  // call to read its value, we have the latest one
  reading = analogRead(A7);

  // The returned value from the Core is going to be in the range from 0 to 4095
  // Calculate the voltage from the sensor reading
  voltage = (reading * 3.3) / 4095;

  // Calculate the temperature and update our static variable
  temperature = (voltage - 0.5) * 100;
  tempf = (temperature * 1.8) + 32;
  delay(500);
}

The function looks like this:

void sensorUpdate() { 
    static unsigned long sensor1_updateTimer = millis(); //analog update timer
    static unsigned long sensor2_updateTimer = millis(); //analog update timer
    
    //check when to get the sensor readings
    if (millis()-sensor1_updateTimer > SENSOR1_UPDATETIMERINTERVALMS) {
        sensor1_updateTimer = millis();
        
        #if UARTDEBUG == 1
        Serial.println("Aquire sensor1 data... ");
        #endif
        xivelydatapoint_sensor1tot += analogRead(SENSOR1_PORT);
        xivelydatapoint_sensor1totcount++;
    }

The code is obfuscated to me and am I’m unsure if I just add the content from my loop function bellow #endif and then just add my final variable after +=

Last if anyone could give me some good troubleshoot tips and how to check if variables are right that would be greatly appreciated.

**

Thanks in advance to anyone who helps :smiley:

**

@peekay123 do you have some time to look through this? :smile:

@kennethlimcp I’ll have a look :wink:

1 Like

@Icon, unlike your loop() code which uses a delay(500) to slow down the sampling, the sensorUpdate() example uses non-blocking millis() timers (one for each sensor). You can modify sensorUpdate() for your code using a single timer and making sure you update the xively variables with your data as required. Give it a shot and let me know if you need assistance

The most common way to debug is to use Serial.print() to output variable values and other debug messages you've added in your code. This will print to a serial terminal on your PC or Mac while connected to the Core via USB. In the Xively example there is a compile flag called "UARTDEBUG" that when set to "1" (default) will automatically setup the serial port and print debug message to the console. :smile:
.

1 Like

@peekay123 I have no clue about millis(), how to implement it or how to integrate it into my code. Just looking at the example code from the library is very overwhelming for me.

I don’t know how to use millis() in the code as I haven’t done that before.

Should the new sensorUpdate() look like this:

void sensorUpdate() { 
    // Keep reading the sensor value so when we make an API
  // call to read its value, we have the latest one
  reading = analogRead(A7);

  // The returned value from the Core is going to be in the range from 0 to 4095
  // Calculate the voltage from the sensor reading
  voltage = (reading * 3.3) / 4095;

  // Calculate the temperature and update our static variable
  temperature = (voltage - 0.5) * 100;
  tempf = (temperature * 1.8) + 32;
 mills(500);   
 }

There is a lot going on here that I don’t understand. Could you maybe explain it part by part?

Thanks

Update: I tried just to verify the code and it wouldn’t compile. I got all kinds of errors about deprecated conversions, variables not defined in scope and comparison between signed and unsigned integer expressions.

@Icon, millis() is a millisecond counter (there is also a microseconds() counter) that is updated in the background. To use it as a non-blocking delay, here is the basic form:

//This could go in setup():
unsigned long starttime = millis();   // Set starttime to the current millis() count

if (millis() - starttime >= 5000)    //  Current ms - start ms = elapsed millis()
{                                  // If the elapsed time is 5000ms (5 secs) or more, then run the code
  // This code will run every 5000ms
  //*** ADD CODE HERE ***
  starttime = millis();  // Reset the starttime to current millis() value for new delay
}

This form of delay simply continues if the desired amount of time has not passed and continues running the user code. Thus it is considered non-blocking unlike delay(X) which is.

Following this form, you would setup a global var, say “sampleInterval” that you set in setup(). Then in sensorUpdate(), you wrap the sampling code inside the “if” code above so it only runs when XXXX ms have passed. Hope that helps! :smile: