1
+ /*
2
+ Port of WebServer on FreeRTOS
3
+
4
+ A simple web server that shows the value of the analog input pins.
5
+ 2 task are created:
6
+ Ethernet: to manage the server
7
+ Analog to read input values
8
+
9
+ Circuit:
10
+ STM32 board with Ethernet support
11
+ Analog inputs attached to pins A0 through A5 (optional)
12
+
13
+ created 18 Dec 2009
14
+ by David A. Mellis
15
+ modified 9 Apr 2012
16
+ by Tom Igoe
17
+ modified 02 Sept 2015
18
+ by Arturo Guadalupi
19
+ modified 23 Jun 2017
20
+ by Wi6Labs
21
+ modified 1 Jun 2018
22
+ by sstaub
23
+ */
24
+
25
+ #include < LwIP.h>
26
+ #include < STM32Ethernet.h>
27
+ #include < STM32FreeRTOS.h>
28
+
29
+ // Enter an IP address for your controller below.
30
+ // The IP address will be dependent on your local network:
31
+ IPAddress ip (192 , 168 , 0 , 177 );
32
+
33
+ // Initialize the Ethernet server library
34
+ // with the IP address and port you want to use
35
+ // (port 80 is default for HTTP):
36
+ EthernetServer server (80 );
37
+
38
+ #define ANALOG_CHANEL_NUMBER 6
39
+ int sensorReading[ANALOG_CHANEL_NUMBER];
40
+
41
+ void taskAnalog (void * arg) {
42
+ UNUSED (arg);
43
+ while (1 ) {
44
+ for (int analogChannel = 0 ; analogChannel < ANALOG_CHANEL_NUMBER; analogChannel++) {
45
+ sensorReading[analogChannel] = analogRead (analogChannel);
46
+ }
47
+ vTaskDelay (1000 ); // read Analog every seconds
48
+ }
49
+ }
50
+
51
+ // task code
52
+ void taskETH (void * arg) {
53
+ UNUSED (arg);
54
+ // start the Ethernet connection and the server:
55
+ Ethernet.begin (ip);
56
+ server.begin ();
57
+ Serial.print (" server is at " );
58
+ Serial.println (Ethernet.localIP ());
59
+
60
+ while (1 ) {
61
+ // listen for incoming clients
62
+ EthernetClient client = server.available ();
63
+ if (client) {
64
+ Serial.println (" new client" );
65
+ // an http request ends with a blank line
66
+ bool currentLineIsBlank = true ;
67
+ while (client.connected ()) {
68
+ if (client.available ()) {
69
+ char c = client.read ();
70
+ Serial.write (c);
71
+ // if you've gotten to the end of the line (received a newline
72
+ // character) and the line is blank, the http request has ended,
73
+ // so you can send a reply
74
+ if (c == ' \n ' && currentLineIsBlank) {
75
+ // send a standard http response header
76
+ client.println (" HTTP/1.1 200 OK" );
77
+ client.println (" Content-Type: text/html" );
78
+ client.println (" Connection: close" ); // the connection will be closed after completion of the response
79
+ client.println (" Refresh: 5" ); // refresh the page automatically every 5 sec
80
+ client.println ();
81
+ client.println (" <!DOCTYPE HTML>" );
82
+ client.println (" <html>" );
83
+ // output the value of each analog input pin
84
+ for (int analogChannel = 0 ; analogChannel < ANALOG_CHANEL_NUMBER; analogChannel++) {
85
+ client.print (" analog input " );
86
+ client.print (analogChannel);
87
+ client.print (" is " );
88
+ client.print (sensorReading[analogChannel]);
89
+ client.println (" <br />" );
90
+ }
91
+ client.println (" </html>" );
92
+ break ;
93
+ }
94
+ if (c == ' \n ' ) {
95
+ // you're starting a new line
96
+ currentLineIsBlank = true ;
97
+ } else if (c != ' \r ' ) {
98
+ // you've gotten a character on the current line
99
+ currentLineIsBlank = false ;
100
+ }
101
+ }
102
+ }
103
+ // give the web browser time to receive the data
104
+ delay (1 );
105
+ // close the connection:
106
+ client.stop ();
107
+ Serial.println (" client disconnected" );
108
+ }
109
+ }
110
+ }
111
+
112
+ void setup () {
113
+ // Open serial communications and wait for port to open:
114
+ Serial.begin (9600 );
115
+ while (!Serial) {
116
+ ; // wait for serial port to connect. Needed for native USB port only
117
+ }
118
+
119
+ portBASE_TYPE s = xTaskCreate (taskETH, NULL , 200 , NULL , 1 , NULL );
120
+ if (s != pdPASS) {
121
+ printf (" Ethernet task creation failed\n " );
122
+ while (1 );
123
+ }
124
+
125
+ s = xTaskCreate (taskAnalog, NULL , 200 , NULL , 2 , NULL );
126
+ if (s != pdPASS) {
127
+ printf (" Analog task creation failed\n " );
128
+ while (1 );
129
+ }
130
+
131
+ vTaskStartScheduler ();
132
+ Serial.println (" Scheduler failed" );
133
+ while (1 );
134
+ }
135
+
136
+
137
+ void loop () {
138
+ // Not used.
139
+ }
0 commit comments