CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

HTTP client or other client

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
syide



Joined: 10 Nov 2008
Posts: 16
Location: Malaysia

View user's profile Send private message Yahoo Messenger

HTTP client or other client
PostPosted: Mon Mar 02, 2009 12:03 pm     Reply with quote

Can someone give the tips how to make HTTP client in CCS ?
What do we need to make http client ?

Because the example from CCS only have HTTP server.
Can I modify it to become HTTP client ?

By the way I trying to send data from PIC to webserver.
I use PPP to connect to the modem(HC25).
jgschmidt



Joined: 03 Dec 2008
Posts: 184
Location: Gresham, OR USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Mar 03, 2009 12:58 am     Reply with quote

The CCS TCP/IP libraries come with examples. Ex13.c is a TCP/IP client that talks to a TCP/IP server. If you want to communicate with a web server, just set your destination port to 80 and format your requests per HTTP protocol.

I have this working with a pic-based thermostat that is a http client that periodically updates and retrieves settings from a web server. I used the CCS Embedded Ethernet board and PCWH 4.084 and also have it running on Olimex Mini-Web Server boards.

Before you try to talk to a web server, get this exercise to work with a TCP/IP server that lets you monitor the connection. Talking to a web server has many opportunities for failure so you want to be sure your PIC-based client is working properly first.

Happy coding.
syide



Joined: 10 Nov 2008
Posts: 16
Location: Malaysia

View user's profile Send private message Yahoo Messenger

PostPosted: Tue Mar 03, 2009 2:14 am     Reply with quote

Thanks for reply jgschmit.

The problem here I will not use CCS Embedded Ethernet board. I will use PIC 18f4620 connect to HC25 siemen modem. Also i used rs232 to connect the PIC to modem and not using ethernet but use PPP.

PIC 18f4620 <=>max232<=>rs232<=>modem


Can it connect to TCP/IP server anyway use this thing?
Question

I also try to modified the example that use PPP and not MAC protocol to connect to IP. Can anyone give tips about it
syide



Joined: 10 Nov 2008
Posts: 16
Location: Malaysia

View user's profile Send private message Yahoo Messenger

TCP client
PostPosted: Tue Mar 03, 2009 3:57 am     Reply with quote

I try to change the example 13.c that use MAC_stack to PPP_stack. But i have problem to eliminate the ARP part and replace it with PPP part. Because as far as I know PPP cannot be use with ARP because ARP use for enthernet.

Code:

#define STACK_USE_ICMP  1
#define STACK_USE_PPP   1
#define STACK_USE_TCP   1
#include "ccstcpip.h"


//IP address of the PC running TCPSERVER.EXE
IP_ADDR server;

#define EXAMPLE_TCP_PORT   (int16)1000

//this function is called by MyTCPTask() when the specified socket is connected
//to the PC running the TCPSERVER.EXE demo.
//returns TRUE if BUTTON2 was pressed, therefore we must disconnect the socket
int8 TCPConnectedTask(TCP_SOCKET socket) {
   char c;
   static int8 counter;
   char str[20];
   static int8 button1_held;

   if (TCPIsGetReady(socket)) {
      printf(lcd_putc,"\n                 \n");
      while (TCPGet(socket, &c)) {
         lcd_putc(c);
      }
   }

//when button 1 is pressed: send message over TCP
//when button 2 is pressed: disconnect socket
   if (BUTTON1_PRESSED() && !button1_held && TCPIsPutReady(socket)) {
      button1_held=TRUE;
      sprintf(str,"BUTTON C=%U",counter++);
      TCPPutArray(socket,str,strlen(str));
      TCPFlush(socket);
   }
   if (!BUTTON1_PRESSED()) {
      button1_held=FALSE;
   }
  #if defined(BUTTON2_PRESSED())
   if (BUTTON2_PRESSED()) {
      return(TRUE);
   }
  #endif
   return(FALSE);
}

