How to send a data from PHP Script to node-red dashboard

Hi everyone,

So i've been working on a project about IoT, and currently im using arduino for my project, especially ESP32-Cam for monitoring plants. And since im new using Node-Red is there are a way to send a data from PHP Script. I installed my ESP32-Cam with opencv to detect color value, for reference im using ESP32-CAM Web Server with OpenCV.js: Color Detection and Tracking | Random Nerd Tutorials for the color detection. In the code

let BP = rgbaPlanes.get(2);  // SELECTED COLOR PLANE
let GP = rgbaPlanes.get(1);
let RP = rgbaPlanes.get(0);

the codes above are for the data of color value, and i want to send it to node-red dashboard.
If someone who has done a similar project, would you kindly share your knowledge with me and the community.
Thankyou for your time to reply my question.

Use MQTT to send the data from the arduino to node-red. There is an JSON library you can use. https://arduinojson.org and you should use the Arduino Client for MQTT

Why are you usng a PHP script?

If you really need to keep using PHP, you have a number of options.

The traditional route is to use PHP to provide a REST API that you can query from Node-RED and so pass the data to your Dashboard.

However, you could use MQTT with PHP which would be a good solution. Or you could push data to Node-RED using an HTTP node or even use websockets.

Oh im sorry, i forgot that im also using MQTT to send the data

the code above are inside of PHP script sir, the main process are on arduino, but the webcam footage are shown on custom website

So when using MQTT do i have to send it to the dashboard using the publish.client or something else?

No, you just add an MQTT -in node to your flow and send the output to your Dashboard.

Yes, i understand about that sir, what i meant is, do i have to just put the client.publish code inside the script so that the camera can send the color value to node-red, or do i have to use other method

If it's any help - here's a function I wrote a year or so ago to publish data & reports from a Wemos D1 Mini (being used as a controller for a weather station) via MQTT to a MQTT-In node in Node-RED.

Some of the include statements needed at the top of the Arduino code.

#include <PubSubClient.h>
#include <ArduinoJson.h>
void publish_situation(String message)
{
    StaticJsonDocument<256> doc;

    doc["nodeID"] = thisNodeID;
    doc["report"] = "situation";
    doc["status"] = message;
    doc["ssid"] = WiFi.SSID();
    doc["rssi"] = WiFi.RSSI();

    char buffer[256];
    size_t n = serializeJson(doc, buffer);

    char topic[30];
    strcpy(topic, mqtt_situation_topic);
    strcat(topic, "/");
    strcat(topic, thisNodeID);

    mqtt_client.publish(topic, buffer, n);
}
//------------ End of publish_mqtt()

This is how I call it in 'main'.

           publish_situation("Sending report");

And this is in an 'include' file to define the topic.

const char *mqtt_situation_topic = "homeDevice/situation";

I'm using the brilliant library... bblanchon/ArduinoJson@^6.16.1
Make sure you install version-6 as it has loads of improvements over version-5.

I have a number of nodes that send data to Node-RED. Using the above code the MQTT publish topic is...

 homeDevice/situation/<thisNodeID>  e.g. homeDevice/situation/node41
1 Like

Can i upload the code to my esp32-cam with opencv installed?

No. The ESP32-cam is a microcontroller not a micro computer. That means it only runs one program but it runs t fast.

You would have to hack the OpenCV code and add in the MQTT code to do this.

actually sir, its like color tracking but the website that i referred called it " ESP32-CAM Web Server with OpenCV.js: Color Detection and Tracking" so the opencv is located inside the website. once the camera is turned on the website also turn on

While there is a small web server which runs in the browser, that code starts off stored on the esp32-cam and is sent to the browser when the esp32-cam is powered on. The browser code has a script that loads the opencv.js code from https://docs.opencv.org/master/opencv.js.

If you read the web page you refer to, you will find an explainaton of what part of the code is sent to the browser.

But this still means that if you want to add the MQTT/JSON code, that code needs to exist on the esp32-cam so you will have to change (hack) the code that is run on the esp32-cam to add that code in.

Ok sir, so this means i need to search on how to send JSON from Arduino to Node-Red via MQTT

Did you try the example that @dynamicdave shared ?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.