Discuss here different C compiler set ups, and compiling executables for the ESP8266

User avatar
By Natan Jr
#58755 Hello guys, I am trying to send data through the GET method via url to a server, I get the data from the sensor and send it to url, however when it is to execute the post code the arduino hangs. I wonder what's going on ??
Image
Code: Select allvoid setup()
{
  int status;
  Serial.begin(9600);
 
  pinMode(2, INPUT);
  attachInterrupt(0, incpulso, RISING);
  // To turn the MG2639 shield on, and verify communication
  // always begin a sketch by calling cell.begin().
  status = esp8266.begin();
  if (status <= 0)
  {
    Serial.println(F("Unable to communicate with shield. Looping"));
    while(1) ;
  }
 
  esp8266.setMode(ESP8266_MODE_STA); // Set WiFi mode to station
  if (esp8266.status() <= 0) // If we're not already connected
  {
    if (esp8266.connect(mySSID, myPSK) < 0)
    {
      Serial.println(F("Error connecting"));
      while (1) ;
    }   
  }
 
  // Get our assigned IP address and print it:
  Serial.print(F("My IP address is: "));
  Serial.println(esp8266.localIP());
 
 
}

void loop()
{
    postToPhant();
    delay(50000);
}
void postToPhant()
{
  // Create a client, and initiate a connection
  ESP8266Client client;
  float myip = esp8266.localIP();// ip arduino
  if (client.connect(phantServer, 80)<=0 )
  {
    Serial.println(F("Failed to connect to server."));
    return;
  }
   sensor = fluxo();
  ///Serial.println("Connected.");
    String url = "/arduino/";
  url += "salvardados.php?";
  url += "sensor=";
  url += sensor;
 
  Serial.print("Requesting URL: ");
  Serial.println(url);
 
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + phantServer + "\r\n" +
               "Connection: close\r\n\r\n");
 
  Serial.println(F("Posting to Phant!"));

 
  if (client.connected())
    client.stop(); // stop() closes a TCP connection.
}
float fluxo ()
  {
   for (int x=0; x<4; x++)
  {
   contaPulso = 0;   //Zera a variável para contar os giros por segundos
  sei();      //Habilita interrupção
  delay (1000); //Aguarda 1 segundo
  cli();      //Desabilita interrupção
 
  vazao = contaPulso / 5.5; //Converte para L/min

  media = media+vazao; //Soma a vazão para o calculo da media
    i++;
  Serial.print(vazao); //Imprime na serial o valor da vazão
  Serial.println(" L/min - "); //Imprime L/min
  Serial.print(i); //Imprime a contagem i (segundos)
  Serial.println("s"); //Imprime s indicando que está em segundos
    if(i==4)
   {
    return media;
    }
  }
 }

void incpulso ()
{
  contaPulso++; //Incrementa a variável de contagem dos pulsos
}


thanks for help :) :) :)
User avatar
By Ribeiro Santos
#58782 A função delay nunca pode demorar mais de 10, no código está muito mais
Code: Select alldelay(50000)
;

utilize uma flag no loop e quando esta chegar aos 50000 execute então o código. Chame periodicamente a função yield() para que o processador continue a executar as outras tarefas (wifi, ...)

Por outro lado a função do interrput tem de ter o atributo: ICACHE_RAM_ATTR ou seja
Code: Select allvoid ICACHE_RAM_ATTR  incpulso ()


.....

The delay function should have a maximum of 10, or may crash the ESP. You have 50000. You should use, in the loop function, a kind of flag with time, and when de flag has the desired value execute the postToPhant(); Use and abuse of yield() function, calling that allow the processor to execute other tasks (like wifi, rtc,...)

In interrput functions you should have the attribute ICACHE_RAM_ATTR, or
Code: Select allvoid ICACHE_RAM_ATTR  incpulso ()
User avatar
By Natan Jr
#58797 I made some changes and the code stayed like this, but now hangs when it will carry out the post. Print only "POS"
Code: Select allvoid setup() {
  // put your setup code here, to run once:
  int status;
  Serial.begin(9600);
 
  // To turn the MG2639 shield on, and verify communication
  // always begin a sketch by calling cell.begin().
  status = esp8266.begin();
  if (status <= 0)
  {
    Serial.println(F("Unable to communicate with shield. Looping"));
    while(1) ;
  }
 
  esp8266.setMode(ESP8266_MODE_STA); // Set WiFi mode to station
  if (esp8266.status() <= 0) // If we're not already connected
  {
    if (esp8266.connect(mySSID, myPSK) < 0)
    {
      Serial.println(F("Error connecting"));
      while (1) ;
    }   
  }
  pinMode(2, INPUT);
 void  ICACHE_RAM_ATTR incpulso ();
  //attachInterrupt(0, incpulso, RISING);
  // Get our assigned IP address and print it:
  Serial.print(F("My IP address is: "));
  Serial.println(esp8266.localIP());
  yield();
}

void loop() {
  // put your main code here, to run repeatedly:

  b = postToPhant(vazao);
    if (b==1)
    {
      Serial.println("Numero postado");
      }
    delay(10000);
}

