Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

AT90CAN128 (CAN BUS) I2C with ATMEGA32-web server for monitor temp and current

Status
Not open for further replies.

noel_t

Junior Member level 3
Joined
May 31, 2011
Messages
30
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,288
Activity points
1,592
Hi there,



Temperature and current from cell are monitored through CAN BUS by AT90CAN128.
AT90CAN128 is connected by I2C to ATMEGA32.
I need to implement a simple web server on ATMEGA32 to display these two (2) measurement data on the web browser.

DO you have any idea if someone has already approach such a project?
Could you please help me that how i can approach this problem?
 

Temperature and current from cell are monitored through CAN BUS by AT90CAN128.
AT90CAN128 is connected by I2C to ATMEGA32.
I need to implement a simple web server on ATMEGA32 to display these two (2) measurement data on the web browser.
What is the refresh rate of the measurement data,you intend to keep for this system..?
How will you be connecting the ATMEGA 32 to the server..?
 
  • Like
Reactions: noel_t

    noel_t

    Points: 2
    Helpful Answer Positive Rating
What is the refresh rate of the measurement data,you intend to keep for this system..?
How will you be connecting the ATMEGA 32 to the server..?
To be honest, I dont know how much is therefresh rate.
Atmega32 is connected by ethernet shield ENC20J60 to the network.
 

Frankly, I not sure the ATMega32 is really required.

There are several chipsets and embedded network modules which offer Ethernet connectivity as well as an embedded TCP/IP stack.

The AT90CAN128 has 128KB of flash, probably more than enough to store a several webpages and if not you can always add a serial EPROM to the design for more storage resources.

Companies like WIZnet, Lantronix and Moxa, all offer Ethernet Modules with embedded TCP/IP stacks, therefore the module carries most of the work load, the AT90CAN128 simple needs to send a formatted webpage for the embedded webserver to transmit.

WIZnet also offers the chipsets at very reasonable prices if you want to incorporate them into your design, rather than utilize a self-contained module.

WIZnet Ethernet/Network Modules



Moxa Embedded Networking

And if you are up to the challenge, developing a PC App which connects to the embedded Ethernet module using socket API is also an option.


BigDog
 
  • Like
Reactions: noel_t

    noel_t

    Points: 2
    Helpful Answer Positive Rating
Frankly, I not sure the ATMega32 is really required.

There are several chipsets and embedded network modules which offer Ethernet connectivity as well as an embedded TCP/IP stack.

The AT90CAN128 has 128KB of flash, probably more than enough to store a several webpages and if not you can always add a serial EPROM to the design for more storage resources.

Companies like WIZnet, Lantronix and Moxa, all offer Ethernet Modules with embedded TCP/IP stacks, therefore the module carries most of the work load, the AT90CAN128 simple needs to send a formatted webpage for the embedded webserver to transmit.

WIZnet also offers the chipsets at very reasonable prices if you want to incorporate them into your design, rather than utilize a self-contained module.

WIZnet Ethernet/Network Modules



Moxa Embedded Networking

And if you are up to the challenge, developing a PC App which connects to the embedded Ethernet module using socket API is also an option.


BigDog

Thank you for the information.
Could you please have a look on my codes and see how I can implement the common gateway interface (cgi) on ATMEGA32.
Currently I can send CAN messages to ATMEGA32. ENC20j60 also works fine. I just need to show this coming messages from AT90CAN128 on the web browser by making a web server. (CAN messages can be measurement data in the future)
I am still thinking how I can combine these functions only on AT90CAN128. But at this stage it is not that important. I just want to implement this web server on ATMEGA32.
 

Attachments

  • proj2.rar
    139.5 KB · Views: 90

You might take a look at the following project/library which implements a simple HTTP server on an ATMega88, so you should not have any major issues porting it to an ATMega32:

HTTP/TCP with an atmega88 microcontroller (AVR web server)


I've also attached an example project for the ATMega32, originally from AVR Portal, which implements a simple HTTP server using the ENC28J60.


BigDog
 

