my arduino project electrical monitoring system using: arduino 1 (plus ethernet shield), voltage , ampere sensors (0-5v), , external server in digitalocean. sketch collect , posts information real time database (influxdb) , representing collected data using grafana. these projects combination, easy use , produce nce result.
mi first test has been collecting voltage inside house, , working charm. time expand adquisition of new variable (amp), , including computation of instant power, , posting information server. @ end have included full arduino sketch.
however 3 variables, having big issues trying build json object has http posted real time database server. unfotunately not expert in processing/c++, find pyhon more string friendly, , lost.
in code trying build json object according influxdb specifications, tried following code 2 variables (vac , iac):
this working, can deal 2 realtime variables. in example above valuevolt, valueamp , valuekw float elements. result in serial console more or less following:
however adding 3 variables (voltage, intensity , power), not working, when print object, string data empty, has not been built.
no results in serial console. why can expand json object include 3 varaibles?, hint?. know there ways concatenate strings, seen in different examples in ide, in way find more direct , clear make later changes.
thank in advanced!
full code, based in thingspeak.com sketch :
mi first test has been collecting voltage inside house, , working charm. time expand adquisition of new variable (amp), , including computation of instant power, , posting information server. @ end have included full arduino sketch.
however 3 variables, having big issues trying build json object has http posted real time database server. unfotunately not expert in processing/c++, find pyhon more string friendly, , lost.
in code trying build json object according influxdb specifications, tried following code 2 variables (vac , iac):
code: [select]
string data;
data = "[{"
"\"name\":\"voltamppot\","
"\"columns\":[\"vacrms\""
",\"iacrms\""
// ",\"pac\""
"],"
"\"points\":[[" + string(valuevolt) + ","
//+ string(valueamp) + ","
//+ string(valuekw) + ","
"]]"
"}]";
serial.println(data);
this working, can deal 2 realtime variables. in example above valuevolt, valueamp , valuekw float elements. result in serial console more or less following:
code: [select]
[{"name":"voltamppot","columns":["vacrms","iacrms"],"points":[[168.91,]]}]
[{"name":"voltamppot","columns":["vacrms","iacrms"],"points":[[145.60,]]}]
[{"name":"voltamppot","columns":["vacrms","iacrms"],"points":[[139.88,]]}]
[{"name":"voltamppot","columns":["vacrms","iacrms"],"points":[[135.92,]]}]
etc
however adding 3 variables (voltage, intensity , power), not working, when print object, string data empty, has not been built.
code: [select]
string data;
data = "[{"
"\"name\":\"voltamppot\","
"\"columns\":[\"vacrms\""
",\"iacrms\""
",\"pac\""
"],"
"\"points\":[[" + string(valuevolt) + ","
+ string(valueamp) + ","
+ string(sensoramp) + ","
"]]"
"}]";
serial.println(data);
no results in serial console. why can expand json object include 3 varaibles?, hint?. know there ways concatenate strings, seen in different examples in ide, in way find more direct , clear make later changes.
thank in advanced!
full code, based in thingspeak.com sketch :
code: [select]
// librerias ethernet + http
#include <ethernet.h>
#include <spi.h>
// definicion variables globales
unsigned long tiempo_updatepost = 1*1000; // 1 sg para http post
unsigned long tiempo_updateled = 500; // 0.5 sg para led
unsigned long previousmillis = 0;
unsigned long currentmillis = 0;
int voltpin = a0;
int sensorvolt = 0; // voltaje integer (0-1023)
float valuevolt = 0.0; // voltaje vacrms (0-450v)
int amppin = a1;
int sensoramp = 0 ; // amperaje integer (0-1023)
float valueamp = 0.0; // amperios (0-25a)
float valuekw = 0.0; // kw = volt (450v) x amp (25a) = 11.250 kw
byte mac[] = { 0x90, 0xa2, 0xda, 0x0d, 0xfe, 0xed };
ethernetclient client;
string server[] = "xxxxxx";
long int lastconnectiontime = 0;
boolean lastconnected = false;
int failedcounter = 0;
void setup() {
// 1. inicializacin de pins digitales
//pinmode(ledpin, output);
// 2. inicializacin puerto serie
serial.begin(9600);
serial.println("--------------------- setup -------------------");
serial.println("");
serial.println("--- 1. setup: inicializacion serial completada ---");
serial.println("");
// 3. iniciacion ethernet stack
startethernet();
serial.println("------------------ fin setup ------------------");
serial.println("");
}
void loop () {
// 1. lectura red ethernet
if (client.available())
{
char c=client.read();
serial.print(c);
}
if (!client.connected() && lastconnected) {
serial.println("...desconectado de red ethernet");
client.stop();
}
// 2. calculo de variables / datos postear
sensorvolt = analogread(voltpin);
valuevolt = (float(sensorvolt) / 1023.0 ) * 450.0 ; // rango sensor 0-450v
sensoramp = analogread(amppin);
valueamp = (float(sensoramp) / 1023.0 ) * 25.0 ; // rango sensor 0-50a
valuekw = valueamp * valuevolt;
string data;
data = "[{"
"\"name\":\"voltamppot\","
"\"columns\":[\"vacrms\""
",\"iacrms\""
// ",\"pac\""
"],"
"\"points\":[[" + string(valuevolt) + ","
//+ string(valueamp) + ","
//+ string(sensoramp) + ","
"]]"
"}]";
serial.println(data);
// 3. http post :
if (!client.connected() && ((millis() - lastconnectiontime) > tiempo_updatepost))
{
updatehttppost(data); // funcion postea datos servidor
}
// resetear la conexion internet si ha habido 3 fallos de conexion
if (failedcounter > 3) {
failedcounter = 0;
startethernet();
}
lastconnected = client.connected();
}
/////////////////////////////
// funcion:startethernet() //
/////////////////////////////
void startethernet()
{
client.stop();
serial.println("--- 2. setup: startethernet(): conectando red ethernet ...");
delay(1000);
if (ethernet.begin(mac) == 0) {
serial.println("--- 2. setup: startethernet(): fallo configuracion ethernet ---");
serial.println("");
}
else{
serial.print("--- 2. setup: startethernet(): conexion dhcp, ip: ");
serial.print(ethernet.localip());
serial.println(" ---");
serial.println();
}
}
/////////////////////////////
// funcion: httppost() //
/////////////////////////////
void updatehttppost(string postdata)
{
if (client.connect("xxxxxxx", 8086)) // conexion servidor
{
serial.println("--------------start post-------------------------");
// cabeceras (http post headers)
client.print("post /db/test_voltios/series?u=root&p=root http/1.1\n");
serial.print("post /db/test_vakw/series?u=root&p=root http/1.1\n");
client.println("host: xxxxxx");
serial.println("host: xxxxx");
client.print("user-agent: arduino/1.0\n");
serial.print("user-agent: arduino/1.0\n");
client.print("connection: close\n");
serial.print("connection: close\n");
client.print("content-type: application/x-www-form-urlencoded\n");
serial.print("content-type: application/x-www-form-urlencoded\n");
client.print("content-length: ");
serial.print("content-length: ");
client.print(postdata.length());
serial.print(postdata.length());
client.print("\n\n"); // se necesitan dos line breaker para cerrar header
serial.print("\n\n");
// cuerpo del mensaje (http post body)
client.print(postdata);
serial.print(postdata);
lastconnectiontime = millis();
// chequeo sigo conectado -> post con exito
if (client.connected())
{
serial.println();
serial.println("----------------fin post-------------------------");
serial.println();
serial.println();
failedcounter=0;
} else {
failedcounter++;
serial.println("conexion con servidor perdida");
serial.print("conexion perdida nº ");
serial.println(string(failedcounter));
serial.println();
}
}
else // no sido capaz de conectar con el servidor
{
failedcounter++;
serial.println("no puedo conectarme servidor");
serial.print("conexion perdida num. ");
serial.println(string(failedcounter));
serial.println();
lastconnectiontime = millis();
}
}
how memory have available? i'm guessing string usage killing memory. there no reason use strings. trying can done using strings, no dynamic memory allocation fragmenting memory.
http://playground.arduino.cc/code/availablememory
http://playground.arduino.cc/code/availablememory
Arduino Forum > Using Arduino > Programming Questions > String Manipulation giving unexpected results
arduino
Comments
Post a Comment