I have finally succeeded !!!
This stage of the project consists of taking 3 temperatures from websites and sending them through modbus.
As the first stage I only worked sending the temperatures
Local request = http.get (BOMBEO13_temp)
…
I will work later to add other information such as humidity, rain, wind, but that information comes in a single string data.
Local request = http.get (PUMP_13_data)
…
(I already have it in the agent)
I have added code to turn on the red led when there is some error in the modbus functions, and turn on green led when the modbus reading is done correctly.
Thanks everyone for the brainstorming!
I share the code, I hope it helps someone !!!
AGENT
local BOMBEO13_temp = "https://api........";
local BOMBEO13_datos = "https://api........";
local T1500Fca_temp = "https://api........";
local T1500Fca_datos = "https://api........";
local T1545G_temp = "https://api........";
local T1545G_datos = "https://api........";
local todevice = {};
function run(){
/*-------------BOMBEO13 ----------------------*/
local request = http.get(BOMBEO13_temp)
function handleResponse(responseTable) {
// Called when the imp receives a response from the remote service
if (responseTable.statuscode == 200) {
//server.log("BOMBEO13 Temp" +" " +responseTable.body);
todevice.B13 <- responseTable.body.tointeger();
//server.log(todevice.B13);
device.send("BOMBEO13", todevice.B13);
} else {
// Log an error
server.log("Error BOMBEO13: " + responseTable.statuscode);
}
}
request.sendasync(handleResponse);
local request = http.get(BOMBEO13_datos)
function handleResponse(responseTable) {
// Called when the imp receives a response from the remote service
if (responseTable.statuscode == 200) {
//server.log("BOMBEO13 Datos" +" " + responseTable.body);
//device.send("BOMBEO13", responseTable.body);
} else {
// Log an error
server.log("Error BOMBEO13: " + responseTable.statuscode);
}
}
request.sendasync(handleResponse);
/*-------------T1500Fca ----------------------*/
local request = http.get(T1500Fca_temp)
function handleResponse(responseTable) {
// Called when the imp receives a response from the remote service
if (responseTable.statuscode == 200) {
// server.log("T1500Fca Temp" +" " + responseTable.body);
todevice.FCA <- responseTable.body.tointeger();
device.send("FCA", todevice.FCA);
} else {
// Log an error
server.log("Error T1500Fca: " + responseTable.statuscode);
}
}
request.sendasync(handleResponse);
local request = http.get(T1500Fca_datos)
function handleResponse(responseTable) {
// Called when the imp receives a response from the remote service
if (responseTable.statuscode == 200) {
// server.log("T1500Fca Datos" +" " + responseTable.body);
// device.send("FCA", responseTable.body);
} else {
// Log an error
server.log("Error response: " + responseTable.statuscode);
}
}
request.sendasync(handleResponse);
/*-------------T1545G ----------------------*/
local request = http.get(T1545G_temp)
function handleResponse(responseTable) {
// Called when the imp receives a response from the remote service
if (responseTable.statuscode == 200) {
//server.log("T1545G Temp" +" " + responseTable.body);
todevice.T1545G <- responseTable.body.tointeger();
device.send("5G", todevice.T1545G);
} else {
// Log an error
server.log("Error T1545G: " + responseTable.statuscode);
}
}
request.sendasync(handleResponse);
local request = http.get(T1545G_datos)
function handleResponse(responseTable) {
// Called when the imp receives a response from the remote service
if (responseTable.statuscode == 200) {
// server.log("T1545G Datos" +" " + responseTable.body);
// device.send("5G", responseTable.body)
} else {
// Log an error
server.log("Error response: " + responseTable.statuscode);
}
}
request.sendasync(handleResponse);
imp.wakeup(240, run)
}
run();
DEVICE
#require "CRC16.class.nut:1.0.0"
#require "ModbusSlave.class.nut:1.0.0"
#require "Modbus485Slave.class.nut:1.0.0"
red <- hardware.pinP;
yellow <- hardware.pinQ;
green <- hardware.pinT;
local BOMBEO13;
local FRANCISCA;
local ULTIMA;
red.configure(DIGITAL_OUT, 1);
yellow.configure(DIGITAL_OUT, 1);
green.configure(DIGITAL_OUT, 1);
red.write(0)
green.write(1);
modbus <- Modbus485Slave(hardware.uart2, hardware.pinL, 1, { debug = false });
modbus.setSlaveID(1);
function B13(res) {
//server.log("BOMBEO13 " + res);
server.log("B13="+res+" type="+typeof res);
BOMBEO13= res;
}
function FCA(res) {
server.log("T1500Fca " + res);
FRANCISCA = res;
}
function T1545G(res) {
server.log("T1545G " + res);
ULTIMA = res;
}
agent.on("BOMBEO13", B13);
agent.on("FCA", FCA);
agent.on("5G", T1545G);
modbus.onRead(function(slaveID, functionCode, startingAddress, quantity) {
red.write(1);
green.write(0);
return [BOMBEO13, FRANCISCA, ULTIMA,]}.bindenv(this));
modbus.onError(function(error){
//server.error(error);
green.write(1);
red.write(0)
}.bindenv(this));
FIN