Attachments

  • AVR-WEB-SERVER.zip
    385.7 KB · Views: 97
  • Like
Reactions: noel_t

    noel_t

    Points: 2
    Helpful Answer Positive Rating
Do you have any idea why I am getting this Error while building-compiling my code?I have ATMEL studio 6.1 service pack 2

- - - Updated - - -

You might take a look at the following project/library which implements a simple HTTP server on an ATMega88, so you should not have any major issues porting it to an ATMega32:

HTTP/TCP with an atmega88 microcontroller (AVR web server)


I've also attached an example project for the ATMega32, originally from AVR Portal, which implements a simple HTTP server using the ENC28J60.


BigDog
Do you have any idea why I am getting this Error while building-compiling my code?I have ATMEL studio 6.1 service pack 2
 

Attachments

  • Capture.JPG
    Capture.JPG
    25.3 KB · Views: 94
  • Capture2.JPG
    Capture2.JPG
    25 KB · Views: 95

It's difficult to say with certainty, however it maybe due to a symbol/name mismatch.

You might have to change SIG_2WIRE_SERIAL to the current symbol/name used in I2C.c or its header file.

Take a look/search in the appropriate source files.

If I have time later tonight, I take a look at this end.


BigDog
 

It's difficult to say with certainty, however it maybe due to a symbol/name mismatch.

You might have to change SIG_2WIRE_SERIAL to the current symbol/name used in I2C.c or its header file.

Take a look/search in the appropriate source files.

If I have time later tonight, I take a look at this end.


BigDog

I already fixed this problem, in the I2c.c file I downloaded form github, there was not such a SIG_2WIRE_SERIAL and it worked for me.
It seems ATMEL STUDIO 6.2 doesn't support or compile previous commands or datatype. Soon I write to you about my progress :)
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#define F_CPU 16000000UL //Quartz clock frequency 16 MHz
 
#include <string.h> //libs
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <avr/interrupt.h>
 
#include "i2c.h" //header file for I2C interface functions
#include "UART_routines.h" //header file for UART interface functions
#include "avr_compat.h" //header file for diverse uC reductions/simplifications
#include "enc28j60.h" //header file for Ethernet-Chip define
#include "ip_arp_udp_tcp.h" //header file for describing IP, ARP, UDP, TCP protocols on uC
#include "net.h" //header file for some defines
 
//-----------------------------------I2C-----------------------------------//
#define LOCAL_ADDR 0x07 //I2C address of this uC, I2C slave
#define TARGET_ADDR 0xA0 //I2C address of target uC, I2C master
//-----------------------------------I2C-----------------------------------//
 
//-----------------------------------ENC-----------------------------------//
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24}; // MAC address of uC
static uint8_t myip[4] = {192,168,0,25}; // IP address of uC
#define MYWWWPORT 80 // listen port for tcp/www
#define MYUDPPORT 1200 // listen port for udp
#define BUFFER_SIZE 550 // buffer for messages
static uint8_t buf[BUFFER_SIZE+1];
static int nPingCount = 0, nAccessCount = 0; // Global counters
// the password string (only the first 5 char checked), (only a-z,0-9,_ characters):
static char password[]="secret"; // must not be longer than 9 char
//-----------------------------------ENC-----------------------------------//
 
//LED control defines
#define LED_GREEN_ON SETBIT(PORTA,0)
#define LED_GREEN_OFF CLEARBIT(PORTA,0)
#define LED_RED_ON SETBIT(PORTA,1)
#define LED_RED_OFF CLEARBIT(PORTA,1)
 
//unsigned char BUF[50];
unsigned char BUF123[50];
unsigned char receiveData[10];
u08 receiveDataLength;
int data; data = 0; 
 
// slave operations 
// calls when message per I2C was received
void i2cSlaveReceiveService(u08 receiveDataLength, u08* receiveData)
{
    LED_RED_ON; //LED ON
    //sprintf(BUF,"slave received: %s (length %d)",receiveData,receiveDataLength);
    sprintf(BUF123,"%s",receiveData); 
    usart_write_str(BUF123);TX_NEWLINE; //show received message in UART-software (Terminal)
    //_delay_ms(1); //when needed, some delay
    LED_RED_OFF;  //LED off
}
 