float postToPhant(float valor)
{
  yield();
  // Create a client, and initiate a connection
  ESP8266Client client;
  float myip = esp8266.localIP();// ip arduino
  if (client.connect(phantServer, 80)<=0 )
  {
    Serial.println(F("Failed to connect to server."));
    return;
  } else {
   Serial.println("Conectado");
   delay(10000);
  sensor = medir();
  yield();
  Serial.println(sensor);
   
  Serial.println("Postando..");// ERROR the SERIAL show just "POS"
    String url = "/arduino/salvardados.php?sensor=";
  //url += "salvardados.php?sensor=";
  //url += "sensor=";
  url += sensor;
 
  Serial.print("Requesting URL: ");
  Serial.println(url);
 
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + phantServer + "\r\n" +
               "Connection: close\r\n\r\n");

  Serial.println(F("Posting to Phant!"));

 

  // available() will return the number of characters
  // currently in the receive buffer.
  /*while (client.available())
    Serial.write(client.read()); // read() gets the FIFO char
 
  // connected() is a boolean return value - 1 if the
  // connection is active, 0 if it's closed.*/
  }
  if (client.connected())
    client.stop(); // stop() closes a TCP connection.
    return 1;
}
float medir ()
{
  for (int x=0; x<4; x++)
   {
     contaPulso = 0;   //Zera a variável para contar os giros por segundos
  sei();      //Habilita interrupção
  delay (1000); //Aguarda 1 segundo
  cli();      //Desabilita interrupção
 
  vazao = contaPulso / 5.5; //Converte para L/min
  media=media+vazao; //Soma a vazão para o calculo da media
  i++;
 
  Serial.print(vazao); //Imprime na serial o valor da vazão
  Serial.print(" L/min - "); //Imprime L/min
  Serial.print(i); //Imprime a contagem i (segundos)
  Serial.println("s"); //Imprime s indicando que está em segundos
 
   }
   Serial.print(vazao);
 
  }
void incpulso ()
{
  contaPulso++; //Incrementa a variável de contagem dos pulsos
}
User avatar
By Natan Jr
#58808 thank you man, i'm I changed the code and the error continues before calling get look

void setup() {
// put your setup code here, to run once:
int status;
Serial.begin(9600);

// To turn the MG2639 shield on, and verify communication
// always begin a sketch by calling cell.begin().
status = esp8266.begin();
if (status <= 0)
{
Serial.println(F("Unable to communicate with shield. Looping"));
while(1) ;
}

esp8266.setMode(ESP8266_MODE_STA); // Set WiFi mode to station
if (esp8266.status() <= 0) // If we're not already connected
{
if (esp8266.connect(mySSID, myPSK) < 0)
{
Serial.println(F("Error connecting"));
while (1) ;
}
}
pinMode(9, INPUT);
// void ICACHE_RAM_ATTR incpulso ();
//attachInterrupt(0, incpulso, RISING);
// Get our assigned IP address and print it:
Serial.print(F("My IP address is: "));
Serial.println(esp8266.localIP());
yield();
}

void loop() {
// put your main code here, to run repeatedly:

b = postToPhant(vazao);
if (b==1)
{
Serial.println("Numero postado");
}
delay(10000);
}

float postToPhant(float valor)
{
yield();
// Create a client, and initiate a connection
ESP8266Client client;
float myip = esp8266.localIP();// ip arduino
if (client.connect(phantServer, 80)<=0 )
{
Serial.println(F("Failed to connect to server."));
return;
} else {
Serial.println("Conectado");
delay(10000);
sensor = medir();
yield();
Serial.println(sensor);

Serial.println("Postando..");// The error is here, show in SERIAL only "POS".
String url = "/arduino/salvardados.php?sensor=";
//url += "salvardados.php?sensor=";
//url += "sensor=";
url += sensor;

Serial.print("Requesting URL: ");
Serial.println(url);

// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + phantServer + "\r\n" +
"Connection: close\r\n\r\n");

Serial.println(F("Posting to Phant!"));



// available() will return the number of characters
// currently in the receive buffer.
/*while (client.available())
Serial.write(client.read()); // read() gets the FIFO char

// connected() is a boolean return value - 1 if the
// connection is active, 0 if it's closed.*/
}
if (client.connected())
client.stop(); // stop() closes a TCP connection.
return 1;
}
float medir ()
{
for (int x=0; x<4; x++)
{
contaPulso = 0; //Zera a variável para contar os giros por segundos
sei(); //Habilita interrupção
delay (1000); //Aguarda 1 segundo
cli(); //Desabilita interrupção

vazao = contaPulso / 5.5; //Converte para L/min
media=media+vazao; //Soma a vazão para o calculo da media
i++;

Serial.print(vazao); //Imprime na serial o valor da vazão
Serial.print(" L/min - "); //Imprime L/min
Serial.print(i); //Imprime a contagem i (segundos)
Serial.println("s"); //Imprime s indicando que está em segundos

}

Serial.print(vazao);
return vazao;
}
void incpulso ()
{
contaPulso++; //Incrementa a variável de contagem dos pulsos
}