Tengo alguna función que no me compila. Una es la “dtostrf”. También una librería que ha veces utilizo para provocar “resets”, la “avr/wdt.h”.
Si el mismo programa lo compilo para Arduino Uno, Mega, Ethernet,... la “dtostrf” y “avr/wdt.h”, funcionan correctamente.
¿Cómo puedo solucionar este tema? Hay alguna alternativa para “dtostrf” y para “avr/wdt.h”
Hola @aprentik lee las normas del foro y mira como se postean los códigos en otros hilos y hazlo con el tuyo asi podemos entender mejor tu problema.
Los programas funcionan pero no todos los Arduinos son AVR. Los MKR tienen otra tecnología y requieres otras consideraciones.
dtostrf es genérica a cualquier tipo de arduino pero "avr/wdt.h" no. Hay que ver puntualmente que estas haciendo o usando para provocar los problemas que mencionas pero que no puedo imaginar al no ver el código.
Gracias @Surbyte.
No puedo poner el código porque está impresentable y no tiene sentido nada de lo que hace, es un código de pruebas donde he probado algunas cosillas que me interesa hacer funcionar. Este Arduino es muy interesante, es muy potente.
Ordenaré este código y lo publicaré, identificando estos problemas que he mencionado.
Gracias.
Aquí el codigo. En el codigo que paso está documentado lo que hace referencia a la libreria avr/wdt.h También la funcion dtostrf.
Tal como está, si se compila.
Es un programa que no hace nada, solo conectarse a la wifi. La funció "RegistreMySQL" se tiene que implementar.
//Arduino MKR1000 WIFI
//Problemas al compilar avr/wdt.h
//Problemas con la funcion dtostrf
#include <WiFi101.h>
#include <SPI.h>
//#include <avr/wdt.h> // Control de prenjades.
WiFiClient client;
char server[] = "www.google.com";
int status = WL_IDLE_STATUS;
char ssid[] = "APTest"; // your network SSID (name)
char pass[] = "Test2020"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
bool val = true;
int idSonda = 1011;
int CO2 = 0 ;
void setup()
{
Serial.begin(9600); // initialize serial communication
Serial.println(" ");
Serial.print("Start Serial ");
// Check for the presence of the shield
Serial.print("WiFi101 shield: ");
if (WiFi.status() == WL_NO_SHIELD)
{
Serial.println("NOT PRESENT");
return; // don't continue
}
Serial.println("DETECTED");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED)
{
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
printWifiStatus(); // you're connected now, so print out the status
}
void loop()
{
if(!client.available())
{
Serial.println("Client no disponible...");
}
while (client.available())
{
char c = client.read();
Serial.write(c);
Serial.println(c);
}
// if the server's disconnected, stop the client:
if (!client.connected())
{
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
}
RegistreMySQL(idSonda, "CO2", CO2);
delay(6000);
}
void RegistreMySQL(int idSondaSQL, char IdTipusSondaSQL[], float ValorSQL)
{
String response;
char idSondaInsertat[4];
char ValorInsertar[3];
//wdt_enable(WDTO_8S); //8segons es el màxim !!!
//wdt_reset();
//wdt_disable();
//dtostrf( idSondaSQL, 1, 0, idSondaInsertat); // zero decimals
//dtostrf( ValorSQL, 1, 2, ValorInsertar); // dos decimals
char INSERT_POST[] = "/ausacontrolapi/api/config"; // <-- *** Línea modificada ***
char INSERT_BODY[] = "nomUsuari=User&passwordUsuari=Pword&idSonda=%s&idTipusSonda=%s&valor=%s"; // <-- *** Línea afegida ***
char query[128];
Serial.println("Fi RegistreMySQL");
}
// Funció temps d'espera entre captura de dades, en minuts-------------------
void Esperem (int minuts)
{
Serial.print(" -> Esperant (minuts) :");
Serial.print( minuts);
Serial.print(" ");
for (int a=0; a<minuts; a++)
{
delay(60000); //60000 es un minut
Serial.print(".");
}
Serial.println (" ");
}
// Fi funció temps d'espera--------------------------------------------------
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
Serial.println("-----------------------------------------------------------");
Entiendo que estás haciendo pruebas pero atento que el buffer de salida de dtostrf() (tomo el caso de ValorInsertar) se queda "corto".
dtostrf(ValorSQL, 1, 2, ValorInsertar);
Tienes (al menos) 1 caracter para el entero, 2 caracteres para los decimales, un caracter '.' (punto decimal) más un caracter '\0' como finalización de la cadena. Entonces como mínimo tienes 5 caracteres y tu defines el buffer con una longitud de 3
char ValorInsertar[3];
Eso si no provoca un error en tiempo de compilación lo va a provocar en tiempo de ejecución con resultados imprevisibles.
Te aconsejo que definas el buffer como mínimo con 8 caracteres (lo que da lugar suficiente para 6 dígitos mas el '.' y '\0'), o sea
Gracias @anon90500195.
Si tienes razon, Char ValorInsertat[8] mejor. El problema principal es que "dtostrf" no se compila para la Arduino MKRWIFI 1000.
//Arduino MKR1000 WIFI
//Problemas al compilar avr/wdt.h
//Problemas con la funcion dtostrf
#include <WiFi101.h>
#include <SPI.h>
//#include <avr/wdt.h> // Control de prenjades.
WiFiClient client;
char server[] = "www.google.com";
int status = WL_IDLE_STATUS;
char ssid[] = "APTest"; // your network SSID (name)
char pass[] = "Test2020"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
bool val = true;
int idSonda = 1011;
int CO2 = 0 ;
void setup()
{
Serial.begin(9600); // initialize serial communication
Serial.println(" ");
Serial.print("Start Serial ");
// Check for the presence of the shield
Serial.print("WiFi101 shield: ");
if (WiFi.status() == WL_NO_SHIELD)
{
Serial.println("NOT PRESENT");
return; // don't continue
}
Serial.println("DETECTED");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED)
{
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
printWifiStatus(); // you're connected now, so print out the status
}
void loop()
{
if(!client.available())
{
Serial.println("Client no disponible...");
}
while (client.available())
{
char c = client.read();
Serial.write(c);
Serial.println(c);
}
// if the server's disconnected, stop the client:
if (!client.connected())
{
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
}
RegistreMySQL(idSonda, "CO2", CO2);
delay(6000);
}
void RegistreMySQL(int idSondaSQL, char IdTipusSondaSQL[], float ValorSQL)
{
String response;
char idSondaInsertat[4];
char ValorInsertar[3];
//wdt_enable(WDTO_8S); //8segons es el màxim !!!
//wdt_reset();
//wdt_disable();
//dtostrf( idSonda, 1, 0, idSondaInsertat); // zero decimals
//dtostrf( ValorSQL, 1, 2, ValorInsertar); // dos decimals
char INSERT_POST[] = "/ausacontrolapi/api/config"; // <-- *** Línea modificada ***
char INSERT_BODY[] = "nomUsuari=user&passwordUsuari=pwww&idSonda=%s&idTipusSonda=%s&valor=%s"; // <-- *** Línea afegida ***
char query[128];
Serial.println("Fi RegistreMySQL");
}
// Funció temps d'espera entre captura de dades, en minuts-------------------
void Esperem (int minuts)
{
Serial.print(" -> Esperant (minuts) :");
Serial.print( minuts);
Serial.print(" ");
for (int a=0; a<minuts; a++)
{
delay(60000); //60000 es un minut
Serial.print(".");
}
Serial.println (" ");
}
// Fi funció temps d'espera--------------------------------------------------
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
Serial.println("-----------------------------------------------------------");
Para un Arduino Uno, Mega,... si se compila.
Sino tiene solución tengo que buscar alguna función equivalente.
Gracias
float var = 13.45f;
void setup() {
Serial.begin(9600);
char buffer[20];
sprintf(buffer, "%f", var);
Serial.println(buffer);
}
void loop() {
// put your main code here, to run repeatedly:
}
No tengo MKR solo compilé.
EDITO: ahora amplié la idea usando tu rutina con datos genéricos y tmb compila.
Hola.
La libreria <avr/dtostrf.h> me ha dejado de funcionar !!!
Da el error: avr/dtostrf.h: No such file or directory.
Ayer funcionava y hoy no !!!!
Mas información. Ayer pare el PC y hoy lo he encendido, no hay mas diferencia que esta !!!
Voy a substituir la funcion dtostrf por la propuesta de @Surbyte y ja no utilizaré esta libreria.
Los AVR tienen poca flash y pero estos MKR no, entonces el soporte de punto flotante esta disponible y puedes trabajar directamente con sprintf y todas la potencia que ésta función tiene.