//function for find char in string
char* strfind(char *dest, const char* sour, int nStart, int nEnd)
{
    for (int i = nStart; i <= nEnd; i++)
        dest[i - nStart] = sour[i];
    dest[nEnd - nStart + 1] = '\0';
    return dest;
}
 
// function for check password in string from udp client
uint8_t verify_password(char *str)
{
        // the first characters of the received string are
        // a simple password/cookie:
        if (strncmp(password,str,5)==0){
                return(1);
        }
        return(0);
}
 
int main(void)
{
    _delay_ms(100); // some delay for VCC stabilization
    
    DDRA = 0x03; // PA0, PA1 - LEDs, pins are out
    LED_GREEN_ON; // LED ON
    
    usart_init(9600); /*initialize UART with baud rate 9600*/
    
    //some variables for udp-server
    uint8_t cmd_pos=0;
    char cmdval;
    char *p = NULL;
    int count_num;
    char str999[30];
    unsigned short data_num;
    char data_num_str[30];
    uint8_t payloadlen=0;
    char str[30];
    
    /*initialize enc28j60*/
    uint16_t plen, dat_p;
    enc28j60Init(mymac);
    enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
    enc28j60PhyWrite(PHLCON,0x476);
    _delay_ms(100);
    init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT);
    
    // initialize i2c function library 
    void i2cInit(void); 
    // set local device address and allow response to general call 
    void i2cSetLocalDeviceAddr(u08 deviceAddr, u08 genCallEn); 
    // string for out, to test running of UART and I2C
    usart_write_str("*************TWI SLAVE RUNNING*************");TX_NEWLINE;
    // start of interrupt for receiving of I2C messages
    void i2cSetSlaveReceiveHandler( i2cSlaveReceiveService ); 
    
    while(1)
    {
        // get the next new packet:
        plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
        
        if(eth_type_is_arp_and_my_ip(buf,plen))
        {
            make_arp_answer_from_request(buf);
            continue;
        }
        
        if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V)
        {
            // a ping packet, let's send pong
            nPingCount++;
            make_echo_reply_from_request(buf,plen);
            continue;
        }
        
        // tcp port www start, compare only the lower byte
        if (buf[IP_PROTO_P]==IP_PROTO_TCP_V && buf[TCP_DST_PORT_H_P]== 0 && buf[TCP_DST_PORT_L_P]== MYWWWPORT) 
        {
            if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V)
            {
                // make_tcp_synack_from_syn does already send the syn,ack
                make_tcp_synack_from_syn(buf);
                continue;
            }
            if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V)
            {
                init_len_info(buf); // init some data structures
                dat_p = get_tcp_data_pointer();
                if (dat_p==0) // we can possibly have no data, just ack:
                {
                    if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V)
                    {
                        make_tcp_ack_from_any(buf);
                    }
                    continue;
                }
                // count page requests
                nAccessCount++;
                
                // generate WEBPAGE
                char szS1[200] = {0};
                sprintf(szS1, BUF123);
                    
                plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));
                //plen=fill_tcp_data_p(buf,plen,PSTR("<html><head><title>Webserver on ATMega32</title></head><body bgcolor=\"#CCFFFF\"><font face=\"tahoma\" color=\"#000033\"><strong>Webserver running on Atmega32</strong>"));
                //plen=fill_tcp_data_p(buf,plen,PSTR("<html>"));
                //active data
                plen=fill_tcp_data(buf,plen,szS1);
                //plen=fill_tcp_data_p(buf,plen,PSTR("</html>"));
                //plen=fill_tcp_data_p(buf,plen,PSTR("</font></body></html>"));
                // send data over TCP IP
                make_tcp_ack_from_any(buf);
                make_tcp_ack_with_data(buf,plen);
            }
        }
        
        // udp start, we listen on udp port 1200=0x4B0
        if (buf[IP_PROTO_P]==IP_PROTO_UDP_V&&buf[UDP_DST_PORT_H_P]==4&&buf[UDP_DST_PORT_L_P]==0xb0)
        {
            payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
            // you must sent a string starting with v
            // e.g udpcom version 10.0.0.24
            if (verify_password((char *)&(buf[UDP_DATA_P])))
            {
                // find the first comma which indicates 
                // the start of a command:
                cmd_pos=0;
                while(cmd_pos<payloadlen)
                {
                    cmd_pos++;
                    if (buf[UDP_DATA_P+cmd_pos]==',')
                    {
                        cmd_pos++; // put on start of cmd
                        break;
                    }
                }
                // a command is one char and a value. At
                // least 3 characters long. It has an '=' on
                // position 2:
                if (cmd_pos<2 || cmd_pos>payloadlen-3 || buf[UDP_DATA_P+cmd_pos+1]!='=')
                {
                    strcpy(str,"e=no_cmd");
                    goto ANSWER;
                }
                // supported command is t=?
                // t=2 t=1 are data
                if (buf[UDP_DATA_P+cmd_pos]=='t')
                {
                    cmdval=buf[UDP_DATA_P+cmd_pos+2];
                    sprintf(str999,"");
                    if(cmdval=='1'||cmdval=='2'||cmdval=='3'||cmdval=='4'||cmdval=='5')
                    {
                        count_num = (int)cmdval;
                        
                        if (cmdval=='1') 
                        {
                            p = strfind(str999,buf,UDP_DATA_P+cmd_pos+2+1+1,UDP_DATA_P+cmd_pos+2+1+1);
                            
                        }
                        if (cmdval=='2') 
                        {
                            p = strfind(str999,buf,UDP_DATA_P+cmd_pos+2+1+1,UDP_DATA_P+cmd_pos+2+1+2);
                        }               
                
                        data_num = atoi(str999);
                        
                        sprintf(data_num_str,"%d",data_num);
                        usart_write_str(data_num_str);TX_NEWLINE; //show received message in UART-software (Terminal)
                        
                        goto ANSWER;
                    }
                    if(cmdval=='?')
                    {
                        strcpy(str,"Status: OK");
                        goto ANSWER;
                    }
                    else
                    {
 
                    }
                }
                strcpy(str,"e=no_such_cmd");
                goto ANSWER;
            }
            strcpy(str,"e=invalid_pw");
 
ANSWER:
            make_udp_reply_from_request(buf,str,strlen(str),MYUDPPORT);
        }
        
    }
}



