Skip to content

Commit 0f165d6

Browse files
authored
Add files via upload
1 parent 3621f15 commit 0f165d6

File tree

6 files changed

+597
-0
lines changed

6 files changed

+597
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// the setup function runs once when you press reset or power the board
2+
void setup() {
3+
// initialize digital pin LED_BUILTIN as an output.
4+
pinMode(LED_BUILTIN, OUTPUT);
5+
}
6+
7+
// the loop function runs over and over again forever
8+
void loop() {
9+
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
10+
delay(1000); // wait for a second
11+
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
12+
delay(1000); // wait for a second
13+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include <FlashIAPBlockDevice.h>
2+
#include <TDBStore.h>
3+
4+
using namespace mbed;
5+
6+
// Get limits of the In Application Program (IAP) flash, ie. the internal MCU flash.
7+
#include "FlashIAPLimits.h"
8+
auto iapLimits { getFlashIAPLimits() };
9+
10+
// Create a block device on the available space of the FlashIAP
11+
FlashIAPBlockDevice blockDevice(iapLimits.start_address, iapLimits.available_size);
12+
13+
// Create a key/value store on the Flash IAP block device
14+
TDBStore store(&blockDevice);
15+
16+
// Dummy sketch stats for demonstration purposes
17+
struct SketchStats {
18+
uint32_t startupTime;
19+
uint32_t randomValue;
20+
uint32_t runCount;
21+
};
22+
23+
void setup()
24+
{
25+
Serial.begin(115200);
26+
while (!Serial);
27+
28+
// Wait for terminal to come up
29+
delay(1000);
30+
31+
Serial.println("FlashIAPBlockDevice + TDBStore Test");
32+
33+
// Feed the RNG for later content generation
34+
srand(micros());
35+
36+
// Initialize the flash IAP block device and print the memory layout
37+
blockDevice.init();
38+
Serial.printf("FlashIAP block device size: %llu\r\n", blockDevice.size());
39+
Serial.printf("FlashIAP block device read size: %llu\r\n", blockDevice.get_read_size());
40+
Serial.printf("FlashIAP block device program size: %llu\r\n", blockDevice.get_program_size());
41+
Serial.printf("FlashIAP block device erase size: %llu\r\n", blockDevice.get_erase_size());
42+
// Deinitialize the device
43+
blockDevice.deinit();
44+
45+
// Initialize the key/value store
46+
Serial.print("Initializing TDBStore: ");
47+
auto result = store.init();
48+
Serial.println(result == MBED_SUCCESS ? "OK" : "KO");
49+
if (result != MBED_SUCCESS)
50+
while (true);
51+
52+
// An example key name for the stats on the store
53+
const char statsKey[] { "stats" };
54+
55+
// Keep track of the number of sketch executions
56+
uint32_t runCount { 0 };
57+
58+
// Previous stats
59+
SketchStats previousStats;
60+
61+
// Get previous run stats from the key/value store
62+
Serial.println("Retrieving Sketch Stats");
63+
result = getSketchStats(statsKey, &previousStats);
64+
if (result == MBED_SUCCESS) {
65+
Serial.println("Previous Stats");
66+
Serial.print("\tStartup Time: ");
67+
Serial.println(previousStats.startupTime);
68+
Serial.print("\tRandom Value: ");
69+
Serial.println(previousStats.randomValue);
70+
Serial.print("\tRun Count: ");
71+
Serial.println(previousStats.runCount);
72+
73+
runCount = previousStats.runCount;
74+
75+
} else if (result == MBED_ERROR_ITEM_NOT_FOUND) {
76+
Serial.println("First execution");
77+
} else {
78+
Serial.println("Error reading from key/value store.");
79+
while (true);
80+
}
81+
82+
//Update the stats and save them to the store
83+
SketchStats currentStats { millis(), rand(), ++runCount };
84+
result = setSketchStats(statsKey, currentStats);
85+
86+
if (result == MBED_SUCCESS) {
87+
Serial.println("Sketch Stats updated");
88+
Serial.println("Current Stats");
89+
Serial.print("\tStartup Time: ");
90+
Serial.println(currentStats.startupTime);
91+
Serial.print("\tRandom Value: ");
92+
Serial.println(currentStats.randomValue);
93+
Serial.print("\tRun Count: ");
94+
Serial.println(currentStats.runCount);
95+
} else {
96+
Serial.println("Error storing to key/value store");
97+
while (true);
98+
}
99+
}
100+
101+
void loop()
102+
{
103+
// Do nothing
104+
}
105+
106+
// Retrieve a SketchStats from the k/v store
107+
int getSketchStats(const char* key, SketchStats* stats)
108+
{
109+
// Retrieve key/value info
110+
TDBStore::info_t info;
111+
auto result = store.get_info(key, &info);
112+
if (result == MBED_ERROR_ITEM_NOT_FOUND)
113+
return result;
114+
115+
// Allocate space for the value
116+
uint8_t buffer[info.size] {};
117+
size_t actual_size;
118+
119+
// Get the value
120+
result = store.get(key, buffer, sizeof(buffer), &actual_size);
121+
if (result != MBED_SUCCESS)
122+
return result;
123+
124+
memcpy(stats, buffer, sizeof(SketchStats));
125+
return result;
126+
}
127+
128+
// Store a SketchStats to the the k/v store
129+
int setSketchStats(const char* key, SketchStats stats)
130+
{
131+
auto result = store.set(key, reinterpret_cast<uint8_t*>(&stats), sizeof(SketchStats), 0);
132+
return result;
133+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# define SECRET_SSID "PortentaAccessPoint"
2+
# define SECRET_PASS "123Qwerty"
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#include <WiFi.h>
2+
#include "arduino_secrets.h"
3+
4+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
5+
char ssid[] = SECRET_SSID; // your network SSID (name)
6+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
7+
int keyIndex = 0; // your network key Index number (needed only for WEP)
8+
9+
int status = WL_IDLE_STATUS;
10+
11+
WiFiServer server(80);
12+
13+
void setup() {
14+
// put your setup code here, to run once:
15+
Serial.begin(9600);
16+
while (!Serial) {
17+
; // wait for serial port to connect. Needed for native USB port only
18+
}
19+
20+
Serial.println("Access Point Web Server");
21+
22+
pinMode(LEDR, OUTPUT);
23+
pinMode(LEDG, OUTPUT);
24+
pinMode(LEDB, OUTPUT);
25+
26+
// by default the local IP address of will be 192.168.3.1
27+
// you can override it with the following:
28+
// WiFi.config(IPAddress(10, 0, 0, 1));
29+
30+
// print the network name (SSID);
31+
Serial.print("Creating access point named: ");
32+
Serial.println(ssid);
33+
34+
//Create the Access point
35+
status = WiFi.beginAP(ssid, pass);
36+
if (status != WL_AP_LISTENING) {
37+
Serial.println("Creating access point failed");
38+
// don't continue
39+
while (true);
40+
}
41+
42+
// wait 10 seconds for connection:
43+
delay(10000);
44+
45+
// start the web server on port 80
46+
server.begin();
47+
48+
// you're connected now, so print out the status
49+
printWiFiStatus();
50+
51+
}
52+
53+
void loop() {
54+
55+
// compare the previous status to the current status
56+
if (status != WiFi.status()) {
57+
// it has changed update the variable
58+
status = WiFi.status();
59+
60+
if (status == WL_AP_CONNECTED) {
61+
// a device has connected to the AP
62+
Serial.println("Device connected to AP");
63+
} else {
64+
// a device has disconnected from the AP, and we are back in listening mode
65+
Serial.println("Device disconnected from AP");
66+
}
67+
}
68+
69+
WiFiClient client = server.available(); // listen for incoming clients
70+
71+
if (client) { // if you get a client,
72+
Serial.println("new client"); // print a message out the serial port
73+
String currentLine = ""; // make a String to hold incoming data from the client
74+
75+
while (client.connected()) { // loop while the client's connected
76+
77+
if (client.available()) { // if there's bytes to read from the client,
78+
char c = client.read(); // read a byte, then
79+
Serial.write(c); // print it out the serial monitor
80+
if (c == '\n') { // if the byte is a newline character
81+
82+
// if the current line is blank, you got two newline characters in a row.
83+
// that's the end of the client HTTP request, so send a response:
84+
if (currentLine.length() == 0) {
85+
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
86+
// and a content-type so the client knows what's coming, then a blank line:
87+
client.println("HTTP/1.1 200 OK");
88+
client.println("Content-type:text/html");
89+
client.println();
90+
91+
// the content of the HTTP response follows the header:
92+
client.print("<html><head>");
93+
client.print("<style>");
94+
client.print("* { font-family: sans-serif;}");
95+
client.print("body { padding: 2em; font-size: 2em; text-align: center;}");
96+
client.print("a { -webkit-appearance: button;-moz-appearance: button;appearance: button;text-decoration: none;color: initial; padding: 25px;} #red{color:red;} #green{color:green;} #blue{color:blue;}");
97+
client.print("</style></head>");
98+
client.print("<body><h1> LED CONTROLS </h1>");
99+
client.print("<h2><span id=\"red\">RED </span> LED </h2>");
100+
client.print("<a href=\"/Hr\">ON</a> <a href=\"/Lr\">OFF</a>");
101+
client.print("<h2> <span id=\"green\">GREEN</span> LED </h2>");
102+
client.print("<a href=\"/Hg\">ON</a> <a href=\"/Lg\">OFF</a>");
103+
client.print("<h2> <span id=\"blue\">BLUE</span> LED </h2>");
104+
client.print("<a href=\"/Hb\">ON</a> <a href=\"/Lb\">OFF</a>");
105+
client.print("</body></html>");
106+
107+
// The HTTP response ends with another blank line:
108+
client.println();
109+
// break out of the while loop:
110+
break;
111+
} else { // if you got a newline, then clear currentLine:
112+
currentLine = "";
113+
}
114+
} else if (c != '\r') { // if you got anything else but a carriage return character,
115+
currentLine += c; // add it to the end of the currentLine
116+
}
117+
118+
// Check to see if the client request was "GET /H" or "GET /L":
119+
if (currentLine.endsWith("GET /Hr")) {
120+
digitalWrite(LEDR, LOW); // GET /Hr turns the Red LED on
121+
}
122+
if (currentLine.endsWith("GET /Lr")) {
123+
digitalWrite(LEDR, HIGH); // GET /Lr turns the Red LED off
124+
}
125+
if (currentLine.endsWith("GET /Hg")) {
126+
digitalWrite(LEDG, LOW); // GET /Hg turns the Green LED on
127+
}
128+
if (currentLine.endsWith("GET /Lg")) {
129+
digitalWrite(LEDG, HIGH); // GET /Hg turns the Green LED on
130+
}
131+
if (currentLine.endsWith("GET /Hb")) {
132+
digitalWrite(LEDB, LOW); // GET /Hg turns the Green LED on
133+
}
134+
if (currentLine.endsWith("GET /Lb")) {
135+
digitalWrite(LEDB, HIGH); // GET /Hg turns the Green LED on
136+
}
137+
138+
}
139+
}
140+
// close the connection:
141+
client.stop();
142+
Serial.println("client disconnected");
143+
}
144+
145+
}
146+
147+
void printWiFiStatus() {
148+
// print the SSID of the network you're attached to:
149+
Serial.print("SSID: ");
150+
Serial.println(WiFi.SSID());
151+
152+
// print your WiFi shield's IP address:
153+
IPAddress ip = WiFi.localIP();
154+
Serial.print("IP Address: ");
155+
Serial.println(ip);
156+
157+
// print where to go in a browser:
158+
Serial.print("To see this page in action, open a browser to http://");
159+
Serial.println(ip);
160+
}

0 commit comments

Comments
 (0)