Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
27d36d8
Fix MacOS (case) filename clashes
manchoz Jul 6, 2021
357dba7
Add examples
manchoz Jul 6, 2021
c39cfee
Add helpers begin methods
manchoz Feb 1, 2021
05a1b80
Fix WebClient example
manchoz Feb 1, 2021
540e0ab
Add Static IP support
manchoz Jul 5, 2021
a5e512a
Manage Client status
manchoz Jul 5, 2021
d1501d0
Add support for Static Network configuration
manchoz Jul 5, 2021
b04124a
Update examples
manchoz Jul 5, 2021
817b47a
Add forgotten mac parameter
manchoz Jul 5, 2021
cf184fd
Add checks on socket validity before operations
manchoz Jul 6, 2021
9172c17
Manage sockets already connected
manchoz Jul 6, 2021
4aad72b
Add client socket management
manchoz Jul 6, 2021
5f802c1
WiFi: Cleanup libraries and error handling
facchinm Jul 7, 2021
4ded56c
WiFi: make library compliant with Arduino APIs
facchinm Jul 7, 2021
ec4109d
Initial: start moving WiFi generic classes into a standalone library
facchinm Jul 8, 2021
e0e8647
WiFi: port Client and Server to the new wrapper class
facchinm Jul 8, 2021
02b1239
TEMP: MbedClient: add horrible Client "constructor"
facchinm Jul 8, 2021
73cbc6b
WiFi: port UDP to new MbedUdp class
facchinm Jul 8, 2021
175d09f
Ethernet: port library to SocketWrapper
facchinm Jul 8, 2021
37edb74
MbedClient: fix copy constructor
facchinm Jul 8, 2021
974f4c2
Network: cleanup libraries and licenses
facchinm Jul 9, 2021
d4bb9d8
Network: apply clang-format on libraries
facchinm Jul 9, 2021
4340946
MbedUdp: make socket non blocking
facchinm Jul 9, 2021
123694b
SocketWrapper: fix download() API visibility
facchinm Jul 9, 2021
bc92047
MbedUDP: take into account write() errors
facchinm Jul 9, 2021
7fd7c9a
Remove protected leftovers
manchoz Jul 13, 2021
d9fa625
Add EthernetServer::available()
manchoz Jul 13, 2021
8c7962f
Add ::dnsServerIP
manchoz Jul 13, 2021
a175f35
Unlock mutexes before return
manchoz Jul 13, 2021
15ca271
Fix Typo
manchoz Jul 13, 2021
d3e8ee3
Manage copy and assignement
manchoz Jul 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
Advanced Chat Server

A more advanced server that distributes any incoming messages
to all connected clients but the client the message comes from.
To use, telnet to your device's IP address and type.
You can see the client's input in the serial monitor as well.
Using an Arduino Wiznet Ethernet shield.

Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13

created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
redesigned to make use of operator== 25 Nov 2013
by Norbert Truchsess

*/

#include <SPI.h>
#include <PortentaEthernet.h>
#include <Ethernet.h>

// The IP address will be dependent on your local network.
// gateway and subnet are optional:
IPAddress ip(192, 168, 1, 177);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);


// telnet defaults to port 23
EthernetServer server(23);

EthernetClient clients[8];

void setup() {

// initialize the Ethernet device
Ethernet.begin(ip, myDns, gateway, subnet);

// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}

// start listening for clients
server.begin();

Serial.print("Chat server address:");
Serial.println(Ethernet.localIP());
}

void loop() {
// check for any new client connecting, and say hello (before any incoming data)
EthernetClient newClient = server.available();
if (newClient) {
for (byte i=0; i < 8; i++) {
if (!clients[i]) {
Serial.print("We have a new client #");
Serial.println(i);
newClient.print("Hello, client number: ");
newClient.println(i);
// Once we "accept", the client is no longer tracked by EthernetServer
// so we must store it into our list of clients
clients[i] = newClient;
break;
}
}
}

// check for incoming data from all clients
for (byte i=0; i < 8; i++) {
if (clients[i] && clients[i].available() > 0) {
// read bytes from a client
byte buffer[80];
int count = clients[i].read(buffer, 80);
// write the bytes to all other connected clients
for (byte j=0; j < 8; j++) {
if (j != i && clients[j].connected()) {
clients[j].write(buffer, count);
}
}
}
}

// stop any clients which disconnect
for (byte i=0; i < 8; i++) {
if (clients[i] && !clients[i].connected()) {
Serial.print("disconnect client #");
Serial.println(i);
clients[i].stop();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
/*
SCP1000 Barometric Pressure Sensor Display

Serves the output of a Barometric Pressure Sensor as a web page.
Uses the SPI library. For details on the sensor, see:
http://www.sparkfun.com/commerce/product_info.php?products_id=8161

This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip

TODO: this hardware is long obsolete. This example program should
be rewritten to use https://www.sparkfun.com/products/9721

Circuit:
SCP1000 sensor attached to pins 6,7, and 11 - 13:
DRDY: pin 6
CSB: pin 7
MOSI: pin 11
MISO: pin 12
SCK: pin 13

created 31 July 2010
by Tom Igoe
*/

#include <PortentaEthernet.h>
#include <Ethernet.h>
// the sensor communicates using SPI, so include the library:
#include <SPI.h>


// assign an IP address for the controller:
IPAddress ip(192, 168, 1, 20);


// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);


//Sensor's memory register addresses:
const int PRESSURE = 0x1F; //3 most significant bits of pressure
const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure
const int TEMPERATURE = 0x21; //16 bit temperature reading

// pins used for the connection with the sensor
// the others you need are controlled by the SPI library):
const int dataReadyPin = 6;
const int chipSelectPin = 7;

float temperature = 0.0;
long pressure = 0;
long lastReadingTime = 0;

void setup() {

// start the SPI library:
SPI.begin();

// start the Ethernet connection
Ethernet.begin(ip);

// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}

// start listening for clients
server.begin();

// initalize the data ready and chip select pins:
pinMode(dataReadyPin, INPUT);
pinMode(chipSelectPin, OUTPUT);

//Configure SCP1000 for low noise configuration:
writeRegister(0x02, 0x2D);
writeRegister(0x01, 0x03);
writeRegister(0x03, 0x02);

// give the sensor and Ethernet shield time to set up:
delay(1000);

//Set the sensor to high resolution mode tp start readings:
writeRegister(0x03, 0x0A);

}

void loop() {
// check for a reading no more than once a second.
if (millis() - lastReadingTime > 1000) {
// if there's a reading ready, read it:
// don't do anything until the data ready pin is high:
if (digitalRead(dataReadyPin) == HIGH) {
getData();
// timestamp the last time you got a reading:
lastReadingTime = millis();
}
}

// listen for incoming Ethernet connections:
listenForEthernetClients();
}


void getData() {
Serial.println("Getting reading");
//Read the temperature data
int tempData = readRegister(0x21, 2);

// convert the temperature to celsius and display it:
temperature = (float)tempData / 20.0;

//Read the pressure data highest 3 bits:
byte pressureDataHigh = readRegister(0x1F, 1);
pressureDataHigh &= 0b00000111; //you only needs bits 2 to 0

//Read the pressure data lower 16 bits:
unsigned int pressureDataLow = readRegister(0x20, 2);
//combine the two parts into one 19-bit number:
pressure = ((pressureDataHigh << 16) | pressureDataLow) / 4;

Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" degrees C");
Serial.print("Pressure: " + String(pressure));
Serial.println(" Pa");
}

void listenForEthernetClients() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("Got a client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// 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: " + String(pressure));
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
delay(1);
// close the connection:
client.stop();
}
}


//Send a write command to SCP1000
void writeRegister(byte registerName, byte registerValue) {
// SCP1000 expects the register name in the upper 6 bits
// of the byte:
registerName <<= 2;
// command (read or write) goes in the lower two bits:
registerName |= 0b00000010; //Write command

// take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW);

SPI.transfer(registerName); //Send register location
SPI.transfer(registerValue); //Send value to record into register

// take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH);
}


//Read register from the SCP1000:
unsigned int readRegister(byte registerName, int numBytes) {
byte inByte = 0; // incoming from the SPI read
unsigned int result = 0; // result to return

// SCP1000 expects the register name in the upper 6 bits
// of the byte:
registerName <<= 2;
// command (read or write) goes in the lower two bits:
registerName &= 0b11111100; //Read command

// take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW);
// send the device the register you want to read:
int command = SPI.transfer(registerName);
// send a value of 0 to read the first byte returned:
inByte = SPI.transfer(0x00);

result = inByte;
// if there's more than one byte returned,
// shift the first byte then get the second byte:
if (numBytes > 1) {
result = inByte << 8;
inByte = SPI.transfer(0x00);
result = result | inByte;
}
// take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH);
// return the result:
return (result);
}
Loading