How to Post Electric Imp Values to mysql database using php?

Are there any examples for that?. for example passing the temp value to mySQL database without any button press? thanks

I managed to get it to work but not sure why my led not turning ON/OFF after reaching certain temperature. . . any idea why? what i need to change?. I’m using ubidots too so not sure what’s causing it not to going Led ON/OFF . my Original Agent code turns the LED ON/OFF after hitting certain temperature but the Modified Agent causing Led not to turn ON/OFF .

Original Agent

device.on("saveValue", function(lux) { server.log("Trying to post to Ubi the value:"); server.log(lux); local headers = { "Content-Type": "application/json", "X-Auth-Token": "drzba5hKz5esp25Aofm1ho0Ay8rhHEFYLMtW0TKoIcFmJXtJo6I5elwTItwO" }; // Replace the token with yours local url = ""; // Replace the Variable ID with yours local string = {"value": lux}; local request =, headers, http.jsonencode(string)); local response = request.sendsync(); });

modified Agent

device.on("saveValue", function(lux) { server.log("Trying to post to Ubi the value:"); server.log(lux); local headers = { "Content-Type": "application/json", "X-Auth-Token": "drzba5hKz5esp25Aofm1ho0Ay8rhHEFYLMtW0TKoIcFmJXtJo6I5elwTItwO" }; // Replace the token with yours local url = ""; // Replace the Variable ID with yours local myUrl = format ("", lux); local string = {"value": lux}; local request =, headers, http.jsonencode(string)); server.log(myUrl); local request =, headers, http.jsonencode(string)); local response = request.sendsync(); });


`//ADC-to-ubidots Example
function mainLoop() {
imp.wakeup(1.0, mainLoop);
local lux =;
agent.send(“saveValue”, lux);
} mainLoop();

// Create a global variabled called ‘led’ and assign the ‘pin9’ object to it
// The <- is Squirrel’s way of creating a global variable and assigning its initial value
ledGreen <- hardware.pin7;
ledRed <- hardware.pin5;

// Configure ‘led’ to be a digital output with a starting value of digital 0 (low, 0V)
ledGreen.configure(DIGITAL_OUT, 0);
ledRed.configure(DIGITAL_OUT, 0);

// Function called to turn the LED on or off
// according to the value passed in (1 = on, 0 = off)
function setLedStateGreen(stateGreen) {
server.log("Set LED to state: " + stateGreen);

function setLedStateRed(stateRed) {
server.log("Set LED to state: " + stateRed);

// Register a handler for incoming “set.led” messages from the agent
agent.on(“ch1”, setLedStateGreen);
agent.on(“ch2”, setLedStateRed);


What or who is responsible for controlling the LED’s?
Is it done all within the imp, or do you send the temp to ubidots and then ubidots is supposed to turn them on or off? I guess what I’m asking is all of the temp processing and decision-making done by ubidots? Do you have ‘setpoints’ defined in ubidots?

Because I’m not so familiar with ubidots, does ubidots have the ability to POST or GET to a remote URL, in this case your agent?

You have the agent sending the temp to ubidots … can ubidots send things to the agent?

hello, I am using ubidots and mySQL database. so basically i’m passing the values to ubidots so when it hit certain temp the led turn ON/OFF but after using the modified agent code the Ubidots not turning ON/OFF the led at all but the values are storing in mySQL database. Ubidots have GET/POST to remote url … i’m using GET method to call the agent url

maybe I’m missing something on the device side?

Once every 1 second, your device sends the temp to the agent, and the agent sends the data to Ubidots. That part works.

But in your agent, you also have to “listen” for incoming data from Ubidots. Then, upon receiving data, sent it to the device.

So you’re missing this part in your agent:

`// This section handles incoming data from external website
http.onrequest(function(req, resp){
if (req.method == "GET"){
local body = http.urldecode(req.body);
server.log("Incoming Data: " +;
// Now send the data to the imp device
resp.send(200, "OK");

I’m not sure what Ubidots actually sends to the agent. It has to send something about the LED? Ubidots can send one word such as “red” or “green”?

The agent will send whatever data is received from Ubidots to the device.

In the device, you have to determine what the word is “red” or “green” and light the appropriate LED.

`// This section within device receives data from the agent
agent.on("data", function(value) {
// value will be whatever the "data" is ... red, green ?

In summary, there are a few functions that need to be addressed …

device.on() … receive data from device and send it out to a remote URL
http.onrequest( ){ … listen for incoming data from remote URL
device.send( ); … send data to device

agent.on( ) … receive data from agent.
agent.send( ) … send data to agent

there’s any way to get the word ON/OFF to store into mySQL when the led is on or off?

If you have a website with MySQL and using PHP, why do you use Ubidots? I’m not quite getting that.

I’m using Ubidots to turn leds on/off. . .it’s much easier to use Ubidots for leds really.

So you’re using Ubidots. And you use a website with MySQL to store data. How does the temp go to both Ubidots and your website? Sorry for all the questions but I can’t put my head around it (using both Ubidots and a website with MySQL/PHP).

I’m using pretty similar steps for MySQL I used for my ubidots and it works . the only difference is the agent to ubidots is direct but for the data to store into database i have to pass the agent value to php and then pass the value from php into mysql .

Agent to PHP, and PHP to MySQL is correct. And why can’t PHP then determine if the LED should be red or green. PHP can command the agent to turn on or off the LED and also write that into the database. Don’t even use ubidots. For fancy graphs, write a PHP script that can use Google charts or JQuery for a dashboard or gui report. PHP could also command the agent to turn a heater on or off to control the temp. What does ubidots do for you that your own website cannot do?

Also, have an amber LED that the PHP script can flash each time it receives a temp from the agent. By watching it at the imp location, you have an indication that the PHP script is successfully communicating without needing to access the website or electric imp IDE.