wiznet-stm32 nucleo

Dependencies:   mbed WIZnet_Library_W5100_W3150

Committer:
nojetry
Date:
Thu Jul 23 06:58:07 2020 +0000
Revision:
0:a7892b6808b1
Test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nojetry 0:a7892b6808b1 1 #include "mbed.h"
nojetry 0:a7892b6808b1 2 #include "WIZnetInterface.h"
nojetry 0:a7892b6808b1 3
nojetry 0:a7892b6808b1 4 #define ST_NUCLEO
nojetry 0:a7892b6808b1 5 #define MYPORT 80
nojetry 0:a7892b6808b1 6
nojetry 0:a7892b6808b1 7 const char *IP_Addr = "10.47.88.197";
nojetry 0:a7892b6808b1 8 const char *IP_Subnet = "255.255.255.0";
nojetry 0:a7892b6808b1 9 const char *IP_Gateway = "10.47.88.1";
nojetry 0:a7892b6808b1 10 unsigned char MAC_Addr[6] = {0x3c, 0xe1, 0xa1, 0xc5, 0x2c, 0xfa};
nojetry 0:a7892b6808b1 11
nojetry 0:a7892b6808b1 12 Serial pc(USBTX, USBRX);
nojetry 0:a7892b6808b1 13 AnalogIn adc_temp(PA_0);
nojetry 0:a7892b6808b1 14 DigitalOut myled(LED2);
nojetry 0:a7892b6808b1 15 DigitalOut enable_pin(PC_8);
nojetry 0:a7892b6808b1 16
nojetry 0:a7892b6808b1 17
nojetry 0:a7892b6808b1 18
nojetry 0:a7892b6808b1 19 SPI spi(PB_15,PB_14,PB_13); //(mosi, miso, sclk, ssel=nc)
nojetry 0:a7892b6808b1 20 WIZnetInterface ethernet(&spi,PC_6,PC_5); //(spi* , cs , reset)
nojetry 0:a7892b6808b1 21
nojetry 0:a7892b6808b1 22 char page_length[]= {"\r\nHTTP/1.0 200 OK\r\nContent-Type: text/html\r\nContent-Length: 2400\r\n\r\n"};
nojetry 0:a7892b6808b1 23 char page_head []= {"<!doctype html>\n\t<head>\n\t\t<style type=\"text/css\">\n\t\t\t.margem_laranja{\n\t\t\t\twidth: 98%;\n\t\t\t\theight: 40px;\n\t\t\t\tbackground-color:#F5B317;\n\t\t\t\tcolor: #FFFFFF;\n\t\t\t}\n\t\t\t.margem_cinza{\n\t\t\t\twidth: 98%;\n\t\t\t\theight: 40px;\n\t\t\t\tbackground-color:#6F6B5F;\n\t\t\t\tcolor: #FFFFFF;\n\t\t\t}\n\t\t\tbutton{\n\t\t\t\tfont-family:verdana;\n\t\t\t\tfont-size:10pt;\n\t\t\t\tbackground-color:#B0F0D2;\n\t\t\t\tborder: 50pt;\n\t\t\t\tborder-radius: 15px;\n\t\t\t\theight:15pt;\n\t\t\t\twidth:50pt;\n\t\t\t}\n\t\t</style>\n\t\t<title>Webserver</title>\n\t\t<meta http-equiv=\"refresh\" content=\"4\">\n\t\t<meta name=\"viewport\" content=\"width=320\">\n\t\t<meta name=\"viewport\" content=\"width=device-width\">\n\t\t<meta charset=\"utf-8\">\n\t\t<meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\">\n\t</head>"};
nojetry 0:a7892b6808b1 24 char page_body []= {"<body bgcolor=\"#E0FFFF\">\n\t\t<center>\n\t\t<div class=\"margem_laranja\"></div>\n\t\t<font size=\"7\" face=\"verdana\" color=\"#2E2E2E\">ARM Webserver </font>\n\t\t<br /><br /><br />\n\t\t<font size=\"5\" face=\"verdana\" color=\"black\">Temperature</font><br /><br />\n\t\t<font size=\"7\" face=\"verdana\" color=\"black\">"};
nojetry 0:a7892b6808b1 25 char page_temp []={" "};
nojetry 0:a7892b6808b1 26 char page_body_2[]= {" °C</font><br /><br />\n\t\t<font size=\"5\" face=\"verdana\" color=\"black\">LED</font><br /><br />\n\t\t<form action=\"1\" method=\"get\">\n\t\t\t<button type=submit style=\"width:200px;\" id=\"bot\" >LED on</button>\n\t\t</form><br />\n\t\t<form action=\"0\" method=\"get\">\n\t\t\t<button type=submit style=\"width:200px;\" id=\"bot\" >LED off</button>\n\t\t</form><br /><br />\n\t\t<font size=\"2\" face=\"verdana\" color=\"black\">Webserver on STM32 Nucleo with ethernet shield from Wiznet NM1070B+/W3150 </font><br /><br />\n\t\t<font size=\"2\" face=\"verdana\" color=\"black\">Made by: Jens Trydal</font><br />\n\t\t<font size=\"2\" face=\"verdana\" color=\"black\">Email: jens.trydal@hotmail.com</font><br />\n\t\t<font size=\"2\" face=\"verdana\" color=\"black\">-</font><br />\n\t</body>\n</html>\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"};
nojetry 0:a7892b6808b1 27 char get_buf []= {" "};
nojetry 0:a7892b6808b1 28
nojetry 0:a7892b6808b1 29 int get_request (char* buf_get, int size,TCPSocketConnection& client){
nojetry 0:a7892b6808b1 30
nojetry 0:a7892b6808b1 31 char buffer[300];
nojetry 0:a7892b6808b1 32 client.receive(buffer, sizeof(buffer) );
nojetry 0:a7892b6808b1 33 int get_size=0;
nojetry 0:a7892b6808b1 34 for(int i=0, k=0; i<sizeof(buffer) && k < 2; i++) {
nojetry 0:a7892b6808b1 35 if(buffer[i]==' ') {
nojetry 0:a7892b6808b1 36 k++;
nojetry 0:a7892b6808b1 37 }
nojetry 0:a7892b6808b1 38 if (k==1) {
nojetry 0:a7892b6808b1 39 buf_get[get_size] = buffer[i];
nojetry 0:a7892b6808b1 40 get_size++;
nojetry 0:a7892b6808b1 41 }
nojetry 0:a7892b6808b1 42 }
nojetry 0:a7892b6808b1 43 return get_size;
nojetry 0:a7892b6808b1 44 }
nojetry 0:a7892b6808b1 45
nojetry 0:a7892b6808b1 46 int main(){
nojetry 0:a7892b6808b1 47
nojetry 0:a7892b6808b1 48 enable_pin = 1;
nojetry 0:a7892b6808b1 49 pc.baud(115200);
nojetry 0:a7892b6808b1 50 pc.printf("Starting\r\n");
nojetry 0:a7892b6808b1 51
nojetry 0:a7892b6808b1 52 while(1) {
nojetry 0:a7892b6808b1 53 int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
nojetry 0:a7892b6808b1 54 if (!ret) {
nojetry 0:a7892b6808b1 55 pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
nojetry 0:a7892b6808b1 56 ret = ethernet.connect();
nojetry 0:a7892b6808b1 57 if (!ret) {
nojetry 0:a7892b6808b1 58 pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
nojetry 0:a7892b6808b1 59 ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
nojetry 0:a7892b6808b1 60 } else {
nojetry 0:a7892b6808b1 61 pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
nojetry 0:a7892b6808b1 62 exit(0);
nojetry 0:a7892b6808b1 63 }
nojetry 0:a7892b6808b1 64 } else {
nojetry 0:a7892b6808b1 65 pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
nojetry 0:a7892b6808b1 66 exit(0);
nojetry 0:a7892b6808b1 67 }
nojetry 0:a7892b6808b1 68
nojetry 0:a7892b6808b1 69 TCPSocketServer server;
nojetry 0:a7892b6808b1 70 server.bind(MYPORT);
nojetry 0:a7892b6808b1 71 server.listen();
nojetry 0:a7892b6808b1 72
nojetry 0:a7892b6808b1 73 while (1) {
nojetry 0:a7892b6808b1 74 pc.printf("\nWait for new connection...\r\n");
nojetry 0:a7892b6808b1 75 TCPSocketConnection client;
nojetry 0:a7892b6808b1 76 server.accept(client);
nojetry 0:a7892b6808b1 77 client.set_blocking(false, 0); // Timeout=0.
nojetry 0:a7892b6808b1 78 pc.printf("Connection from: %s\r\n", client.get_address());
nojetry 0:a7892b6808b1 79 int temp = (adc_temp.read()*10);
nojetry 0:a7892b6808b1 80 page_temp [0]= (adc_temp.read()*10) +'0';
nojetry 0:a7892b6808b1 81 page_temp [1]= ((adc_temp.read()*100)-temp*10) +'0' ;
nojetry 0:a7892b6808b1 82
nojetry 0:a7892b6808b1 83 if (client.is_connected() == true){
nojetry 0:a7892b6808b1 84
nojetry 0:a7892b6808b1 85 int j = get_request(get_buf , sizeof(get_buf), client);
nojetry 0:a7892b6808b1 86
nojetry 0:a7892b6808b1 87 if(client.is_fin_received()) {
nojetry 0:a7892b6808b1 88 client.close();
nojetry 0:a7892b6808b1 89 }
nojetry 0:a7892b6808b1 90
nojetry 0:a7892b6808b1 91 client.send (page_length, sizeof(page_length));
nojetry 0:a7892b6808b1 92 client.send (page_head, sizeof(page_head));
nojetry 0:a7892b6808b1 93 client.send (page_body, sizeof(page_body));
nojetry 0:a7892b6808b1 94 client.send (page_temp, sizeof(page_temp));
nojetry 0:a7892b6808b1 95 client.send (page_body_2, sizeof(page_body_2));
nojetry 0:a7892b6808b1 96
nojetry 0:a7892b6808b1 97 pc.printf("get_buf[2] = %c \n", get_buf[2] );
nojetry 0:a7892b6808b1 98 if(get_buf[2] == '1'){
nojetry 0:a7892b6808b1 99 pc.printf("LED on \n");
nojetry 0:a7892b6808b1 100 myled = 1;
nojetry 0:a7892b6808b1 101 }else if(get_buf[2] == '0'){
nojetry 0:a7892b6808b1 102 pc.printf("LED off \n");
nojetry 0:a7892b6808b1 103 myled = 0;
nojetry 0:a7892b6808b1 104 }
nojetry 0:a7892b6808b1 105
nojetry 0:a7892b6808b1 106 pc.printf("print \n");
nojetry 0:a7892b6808b1 107 for(int i=0 ; i< j ; i++) {
nojetry 0:a7892b6808b1 108 pc.printf("%c",get_buf[i]);
nojetry 0:a7892b6808b1 109 }
nojetry 0:a7892b6808b1 110 pc.printf("\n");
nojetry 0:a7892b6808b1 111
nojetry 0:a7892b6808b1 112 }
nojetry 0:a7892b6808b1 113 pc.printf("\n Disconnected.\r\n");
nojetry 0:a7892b6808b1 114 }
nojetry 0:a7892b6808b1 115 }
nojetry 0:a7892b6808b1 116 }