void MyTCPTask() {
   static TICKTYPE lastTick;
   static TCP_SOCKET socket=INVALID_SOCKET;
   static enum {
      MYTCP_STATE_NEW=0, MYTCP_STATE_ARP_REQ=1, MYTCP_STATE_ARP_WAIT=2,
      MYTCP_STATE_CONNECT=3, MYTCP_STATE_CONNECT_WAIT=4,
      MYTCP_STATE_CONNECTED=5, MYTCP_STATE_DISCONNECT=6,
      MYTCP_STATE_FORCE_DISCONNECT=7
   } state=0;
   static NODE_INFO remote;
   TICKTYPE currTick;
   int8 dis;

   currTick=TickGet();

   switch (state) {
      case MYTCP_STATE_NEW:
         memcpy(&remote.IPAddr, &server, sizeof(IP_ADDR));
         printf(lcd_putc,"\nARP REQUEST     ");
         state=MYTCP_STATE_ARP_REQ;

      case MYTCP_STATE_ARP_REQ:
         if (ARPIsTxReady()) {
            ARPResolve(&remote.IPAddr);
            lastTick=currTick;
            state=MYTCP_STATE_ARP_WAIT;
         }
         break;

      case MYTCP_STATE_ARP_WAIT:
         if (ARPIsResolved(&remote.IPAddr, &remote.MACAddr)) {
            state=MYTCP_STATE_CONNECT;
            printf(lcd_putc,"\nCONNECTING      ");
         }
         else if (TickGetDiff(currTick, lastTick) > (TICKS_PER_SECOND * 2)) {
            state=MYTCP_STATE_ARP_REQ;
         }
         break;

      case MYTCP_STATE_CONNECT:
         socket=TCPConnect(&remote, EXAMPLE_TCP_PORT);
         if (socket!=INVALID_SOCKET) {
            lastTick=TickGet();
            state=MYTCP_STATE_CONNECT_WAIT;
         }
         else {
            printf(lcd_putc,"\nSOCKET ERROR    ");
         }
         break;

      case MYTCP_STATE_CONNECT_WAIT:
         if (TCPIsConnected(socket)) {
            state=MYTCP_STATE_CONNECTED;
            printf(lcd_putc,"\nCONNECTED!      ");
         }
         else if (TickGetDiff(currTick, lastTick) > (TICKS_PER_SECOND * 10)) {
            state=MYTCP_STATE_FORCE_DISCONNECT;
         }
         break;

      case MYTCP_STATE_CONNECTED:
         if (TCPIsConnected(socket)) {
            dis=TCPConnectedTask(socket);
            if (dis) {
               state=MYTCP_STATE_DISCONNECT;
               lastTick=currTick;
            }
         }
         else {
            printf(lcd_putc,"\nDISCONNECTED    ");
            state=MYTCP_STATE_CONNECT;
         }
         break;

      case MYTCP_STATE_DISCONNECT:
         printf(lcd_putc,"\nDISCONNECTING   ");
         if (TCPIsPutReady(socket)) {
            state=MYTCP_STATE_FORCE_DISCONNECT;
         }
         else if (TickGetDiff(currTick, lastTick) > (TICKS_PER_SECOND * 10)) {
            state=MYTCP_STATE_FORCE_DISCONNECT;
         }
         break;

      case MYTCP_STATE_FORCE_DISCONNECT:
         TCPDisconnect(socket);
         state=MYTCP_STATE_CONNECT;
         break;
   }
}

void ServerAddrInit(void) {
   //IP address of the PC running TCPSERVER.EXE
   server.v[0]=192;
   server.v[1]=168;
   server.v[2]=100;
   server.v[3]=123;
}

char ppp_username[32];
char ppp_password[32];
char ppp_phonenumber[20];

void ISPInit(void) {
   sprintf(ppp_username, "  ");
   sprintf(ppp_password, "  ");
   sprintf(ppp_phonenumber, "5551234");
}

