Wordpress and subscribe

I am currently reading an analog value on my photon and can see that reading in console thanks to Ric and bdub (cheers) i would like to see that value on my website which i have created on wordpress, is this possible and how would i go about doing it. i have tryed following a few tutorials but had no success
here is my publish code

 sprintf(publishstring,"%f",analogcalc);
        Spark.publish("HeaterCurrent",publishstring);

thanks…Ben

To save us from redirecting you to those same tutorials, it'd be neat if you could mention which tutorials you've followed, and what you've tried.

In essence, this isn't even a Particle specific question, but rather "Wordpress server sent events". Or even lower level "running javascript in wordpress".

I tried to follow Ric’s tutorial where he controls a servo via a html page and tried to cut out what I didn’t need. I know virtually zero about java script. I have been playing with electrons for a while now and can get them interacting with each other but whenever I try to get the data to a Webpage I get stuck.

Here's where a link would've been nice. Saves us the trouble of having to guess what exactly it was you've tried (sounds more like BKO's tutorial though).

Does that tutorial work (disregarding the wordpress part)?

The easiest way to do this, in my opinion, is to push your data to Ubidots.com and then create a dashboard with any info you want, and then past that dashboard into your WordPress account using the iFrame embed code they have for the dashboard you create.

There is a Ubidots Library in the Online IDE to get you started.

Without knowing more about exactly how you want things to work, I’m wondering if you might be better off using a webhook instead of Publish.

With publish, you need to have something that’s monitoring your event stream as new data comes in. But with a webhook, you could post the event data to your server as they happen, and have a script that stores the information to be reviewed later. This could be done in a WordPress plugin, for instance. The plugin could be set up to watch for the webhook requests on a certain URL, store the data in the database (probably using the Settings API), and then you could have a dashboard module to display the most recent values.

But if you don’t need to store the data, and just want to be able to view live events as they happen, then you could probably just use Particle’s JavaScript libraries for that.

So, if you can tell us more about how you think you want things to work, that would be helpful.

Thanks for all your helpful replies, at the moment i am going down the ubidots route. I (at the moment just want to see the live data which is being published. I am reading an analog value from a current transformenr which is connected to my heater in my office. I am an electrician however i only have basic coding knowledge so all the wiring is pretty straight forward but i just struggle a bit with the coding, im not to bad at local code controlling inputs and outputs its just when it comes to the data to web bits i struggle.
i have followed the ubidots tutorial however whenn i flash the firmware my photon goes into red led state is there anything
standing out?

// This #include statement was automatically added by the Particle IDE.
#include <MQTT.h>

// This #include statement was automatically added by the Particle IDE.
#include <UbidotsMQTT.h>

// This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"

// This #include statement was automatically added by the Particle IDE.
#include "UbidotsMQTT.h"
#define TOKEN "bens token"  // Add here your Ubidots TOKEN
#define VARIABLE_IDENTIFIER_ONE "humidity" // Add a variable identifier, it must be in lowercase
#define VARIABLE_IDENTIFIER_TWO "temperature" // Add a variable identifier, it must be in lowercase
#define DATA_SOURCE_NAME "my-device"

void callback(char* topic, byte* payload, unsigned int length);

Ubidots client(TOKEN, callback);

void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    String message(p);
    Serial.write(payload, length);
    Serial.println(topic);
}

void setup() {
    Serial.begin(115200);
    while (client.connect());
    client.setDataSourceLabel(DATA_SOURCE_NAME);
}

void loop() {
    float hum = analogRead(A0);
    float temp = analogRead(A1);
    client.add(VARIABLE_IDENTIFIER_ONE, hum);
    client.add(VARIABLE_IDENTIFIER_TWO, temp);
    client.sendValues();
}

cheers Ben

Can you explain more precisely or post a video how this red flashing looks?

Till then, just a few hints that won’t change a lot, but should be kept in mind.

Remove the double includes for libraries.

I’d get rid of String variables and substitute them with char[] - especially in frequently called functions.

Since TOKEN is a rather common term and #definesare not really good with scopes, I'd at least change that to something unique or even better replace your#define`s with

const char myToken[] = "....";

This way you’d get an error and not just a (unseen) warning in case of collisions.

Slow down your loop() a bit to give the MQTT broker some slack :wink:


For formatting posts, these tripple grave accent need to be all alone (with the optional coding scheme) on their lines to do the formatting right
Like this

 ```cpp
 // no blanks or other text before or after
I have corrected that in your post already.

@benjiiiiuk I would recommend not using the MQTT library and instead use the legacy library.

I modified your code for you to test out.

Start over using this code and enter your Token:

// This #include statement was automatically added by the Particle IDE.
#include <Ubidots.h>

// This #include statement was automatically added by the Particle IDE.
#include "Ubidots/Ubidots.h"

#define TOKEN "Bens Token"  // Put here your Ubidots TOKEN
#define DATA_SOURCE_NAME "BensTest"

Ubidots ubidots(TOKEN); // A data source with particle name will be created in your Ubidots account

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

  ubidots.setDatasourceName(DATA_SOURCE_NAME); //This name will automatically show up in Ubidots the first time you post data.
}

void loop() {
    float value1 = analogRead(A0);
    float value2 = analogRead(A1);
    ubidots.add("Humidity", value1);  // Change for your variable name
    ubidots.add("Temp", value2);

    ubidots.sendAll(); // Send fuel gauge data to your Ubidots account.

    delay(10000);  //Delay sending again for 10 seconds.  
}

Then you will need to add the legacy Ubidots library to this sketch by following the screen shots below:

Choose the name of the sketch you just created so we add the library to only the new code sketch:

The code should successfully compile and you should be to flash the code to a Photon or Electron.

Once you send a publish even to Ubidots it should automatically create new sources using the same names in your code called BensTest, and then individual variables called Humidity & Temp.

Hopefully that works, it’s working for me :smiley:

1 Like

That’s great thank you very much. i know have it working with your above explanation. i kind of understand what is going on. i have also manage to incorporate this innto my website. if you use the link given by ubidots does that mean your token is not exsposed? Thanks again

@benjiiiiuk When you share your dashboard using the share code your token is not exposed so nothing to worry about there.

You can also setup email, or SMS alerts triggered by thresholds if you desire which is handy.

Share a screen shot of your dashboard if your willing. Always nice to see how others are using stuff.

Here is a screen shot of my dashboard.

I found Ubidots easy to use, thanks for the advice! it seems like a good way to get your data to the web without exposing the token.

Thanks again for everyone’s help and advice.

2 Likes

Whether someone has an example of how to use Ubidots with webhook?
I know there is an example of only one variable but want to know how it can be used with multiple variables.
https://ubidots.com/docs/devices/particleWebhook.html#particle-webhook
@RWB @ScruffR

@aguspg or @Metavix maybe able to help with the Ubidots + webhook setup.

I’ve never used it that way before but know it’s possible.