Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wifiservertest #17

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
247 changes: 247 additions & 0 deletions firmware/BomberCat_Webserver/BomberCat_Webserver.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
/*********************************************************************************
Example WebServer with NFC Copy Cat WiFi
by Jaz, Electronic Cats (https://electroniccats.com/)
Date: 17/05/2022

This example demonstrates how to use NFC Copy Cat by Electronic Cats
https://github.com/ElectronicCats/NFC-Copy-Cat-WiFi

Development environment specifics:
IDE: Arduino 1.8.19
Hardware Platform:
NFC Copy Cat
- ESP32-S2

Electronic Cats invests time and resources providing this open source code,
please support Electronic Cats and open-source hardware by purchasing
products from Electronic Cats!

This code is beerware; if you see me (or any other Electronic Cats
member) at the local, and you've found our code helpful,
please buy us a round!
Distributed as-is; no warranty is given.
***********************************************************************************/

#include <Arduino.h>
#include <WiFiNINA.h>
#include "PluggableUSBMSD.h"
#include "FlashIAPBlockDevice.h"

// Create AsyncWebServer object on port 80
//AsyncWebServer server(80);
WiFiServer server(80);

// Search for parameter in HTTP POST request
const char* PARAM_INPUT_1 = "ssid";
const char* PARAM_INPUT_2 = "pass";
const char* PARAM_INPUT_3 = "ip";

//Variables to save values from HTML form
String ssid;
String pass;
String ip;

// File paths to save input values permanently
const char* ssidPath = "/ssid.txt";
const char* passPath = "/pass.txt";
const char* ipPath = "/ip.txt";

IPAddress localIP;
//IPAddress localIP(192, 168, 1, 200); // hardcoded

// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);

// Timer variables
unsigned long previousMillis = 0;
const long interval = 10000; // interval to wait for Wi-Fi connection (milliseconds)

// Set LED GPIO
const int ledPin = 13;
// Stores LED state

//WiFi Test LED
#define led9 9

String ledState;

char ssidId[15]; //Create a Unique AP from MAC address

// Initialize SPIFFS

// Read File from SPIFFS
/*String readFile(fs::FS &fs, const char * path) {
Serial.printf("Reading file: %s\r\n", path);

File file = fs.open(path);
if (!file || file.isDirectory()) {
Serial.println("- failed to open file for reading");
return String();
}

String fileContent;
while (file.available()) {
fileContent = file.readStringUntil('\n');
break;
}
return fileContent;
}
*/
// Write file to SPIFFS
/*void writeFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Writing file: %s\r\n", path);

File file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println("- failed to open file for writing");
return;
}
if (file.print(message)) {
Serial.println("- file written");
} else {
Serial.println("- frite failed");
}
}
*/
// Initialize WiFi
bool initWiFi() {
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}

String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
return true;
}

/* //Test Code
// Replaces placeholder with LED state value
String processor(const String& var) {
if (var == "STATE") {
if (digitalRead(ledPin)) {
ledState = "ON";
}
else {
ledState = "OFF";
}
return ledState;
}
return String();
}
*/

void setup() {
// Serial port for debugging purposes
Serial.begin(115200);

WiFiStorageFile fs = WiFiStorage.open("/fs/testfile");

// Set GPIO 2 as an OUTPUT
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);

pinMode(led9, OUTPUT);

// Load values saved in SPIFFS
ssid = readFile(SPIFFS, ssidPath);
pass = readFile(SPIFFS, passPath);
ip = readFile(SPIFFS, ipPath);
Serial.println(ssid);
Serial.println(pass);
Serial.println(ip);

if (initWiFi()) {
/* // Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/index.html", "text/html", false, processor);
});
server.serveStatic("/", SPIFFS, "/");

// Route to set GPIO state to HIGH
server.on("/on", HTTP_GET, [](AsyncWebServerRequest * request) {
digitalWrite(ledPin, HIGH);
request->send(SPIFFS, "/index.html", "text/html", false, processor);
});

// Route to set GPIO state to LOW
server.on("/off", HTTP_GET, [](AsyncWebServerRequest * request) {
digitalWrite(ledPin, LOW);
request->send(SPIFFS, "/index.html", "text/html", false, processor);
});
server.begin();
*/
}
else {
uint64_t chipid = ESP.getEfuseMac(); //The chip ID is essentially its MAC address(length: 6 bytes).
uint16_t chip = (uint16_t)(chipid >> 32);
snprintf(ssidId, 15, "NFC-Copy-%04X", chip);
// Connect to Wi-Fi network with SSID and password
Serial.println("Setting AP (Access Point)");
// NULL sets an open Access Point
WiFi.softAP(ssidId, NULL);

IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);