void main(void) {

   MODEM_RESP resp;
   
   printf("\r\n\nCCS TCP/IP TUTORIAL, EXAMPLE 13 (TCP CLIENT)\r\n");

   MACAddrInit();
   IPAddrInit();
   ISPInit();

   ServerAddrInit();

   init_user_io();

   lcd_init();

   printf(lcd_putc,"\fCCS TCP TUTORIAL\nINIT");

   StackInit();

   while(TRUE) {
   
    if (!ppp_is_connected() && !ppp_is_connecting()) {
         printf(lcd_putc,"\fDialing");
         resp=ppp_connect(ppp_username, ppp_password, ppp_phonenumber);
         if (resp==MODEM_BUSY) {
            printf(lcd_putc,"\fBusy Signal");
            delay_ms(2000);
         }
         else if (resp==MODEM_NO_DIALTONE) {
            printf(lcd_putc,"\fNo Dialtone");
            delay_ms(2000);
         }
         else if (resp!=MODEM_CONNECTED) {
            printf(lcd_putc,"\fDial Error");
            delay_ms(2000);
         }
         else {
            printf(lcd_putc,"\f%LUbps", connected_baudrate);
            printf(lcd_putc,"\nNegotiating PPP");
         }
      }
   
      StackTask();
      MyTCPTask();
   }
}
gokhangokcen



Joined: 30 Oct 2015
Posts: 4
Location: Turkey

View user's profile Send private message Send e-mail Visit poster's website

ccs c http client
PostPosted: Sat Oct 31, 2015 12:06 am     Reply with quote

hello, i have one question.

i need ccs c web client. My code is here:

main.c
Code:

#include <main.h>
#use rs232(BAUD=9600,XMIT=PIN_C6,RCV=PIN_C7,BITS=8,PARITY=N,STOP=1)

#define STACK_USE_CCS_HTTP_CLIENT

int1 g_MyHttpSending = FALSE;
char g_MyHttpResponse[254];
int deger;
long i;

#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(e)

/* The CCS HTTP Client library is very flexible in the manners of which
data can be sent to the remote HTTP server an how to read response
data from the remote HTTP server. See the ccs_http_client.h file for
a full documentation of this API. */
void MyHttpSend(void)
{
   // there are also non-ROM versions of these functions if you want to
   // dynamically control the parameters.
   HttpClientSetHostNameROM((rom char*)"192.168.1.106");
   HttpClientSetUrlROM((rom char*)"/a.asp");
   HttpClientSetHostPort(80);

   HttpClientStart();
}

void StackPrintfChanges(void)
{
   static enum {PRINT_INIT=0, PRINT_NO_MAC, PRINT_NO_DHCP, PRINT_IDLE} state=0;

   switch(state)
   {
      case PRINT_INIT:
         printf("\n\rNo MAC Link: %X:%X:%X:%X:%X:%X", MY_MAC_BYTE1, MY_MAC_BYTE2, MY_MAC_BYTE3, MY_MAC_BYTE4, MY_MAC_BYTE5, MY_MAC_BYTE6);
         state = PRINT_NO_MAC;
         break;

      case PRINT_NO_MAC:
         if (MACIsLinked())
         {
           #if defined(STACK_USE_DHCP_CLIENT)
            if (!DHCPIsEnabled(0))
           #else
            if (0)
           #endif
            {
               printf("\n\rDHCP Disabled: %u.%u.%u.%u", MY_IP_BYTE1, MY_IP_BYTE2, MY_IP_BYTE3, MY_IP_BYTE4);
               state = PRINT_IDLE;
            }
            else
            {
               printf("\n\rDHCP Not Bound");
               state = PRINT_NO_DHCP;
            }
         }
         break;

     #if defined(STACK_USE_DHCP_CLIENT)
      case PRINT_NO_DHCP:
         if (!MACIsLinked())
         {
            state = PRINT_INIT;
            break;
         }
         if (DHCPIsBound(0))
         {
            state = PRINT_IDLE;
            printf("\n\rDHCP Bound: %u.%u.%u.%u", MY_IP_BYTE1, MY_IP_BYTE2, MY_IP_BYTE3, MY_IP_BYTE4);
         }
         break;
     #endif

      case PRINT_IDLE:
         if (
               !MACIsLinked()
              #if defined(STACK_USE_DHCP_CLIENT)
               || (DHCPIsEnabled(0) && !DHCPIsBound(0))
              #endif
            )
         {
            state = PRINT_INIT;
         }
         break;
   }
}

