Core Not Reading/Writing Analog or DIgital Values [Solved]

Hello all! I am having an issue with my “stealth” core reading and writing analog and digital values. I have connected a piezoelectric element to the A3 pin of my core and I am attempting to read the analog value of the pin using curl. When I make the GET request, the returned result is always zero. I’ve moved the element to other analog pins (and changed the relevant code), but the result is the same. Measuring the output of the piezoelectric element with a multimeter returns a non-zero value. The code on my core is posted below:

#include "application.h"
#include "math.h"

const int piezoPin = A3; //piezoelectric element connected to A3
const int ledPin = D7; //D7 LED on Spark Core
double inst_volts; //average volts in
double instVoltSum;
int maxVolts = 3; //max volts generated by piezo element
int i = 0; //counter

char publishString[40];

void setup() {
    pinMode(piezoPin, INPUT);
    digitalWrite(ledPin, LOW);
    Spark.variable("inst_volts", &inst_volts, INT);
}

void loop() {
    instVoltSum = 0;
    for(i = 0; i < 10; i++){
        instVoltSum += analogRead(piezoPin);
    }

    inst_volts = (instVoltSum/10)*(maxVolts/4096); //average volts calculation

    if(inst_volts > 1.5){
        sprintf(publishString, "The average generated voltage was: %d", (double) inst_volts);
        Spark.publish("Energy", publishString);
        digitalWrite(ledPin, HIGH);
    } else {
        digitalWrite(ledPin, LOW);
    }
}

The GET request is as follows:

https://api.spark.io/v1/devices/[Core ID]/inst_volts?access_token=[access_token]

As stated, the returned result is always zero. I’ve also reflashed the core with Tinker and attempted to read the analog pin. The app presents a “404” error. A similar error occurs when attempting to write the D7 LED “HIGH”.

Has anyone else experienced these issues, and if so, how were they resolved?

Thanks for your help!

Hi @gaudsend,

It looks like inst_volts is declared as a double, but your Spark.variable call is calling it an INT, can you change your spark variable line to:

 Spark.variable("inst_volts", &inst_volts, DOUBLE);

Thanks!
David

Thanks for the catch @Dave. Unfortunately, that didn’t resolve the issue. The Tinker app still reports a 404 error and the curl GET request still returns a result of zero.

As a note, I have also connected a temperature sensor to A3 in an attempt to determine if the issue was confined to the piezo element. The temp sensor reading also returned “0”.

Hi @gaudsend,

If you’re running a custom app, then the normal “Tinker” app won’t interact with your app. The Tinker mobile app expects 4 functions (digitalread, digitalwrite, analogread, and analogwrite) to be exposed. Can you try using the spark-cli with your firmware? https://github.com/spark/spark-cli

spark list
spark get your_core inst_volts

edit: I try to be really explicit with int / double math as well, so just to be safe:

maybe change: 
int maxVolts = 3;
inst_volts = (instVoltSum/10)*(maxVolts/4096)

to:
double maxVolts = 3;
inst_volts = (instVoltSum/10.0)*(maxVolts/4096.0)

Thanks!
David

1 Like

Hi @Dave,

I am aware that the custom app and the Tinker app won’t interact. I have flashed my app, tested using curl, then flashed Tinker and tested using the iPhone app. The results are as posted above. I have also used spark-cli for claiming the core. What commands would you suggest? I’ll implement your code changes as well!

Thanks!

1 Like

Hmmm, your code change fixed the issue! Interesting. I’ll be more careful with int/double variables.

Thank you very much for your help!

1 Like

Hi @gaudsend,

Awesome, glad I could help! :slight_smile:

Thanks,
David