Webhook publishing

i’m trying to use webhooks to send data to Thingspeak, as shown here


but when i send data from void loop() like this
Particle.publish(“voltage”, voltage, PRIVATE);
it shows an error saying

what even is the problem here? how do i solve it?

The second parameter for Particle.publish() needs to be a string, not a numeric data type.

The error message states that you are passing a double which is not a string :wink:

oh god
what should i do if i need a double instead

You'd translate the double into a string :wink:

The simplest way would be

Particle.publish("voltage", String(voltage), PRIVATE);

but since I tend to avoid String wherever possible, I'd rather do this

  char txt[32];
  snprintf(txt, sizeof(txt), "%.2f", voltage);
  Particle.publish("voltage", txt, PRIVATE);
1 Like

Thanks for helping! The code is working now – I’m on the next stage of frustration: Why isn’t this updating? Why isn’t it measuring the voltage? IT’S DUE SOON COOPERATE WITH ME

I have been - in my free time, I may note!

It's absolutely not appreciated to have all-caps demands thrown at people who volunteer in helping others.

oh wait sorry i was screaming at the particle
you see, i used code from somebody else and modified it to use webhooks instead of the nonfunctioning (to me at least) IFTTT but it isn’t registering any voltage change from the piezoelectric transducer i hooked it up with (i have a project to measure electricity generated from the transducers) so i’m not very happy about that currently

Can you post your code (e.g. via SHARE THIS REVISION in Web IDE)?
Also some schematics about your HW setup would be required.


BTW, Particle devices don't read the forum posts - us humans do.

ummm… everything is almost directly copied off


and the wiring is

This might also translate to: "It may almost work as the original, but not quite."

As I said in the other thread already

If you want to be helped to get your version do what you want, you should show your version of it, not some almost similar project.

1 Like

(How I wish people would actually do what they're told :pensive: )
Screenshots are not really the preferred means to share code, as it would mean typing off the image.
That's why I mentioned this

Which is refering to this

However, having a quick look at your code I did spot at least one issue.

And exactly that pin conflict is not present in the original code you linked to, so looking at that would not have done any good but only wasted time, if I had looked at it for any error.

Also the Arduino ADCs use 5V at 10 bit (raw reading 0..1023) while the Particle ADCs are 3.3V @12bit (0..4095).

And to sprinkle over the top, the piezo connections don't look too reassuring either.
To have a proper connection you should employ a soldering iron :wink:

1 Like

in the original code, the ledPin variable didn’t exist, yet the code called for it, resulting in multiple errors.

and… my soldering iron isn’t exactly working. it isn’t heating anything at all so technically i don’t have a soldering iron

https://go.particle.io/shared_apps/5b2fa288ed8d6b1dad000c4f

On Arduinos the ledPin is usually set as #13 which is connected to the onboard LED.
For Particle devices that would be D7 and definetly not the same pin you are using to read the sensor.
If you strip down your code to the bare minimum to actually test the sensor input, you could use something like Arduino Serial Plotter to see if you get any useful readings at all since the ADCs on Particle devices operate slightly different than Arduino's.

const int ledPin      = D7;
const int knockSensor = A0;
const int threshold   = 100;
int sensorReading     = 0;

void setup() {
    pinMode(ledPin, OUTPUT);
    Serial.begin(115200);
}

void loop() {
    sensorReading = analogRead(knockSensor);
    Serial.printlnf("%d", sensorReading);
    if (sensorReading >= threshold) {
        digitalWrite(ledPin, !digitalRead(ledPin));
    }
    delay(20);
}

i made the corrections you showed me!
https://go.particle.io/shared_apps/5b30887254e3ea38f6000b40
but now there’s nothing underneath the Current Voltage line… it’s completely blank.
can i replace Serial.printlnf() with lcd.print() ?

You can try this to test whether your piezo is working at all

void setup() {
  pinMode(A0, OUTPUT);
}
void loop() {
  digitalWrite(A0, !digitalRead(A0));
}

This should make the piezo emit a sound.

This is what I see on Arduino Serial Plotter with the code I provided in my previous post when knocking the sensor

1 Like

yep, it is working (very loudly)
but right now all it’s showing underneath (changing the print) is always above 4000
it’s usually hanging around 4086 - 4088 but it goes down to as far as 4031 when i tap it. huh.
https://go.particle.io/shared_apps/5b30971754e3ea38f6000bab

Have you tried Arduino Serial Plotter?
What does it show you?

What I can deduce from what I’m seeing is, that the threshold of 100 is definetly too low (at least for my piezo) and that the recovery time after a trigger might call for some kind of pull-down to “discharge” quicker.

ok… give me some time to google “arduino serial plotter” and some more to start using it
wait what is 'arduino ide"

Arduino Serial Plotter was just a convenient way to get the readings plottet nicely to get a feeling for the data your setup is producing.
If you don’t want to install Arduino IDE (integrated development environment) which contains the Serial Plotter, you could try to find any other tool that would catch the serial output and present it as chart.

However I’ve slightly reworked the knock detection logic this way

const int knockSensor = A0;
const int ledPin      = D7;
const int threshold   = 250;    // minimum difference between two readings to trigger
const int debounce    = 200;    // debounce period after a trigger in milliseconds

void setup() {
    Serial.begin(115200);
    pinMode(ledPin, OUTPUT);
}

void loop() {
    static int oldReading = analogRead(knockSensor);
    int        newReading = analogRead(knockSensor);
    
    Serial.printlnf("%d %d 0 4095", newReading, abs(oldReading-newReading));
    if (abs(oldReading-newReading) >= threshold) {
        digitalWrite(ledPin, !digitalRead(ledPin));
        delay(debounce);
        oldReading = analogRead(knockSensor); // new base after trigger
    }
    else
        oldReading = newReading;
        
    delay(20);
}

which produces this kind of data when knocking the sensor


The blue line is the raw data and the red (abs(oldReading - newReading)) is used to detect the trigger.

1 Like