void IPAddressInit(void)
{
   //MAC address of this unit
   MY_MAC_BYTE1=MY_DEFAULT_MAC_BYTE1;
   MY_MAC_BYTE2=MY_DEFAULT_MAC_BYTE2;
   MY_MAC_BYTE3=MY_DEFAULT_MAC_BYTE3;
   MY_MAC_BYTE4=MY_DEFAULT_MAC_BYTE4;
   MY_MAC_BYTE5=MY_DEFAULT_MAC_BYTE5;
   MY_MAC_BYTE6=MY_DEFAULT_MAC_BYTE6;

   //IP address of this unit
   MY_IP_BYTE1=MY_DEFAULT_IP_ADDR_BYTE1;
   MY_IP_BYTE2=MY_DEFAULT_IP_ADDR_BYTE2;
   MY_IP_BYTE3=MY_DEFAULT_IP_ADDR_BYTE3;
   MY_IP_BYTE4=MY_DEFAULT_IP_ADDR_BYTE4;

   //network gateway
   MY_GATE_BYTE1=MY_DEFAULT_GATE_BYTE1;
   MY_GATE_BYTE2=MY_DEFAULT_GATE_BYTE2;
   MY_GATE_BYTE3=MY_DEFAULT_GATE_BYTE3;
   MY_GATE_BYTE4=MY_DEFAULT_GATE_BYTE4;

   //subnet mask
   MY_MASK_BYTE1=MY_DEFAULT_MASK_BYTE1;
   MY_MASK_BYTE2=MY_DEFAULT_MASK_BYTE2;
   MY_MASK_BYTE3=MY_DEFAULT_MASK_BYTE3;
   MY_MASK_BYTE4=MY_DEFAULT_MASK_BYTE4;
}

void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_comparator(NC_NC_NC_NC);
setup_wdt(WDT_OFF);
setup_ccp1(CCP_OFF);

set_tris_a(0b00000010);
set_tris_b(0x00);
set_tris_c(0b10010000);
set_tris_d(0x00);
set_tris_e(0x00);

output_a(0x00);
output_b(0x00);
output_c(0x00);
output_d(0x00);
output_e(0x00);


   IPAddressInit();
   TickInit();
   enable_interrupts(GLOBAL);
   StackInit();


   while(TRUE)
   {

      StackTask();

      StackPrintfChanges();

      StackApplications();

      if (!input(PIN_A1))    // todo: specify action condition, like a button press
      {
         g_MyHttpSending = TRUE;
         MyHttpSend();
         output_high(PIN_B1);
         puts(g_MyHttpResponse);
         delay_ms(1000);
         output_low(PIN_B1);
         deger=HttpClientGetResult();
         putc(deger);
      }
      else if (!HttpClientIsBusy() && g_MyHttpSending)
      {
         g_MyHttpSending = FALSE;

         // todo: if you want to see the pass/fail of the request,
         //       use HttpClientGetResult()

         // todo: if you want to see the data read from the server,
         //       look at the g_MyHttpResponse[] string
      }
 i++;
      if(i>=1000)
      {
      output_toggle(PIN_B0);
      i=0;
      }
     
      if(g_MyHttpResponse[0]=='O')
      {output_high(PIN_B3);}
      else{output_low(PIN_B3);}
      //TODO: User Code
   }

}