// Web Server Root URL
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/wifimanager.html", "text/html");
});

server.serveStatic("/", SPIFFS, "/");

server.on("/", HTTP_POST, [](AsyncWebServerRequest * request) {
int params = request->params();
for (int i = 0; i < params; i++) {
AsyncWebParameter* p = request->getParam(i);
if (p->isPost()) {
// HTTP POST ssid value
if (p->name() == PARAM_INPUT_1) {
ssid = p->value().c_str();
Serial.print("SSID set to: ");
Serial.println(ssid);
// Write file to save value
writeFile(SPIFFS, ssidPath, ssid.c_str());
}
// HTTP POST pass value
if (p->name() == PARAM_INPUT_2) {
pass = p->value().c_str();
Serial.print("Password set to: ");
Serial.println(pass);
// Write file to save value
writeFile(SPIFFS, passPath, pass.c_str());
}
// HTTP POST ip value
if (p->name() == PARAM_INPUT_3) {
ip = p->value().c_str();
Serial.print("IP Address set to: ");
Serial.println(ip);
// Write file to save value
writeFile(SPIFFS, ipPath, ip.c_str());
}
//Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
request->send(200, "text/plain", "Done. ESP will restart, connect to your router and go to IP address: " + ip);
delay(3000);
ESP.restart();
});
server.begin();
}
}

void loop() {
if (WiFi.status() == WL_CONNECTED) {
digitalWrite(led9, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led9, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}
}
108 changes: 108 additions & 0 deletions firmware/BombercatWifiServer/BombercatWifiServer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
WiFi Web Server with Files via USB for BomberCat

This example is written for a network using WPA encryption. For
WEP or WPA, change the WiFi.begin() call accordingly.

Eric Chavez

*/
#include "PluggableUSBMSD.h"
#include "FlashIAPBlockDevice.h"
#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
#include "http.h"

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key index number (needed only for WEP)

int status = WL_IDLE_STATUS;

typedef enum {
DATA_STORAGE_STATE,
DATA_LOGGER_IDLE_STATE,
DATA_LOGGER_RUNNING_STATE
} demo_state_e;

static FlashIAPBlockDevice bd(XIP_BASE + 0x100000, 0x100000);

USBMSD MassStorage(&bd);

void USBMSD::begin()
{
int err = getFileSystem().mount(&bd);
if (err) {
err = getFileSystem().reformat(&bd);
}
}

mbed::FATFileSystem &USBMSD::getFileSystem()
{
static mbed::FATFileSystem fs("root");
return fs;
}


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

// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}

String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}

// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}
server.begin();
// you're connected now, so print out the status:
printWifiStatus();

//readContents();
}


void loop() {
httpSketch();
}



void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
2 changes: 2 additions & 0 deletions firmware/BombercatWifiServer/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define SECRET_SSID "EC_2.4"
#define SECRET_PASS "electronicCats"
Binary file added firmware/BombercatWifiServer/data/Icons/Home1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added firmware/BombercatWifiServer/data/Icons/Home2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added firmware/BombercatWifiServer/data/Icons/MagS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added firmware/BombercatWifiServer/data/Icons/NFC.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions firmware/BombercatWifiServer/data/Icons/Símbolo EC.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added firmware/BombercatWifiServer/data/Icons/WiFi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added firmware/BombercatWifiServer/data/Icons/ap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added firmware/BombercatWifiServer/data/Icons/card.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added firmware/BombercatWifiServer/data/Icons/info.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added firmware/BombercatWifiServer/data/Icons/logOut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added firmware/BombercatWifiServer/data/Icons/pass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added firmware/BombercatWifiServer/data/Icons/user.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added firmware/BombercatWifiServer/data/Icons/visa.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading