vjmuzik
Well-known member
Is NativeEthernet (and FNET) at a point where it can be included with a Teensyduino installer?
I'm getting ready to package up 1.53-beta3....
I suppose it is, it’s worked so far at least.
Is NativeEthernet (and FNET) at a point where it can be included with a Teensyduino installer?
I'm getting ready to package up 1.53-beta3....
[B]Add MDNS wrapper[/B]
-Add WebServerMDNS example
-Update keywords
MDNS.begin("Teensy41"); //Domain name
MDNS.addService("_http._tcp", 80); //Type of service and port number
Yes it's able to do multicast already.
[B]Support for FNET TLS sockets (server-side only for now)[/B]
-Update WebServerMDNS with optional TLS
[B]Readded mbedtls source and support[/B]
-Fixed MDNS service name overflow
[B]Add client-side TLS support[/B]
-Add WebClientRepeatingTLS example
-Add client.close() to support proper HTTP close before disconnecting
-Add functions to set client and server certificates/keys (if needed)
[B]Change mbedtls client verify to optional[/B]
-Disable unused IPv6 support
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
>>for (int i = 0; i < 50; i++) {
>> client.printf("%5.5d - AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br>",i);
>>}
client.print("<br />");
client.println("</html>");
break;
}
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
*/
#include <NativeEthernet.h>
// Enter a IP address for your controller below.
uint8_t mac[6];
IPAddress ip(192,168,1,177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
// Find Mac address for Teensy
static void teensyMAC(uint8_t *mac) {
uint32_t m1 = HW_OCOTP_MAC1;
uint32_t m2 = HW_OCOTP_MAC0;
mac[0] = m1 >> 8;
mac[1] = m1 >> 0;
mac[2] = m2 >> 24;
mac[3] = m2 >> 16;
mac[4] = m2 >> 8;
mac[5] = m2 >> 0;
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("About to start Ethernet, apparently the cable needs plugged in before it will proceed!");
teensyMAC(mac);
static char teensyMacString[23];
sprintf(teensyMacString, "MAC: %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.println(teensyMacString);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(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("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"1\">");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
client.println("</html>");
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
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
/*--------------------------------------------------------------
Program: eth_websrv_SD_Ajax
Description: Arduino web server that serves up a web page
that displays the status of a switch connected
to pin 3 of the Arduino.
The web page is stored on the SD card.
The web page contains JavaScript code that uses
Ajax to request the state of the switch every
second.
Hardware: Arduino Uno and official Arduino Ethernet
shield. Should work with other Arduinos and
compatible Ethernet shields.
2Gb micro SD card formatted FAT16
Push button switch interfaced to pin 3 of the
Arduino
Software: Developed using Arduino 1.0.5 software
Should be compatible with Arduino 1.0 +
SD card contains web page called index.htm
References: - WebServer example by David A. Mellis and
modified by Tom Igoe
- SD card examples by David A. Mellis and
Tom Igoe
- Ethernet library documentation:
http://arduino.cc/en/Reference/Ethernet
- SD Card library documentation:
http://arduino.cc/en/Reference/SD
Date: 25 March 2013
Modified: 17 June 2013
- removed the use of the String class
Author: W.A. Smith, http://startingelectronics.org
--------------------------------------------------------------*/
#include <NativeEthernet.h>
#include <SD.h>
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 400
// MAC address from Ethernet shield sticker under board
uint8_t mac[6];
IPAddress ip(192, 168, 1, 177); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile;
static void teensyMAC(uint8_t *mac) {
uint32_t m1 = HW_OCOTP_MAC1;
uint32_t m2 = HW_OCOTP_MAC0;
mac[0] = m1 >> 8;
mac[1] = m1 >> 0;
mac[2] = m2 >> 24;
mac[3] = m2 >> 16;
mac[4] = m2 >> 8;
mac[5] = m2 >> 0;
}
// Select SD Card
const int chipSelect = BUILTIN_SDCARD; // For SD Card Chip select
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
void setup()
{
Serial.begin(9600); // for debugging
Serial.println("About to start Ethernet, apparently the cable needs plugged in before it will proceed!");
teensyMAC(mac);
static char teensyMacString[23];
sprintf(teensyMacString, "MAC: %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.println(teensyMacString);
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
pinMode(3, INPUT); // switch is attached to Arduino pin 3
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// buffer first part of HTTP request in HTTP_req array (string)
// leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
// last line of client request is blank and ends with \n
// respond to client only after last line received
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("Connection: keep-alive");
client.println();
// Ajax request
if (StrContains(HTTP_req, "ajax_switch")) {
// read switch state and send appropriate paragraph text
GetSwitchState(client);
}
else { // web page request
// send web page
webFile = SD.open("index.htm"); // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
}
// display received HTTP request on serial port
Serial.println(HTTP_req);
// reset buffer index and all buffer elements to 0
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
// send the state of the switch to the web browser
void GetSwitchState(EthernetClient cl)
{
if (digitalRead(3)) {
cl.println("Switch state: ON");
}
else {
cl.println("Switch state: OFF");
}
}
// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}
// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
char found = 0;
char index = 0;
char len;
len = strlen(str);
if (strlen(sfind) > len) {
return 0;
}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {
return 1;
}
}
else {
found = 0;
}
index++;
}
return 0;
}
// Basic Arduino Web Server version 0.1 modified to include logging
// http://startingelectronics.org/software/arduino/web-server/basic-01/
//
// More details and web files available from:
// http://startingelectronics.org/software/arduino/web-server/01-log-data/
//
// Date: 11 July 2015
//
#include <NativeEthernet.h>
#include <SD.h>
// maximum length of file name including path
#define FILE_NAME_LEN 20
// HTTP request type
#define HTTP_invalid 0
#define HTTP_GET 1
#define HTTP_POST 2
// file types
#define FT_HTML 0
#define FT_ICON 1
#define FT_CSS 2
#define FT_JAVASCRIPT 3
#define FT_JPG 4
#define FT_PNG 5
#define FT_GIF 6
#define FT_TEXT 7
#define FT_INVALID 8
long log_time_ms = 5000; // how often to log data in milliseconds
long prev_log_time = 0; // previous time log occurred
// the media access control (ethernet hardware) address for the shield:
const byte mac[] PROGMEM = { 0x04, 0xE9, 0xE5, 0x0B, 0xA9, 0x92 };
//the IP address for the shield:
const byte ip[] = { 192, 168, 1, 177 }; // does not work if this is put into Flash
// the router's gateway address:
const byte gateway[] PROGMEM = { 192, 168, 0, 1 };
// the subnet:
const byte subnet[] PROGMEM = { 255, 255, 255, 0 };
EthernetServer server(80);
// Select SD Card
const int chipSelect = BUILTIN_SDCARD; // For SD Card Chip select
void setup() {
Serial.begin(115200); // for debugging
if (!SD.begin(chipSelect)) {
return; // SD card initialization failed
}
Ethernet.begin((uint8_t*)mac, ip, gateway, subnet);
server.begin(); // start listening for clients
}
void loop() {
// if an incoming client connects, there will be bytes available to read:
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (ServiceClient(&client)) {
// received request from client and finished responding
break;
}
} // while (client.connected())
delay(1);
client.stop();
} // if (client)
// log the data
LogData();
}// void loop()
void LogData(void)
{
unsigned long current_time; // current millisecond time
File logFile; // file to log data to
current_time = millis(); // get the current time in milliseconds
if ((current_time - prev_log_time) > log_time_ms) {
prev_log_time = current_time;
// the log time has elapsed, so log another set of data
logFile = SD.open("log.txt", FILE_WRITE);
if (logFile) {
logFile.print(current_time);
logFile.print(" = ");
logFile.println(analogRead(5)); // log the analog pin value
logFile.close();
}
}
}
bool ServiceClient(EthernetClient *client)
{
static boolean currentLineIsBlank = true;
char cl_char;
File webFile;
// file name from request including path + 1 of null terminator
char file_name[FILE_NAME_LEN + 1] = {0}; // requested file name
char http_req_type = 0;
char req_file_type = FT_INVALID;
const char *file_types[] = {"text/html", "image/x-icon", "text/css", "application/javascript", "image/jpeg", "image/png", "image/gif", "text/plain"};
static char req_line_1[40] = {0}; // stores the first line of the HTTP request
static unsigned char req_line_index = 0;
static bool got_line_1 = false;
if (client->available()) { // client data available to read
cl_char = client->read();
if ((req_line_index < 39) && (got_line_1 == false)) {
if ((cl_char != '\r') && (cl_char != '\n')) {
req_line_1[req_line_index] = cl_char;
req_line_index++;
}
else {
got_line_1 = true;
req_line_1[39] = 0;
}
}
if ((cl_char == '\n') && currentLineIsBlank) {
// get HTTP request type, file name and file extension type index
http_req_type = GetRequestedHttpResource(req_line_1, file_name, &req_file_type);
if (http_req_type == HTTP_GET) { // HTTP GET request
if (req_file_type < FT_INVALID) { // valid file type
webFile = SD.open(file_name); // open requested file
if (webFile) {
// send a standard http response header
client->println(F("HTTP/1.1 200 OK"));
client->print(F("Content-Type: "));
client->println(file_types[req_file_type]);
client->println(F("Connection: close"));
client->println();
// send web page
while(webFile.available()) {
int num_bytes_read;
char byte_buffer[64];
// get bytes from requested file
num_bytes_read = webFile.read(byte_buffer, 64);
// send the file bytes to the client
client->write(byte_buffer, num_bytes_read);
}
webFile.close();
}
else {
// failed to open file
}
}
else {
// invalid file type
}
}
else if (http_req_type == HTTP_POST) {
// a POST HTTP request was received
}
else {
// unsupported HTTP request received
}
req_line_1[0] = 0;
req_line_index = 0;
got_line_1 = false;
// finished sending response and web page
return 1;
}
if (cl_char == '\n') {
currentLineIsBlank = true;
}
else if (cl_char != '\r') {
currentLineIsBlank = false;
}
} // if (client.available())
return 0;
}
// extract file name from first line of HTTP request
char GetRequestedHttpResource(char *req_line, char *file_name, char *file_type)
{
char request_type = HTTP_invalid; // 1 = GET, 2 = POST. 0 = invalid
char *str_token;
*file_type = FT_INVALID;
str_token = strtok(req_line, " "); // get the request type
if (strcmp(str_token, "GET") == 0) {
request_type = HTTP_GET;
str_token = strtok(NULL, " "); // get the file name
if (strcmp(str_token, "/") == 0) {
strcpy(file_name, "index.htm");
*file_type = FT_HTML;
}
else if (strlen(str_token) <= FILE_NAME_LEN) {
// file name is within allowed length
strcpy(file_name, str_token);
// get the file extension
str_token = strtok(str_token, ".");
str_token = strtok(NULL, ".");
if (strcmp(str_token, "htm") == 0) {*file_type = 0;}
else if (strcmp(str_token, "ico") == 0) {*file_type = 1;}
else if (strcmp(str_token, "css") == 0) {*file_type = 2;}
else if (strcmp(str_token, "js") == 0) {*file_type = 3;}
else if (strcmp(str_token, "jpg") == 0) {*file_type = 4;}
else if (strcmp(str_token, "png") == 0) {*file_type = 5;}
else if (strcmp(str_token, "gif") == 0) {*file_type = 6;}
else if (strcmp(str_token, "txt") == 0) {*file_type = 7;}
else {*file_type = 8;}
}
else {
// file name too long
}
}
else if (strcmp(str_token, "POST") == 0) {
request_type = HTTP_POST;
}
return request_type;
}