Trouble Streaming info to Thingspeak

I’ve been writing a code up for an SR04 Sensor and I have been trying to relay the data to thingspeak (Via a Webhook).

New to coding and don’t know if this is a small error on my part or what. Here is my Code

TCPClient client;

unsigned long myChannelNumber = 175756;
const char * myWriteAPIKey = "GWDW0IJU8YB5RTG3";

#define echoPin 7 // Echo Pin
#define trigPin 2 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop()
 {
  // Get some data
  String data = String(10);
  // Trigger the integration
  Particle.publish("Rangefinder", data, PRIVATE);
  // Wait 60 seconds
  delay(1000);

/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("-1");
 digitalWrite(LEDPin, HIGH); 
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 digitalWrite(LEDPin, LOW); 
 
 }
 
 //Delay 50ms before next reading.
 delay(50);
}

/* My bad, I didn’t have the full code pasted. I got the majority of this from a project online that was already done on an arduino. Do you see anything wrong with my webhook?

// This #include statement was automatically added by the Particle IDE.
#include "ThingSpeak/ThingSpeak.h"
/*
 HC-SR04 Ping distance sensor:
 VCC to arduino 5v 
 GND to arduino GND
 Echo to Photon pin 7 
 Trig to Photon pin 2
 
 This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
 Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
 And modified further by ScottC here: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html
 on 10 Nov 2012.
 */
 /* Thingspeak */
# include "application.h"
# include "ThingSpeak/ThingSpeak.h"

TCPClient client;

unsigned long myChannelNumber = 175756;
const char * myWriteAPIKey = "GWDW0IJU8YB5RTG3";

#define echoPin D7 // Echo Pin
#define trigPin D2 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop()
 {
  // Get some data
  String data = String(10);
  // Trigger the integration
  Particle.publish("Rangefinder", data, PRIVATE);
  // Wait 60 seconds
  delay(1000);

/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("-1");
 digitalWrite(LEDPin, HIGH); 
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 digitalWrite(LEDPin, LOW); 
 
 }
 
 //Delay 50ms before next reading.
 delay(50);
}

It would help if you’d actually mention the error. We can’t just guess what is/isn’t working. Well, we could, but that’s not all that effective, unfortunately.

It wont plot any of the data on thingspeak. It compiles fine. It flashes fine.

Are you seeing any data on ThingsSpeak? What do you expect to see? The only thing you’re publishing is the string “10”.

String data = String(10);
Particle.publish("Rangefinder", data, PRIVATE);

I wanted it to plot whenever there is a change in distance etc. When I ran the code in arduino and opened the serial monitor it showed a constant stream of numbers (If there was no change in distance the number would repeat, if you moved an object closer or farther away it would grow larger or smaller). I wanted it to do the same thing with the Photon from the particle IDE, but plot the info onto my thingspeak channel. Thanks for your guys’ help by the way.

I understand what you want, but you need to be specific about what your current code produces. So, are you seeing a stream of numbers on your serial monitor? Do you see ThingsSpeak plot the value “10” (since that’s all you are sending to them)? You need to send the distance data to ThingsSpeak in your Particle.publish, not just “10”.

if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("-1");
 digitalWrite(LEDPin, HIGH); 
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 Particle.publish("rangefinder", String(distance), PRIVATE);
 digitalWrite(LEDPin, LOW); 
 
 }

I don’t even know how to view my serial monitor on particle. I’m super new to this, so my apologies if I am asking dumb questions. I’ve only been doing this for 5 days, so I’m still learning what the language is and phrases etc. I appreciate you pointing out the number “10”. It makes sense to tell it to relay the distance. Thanks Brother

There are various ways to see the serial monitor. What kind of computer are you using, Mac or PC? On a Mac you can just use Terminal and Screen, not sure how to do it on a PC since I haven’t used one in a couple of decades.

Operating on a PC currently. I went to my particle “console” and clicked on logs. It is adding info every 4 seconds, but the data field says zero. I would have expected the data to show a distance in cm or something. Thanks for your help, Ric.

I see the correct data in the Particle console when I try your code, with the modification that I posted above (I also removed the Particle.publish that you have at the top of loop). The fact that you’re only seeing data every 4 seconds (instead of 1) indicates the the SR04 is timing out. Are you powering it from Vin?

You can use the serial monitor from the Arduino IDE for that, assuming you’ve already got that installed.