- - - Updated - - -

I already fixed this problem, in the I2c.c file I downloaded form github, there was not such a SIG_2WIRE_SERIAL and it worked for me.
It seems ATMEL STUDIO 6.2 doesn't support or compile previous commands or datatype. Soon I write to you about my progress :)

Could you please have a look on the code I have for ATMEGA32-(it is my slave microcontroller). I observed several sample code in the internet but i am still confused. (remember the measurement data are coming from AT90CAN128 microcontroller (MASTER) which is connected by I2C with ATMEGA32 and it is working now)
thank you!
 
Last edited by a moderator:

I use a PCAN-View USB analyzer software, so i send the message from the computer to the can terminal which is connected to a CAN transceiver(PEAK) that is linked to the AT90CAN128. So, what i see when i send message from the computer, the counter of the transmitted messages increases by one and I see on the trace log this message as well.

I don't get any Rx flag because I don't need to send any message back from AT90CAN128 to the computer as I need in this project only to monitor the measurement data from sensors through CANBUS. Therefore, so far every thing seems to be OK.
-----------------------
ATmega32+ENC28J60:
I implemented the TCP/IP stack from tuxgraphics.com and I can load a simple web browser by typing the ip address of the web server in the web browser.
AT90CAN128 and ATmega32 are also physically connected by I2C as you can see in the pictures.

Now my problem is how to modify my program accordingly to show these CAN messages (assuming measurement data) in my web browser.
 

Attachments

  • troubleshoot.rar
    2.1 MB · Views: 84

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top