main.h
Code:

#include <18F4620.h>
//#device ADC=10
#fuses HS,nowdt,nolvp,nodebug,noprotect,nomclr
/*
TCP/IP Stack enabled.
Many TCP/IP configuration settings (servers enabled, ports used,
etc) are defined in TCPIPConfig.h.
Many hardware configuration settings (SPI port and GPIO pins used)
are defined in HardwareProfile.h.
*/

#include "tcpip/p18cxxx.h"
#use delay(clock=10000000,crystal=10000000)


#define MIN(a,b)  ((a > b) ? b : a)

#include <stdint.h>
#include "tcpip/StackTsk2.h"
#include "tcpip/TCPIPConfig.h"
#include "tcpip/HardwareProfile.h"

typedef struct
{
   BYTE vSocketPurpose;
   BYTE vMemoryMedium;
   WORD wTXBufferSize;
   WORD wRXBufferSize;
} TCPSocketInitializer_t;

#if TCP_CONFIGURATION > 0
   TCPSocketInitializer_t TCPSocketInitializer[TCP_CONFIGURATION] =
   {
      #if defined(STACK_USE_CCS_HTTP2_SERVER)
         {TCP_PURPOSE_HTTP_SERVER, TCP_ETH_RAM, STACK_CCS_HTTP2_SERVER_TX_SIZE, STACK_CCS_HTTP2_SERVER_RX_SIZE},
      #endif
      #if defined(STACK_USE_SMTP_CLIENT)
         {TCP_PURPOSE_DEFAULT, TCP_ETH_RAM, STACK_CCS_SMTP_TX_SIZE, STACK_CCS_SMTP_RX_SIZE},
      #endif
      #if defined(STACK_USE_MY_TELNET_SERVER)
         {TCP_PURPOSE_TELNET, TCP_ETH_RAM, STACK_MY_TELNET_SERVER_TX_SIZE, STACK_MY_TELNET_SERVER_RX_SIZE},
      #endif
      #if defined(STACK_USE_CCS_HTTP_CLIENT)
         {TCP_PURPOSE_GENERIC_TCP_CLIENT, TCP_ETH_RAM, STACK_MY_HTTPC_TX_SIZE, STACK_MY_HTTPC_RX_SIZE},
      #endif
   };
#else
   #undef TCP_CONFIGURATION
   #define TCP_CONFIGURATION 1
   TCPSocketInitializer_t TCPSocketInitializer[TCP_CONFIGURATION] =
   {
      {TCP_PURPOSE_DEFAULT, TCP_ETH_RAM, 250, 250}
   };
#endif

#include "tcpip/StackTsk2.c"



I use microcontroller 18f4620 series and enc28j60. i click button but only bad request return. This return answer is "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/" and return error code is "05" error code here.

ccs_http_client.h
Code:

int1 HttpClientIsBusy(void);

typedef enum
{
   // OK
   HTTP_CLIENT_EC_OK = 0,
   
   // HttpClientStart() hasn't been called yet
   HTTP_CLIENT_EC_IDLE = 1,
   
   // TCPOpen() returned an invalid socket.  this will happen if there are no
   // sockets left.  increase the number of scokets in TCPSocketInitializer[].
   // use TCP_PURPOSE_GENERIC_TCP_CLIENT
   HTTP_CLIENT_EC_NO_SOCKETS = 2,
   
   // a connection could not be established with the remote host.
   // check hostname and port settings.  if using a hostname and
   // not an ip, then check that a valid DNS is configured.
   // check that arp is able resolve the MAC address.
   // if the remote host is not on the local network, make sure that gateway
   // address is configured correctly.
   HTTP_CLIENT_EC_NO_CONNECTION = 3,
   
   // the remote HTTP server terminated the connection prematurely.
   HTTP_CLIENT_EC_SERVER_TERMINATED = 4,
   
   // HTTP server didn't respond with HTTP/1.x yyy status code
   HTTP_CLIENT_EC_NO_HTTP_STATUS_CODE = 5,

   // 200 will be replaced with 0 (HTTP_CLIENT_EC_OK), so you will not actually
   // see this value (HTTP_CLIENT_EC_FILE_FOUND) retured by the library
   HTTP_SERVER_STATUS_CODE_FILE_FOUND = 200,
 
   // any number not shown here is a response code from the HTTP server.
   // for example, 500 would be a server side error, 404 would be file not
   // found.
   HTTP_SERVER_STATUS_CODE_FILE_NOT_FOUND = 404
} http_client_ec_t;


