first code

This commit is contained in:
2023-04-09 19:03:18 +02:00
parent 66c3d4db74
commit dc363cffc3
+82 -39
View File
@@ -1,8 +1,10 @@
#include <Arduino.h> #include <Arduino.h>
#include <Ethernet2.h> #include <Ethernet.h>
#include <SPI.h> #include <SPI.h>
#include <shiftregister.h> #include <shiftregister.h>
#define COUNT_HTTP_METHOD 3
#define METHOD_LENGTH 6
byte mac[] = { 0xC3, 0xF9, 0x7C, 0xB1, 0x8C, 0xF3 }; byte mac[] = { 0xC3, 0xF9, 0x7C, 0xB1, 0x8C, 0xF3 };
@@ -12,7 +14,9 @@ IPAddress subnet(255, 255, 128, 0);
EthernetServer server(80); EthernetServer server(80);
ShiftRegister reg() const byte const httpMethos[COUNT_HTTP_METHOD][METHOD_LENGTH] = { 'GET ', 'PATCH ', 'DELETE' };
// ShiftRegister reg()
void listenForEthernetClients(void); void listenForEthernetClients(void);
@@ -30,49 +34,88 @@ void loop() {
void listenForEthernetClients() { void listenForEthernetClients() {
EthernetClient client = server.available(); EthernetClient client = server.available();
if (!client) return; if (!client) return;
Serial.println("Got a client"); Serial.println("Got a client");
// an http request ends with a blank line byte buffer[256];
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
char buffer[7]; uint8_t method = -1;
client.readBytesUntil(' ', buffer, 7);
Serial.print(c); uint8_t i = 0;
// if you've gotten to the end of the line (received a newline uint8_t countChars = client.readBytesUntil(' ', buffer, METHOD_LENGTH);
// character) and the line is blank, the http request has ended, for (countChars--; countChars < METHOD_LENGTH; countChars++ )
// so you can send a reply buffer[countChars] = ' ';
if (c == '\n' && currentLineIsBlank) { for (uint8_t i = 0; i < COUNT_HTTP_METHOD; i++)
// send a standard http response header if (memcmp(buffer, httpMethos[i], METHOD_LENGTH)) {
client.println("HTTP/1.1 200 OK"); method = i;
client.println("Content-Type: text/html"); break;
client.println();
// print the current readings, in HTML format:
client.print("Temperature: ");
client.print("temperature");
client.print(" degrees C");
client.println("<br />");
client.print("Pressure: lUl");
client.print(" Pa");
client.println("<br />");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
} }
}
uint16_t len = client.available();
if (len > 0)
client.read(buffer, len);
Serial.write(buffer, len);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// print the current readings, in HTML format:
client.print("Temperature: ");
client.print("temperature");
client.print(" degrees C");
client.println("<br />");
client.print("Pressure: lUl");
client.print(" Pa");
client.println("<br />");
client.stop();
// // an http request ends with a blank line
// boolean currentLineIsBlank = true;
// while (client.connected()) {
// if (client.available()) {
// char c = client.read();
// Serial.print(c);
// // if you've gotten to the end of the line (received a newline
// // character) and the line is blank, the http request has ended,
// // so you can send a reply
// if (c == '\n' && currentLineIsBlank) {
// // send a standard http response header
// client.println("HTTP/1.1 200 OK");
// client.println("Content-Type: text/html");
// client.println();
// // print the current readings, in HTML format:
// client.print("Temperature: ");
// client.print("temperature");
// client.print(" degrees C");
// client.println("<br />");
// client.print("Pressure: lUl");
// client.print(" Pa");
// client.println("<br />");
// break;
// }
// if (c == '\n') {
// // you're starting a new line
// currentLineIsBlank = true;
// }
// else if (c != '\r') {
// // you've gotten a character on the current line
// currentLineIsBlank = false;
// }
// }
// }
// give the web browser time to receive the data // give the web browser time to receive the data
Serial.println(); Serial.println();
delay(1); delay(1);
client.stop(); // client.stop();
} }