ThingSpeak Sonar Sensor Setup

Hello, I am a novice programmer and I’m trying to publish the sensor data to ThingSpeak any help would be great!

#include "application.h"

void setup() {
    Serial.begin(115200);
}

void loop() {
    // Trigger pin, Echo pin, delay (ms), visual=true|info=false
    ping(D2, D6, 20, true);
    
}

void ping(pin_t trig_pin, pin_t echo_pin, uint32_t wait, bool info)
{
    uint32_t duration, inches, cm;
    static bool init = false;
    if (!init) {
        pinMode(trig_pin, OUTPUT);
        digitalWriteFast(trig_pin, LOW);
        pinMode(echo_pin, INPUT);
        delay(1);
        init = true;
    }

    /* Trigger the sensor by sending a HIGH pulse of 10 or more microseconds */
    digitalWriteFast(trig_pin, HIGH);
    delayMicroseconds(10);
    digitalWriteFast(trig_pin, LOW);
  
    duration = pulseIn(echo_pin, HIGH);
    
    /* Convert the time into a distance */
    // Sound travels at 1130 ft/s (73.746 us/inch)
    // or 340 m/s (29 us/cm), out and back so divide by 2
    // Ref: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
    inches = duration / 74 / 2;
    cm = duration / 29 / 2;
      
    if (info) { /* Visual Output */
        Serial.printf("%2d:", inches);
        for(int x=0;x<inches;x++) Serial.print("#");
        Serial.println();
    } else { /* Informational Output */
        Serial.printlnf("%6d in / %6d cm / %6d us", inches, cm, duration);
    }
    delay(wait); // slow down the output
    
}

A good starting point might be the library samples (e.g. WriteVoltage.ino) provided here
https://build.particle.io/libs/576c40f31298658c3a00068e

Also some threads findable via the search feature ( :mag: ) top right on this page and the keyword ThingSpeak might bring some enlightenment.

Still cant seem to get it to work. I cant seem to send the output from void.

If I only knew what you mean with “from void” :confused:

Have you tried the sample I linked?

Yes, and I just need to get the Serial.printlnf("%6d in / %6d cm / %6d us", inches, cm, duration); part to send the data into a string so I can use Particle.publish(“gen-distance”, distance, PRIVATE);.

That doesn’t have anything to do with ThingSpeak tho’ :confused:

  char msg[64];
  snprintf(msg, sizeof(msg), "%6d in / %6d cm / %6d µs", inches, cm, duration);
  Serial.println(msg);
  Particle.publish("gen-distance", msg, PRIVATE);
1 Like

I edited the file with all your changes and I still can’t get a dot to appear on ThingSpeak!

#include "application.h"

void setup() {
    Serial.begin(115200);
}

void loop() {
    // Trigger pin, Echo pin, delay (ms), visual=true|info=false
    ping(D2, D6, 20, true);
    
}

void ping(pin_t trig_pin, pin_t echo_pin, uint32_t wait, bool info)
{
    uint32_t duration, inches, cm;
    static bool init = false;
    if (!init) {
        pinMode(trig_pin, OUTPUT);
        digitalWriteFast(trig_pin, LOW);
        pinMode(echo_pin, INPUT);
        delay(1);
        init = true;
    }

    /* Trigger the sensor by sending a HIGH pulse of 10 or more microseconds */
    digitalWriteFast(trig_pin, HIGH);
    delayMicroseconds(10);
    digitalWriteFast(trig_pin, LOW);
  
    duration = pulseIn(echo_pin, HIGH);
    
    /* Convert the time into a distance */
    // Sound travels at 1130 ft/s (73.746 us/inch)
    // or 340 m/s (29 us/cm), out and back so divide by 2
    // Ref: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
    inches = duration / 74 / 2;
    cm = duration / 29 / 2;
      
    if (info) { /* Visual Output */
        Serial.printf("%2d:", inches);
        for(int x=0;x<inches;x++) Serial.print("#");
        Serial.println();
    } else { /* Informational Output */
            char msg[64];
            snprintf(msg, sizeof(msg), "%6d in / %6d cm / %6d us", inches, cm, duration);
            Serial.printlnf(msg);
            Particle.publish("gen-distance", msg, PRIVATE);
    }
}

Since you don’t send anything to ThingSpeak!

As I said above

Can you point me to the statement that you see in connection with ThingSpeak?
I can’t see it.

There is a ThingSpeak webhook and to verify that the hook was working properly I used the web hook tutorial and everything worked fine. All I want to do is send the data in cm from the sensor to ThingSpeak so I can visualize it.

Then you might also tell us how your webhook is set up.
But I’m quite positive that just putting together an arbitrary string and publishing that is not enough.

The sample in the docs does only sends one value without any other text in the event message!

1 Like