What's my problem ? Please help me :(
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Sat Oct 31, 2015 7:06 am     Reply with quote

I would suggest dropping the ccs/PIC stack all together.

Telit gsm/gprs modules have a tcp/ip stack that works via AT commands and regular 9600 serial. I'm sure other modems do as well.

Its easy, and it offloads everything OFF your PIC.

OR

go with a 3 dollar ESP8266, which is EVEN EASIER.....


I've got about 16 loggers on the field with the ESP8266 loading data via wifi and GPRS backup for when the wifi is down.

I tried the TCP/IP stack and it is close to impossible.
It took me 1 day to get both esp and gsm modems online.... and it was a breeze.


Save yourself!

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
philipelim



Joined: 05 Jan 2016
Posts: 2

View user's profile Send private message

PostPosted: Tue Jan 05, 2016 5:31 pm     Reply with quote

Gabriel wrote:
[...] go with a 3 dollar ESP8266, which is EVEN EASIER.....

I've got about 16 loggers on the field with the ESP8266 loading data via wifi and GPRS backup for when the wifi is down.

I tried the TCP/IP stack and it is close to impossible.
It took me 1 day to get both esp and gsm modems online.... and it was a breeze.


Save yourself!

G.


Hello Gabriel, I need your help with ESP8266. I managed to set up on my home network, I found that the ESP was connected, asking for the IP for AT and everything right command, but I think I wrongly understood the string sending mode to be viewed by browser.
Example:
I make a printf in a web page format, however when I try to view in the browser, it returns me: page not found ...

Could you explain to me how I can do this ? With Arduino I find enough examples, but with the PIC I do not find anything ... almost.
_________________
Phill Lima
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Jan 06, 2016 3:51 am     Reply with quote

I'm not sure what you are trying to do.

You want to see your PIC data on your computer browser?

Then you should configure your ESP as an AP and connect to it.

Ive only done clients, but the Arduino examples on the net are very easy to "translate" to CCS.

Basically all you need to do is a bunch of printfs.

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
philipelim



Joined: 05 Jan 2016
Posts: 2

View user's profile Send private message

PostPosted: Wed Jan 06, 2016 6:36 pm     Reply with quote

As I understand it in some examples in Arduino, have a possibility of a web page by accessing the IP ESP, read information written by the pic. I made the printf creating a html structure to be viewed in the browser, however it did not work ...

My steps, the ESP connected to my router, then start my printf, example:
Code:
printf ("<HTML> <HEAD> <TITLE> basic HTML </ TITLE> </ HEAD> <BODY> <H1> This is the first level heading </ H1> Common Text below cabeƧalho. Este is the first. paragraph <P> And this is the second <P> </ BODY> </ HTML> 2.1lu% \ r \ n ", temperature).;

My question is what do I need to do before sending the data ? I need to set up the ESP, and how should I begin and end the data ? I know I'm pretty lost on the subject, however as you can explain to me it will be very useful, thank you for attention Gabriel ...

The name Gabriel is quite common where I live, you happen to speak Portuguese?
_________________
Phill Lima
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Thu Jan 07, 2016 1:23 am     Reply with quote

http://www.arduinesp.com/wifiwebserver

This is pretty much what you want... code and all.
You just need to make that CCS.



I'm from Panama, i speak spanish... thus the latin name.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group