Trouble Connecting Particle Photon to thingspeak!

Hi,

I set up a PIR sensor (HC-SR505) and made a thingspeak account, but I can’t figure out how to relay the info over to thingspeak. Please help ASAP. I have a deadline tonight and am new to coding. Thank you so much in advance!!

/*Attached code that is in use on particle IDE. The code compiles fine, but doesn't input any info into thingspeak?

/* Thingspeak */
TCPClient client;
unsigned long myChannelNumber =171680;
const char * myWriteAPIKey = "3QO49T7DZO7WR5ET";
int led = D1;
const int pirPin = D7; // This is the D7 pin that we are going to use
unsigned long motionDelay = 5000; // Motion Delay Timer
unsigned long motionTimer; // Motion Trigger Timer

boolean inMotion = false; // Motion sensor need to be read or not flag

void setup() {
    
// put your setup code here, to run once:

pinMode(pirPin, INPUT); // Prepare the pin as Input pin
pinMode(led, OUTPUT);  // Prepare Led as an OUTPUT

Serial.begin(19200); // Prepare the Serial Monitor

}

void loop() {

// put your main code here, to run repeatedly:


if (digitalRead(pirPin) == HIGH && !inMotion)

{
    if(digitalRead(pirPin) == HIGH)
     digitalWrite (led, HIGH);
     
     if(digitalRead(pirPin) == LOW)
     digitalWrite (led, LOW);

Serial.println("Motion Detected");

motionTimer = millis();

inMotion = true;

} else if (millis() - motionTimer >= motionDelay) {

inMotion = false;

}

}
```
1 Like

I don’t see how you would expect this code to post to thingspeak, as there are clearly parts missing. Did you copy this from an example somewhere?

Yes, I did brother. I am brand new to coding and am still learning. Any subject matter expert that can give advice would be helpful. What all do I need to add to it?

Thank you for your response.

This looks very helpful https://github.com/mathworks/thingspeak-particle

1 Like

I appreciate the fast response brother.

1 Like

Glad I could help.

Are you gonna be on here for a little while in case this isn’t broken down barney style enough for me?

1 Like

So, my main question is, what would I add to the code to make it transfer the data to thingspeak?

1 Like

I’ll hang around, but I trust you will be able to solve this.

I would actually recommend using one of the examples on https://github.com/mathworks/thingspeak-particle and just adding your stuff to it, removing certain parts of the example.

Edit:
This one looks easy to adapt https://github.com/mathworks/thingspeak-particle/blob/master/firmware/examples/WriteVoltage.ino

1 Like

This is what I have tweaked. –

/* Thingspeak */
#include "application.h"
#include "ThingSpeak/ThingSpeak.h" 

TCPClient client; 

unsigned long myChannelNumber =171680;
const char * myWriteAPIKey = "3QO49T7DZO7WR5ET";
int led = D1;
const int pirPin = D7; // This is the D7 pin that we are going to use
unsigned long motionDelay = 5000; // Motion Delay Timer
unsigned long motionTimer; // Motion Trigger Timer

boolean inMotion = false; // Motion sensor need to be read or not flag

void setup() {
    
// put your setup code here, to run once:
 {
     ThingSpeak.begin(client); 
} 


pinMode(pirPin, INPUT); // Prepare the pin as Input pin
pinMode(led, OUTPUT);  // Prepare Led as an OUTPUT

Serial.begin(19200); // Prepare the Serial Monitor

}

void loop() {

// put your main code here, to run repeatedly:


if (digitalRead(pirPin) == HIGH && !inMotion)

{
    if(digitalRead(pirPin) == HIGH)
     digitalWrite (led, HIGH);
     
     if(digitalRead(pirPin) == LOW)
     digitalWrite (led, LOW);

Serial.println("Motion Detected");

motionTimer = millis();

inMotion = true;

} else if (millis() - motionTimer >= motionDelay) {

inMotion = false;

float voltage = sensorValue * (3.3 / 4095.0); 
  // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different 
  // pieces of information in a channel.  Here, we write to field 1. 
 ThingSpeak.writeField(myChannelNumber, 1, voltage, myWriteAPIKey); 
 delay(20000);  

}

}
// error codes as follows "
photon_pir.cpp:3:36: fatal error: ThingSpeak/ThingSpeak.h: No such file or directory
 
                                    ^

compilation terminated.
make[1]: *** [../build/target/user/platform-6photon_pir.o] Error 1
make: *** [user] Error 2

Error: Could not compile. Please review your code. " 

Thanks bro.

You need to add the Thingspeak library.

In the Particle Web IDE, click the libraries tab, find ThingSpeak, and choose “Include in App”

Also you probably want to change this stuff:

float voltage = sensorValue * (3.3 / 4095.0); 
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different 
// pieces of information in a channel. Here, we write to field 1. 
ThingSpeak.writeField(myChannelNumber, 1, voltage, myWriteAPIKey); 
delay(20000);

There are also a bunch of Curly brackets that need to be fixed.

Protip:
You can make your code look much nicer on the forums by placing:

```cpp



```

around your code.

Also, what are you sending to thingspeak? The inMotion variable?

2 Likes

Roger. I included in app.

I just want thingspeak to display when motion is detected

(Roger was meaning understood) I’m a vet, my bad lol

This should work:


/* Thingspeak */
# include "application.h"
# include "ThingSpeak/ThingSpeak.h"

TCPClient client;

unsigned long myChannelNumber = 171680;
const char * myWriteAPIKey = "3QO49T7DZO7WR5ET";
int led = D1;
const int pirPin = D7; // This is the D7 pin that we are going to use
unsigned long motionDelay = 5000; // Motion Delay Timer 
unsigned long motionTimer; // Motion Trigger Timer

void setup()
{
ThingSpeak.begin(client);  
pinMode(pirPin, INPUT); // Prepare the pin as Input pin
pinMode(led, OUTPUT); // Prepare Led as an OUTPUT
Serial.begin(9600); // Prepare the Serial Monitor
}

void loop()
{
// put your main code here, to run repeatedly

if (digitalRead(pirPin) == HIGH && millis() - motionTimer >= motionDelay)
{
if (digitalRead(pirPin) == HIGH)
{ 
digitalWrite (led, HIGH);
}

if(digitalRead(pirPin) == LOW)
{
digitalWrite (led, LOW);
}

Serial.println("Motion Detected");

motionTimer = millis();

ThingSpeak.writeField(myChannelNumber, 1, 1, myWriteAPIKey); 

}
}

A 1 is sent to thingspeak whenever there is motion.

Update:
I fixed this

Thanks so much brother. The only error that is popping up is this: "photon_pir.cpp:3:36: fatal error: ThingSpeak/ThingSpeak.h: No such file or directory
void setup();
^

compilation terminated.
make[1]: *** […/build/target/user/platform-6photon_pir.o] Error 1
make: *** [user] Error 2"

I’m assuming that means that I need to either:

a) download a file?

or

b) input something into the code to recognize that statement

Are you working from the Web IDE? (build.particle.io)

Yes, sir.

And you added the library?

I went to libraries and typed thingspeak in the search bar. Then I clicked on thingspeak. and It popped up with this
" Files
CheerLights.ino
ReadLastTemperature.ino
ReadPrivateChannel.ino
ReadWeatherStation.ino
ThingSpeak.cpp
ThingSpeak.h
WriteMultipleVoltages.ino
WriteVoltage.ino "

But, if I click on one of those to upload it puts a new code into the IDE.

My bad brother. I really appreciate the help and know that I am a noob. This stuff is like Chinese to me

1 Like