• AVR Freaks

Helpful ReplyHot!if else statements don't works

Page: 12 > Showing page 1 of 2
Author
diego3602
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2018/11/25 16:47:59
  • Location: 0
  • Status: offline
2020/02/07 14:21:40 (permalink)
0

if else statements don't works

Hi, I have an issue with a little simple snippet of code. The problem is that the code always runs the "if" sentence but never the "else" one, no matter the condition. I'm using MPLAB XC32 v1.40. Here is the code:
 
               
                        if ((confirmacion == 0))
                        {
                            for(y=0;y<i;y++)
                            {
                                if (vBuffer[y]=='H' && vBuffer[y+1]=='T' && vBuffer[y+2]=='T' && vBuffer[y+3]=='P' && vBuffer[y+4]=='/' && vBuffer[y+5]=='1' && vBuffer[y+6]=='.' && vBuffer[y+7]=='1' && vBuffer[y+8]==' '
                                        && vBuffer[y+9]=='2' && vBuffer[y+10]=='0' && vBuffer[y+11]=='0' && vBuffer[y+12]==' ' && vBuffer[y+13]=='O' && vBuffer[y+14]=='K')
                                {
                                    contV=0.0;
                                    Vsump=0.0;
                                    Esum=0.0;
                                    contador=0;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            for(y=0;y<i;y++)
                            {
                                if (vBuffer[y]=='v' && vBuffer[y+1]=='a' && vBuffer[y+2]=='l' && vBuffer[y+3]=='u' && vBuffer[y+4]=='e' && vBuffer[y+5]=='\"' && vBuffer[y+6]==':' && vBuffer[y+7]==' ')//&& vBuffer[y+5]==':' && vBuffer[y+6]==' ')
                                {
                                    if (vBuffer[y+9] != '.') //si y+9 no es un punto, es el caso de recepcion de valor de proteccion (Ej:180.0)
                                    {
                                        for (pos=y+7;pos<i;pos++)
                                            {
                                                if (vBuffer[pos]=='.')
                                                {
                                                    aux[0] = vBuffer[y+8];
                                                    aux[1] = vBuffer[y+9];
                                                    aux[2] = vBuffer[y+10];
                                                    valor = atoi(aux);
                                                    if (valor < 201)
                                                    {
                                                        rele = 1;
                                                        corte = 1;
                                                    }
                                                    else
                                                    {
                                                        corte = 0;
                                                    }
                                                    break;
                                                }

                                            }

                                    }
                                    else if (vBuffer[y+9] == '.') //Si y+9 no es punto, es el caso de valor de switch (Ej:1.0)
                                    {
                                        for (pos=y+7;pos<i;pos++)
                                        {
                                            if (vBuffer[pos]=='.')
                                            {
                                              if (vBuffer[pos-1]==0x31)
                                                {
                                                  rele = 1;
                                                }
                                              else if (vBuffer[pos-1]==0x30 && corte == 0)
                                                {
                                                  rele = 0;
                                                }
                                              break;
                                            }
                                        }
                                    }
                                }
                            }
                        }

 
Thanks!!!
#1
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3835
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: if else statements don't works 2020/02/07 14:31:57 (permalink)
0 (2)
4 x else
 
Which one?

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#2
diego3602
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2018/11/25 16:47:59
  • Location: 0
  • Status: offline
Re: if else statements don't works 2020/02/07 15:01:53 (permalink)
0
sorry, this if-else
 
                       if ((confirmacion == 0))
                        {
                            for(y=0;y<i;y++)
                            {
                                if (vBuffer[y]=='H' && vBuffer[y+1]=='T' && vBuffer[y+2]=='T' && vBuffer[y+3]=='P' && vBuffer[y+4]=='/' && vBuffer[y+5]=='1' && vBuffer[y+6]=='.' && vBuffer[y+7]=='1' && vBuffer[y+8]==' '
                                        && vBuffer[y+9]=='2' && vBuffer[y+10]=='0' && vBuffer[y+11]=='0' && vBuffer[y+12]==' ' && vBuffer[y+13]=='O' && vBuffer[y+14]=='K')
                                {
                                    contV=0.0;
                                    Vsump=0.0;
                                    Esum=0.0;
                                    contador=0;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            for(y=0;y<i;y++)
#3
PStechPaul
Super Member
  • Total Posts : 2555
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: online
Re: if else statements don't works 2020/02/07 15:27:25 (permalink)
+1 (1)
It would be better to use the strcmp() function to identify the URL.
 
https://www.tutorialspoint.com/c_standard_library/c_function_strcmp.htm
 
I assume that is the problematic if() statement, and not "confirmacion".

 
#4
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11673
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: if else statements don't works 2020/02/07 15:39:09 (permalink)
+1 (1)
You haven't shown how confirmacion is declared or what value it has, so no one can answer your question.
 
And as PStechPaul points out, you really need to learn how to use strings in C.  That code is absolutely horrific.
#5
diego3602
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2018/11/25 16:47:59
  • Location: 0
  • Status: offline
Re: if else statements don't works 2020/02/07 15:40:20 (permalink)
0
yhanks PSteechPaul I'll keep it in mind. 
 
"confirmacion" is a variable int type, even when is declared with 1 the if always runs. 
i tried with else if and doesn't work either. i was reading other posts that may enabling "Repair Mismatched CR/LF on Save" can solve this problem, but i can't find this option on mplab xc32.
 
thanks.
post edited by diego3602 - 2020/02/07 15:43:21
#6
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11673
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: if else statements don't works 2020/02/07 15:46:39 (permalink)
+2 (2)
XC32 is a compiler, so it would not have any options related to saving files.  There are no compiler options that would prevent an if() statement from working correctly.  Telling us confirmacion is an int does not help.  Post complete code that has the problem, and explain how you know that it's always going to the "else".
#7
NKurzman
A Guy on the Net
  • Total Posts : 18388
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: if else statements don't works 2020/02/07 15:51:08 (permalink)
+2 (2)
I have been using XC32 for years.if else works.  The Issue is a Bug in your code.
Why are you using V1.40? Is is years old.
#8
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3835
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: if else statements don't works 2020/02/07 16:01:16 (permalink) ☄ Helpfulby diego3602 2020/02/07 19:03:01
-1 (1)
Insert:

confirmacion = 1;
if ((confirmacion == 0))
{


MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#9
Antipodean
Super Member
  • Total Posts : 1809
  • Reward points : 0
  • Joined: 2008/12/09 10:19:08
  • Location: Didcot, United Kingdom
  • Status: online
Re: if else statements don't works 2020/02/07 16:01:24 (permalink)
+1 (1)
Sounds like the if statements are being optimised away.
 

Do not use my alias in your message body when replying, your message will disappear ...

Alan
#10
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3835
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: if else statements don't works 2020/02/07 16:09:49 (permalink)
-1 (1)
At the end of the main condition print all the values to uart -> putty.
 
That will tell the compiler and linker that they have a use and will not remove any code.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#11
diego3602
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2018/11/25 16:47:59
  • Location: 0
  • Status: offline
Re: if else statements don't works 2020/02/07 17:27:49 (permalink)
0
Thanks every body here's the full code
 

#define __GENERICTCPCLIENT_C
#include "TCPIPConfig.h"
#include <math.h>
#if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)
#include "TCPIP Stack/TCPIP.h"

// Defines the server to be accessed for this application
#ifdef WIFI_NET_TEST
static BYTE ServerName[] = "www" WIFI_NET_TEST_DOMAIN;
#else
static BYTE ServerName[] = "things.ubidots.com";
#endif
//TOKEN
char token[]= "A1E-Sg9Sye2DSLfzJqNzKUS63HANfTgmm2";
//ANALOGICO
//Corriente
char idVariable2[]= "5c64ddfec03f97268619fadf";
char corr[]="";
int size2;
int sizeid2;
int Ief;
//Tension
char idVariable3[]= "5c64de37c03f972640640063";
char tension[]="";
int size3;
int sizeid3;
int Vef; //
float Vsump;
float contV;
//Energia
char idVariable6[]= "5c64ddecc03f972640640053";
char energia[]="";
int size6;
int sizeid6;
int Eef;
float Esum;

//DIGITAL
//Apagado Remoto
char idVariable4[]= "5e1dc1a2591636291f249791"; // Switch
char idVariable5[]= "5debeaae5916361260ced4bc"; // Valor de proteccion
//Agua
char idVariable1[]= "5dec4a4f5916363cbd61c691";
int size1;
int contador;
int env1;
int sizeid1;
char agua[]="";
//Corte
char PA[]="";
//Generico
int sizet1;
int sizet2;
int flag=0;
int y=0, pos=0;
int corte = 0;
int confirmacion;
// Defines the port to be accessed for this application
#if defined(STACK_USE_SSL_CLIENT)
static WORD ServerPort = HTTPS_PORT;
// Note that if HTTPS is used, the ServerName and URL
// must change to an SSL enabled server.
#else
static WORD ServerPort = HTTP_PORT;
#endif
// Defines the URL to be requested by this HTTP client
//static ROM BYTE RemoteURL[] = "";
/*****************************************************************************
Function:
void GenericTCPClient(void)
Summary:
Implements a simple HTTP client (over TCP).
Description:
This function implements a simple HTTP client, which operates over TCP.
The function is called periodically by the stack, and waits for BUTTON1
to be pressed. When the button is pressed, the application opens a TCP
connection to an Internet search engine, performs a search for the word
"Microchip" on "microchip.com", and prints the resulting HTML page to
the UART.
This example can be used as a model for many TCP and HTTP client
applications.
Precondition:
TCP is initialized.
Parameters:
None
Returns:
None
***************************************************************************/
void GenericTCPClient(void)
{
unsigned char i; //undigned char = BYTE
WORD w;
unsigned char vBuffer[400]; //vBuffer[210];
static DWORD Timer;
static TCP_SOCKET MySocket = INVALID_SOCKET;
int valor;
unsigned char aux[3];
static enum _GenericTCPExampleState
{
SM_HOME = 0,
SM_SOCKET_OBTAINED,
#if defined(STACK_USE_SSL_CLIENT)
SM_START_SSL,
#endif
SM_PROCESS_RESPONSE,
SM_DISCONNECT,
SM_DONE
} GenericTCPExampleState = SM_DONE;
switch(GenericTCPExampleState)
{
case SM_HOME:
// Connect a socket to the remote TCP server
MySocket = TCPOpen((DWORD)(PTR_BASE)&ServerName[0], TCP_OPEN_RAM_HOST, ServerPort, TCP_PURPOSE_GENERIC_TCP_CLIENT);
// Abort operation if no TCP socket of type TCP_PURPOSE_GENERIC_TCP_CLIENT is available
// If this ever happens, you need to go add one to TCPIPConfig.h
if(MySocket == INVALID_SOCKET)
break;
#if defined(STACK_USE_UART)
putrsUART((ROM char*)"\r\n\r\nConnecting using Microchip TCP API...\r\n");
#endif
GenericTCPExampleState++;
Timer = TickGet();
break;
case SM_SOCKET_OBTAINED:
// Wait for the remote server to accept our connection request
if(!TCPIsConnected(MySocket))
{
// Time out if too much time is spent in this state
if(TickGet()-Timer > 5*TICK_SECOND)
{
// Close the socket so it can be used by other modules
TCPDisconnect(MySocket);
MySocket = INVALID_SOCKET;
GenericTCPExampleState--;
}
break;
}
Timer = TickGet();
#if defined (STACK_USE_SSL_CLIENT)
if(!TCPStartSSLClient(MySocket,(void *)"thishost"))
break;
GenericTCPExampleState++;
break;
case SM_START_SSL:
if (TCPSSLIsHandshaking(MySocket))
{
if(TickGet()-Timer > 10*TICK_SECOND)
{
// Close the socket so it can be used by other modules
TCPDisconnect(MySocket);
MySocket = INVALID_SOCKET;
GenericTCPExampleState=SM_HOME;
}
break;
}
#endif
// Make certain the socket can be written to
if(TCPIsPutReady(MySocket) < 125u)
break;
char buff[400];
//corriente
uitoa(Ief,corr);
size2=strlen(corr);

//tension
uitoa(Vef,tension);//uitoa(Vef,tension);
size3=strlen(tension);


//agua
uitoa(contador,agua);
size1=strlen(agua);

//Energia
Eef=Esum;
//uitoa(Eef,energia);
if(Eef>0)
{
size6=log10l(Eef)+1;
}
else
{size6=1;}
sizet1=size3+size2+size1+size6+104;//84; // Forma 2... 67 sin io
//corte
uitoa(corte,PA);
switch(flag)
{
case 0: //Flag=0 envio tension corriente y agua
sprintf(buff, "POST /api/v1.6/devices/tesis HTTP/1.1\r\nHost:things.ubidots.com\r\nX-Auth-Token:%s\r\nContent-Type:application/json\r\nConnection:close\r\nContent-Length:%d\r\n\r\n{\"tension\":{\"value\":%s},\"Corriente\":{\"value\":%s},\"Contador\":{\"value\":%s},\"pa\":{\"value\":%s},\"en\":{\"value\":%i}}\r\n\r\n",token,sizet1,tension,corr,agua,PA,Eef);//Forma 2
flag=1;
confirmacion=0;
break;
case 1: //Flag=1 obtengo valor de switch
sprintf(buff, "GET /api/v1.6/variables/%s/values?page_size=1 HTTP/1.1\r\nContent-Type: application/json\r\nX-Auth-Token:%s\r\nHost:things.ubidots.com\r\nConnection: close\r\n\r\n",idVariable4,token);
flag=2;
confirmacion=1;
break;
case 2: //Flag=2 obtengo valor de proteccion
sprintf(buff, "GET /api/v1.6/variables/%s/values?page_size=1 HTTP/1.1\r\nContent-Type: application/json\r\nX-Auth-Token:%s\r\nHost:things.ubidots.com\r\nConnection: close\r\n\r\n",idVariable5,token);
flag=0;
confirmacion=1;
break;

}
TCPPutArray(MySocket,buff,strlen(buff));
TCPFlush(MySocket);
GenericTCPExampleState++;
break;
case SM_PROCESS_RESPONSE:
// Check to see if the remote node has disconnected from us or sent us any application data
// If application data is available, write it to the UART
if(!TCPIsConnected(MySocket))
{
GenericTCPExampleState = SM_DISCONNECT;
// Do not break; We might still have data in the TCP RX FIFO waiting for us
}
// Get count of RX bytes waiting
w = TCPIsGetReady(MySocket);
// Obtain and print the server reply
i = sizeof(vBuffer)-1;
vBuffer[i] = '\0';
while(w)
{
if(w < i)
{
i = w;
vBuffer[i] = '\0';
}
w -= TCPGetArray(MySocket, vBuffer, i);
if(GenericTCPExampleState == SM_PROCESS_RESPONSE)
break;
}
if ((confirmacion == 0))
{
for(y=0;y<i;y++)
{
if (vBuffer[y]=='H' && vBuffer[y+1]=='T' && vBuffer[y+2]=='T' && vBuffer[y+3]=='P' && vBuffer[y+4]=='/' && vBuffer[y+5]=='1' && vBuffer[y+6]=='.' && vBuffer[y+7]=='1' && vBuffer[y+8]==' '
&& vBuffer[y+9]=='2' && vBuffer[y+10]=='0' && vBuffer[y+11]=='0' && vBuffer[y+12]==' ' && vBuffer[y+13]=='O' && vBuffer[y+14]=='K')
{
contV=0.0;
Vsump=0.0;
Esum=0.0;
contador=0;
break;
}
}
}
else if (confirmacion ==1)
{
for(y=0;y<i;y++)
{
if (vBuffer[y]=='v' && vBuffer[y+1]=='a' && vBuffer[y+2]=='l' && vBuffer[y+3]=='u' && vBuffer[y+4]=='e' && vBuffer[y+5]=='\"' && vBuffer[y+6]==':' && vBuffer[y+7]==' ')//&& vBuffer[y+5]==':' && vBuffer[y+6]==' ')
{
if (vBuffer[y+9] != '.') //si y+9 no es un punto, es el caso de recepcion de valor de proteccion (Ej:180.0)
{
for (pos=y+7;pos<i;pos++)
{
if (vBuffer[pos]=='.')
{
aux[0] = vBuffer[y+8];
aux[1] = vBuffer[y+9];
aux[2] = vBuffer[y+10];
valor = atoi(aux);
if (valor < 201)
{
rele = 1;
corte = 1;
}
else
{
corte = 0;
}
break;
}
}
}
else if (vBuffer[y+9] == '.') //Si y+9 no es punto, es el caso de valor de switch (Ej:1.0)
{
for (pos=y+7;pos<i;pos++)
{
if (vBuffer[pos]=='.')
{
if (vBuffer[pos-1]==0x31)
{
rele = 1;
}
else if (vBuffer[pos-1]==0x30 && corte == 0)
{
rele = 0;
}
break;
}
}
}
}
}
}
break;
case SM_DISCONNECT:
// Close the socket so it can be used by other modules
// For this application, we wish to stay connected, but this state will still get entered if the remote server decides to disconnect
TCPDisconnect(MySocket);
MySocket = INVALID_SOCKET;
GenericTCPExampleState = SM_DONE;
break;
case SM_DONE:
DelayMs(1000);
GenericTCPExampleState = SM_HOME;
// Do nothing unless the user pushes BUTTON1 and wants to restart the whole connection/download process
//if(BUTTON3_IO == 0u)
GenericTCPExampleState = SM_HOME;
break;
}
}
#endif //#if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)

#12
diego3602
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2018/11/25 16:47:59
  • Location: 0
  • Status: offline
Re: if else statements don't works 2020/02/07 17:41:46 (permalink)
0
#define __GENERICTCPCLIENT_C
 
#include "TCPIPConfig.h"
#include <math.h>
 
#if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)
 
#include "TCPIP Stack/TCPIP.h"
 
 
// Defines the server to be accessed for this application
#ifdef WIFI_NET_TEST
static BYTE ServerName[] = "www" WIFI_NET_TEST_DOMAIN;
#else
static BYTE ServerName[] = "things.ubidots.com";
#endif
//TOKEN
char token[]= "A1E-Sg9Sye2DSLfzJqNzKUS63HANfTgmm2";
//ANALOGICO
 
 //Corriente
 char idVariable2[]= "5c64ddfec03f97268619fadf";
 char corr[]="";
 int size2;
 int sizeid2;
 int Ief;
//Tension
 char idVariable3[]= "5c64de37c03f972640640063";
 char tension[]="";
 int size3;
 int sizeid3;
 int Vef;  //
 float Vsump;
 float contV;
 
 //Energia
 char idVariable6[]= "5c64ddecc03f972640640053";
 char energia[]="";
 int size6;
 int sizeid6;
 int Eef;
 float Esum;
 
 
 //DIGITAL
 //Apagado Remoto
 char idVariable4[]= "5e1dc1a2591636291f249791"; // Switch
 char idVariable5[]= "5debeaae5916361260ced4bc"; // Valor de proteccion
 //Agua
 char idVariable1[]= "5dec4a4f5916363cbd61c691";
 int size1;
 int contador;
 int env1;
 int sizeid1;
 char agua[]="";
 //Corte
 char PA[]="";
 
 //Generico
 int sizet1;
 int sizet2;
 int flag=0;
 int y=0, pos=0;
 int corte = 0;
 int confirmacion;
 
// Defines the port to be accessed for this application
#if defined(STACK_USE_SSL_CLIENT)
    static WORD ServerPort = HTTPS_PORT;
// Note that if HTTPS is used, the ServerName and URL
// must change to an SSL enabled server.
#else
    static WORD ServerPort = HTTP_PORT;
#endif
 
// Defines the URL to be requested by this HTTP client
//static ROM BYTE RemoteURL[] = "";
 
/*****************************************************************************
  Function:
void GenericTCPClient(void)
 
  Summary:
Implements a simple HTTP client (over TCP).
 
  Description:
This function implements a simple HTTP client, which operates over TCP.
The function is called periodically by the stack, and waits for BUTTON1
to be pressed.  When the button is pressed, the application opens a TCP
connection to an Internet search engine, performs a search for the word
"Microchip" on "microchip.com", and prints the resulting HTML page to
the UART.
 
This example can be used as a model for many TCP and HTTP client
applications.
 
  Precondition:
TCP is initialized.
 
  Parameters:
None
 
  Returns:
  None
  ***************************************************************************/
void GenericTCPClient(void)
{
unsigned char i; //undigned char = BYTE
WORD w;
unsigned char vBuffer[400];  //vBuffer[210];
static DWORD Timer;
static TCP_SOCKET MySocket = INVALID_SOCKET;
        int valor;
        unsigned char aux[3];
static enum _GenericTCPExampleState
{
SM_HOME = 0,
SM_SOCKET_OBTAINED,
#if defined(STACK_USE_SSL_CLIENT)
    SM_START_SSL,
    #endif
SM_PROCESS_RESPONSE,
SM_DISCONNECT,
SM_DONE
} GenericTCPExampleState = SM_DONE;
 
switch(GenericTCPExampleState)
{
case SM_HOME:
// Connect a socket to the remote TCP server
MySocket = TCPOpen((DWORD)(PTR_BASE)&ServerName[0], TCP_OPEN_RAM_HOST, ServerPort, TCP_PURPOSE_GENERIC_TCP_CLIENT);
 
// Abort operation if no TCP socket of type TCP_PURPOSE_GENERIC_TCP_CLIENT is available
// If this ever happens, you need to go add one to TCPIPConfig.h
if(MySocket == INVALID_SOCKET)
break;
 
#if defined(STACK_USE_UART)
putrsUART((ROM char*)"\r\n\r\nConnecting using Microchip TCP API...\r\n");
#endif
 
GenericTCPExampleState++;
Timer = TickGet();
break;
 
case SM_SOCKET_OBTAINED:
// Wait for the remote server to accept our connection request
if(!TCPIsConnected(MySocket))
{
// Time out if too much time is spent in this state
if(TickGet()-Timer > 5*TICK_SECOND)
{
// Close the socket so it can be used by other modules
TCPDisconnect(MySocket);
MySocket = INVALID_SOCKET;
GenericTCPExampleState--;
}
break;
}
 
Timer = TickGet();
 
    #if defined (STACK_USE_SSL_CLIENT)
            if(!TCPStartSSLClient(MySocket,(void *)"thishost"))
                break;
GenericTCPExampleState++;
break;
 
        case SM_START_SSL:
            if (TCPSSLIsHandshaking(MySocket))
            {
if(TickGet()-Timer > 10*TICK_SECOND)
{
// Close the socket so it can be used by other modules
TCPDisconnect(MySocket);
MySocket = INVALID_SOCKET;
GenericTCPExampleState=SM_HOME;
}
                break;
            }
    #endif
 
// Make certain the socket can be written to
if(TCPIsPutReady(MySocket) < 125u)
break;
                        char buff[400];
                        //corriente
                        uitoa(Ief,corr);
                        size2=strlen(corr);                        
                        
//tension                                             
uitoa(Vef,tension);//uitoa(Vef,tension);
                        size3=strlen(tension);
                        
                        
//agua
                        uitoa(contador,agua);
                        size1=strlen(agua);
                        
                        //Energia
                        Eef=Esum;
                        //uitoa(Eef,energia);
                        if(Eef>0)
                        {
                        size6=log10l(Eef)+1;
                        }
                        else
                        {size6=1;}                                               
                        sizet1=size3+size2+size1+size6+104;//84; // Forma 2... 67 sin io
                        //corte
                        uitoa(corte,PA);
 
                        switch(flag)
                        {
                            case 0: //Flag=0 envio tension corriente y agua
                            sprintf(buff, "POST /api/v1.6/devices/tesis HTTP/1.1\r\nHost:things.ubidots.com\r\nX-Auth-Token:%s\r\nContent-Type:application/json\r\nConnection:close\r\nContent-Length:%d\r\n\r\n{\"tension\":{\"value\":%s},\"Corriente\":{\"value\":%s},\"Contador\":{\"value\":%s},\"pa\":{\"value\":%s},\"en\":{\"value\":%i}}\r\n\r\n",token,sizet1,tension,corr,agua,PA,Eef);//Forma 2
                            flag=1;
                            confirmacion=0;
                            break;
 
                            case 1: //Flag=1 obtengo valor de switch
                            sprintf(buff, "GET /api/v1.6/variables/%s/values?page_size=1 HTTP/1.1\r\nContent-Type: application/json\r\nX-Auth-Token:%s\r\nHost:things.ubidots.com\r\nConnection: close\r\n\r\n",idVariable4,token);
                            flag=2;
                            confirmacion=1;
                            break;
 
                            case 2:  //Flag=2 obtengo valor de proteccion
                            sprintf(buff, "GET /api/v1.6/variables/%s/values?page_size=1 HTTP/1.1\r\nContent-Type: application/json\r\nX-Auth-Token:%s\r\nHost:things.ubidots.com\r\nConnection: close\r\n\r\n",idVariable5,token);
                            flag=0;
                            confirmacion=1;
                            break;
                            
                        }
                        TCPPutArray(MySocket,buff,strlen(buff));
TCPFlush(MySocket);
GenericTCPExampleState++;
break;
 
case SM_PROCESS_RESPONSE:
// Check to see if the remote node has disconnected from us or sent us any application data
// If application data is available, write it to the UART
if(!TCPIsConnected(MySocket))
{
GenericTCPExampleState = SM_DISCONNECT;
// Do not break;  We might still have data in the TCP RX FIFO waiting for us
}
 
// Get count of RX bytes waiting
w = TCPIsGetReady(MySocket);
 
// Obtain and print the server reply
                        i = sizeof(vBuffer)-1;
vBuffer[i] = '\0';
while(w)
{
if(w < i)
{
i = w;
vBuffer[i] = '\0';
}
w -= TCPGetArray(MySocket, vBuffer, i);
if(GenericTCPExampleState == SM_PROCESS_RESPONSE)
break;
                        }
 
                        if ((confirmacion == 0))
                        {
                            for(y=0;y<i;y++)
                            {
                                if (vBuffer[y]=='H' && vBuffer[y+1]=='T' && vBuffer[y+2]=='T' && vBuffer[y+3]=='P' && vBuffer[y+4]=='/' && vBuffer[y+5]=='1' && vBuffer[y+6]=='.' && vBuffer[y+7]=='1' && vBuffer[y+8]==' '
                                        && vBuffer[y+9]=='2' && vBuffer[y+10]=='0' && vBuffer[y+11]=='0' && vBuffer[y+12]==' ' && vBuffer[y+13]=='O' && vBuffer[y+14]=='K')
                                {
                                    contV=0.0;
                                    Vsump=0.0;
                                    Esum=0.0;
                                    contador=0;
                                    break;
                                }
                            }
                        }
                        else if (confirmacion ==1)
                        {
                            for(y=0;y<i;y++)
                            {
                                if (vBuffer[y]=='v' && vBuffer[y+1]=='a' && vBuffer[y+2]=='l' && vBuffer[y+3]=='u' && vBuffer[y+4]=='e' && vBuffer[y+5]=='\"' && vBuffer[y+6]==':' && vBuffer[y+7]==' ')//&& vBuffer[y+5]==':' && vBuffer[y+6]==' ')
                                {
                                    if (vBuffer[y+9] != '.')    //si y+9 no es un punto, es el caso de recepcion de valor de proteccion (Ej:180.0)
                                    {
                                        for (pos=y+7;pos<i;pos++)
                                            {
                                                if (vBuffer[pos]=='.')
                                                {
                                                    aux[0] = vBuffer[y+8];
                                                    aux[1] = vBuffer[y+9];
                                                    aux[2] = vBuffer[y+10];
                                                    valor = atoi(aux);
                                                    if (valor < 201)
                                                    {
                                                        rele = 1;
                                                        corte = 1;
                                                    }
                                                    else
                                                    {
                                                        corte = 0;
                                                    }
                                                    break;
                                                }
 
                                            }
 
                                    }
                                    else if (vBuffer[y+9] == '.')   //Si y+9 no es punto, es el caso de valor de switch (Ej:1.0)
                                    {
                                        for (pos=y+7;pos<i;pos++)
                                        {
                                            if (vBuffer[pos]=='.')
                                            {
                                              if (vBuffer[pos-1]==0x31)
                                                {
                                                  rele = 1;
                                                }
                                              else if (vBuffer[pos-1]==0x30 && corte == 0)
                                                {
                                                  rele = 0;
                                                }
                                              break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
break;
 
case SM_DISCONNECT:
// Close the socket so it can be used by other modules
// For this application, we wish to stay connected, but this state will still get entered if the remote server decides to disconnect
TCPDisconnect(MySocket);
MySocket = INVALID_SOCKET;
GenericTCPExampleState = SM_DONE;
break;
 
case SM_DONE:
                    DelayMs(1000);
                    GenericTCPExampleState = SM_HOME;
// Do nothing unless the user pushes BUTTON1 and wants to restart the whole connection/download process
//if(BUTTON3_IO == 0u)
GenericTCPExampleState = SM_HOME;
break;
}
}
 
#endif //#if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)

 

#define __GENERICTCPCLIENT_C
#include "TCPIPConfig.h"#include <math.h>
#if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)
#include "TCPIP Stack/TCPIP.h"

// Defines the server to be accessed for this application#ifdef WIFI_NET_TESTstatic BYTE ServerName[] = "www" WIFI_NET_TEST_DOMAIN;#elsestatic BYTE ServerName[] = "things.ubidots.com";#endif//TOKENchar token[]= "A1E-Sg9Sye2DSLfzJqNzKUS63HANfTgmm2";//ANALOGICO
 //Corriente char idVariable2[]= "5c64ddfec03f97268619fadf"; char corr[]=""; int size2; int sizeid2; int Ief;//Tension char idVariable3[]= "5c64de37c03f972640640063"; char tension[]=""; int size3; int sizeid3; int Vef;  // float Vsump; float contV;
 //Energia char idVariable6[]= "5c64ddecc03f972640640053"; char energia[]=""; int size6; int sizeid6; int Eef; float Esum;

 //DIGITAL //Apagado Remoto char idVariable4[]= "5e1dc1a2591636291f249791"; // Switch char idVariable5[]= "5debeaae5916361260ced4bc"; // Valor de proteccion //Agua char idVariable1[]= "5dec4a4f5916363cbd61c691"; int size1; int contador; int env1; int sizeid1; char agua[]=""; //Corte char PA[]="";
 //Generico int sizet1; int sizet2; int flag=0; int y=0, pos=0; int corte = 0; int confirmacion;
// Defines the port to be accessed for this application#if defined(STACK_USE_SSL_CLIENT)    static WORD ServerPort = HTTPS_PORT; // Note that if HTTPS is used, the ServerName and URL // must change to an SSL enabled server.#else    static WORD ServerPort = HTTP_PORT;#endif
// Defines the URL to be requested by this HTTP client//static ROM BYTE RemoteURL[] = "";
/*****************************************************************************  Function: void GenericTCPClient(void)
  Summary: Implements a simple HTTP client (over TCP).
  Description: This function implements a simple HTTP client, which operates over TCP. The function is called periodically by the stack, and waits for BUTTON1 to be pressed.  When the button is pressed, the application opens a TCP connection to an Internet search engine, performs a search for the word "Microchip" on "microchip.com", and prints the resulting HTML page to the UART.
This example can be used as a model for many TCP and HTTP client applications.
  Precondition: TCP is initialized.
  Parameters: None
  Returns:  None  ***************************************************************************/void GenericTCPClient(void){ unsigned char i; //undigned char = BYTE WORD w; unsigned char vBuffer[400];  //vBuffer[210]; static DWORD Timer; static TCP_SOCKET MySocket = INVALID_SOCKET;        int valor;        unsigned char aux[3]; static enum _GenericTCPExampleState { SM_HOME = 0, SM_SOCKET_OBTAINED, #if defined(STACK_USE_SSL_CLIENT)    SM_START_SSL,    #endif SM_PROCESS_RESPONSE, SM_DISCONNECT, SM_DONE } GenericTCPExampleState = SM_DONE;
switch(GenericTCPExampleState) { case SM_HOME: // Connect a socket to the remote TCP server MySocket = TCPOpen((DWORD)(PTR_BASE)&ServerName[0], TCP_OPEN_RAM_HOST, ServerPort, TCP_PURPOSE_GENERIC_TCP_CLIENT);
// Abort operation if no TCP socket of type TCP_PURPOSE_GENERIC_TCP_CLIENT is available // If this ever happens, you need to go add one to TCPIPConfig.h if(MySocket == INVALID_SOCKET) break;
#if defined(STACK_USE_UART) putrsUART((ROM char*)"\r\n\r\nConnecting using Microchip TCP API...\r\n"); #endif
GenericTCPExampleState++; Timer = TickGet(); break;
case SM_SOCKET_OBTAINED: // Wait for the remote server to accept our connection request if(!TCPIsConnected(MySocket)) { // Time out if too much time is spent in this state if(TickGet()-Timer > 5*TICK_SECOND) { // Close the socket so it can be used by other modules TCPDisconnect(MySocket); MySocket = INVALID_SOCKET; GenericTCPExampleState--; } break; }
Timer = TickGet();
    #if defined (STACK_USE_SSL_CLIENT)            if(!TCPStartSSLClient(MySocket,(void *)"thishost"))                break; GenericTCPExampleState++; break;
        case SM_START_SSL:            if (TCPSSLIsHandshaking(MySocket))            { if(TickGet()-Timer > 10*TICK_SECOND) { // Close the socket so it can be used by other modules TCPDisconnect(MySocket); MySocket = INVALID_SOCKET; GenericTCPExampleState=SM_HOME; }                break;            }    #endif
// Make certain the socket can be written to if(TCPIsPutReady(MySocket) < 125u) break;                        char buff[400];                        //corriente                        uitoa(Ief,corr);                        size2=strlen(corr);                                                 //tension                                              uitoa(Vef,tension);//uitoa(Vef,tension);                        size3=strlen(tension);                                                 //agua                        uitoa(contador,agua);                        size1=strlen(agua);                                                //Energia                        Eef=Esum;                        //uitoa(Eef,energia);                        if(Eef>0)                        {                        size6=log10l(Eef)+1;                        }                        else                        {size6=1;}                                                                       sizet1=size3+size2+size1+size6+104;//84; // Forma 2... 67 sin io                        //corte                        uitoa(corte,PA);
                        switch(flag)                        {                            case 0: //Flag=0 envio tension corriente y agua                            sprintf(buff, "POST /api/v1.6/devices/tesis HTTP/1.1\r\nHost:things.ubidots.com\r\nX-Auth-Token:%s\r\nContent-Type:application/json\r\nConnection:close\r\nContent-Length:%d\r\n\r\n{\"tension\":{\"value\":%s},\"Corriente\":{\"value\":%s},\"Contador\":{\"value\":%s},\"pa\":{\"value\":%s},\"en\":{\"value\":%i}}\r\n\r\n",token,sizet1,tension,corr,agua,PA,Eef);//Forma 2                            flag=1;                            confirmacion=0;                            break;
                            case 1: //Flag=1 obtengo valor de switch                            sprintf(buff, "GET /api/v1.6/variables/%s/values?page_size=1 HTTP/1.1\r\nContent-Type: application/json\r\nX-Auth-Token:%s\r\nHost:things.ubidots.com\r\nConnection: close\r\n\r\n",idVariable4,token);                            flag=2;                            confirmacion=1;                            break;
                            case 2:  //Flag=2 obtengo valor de proteccion                            sprintf(buff, "GET /api/v1.6/variables/%s/values?page_size=1 HTTP/1.1\r\nContent-Type: application/json\r\nX-Auth-Token:%s\r\nHost:things.ubidots.com\r\nConnection: close\r\n\r\n",idVariable5,token);                            flag=0;                            confirmacion=1;                            break;                                                    }                        TCPPutArray(MySocket,buff,strlen(buff)); TCPFlush(MySocket); GenericTCPExampleState++; break;
case SM_PROCESS_RESPONSE: // Check to see if the remote node has disconnected from us or sent us any application data // If application data is available, write it to the UART if(!TCPIsConnected(MySocket)) { GenericTCPExampleState = SM_DISCONNECT; // Do not break;  We might still have data in the TCP RX FIFO waiting for us }
// Get count of RX bytes waiting w = TCPIsGetReady(MySocket);
// Obtain and print the server reply                        i = sizeof(vBuffer)-1; vBuffer = '\0'; while(w) { if(w < i) { i = w; vBuffer = '\0'; } w -= TCPGetArray(MySocket, vBuffer, i); if(GenericTCPExampleState == SM_PROCESS_RESPONSE) break;                        }
                        if ((confirmacion == 0))                        {                            for(y=0;y<i;y++)                            {                                if (vBuffer[y]=='H' && vBuffer[y+1]=='T' && vBuffer[y+2]=='T' && vBuffer[y+3]=='P' && vBuffer[y+4]=='/' && vBuffer[y+5]=='1' && vBuffer[y+6]=='.' && vBuffer[y+7]=='1' && vBuffer[y+8]==' '                                        && vBuffer[y+9]=='2' && vBuffer[y+10]=='0' && vBuffer[y+11]=='0' && vBuffer[y+12]==' ' && vBuffer[y+13]=='O' && vBuffer[y+14]=='K')                                {                                    contV=0.0;                                    Vsump=0.0;                                    Esum=0.0;                                    contador=0;                                    break;                                }                            }                        }                        else if (confirmacion ==1)                        {                            for(y=0;y<i;y++)                            {                                if (vBuffer[y]=='v' && vBuffer[y+1]=='a' && vBuffer[y+2]=='l' && vBuffer[y+3]=='u' && vBuffer[y+4]=='e' && vBuffer[y+5]=='\"' && vBuffer[y+6]==':' && vBuffer[y+7]==' ')//&& vBuffer[y+5]==':' && vBuffer[y+6]==' ')                                {                                    if (vBuffer[y+9] != '.')    //si y+9 no es un punto, es el caso de recepcion de valor de proteccion (Ej:180.0)                                    {                                        for (pos=y+7;pos<i;pos++)                                            {                                                if (vBuffer[pos]=='.')                                                {                                                    aux[0] = vBuffer[y+8];                                                    aux[1] = vBuffer[y+9];                                                    aux[2] = vBuffer[y+10];                                                    valor = atoi(aux);                                                    if (valor < 201)                                                    {                                                        rele = 1;                                                        corte = 1;                                                    }                                                    else                                                    {                                                        corte = 0;                                                    }                                                    break;                                                }
                                            }
                                    }                                    else if (vBuffer[y+9] == '.')   //Si y+9 no es punto, es el caso de valor de switch (Ej:1.0)                                    {                                        for (pos=y+7;pos<i;pos++)                                        {                                            if (vBuffer[pos]=='.')                                            {                                              if (vBuffer[pos-1]==0x31)                                                {                                                  rele = 1;                                                }                                              else if (vBuffer[pos-1]==0x30 && corte == 0)                                                {                                                  rele = 0;                                                }                                              break;                                            }                                        }                                    }                                }                            }                        } break;
case SM_DISCONNECT: // Close the socket so it can be used by other modules // For this application, we wish to stay connected, but this state will still get entered if the remote server decides to disconnect TCPDisconnect(MySocket); MySocket = INVALID_SOCKET; GenericTCPExampleState = SM_DONE; break;
case SM_DONE:                    DelayMs(1000);                    GenericTCPExampleState = SM_HOME; // Do nothing unless the user pushes BUTTON1 and wants to restart the whole connection/download process //if(BUTTON3_IO == 0u) GenericTCPExampleState = SM_HOME; break; }}
#endif //#if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)
#13
diego3602
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2018/11/25 16:47:59
  • Location: 0
  • Status: offline
Re: if else statements don't works 2020/02/07 17:43:04 (permalink)
0
Gort2015
Insert:

confirmacion = 1;
if ((confirmacion == 0))
{



thanks Gort 2015 try it. and follows runs the if statement
#14
diego3602
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2018/11/25 16:47:59
  • Location: 0
  • Status: offline
Re: if else statements don't works 2020/02/07 17:46:30 (permalink)
0
NKurzman
I have been using XC32 for years.if else works.  The Issue is a Bug in your code.
Why are you using V1.40? Is is years old.


Yes, i don't know whats is wrong.. sad: sadsad: sad i'm using V1.40 why i had some problems with librarie plib.h and with it works fine.
#15
ric
Super Member
  • Total Posts : 26057
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: if else statements don't works 2020/02/07 21:12:57 (permalink)
+2 (2)
The only code that ever writes to the "confirmacion" variable is in a function named "GenericTCPClient"
Nothing in this code ever calls "GenericTCPClient"
If it is called from inside an interrupt service, then you must add a "volatile" qualifier to the definition for "confirmacion"
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#16
mlp
boots too small
  • Total Posts : 880
  • Reward points : 0
  • Joined: 2012/09/10 15:12:07
  • Location: previously Microchip XC8 team
  • Status: offline
Re: if else statements don't works 2020/02/09 16:18:07 (permalink)
+1 (1)
diego3602
Thanks every body here's the full code

Bzzt. Try again.
This code contains no main() function, so is not a complete C program that can be compiled and tested.

Mark (this opinion available for hire)
#17
ric
Super Member
  • Total Posts : 26057
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: if else statements don't works 2020/02/09 16:22:17 (permalink)
+1 (1)
"full" != "the code I think is relevant"
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#18
diego3602
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2018/11/25 16:47:59
  • Location: 0
  • Status: offline
Re: if else statements don't works 2020/02/10 07:01:46 (permalink)
0
I tried to declare the variable volatile and set the compiler optimization options to 0.
none of these options turned out to me
#19
diego3602
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2018/11/25 16:47:59
  • Location: 0
  • Status: offline
Re: if else statements don't works 2020/02/10 07:08:05 (permalink)
0
here's the whole thing

Attachment(s)

Attachments are not available: Download requirements not met
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5