first code
This commit is contained in:
+72
-29
@@ -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,24 +34,30 @@ 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()) {
|
uint8_t method = -1;
|
||||||
if (client.available()) {
|
|
||||||
char c = client.read();
|
uint8_t i = 0;
|
||||||
|
uint8_t countChars = client.readBytesUntil(' ', buffer, METHOD_LENGTH);
|
||||||
|
for (countChars--; countChars < METHOD_LENGTH; countChars++ )
|
||||||
|
buffer[countChars] = ' ';
|
||||||
|
for (uint8_t i = 0; i < COUNT_HTTP_METHOD; i++)
|
||||||
|
if (memcmp(buffer, httpMethos[i], METHOD_LENGTH)) {
|
||||||
|
method = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
uint16_t len = client.available();
|
||||||
|
if (len > 0)
|
||||||
|
client.read(buffer, len);
|
||||||
|
Serial.write(buffer, len);
|
||||||
|
|
||||||
char buffer[7];
|
|
||||||
client.readBytesUntil(' ', buffer, 7);
|
|
||||||
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("HTTP/1.1 200 OK");
|
||||||
client.println("Content-Type: text/html");
|
client.println("Content-Type: text/html");
|
||||||
client.println();
|
client.println();
|
||||||
@@ -59,20 +69,53 @@ void listenForEthernetClients() {
|
|||||||
client.print("Pressure: lUl");
|
client.print("Pressure: lUl");
|
||||||
client.print(" Pa");
|
client.print(" Pa");
|
||||||
client.println("<br />");
|
client.println("<br />");
|
||||||
break;
|
|
||||||
}
|
client.stop();
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
// // 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();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user