added sdcard support
This commit is contained in:
parent
1bc26701c1
commit
bdfc6ea7ad
@ -1,85 +1,77 @@
|
|||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include "Ethernet.h"
|
#include <Ethernet.h>
|
||||||
|
#include <SD.h>
|
||||||
|
|
||||||
#define Serial SerialUSB
|
#define Serial SerialUSB
|
||||||
|
|
||||||
// Enter a MAC address and IP address for your controller below.
|
// MAC address from Ethernet shield sticker under board
|
||||||
// The IP address will be dependent on your local network:
|
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
||||||
byte mac[] = {
|
IPAddress ip(10, 0, 0, 20); // IP address, may need to change depending on network
|
||||||
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
|
EthernetServer server(80); // create a server at port 80
|
||||||
};
|
|
||||||
IPAddress ip(10, 0, 0, 49);
|
|
||||||
|
|
||||||
// Initialize the Ethernet server library
|
File webFile;
|
||||||
// with the IP address and port you want to use
|
|
||||||
// (port 80 is default for HTTP):
|
|
||||||
EthernetServer server(80);
|
|
||||||
|
|
||||||
void setup() {
|
void setup()
|
||||||
// Open serial communications and wait for port to open:
|
{
|
||||||
Serial.begin(9600);
|
Ethernet.begin(mac); // initialize Ethernet device
|
||||||
while (!Serial) {
|
server.begin(); // start to listen for clients
|
||||||
; // wait for serial port to connect. Needed for native USB port only
|
Serial.begin(9600); // for debugging
|
||||||
}
|
|
||||||
|
// initialize SD card
|
||||||
|
Serial.println("Initializing SD card...");
|
||||||
// start the Ethernet connection and the server:
|
if (!SD.begin(4)) {
|
||||||
Ethernet.begin(mac, ip);
|
Serial.println("ERROR - SD card initialization failed!");
|
||||||
server.begin();
|
return; // init failed
|
||||||
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"); // the connection will be closed after completion of the response
|
|
||||||
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
|
|
||||||
client.println();
|
|
||||||
client.println("<!DOCTYPE HTML>");
|
|
||||||
client.println("<html>");
|
|
||||||
// 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
|
Serial.println("SUCCESS - SD card initialized.");
|
||||||
delay(1);
|
// check for index.htm file
|
||||||
// close the connection:
|
if (!SD.exists("index.htm")) {
|
||||||
client.stop();
|
Serial.println("ERROR - Can't find index.htm file!");
|
||||||
Serial.println("client disconnected");
|
return; // can't find index file
|
||||||
}
|
}
|
||||||
|
Serial.println("SUCCESS - Found index.htm file.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
// 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: close");
|
||||||
|
client.println();
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@ -16,8 +16,9 @@ byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // set the mac addre
|
|||||||
|
|
||||||
EthernetServer server(80); // initialize the EthernetServer library, using port 80 (default fot HTTP)
|
EthernetServer server(80); // initialize the EthernetServer library, using port 80 (default fot HTTP)
|
||||||
|
|
||||||
typedef struct { // frame structure
|
typedef struct { // frame structure
|
||||||
uint16_t ID = 1025; // ID
|
uint8_t ID = 0; // station's ID
|
||||||
|
uint8_t IDp = 0; // gateway's ID
|
||||||
uint16_t TS = 0; // TimeStamp
|
uint16_t TS = 0; // TimeStamp
|
||||||
uint16_t DT = 0; // Data Type
|
uint16_t DT = 0; // Data Type
|
||||||
uint16_t D1 = 0; // DATA 1
|
uint16_t D1 = 0; // DATA 1
|
||||||
@ -27,25 +28,23 @@ typedef struct { // frame structure
|
|||||||
|
|
||||||
trame message; // creation of the frame message
|
trame message; // creation of the frame message
|
||||||
|
|
||||||
uint16_t ID;
|
|
||||||
|
|
||||||
void setup(){
|
void setup(){
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
while (!Serial); // wait for serial to initialize
|
while (!Serial); // wait for serial to initialize
|
||||||
Serial.print("Passerelle LoRa\n"); // display on serial the name of the device
|
Serial.print("Passerelle LoRa\n"); // display on serial the name of the device
|
||||||
|
|
||||||
if( !LoRa.begin(868E6) ){
|
if( !LoRa.begin(868E6) ){ // initialise LoRa and display a message if an error occur
|
||||||
Serial.print("Echec de l'initialisation LoRa !\n");
|
Serial.print("Echec de l'initialisation LoRa !\n");
|
||||||
while(true);} // initialize LoRa shield LoRa at 868 MHz
|
while(true);}
|
||||||
|
|
||||||
//Ethernet.begin(mac, ip); // initialize Ethernet shield using the set mac adress and set IP
|
//Ethernet.begin(mac, ip); // initialize Ethernet shield using the set mac adress and set IP
|
||||||
Ethernet.begin(mac); // initialize Ethernet shield uding the set mac and DHCP for the IP
|
Ethernet.begin(mac); // initialize Ethernet shield uding the set mac and DHCP for the IP
|
||||||
Server.begin(); // initialize WebServer part of the librairy
|
server.begin(); // initialize WebServer part of the librairy
|
||||||
Serial.print("server is at "); // display on serial the IP you can find the webpage
|
Serial.print("server is at "); // display on serial the IP you can find the webpage
|
||||||
Serial.println(Ethernet.localIP());
|
Serial.println(Ethernet.localIP());
|
||||||
}
|
}
|
||||||
|
|
||||||
//void SerialPrintElapsedTime( boolean espaceFinal=true ){ // to display the elapsed time
|
//void SerialPrintElapsedTime( boolean espaceFinal=true ){ // to display the elapsed time
|
||||||
// unsigned long h,m,s = millis()/1000;
|
// unsigned long h,m,s = millis()/1000;
|
||||||
// m=s/60;
|
// m=s/60;
|
||||||
// h=m/60;
|
// h=m/60;
|
||||||
@ -56,52 +55,43 @@ void setup(){
|
|||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// LoRa receiver
|
// LoRa receiver
|
||||||
String strID = String(message.ID);//0x00
|
int packetSize = LoRa.parsePacket();
|
||||||
String strTS = String(message.TS);//0x0000
|
if (packetSize > 0)
|
||||||
String strDT = String(message.DT);//0x0000
|
{
|
||||||
String strD1 = String(message.D1);//0x0000
|
SerialUSB.println("Nouveau paquet");
|
||||||
String strD2 = String(message.D3);//0x0000
|
message.ID = LoRa.read();
|
||||||
String strD3 = String(message.D3);//0x0000
|
message.IDp = LoRa.read();
|
||||||
|
message.IDp = 0x07;
|
||||||
|
message.TS = ((uint16_t)LoRa.read() | (LoRa.read() << 8));
|
||||||
|
message.DT = ((uint16_t)LoRa.read() | LoRa.read() << 8);
|
||||||
|
message.D1 = ((uint16_t)LoRa.read() | LoRa.read() << 8);
|
||||||
|
message.D2 = ((uint16_t)LoRa.read() | LoRa.read() << 8);
|
||||||
|
message.D3 = ((uint16_t)LoRa.read() | LoRa.read() << 8);
|
||||||
|
SerialUSB.println(message.ID, HEX);
|
||||||
|
SerialUSB.println(message.IDp, HEX);
|
||||||
|
SerialUSB.println(message.TS, HEX);
|
||||||
|
SerialUSB.println(message.DT, HEX);
|
||||||
|
SerialUSB.println(message.D1, HEX);
|
||||||
|
SerialUSB.println(message.D2, HEX);
|
||||||
|
SerialUSB.println(message.D3, HEX);
|
||||||
|
delay(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static byte tampon[LENMAX]={0}; // if the module receive a frame, it willnot be null
|
|
||||||
int longueurTrame;
|
|
||||||
longueurTrame=LoRa.parsePacket(sizeof(message));
|
|
||||||
if( longueurTrame > 0 ){
|
|
||||||
if( longueurTrame>LENMAX ){ // copy of the frame to cache (LENMAX) and verify if the frame is to big
|
|
||||||
Serial.print("Trame reçue trop grande : ");
|
|
||||||
Serial.println(longueurTrame);
|
|
||||||
longueurTrame=LENMAX; // cut the frame to LENMAX size
|
|
||||||
}
|
|
||||||
for( int i=0; i<longueurTrame; i++ ){
|
|
||||||
tampon[i]=(byte)LoRa.read();
|
|
||||||
}
|
|
||||||
// SerialPrintElapsedTime(); // diplay the time the frame arrived
|
// SerialPrintElapsedTime(); // diplay the time the frame arrived
|
||||||
Serial.print("0x");
|
|
||||||
for( int i=0; i<longueurTrame; i++ ){ // display the frame in hexadecimal
|
// post to server
|
||||||
if( tampon[i] < 0x0F ) Serial.print("0");
|
String postdata="&ID="+message.ID+"&IDp="+message.IDp+"&TS="+message.TS+"&DT="+message.DT+"&D1="+message.D1+"&D2="+message.D2+"&D3="+message.D3;
|
||||||
Serial.print( tampon[i], HEX );
|
bool connected = client.connect(server, 80);
|
||||||
}
|
if (connected){
|
||||||
Serial.print( " " );
|
client.println("POST /formulaireCollecte.html HTTP/1.1");
|
||||||
for( int i=0; i<longueurTrame; i++ ){
|
client.println("Host: btslimayrac.ovh");
|
||||||
if( (tampon[i] < 0x20)||(tampon[i] > 0x7E) ){
|
client.println("Cache-Control: no-cache");
|
||||||
Serial.print( "."); // this character isn't printable (displayable)
|
client.println("Content-Type: application/x-www-form-urlencoded");
|
||||||
}
|
client.print("Content-Length: ");
|
||||||
else{
|
client.println(postData.length());
|
||||||
Serial.print( (char)tampon[i] ); // display the frame in ASCII
|
client.println(postData);
|
||||||
Serial.println("\nTaille du pacquet reçu en octets: "+LENMAX);
|
}
|
||||||
Serial.println("ID Passerelle et station reçus : "+strID);
|
|
||||||
Serial.println("Timestamp reçue : "+strTS);
|
|
||||||
Serial.println("Type de données reçus : "+strDT);
|
|
||||||
Serial.println("Champ de données 1 reçus : "+strD1);
|
|
||||||
Serial.println("Champ de données 2 reçus : "+strD2);
|
|
||||||
Serial.println("Champ de données 3 reçus : "+strD3);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Serial.print( "\n" );
|
|
||||||
} // end of if LoRa.parsePacket
|
|
||||||
delay(10);
|
|
||||||
|
|
||||||
// WebServer
|
// WebServer
|
||||||
EthernetClient client = server.available(); // WebServer :listen for incoming clients
|
EthernetClient client = server.available(); // WebServer :listen for incoming clients
|
||||||
if (client) {
|
if (client) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user