Skip to content

Commit 7669880

Browse files
ABOSTMfpistm
authored andcommitted
feat: Add examples to demonstrate how to use FreeRTOS with Ethernet
Both WebClientFreeRTOS and WebServerFreeRTOS are available. Warning: FreeRTOS may disable interrupts between xTaskCreate() and vTaskStartScheduler(). Thus Ethernet, which is using Interrupts, should be initialized after scheduling start. thus it is done within a task. Fixes #36 Signed-off-by: Alexandre Bourdiol <alexandre.bourdiol@st.com>
1 parent abcf362 commit 7669880

File tree

2 files changed

+243
-0
lines changed

2 files changed

+243
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Port of WebClient on FreeRTOS
3+
4+
This sketch connects to a website (http://www.google.com)
5+
6+
Circuit:
7+
* STM32 board with Ethernet support
8+
9+
created 18 Dec 2009
10+
by David A. Mellis
11+
modified 9 Apr 2012
12+
by Tom Igoe, based on work by Adrian McEwen
13+
modified 23 Jun 2017
14+
by Wi6Labs
15+
modified 1 Jun 2018
16+
by sstaub
17+
*/
18+
19+
#include <LwIP.h>
20+
#include <STM32Ethernet.h>
21+
#include <STM32FreeRTOS.h>
22+
23+
// if you don't want to use DNS (and reduce your sketch size)
24+
// use the numeric IP instead of the name for the server:
25+
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
26+
char server[] = "www.google.com"; // name address for Google (using DNS)
27+
28+
// Set the static IP address to use if the DHCP fails to assign
29+
IPAddress ip(192, 168, 0, 177);
30+
31+
// Initialize the Ethernet client library
32+
// with the IP address and port of the server
33+
// that you want to connect to (port 80 is default for HTTP):
34+
EthernetClient client;
35+
36+
37+
// task code
38+
void taskETH(void* arg) {
39+
UNUSED(arg);
40+
// start the Ethernet connection:
41+
if (Ethernet.begin() == 0) {
42+
Serial.println("Failed to configure Ethernet using DHCP");
43+
// try to configure using IP address instead of DHCP:
44+
Ethernet.begin(ip);
45+
}
46+
// give the Ethernet shield a second to initialize:
47+
delay(1000);
48+
Serial.println("connecting...");
49+
50+
// if you get a connection, report back via serial:
51+
if (client.connect(server, 80)) {
52+
Serial.println("connected");
53+
// Make a HTTP request:
54+
client.println("GET /search?q=arduino HTTP/1.1");
55+
client.println("Host: www.google.com");
56+
client.println("Connection: close");
57+
client.println();
58+
} else {
59+
// if you didn't get a connection to the server:
60+
Serial.println("connection failed");
61+
}
62+
63+
while(1){
64+
// if there are incoming bytes available
65+
// from the server, read them and print them:
66+
if (client.available()) {
67+
char c = client.read();
68+
Serial.print(c);
69+
}
70+
71+
// if the server's disconnected, stop the client:
72+
if (!client.connected()) {
73+
Serial.println();
74+
Serial.println("disconnecting.");
75+
client.stop();
76+
77+
// do nothing forevermore:
78+
while (true);
79+
}
80+
}
81+
}
82+
83+
84+
void setup() {
85+
// Open serial communications and wait for port to open:
86+
Serial.begin(9600);
87+
while (!Serial) {
88+
; // wait for serial port to connect. Needed for native USB port only
89+
}
90+
91+
portBASE_TYPE s = xTaskCreate(taskETH, NULL, 200, NULL, 1, NULL);
92+
if (s != pdPASS) {
93+
printf("Ethernet task creation failed\n");
94+
while (1);
95+
}
96+
97+
vTaskStartScheduler();
98+
Serial.println("Scheduler failed");
99+
while (1);
100+
}
101+
102+
void loop() {
103+
// Not used.
104+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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

Comments
 (0)