Dear ubidefeo, thanks for your answer!
First regarding the thingProperties.h and the browser refresh. I have test again and you are right, the contents of thingProperties.h stays in place as log no changes are made on the create thing properties…
Now to my code. Maybe in the future I will sell my project (Bee Hive monitoring/controlling) to customers - hopefully. And therefore, I need the possibility to save some credentials in the SAMD flash of the Arduino MKRs. I know that the flash will be flushed during uploading of a sketch, but this is not problematic for me.
When the Hive Controller was first powered on the customer must enter a password, the controller name and the WiFi Setting by using the CLI on the serial port. Later a “Service Menu” let the customer change the password or the WiFi SSID/Key or debug the controller. I have also implemented a session timeout and some code to "factory reset" the Controller if the customer forgot his password.
My code already works, but I am unsure if this was a good idea push the WiFi credentials with the function MyNwCredentials to the thingsProperties.h. What do you think?
Thanks in Advance
Andreas
Her also the complete sketch:
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
const char THING_ID[] = "024cd546-cc89-4790-b670-a36033796994";
char SSID[33]; // = SECRET_SSID; // Network SSID (name)
char PASS[64]; // = SECRET_PASS; // Network password (use for WPA, or use as key for WEP)
void MyNwCredetials(String MySSID, String MyKEY){
MySSID.toCharArray(SSID, 33);
MyKEY.toCharArray(PASS, 64);
}
void onRestartCpuChange();
float BeesINpm;
float BeesOUTpm;
float BeesOutside;
float HiveHumi;
float HiveTemp;
int OnlinePing;
int WiFiSignalStrength;
CloudLocation HiveLoc;
bool RestartCpu;
void initProperties(){
ArduinoCloud.setThingId(THING_ID);
ArduinoCloud.addProperty(BeesINpm, READ, 60 * SECONDS, NULL);
ArduinoCloud.addProperty(BeesOUTpm, READ, 60 * SECONDS, NULL);
ArduinoCloud.addProperty(BeesOutside, READ, 60 * SECONDS, NULL);
ArduinoCloud.addProperty(HiveHumi, READ, 60 * SECONDS, NULL);
ArduinoCloud.addProperty(HiveTemp, READ, 60 * SECONDS, NULL);
ArduinoCloud.addProperty(OnlinePing, READ, 300 * SECONDS, NULL);
ArduinoCloud.addProperty(WiFiSignalStrength, READ, 60 * SECONDS, NULL);
ArduinoCloud.addProperty(HiveLoc, READ, 60 * SECONDS, NULL);
ArduinoCloud.addProperty(RestartCpu, READWRITE, ON_CHANGE, onRestartCpuChange);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
// Definitions to use the SAMD flash
boolean SerSess = false;
String Password;
String Hostname;
String HCssid;
String HCkey;
const int MaxPw = 32;
const int MinPw = 6;
const int MaxSerRx = 64;
unsigned long SesTimeout = 60000;
typedef struct {
boolean valid;
char name[33];
char pw[41];
char ssid[33];
char key[64];
} SetupStore;
FlashStorage(cpu_flash, SetupStore);
SetupStore Setup;
// thingProperties.h needs this setup...
// HiveController Status LEDs Setup
// CUTTED OUT - the attachment MySketch.ino is complete... // LED connected to pin A6
// Debugging option
boolean DebugBC = false; // BeeCounter debug display on serial monitor
boolean DebugNO = false; // Normal operation debug display on serial monitor
// SHT35 Grove Temp Humi Sensor initialization
// For SAMD core
#// CUTTED OUT - the attachment MySketch.ino is complete...
// Main SETUP
// ====================================================================================================================
void setup() {
// Initialize LED Pins
// CUTTED OUT - the attachment MySketch.ino is complete...
// Reset to factory defaults if the status button was pressed during startup...
pinMode(PushButtonPin, INPUT_PULLUP);
if (digitalRead(PushButtonPin) == LOW) {
Hostname = " ";
Password = " ";
HCssid = " ";
HCkey = " ";
Password.toCharArray(Setup.pw, 41);
Hostname.toCharArray(Setup.name, 33);
HCssid.toCharArray(Setup.ssid, 33);
HCkey.toCharArray(Setup.key, 64);
Setup.valid = false;
cpu_flash.write(Setup);
}
// Controller Setup - get the stored Setup
Setup = cpu_flash.read();
Password = Setup.pw;
Hostname = Setup.name;
HCssid = Setup.ssid;
HCkey = Setup.key;
MyNwCredetials(HCssid, HCkey);
// Initialize serial and wait for port to open:
Serial.begin(38400);
while (Setup.valid == false) { // No valid Setup in the SAMD flash?
while (!Serial) { // Wait until serial IF is ready
Led_NoPw();
}
Init_Setup(); // Now ask for initial informations - serial is ready now
MyNwCredetials(HCssid, HCkey); // Put ssid and key to thingProperties.h
}
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(0);
ArduinoCloud.printDebugInfo();
}
// Main LOOP
// ====================================================================================================================
void loop() {
// Get Beehive (temperature, humidity) and send HiveLocation
// Get WiFi Signal Level
// Get Ctrl Status push button state
// Switch Off all Status LEDs every 5 Seconds
// CUTTED OUT - the attachment MySketch.ino is complete...
// Controller Menue - ask for the password
if (Setup.valid == true && SerSess == false && Serial.available()) {
Serial.println("");
Serial.print(Hostname.substring(0,32));
Serial.println(" - Hive Controller Service Menue");
Serial.println("");
Serial.print("Password: ");
String temp = Ser_Rx(false);
if (Password == Ser_Rx(true)) {
SerSess = true;
}
else {
Serial.println("");
Serial.println("Wrong password! Press <Enter> to try again...");
Serial.println("");
}
}
// Controller Menue - get serial input and start sub-menues
while (SerSess == true) {
HC_Menue();
String MenueChoice = Ser_Rx(false);
if (MenueChoice == "1") {HC_SerStat();}
else if (MenueChoice == "2") {Change_Pw();}
else if (MenueChoice == "3") {Change_Hn();}
else if (MenueChoice == "4") {Change_WiFi();}
else if (MenueChoice == "5") {Show_TempHumi();}
else if (MenueChoice == "6") {Debug_NO();}
else if (MenueChoice == "0") {
SerSess = false;
SerPri_ClScr();
SerPri_NoOpe();
}
}
// Send and get updates from the Arduino IoT Cloud
unsigned long currentMillisIOT = millis();
if (currentMillisIOT - previousMillisIOT >= intervalIOT) {
// CUTTED OUT - the attachment MySketch.ino is complete...
}
// Get initial Password
void Init_Setup() {
// CUTTED OUT - the attachment MySketch.ino is complete...
}
// Serial Communication - get characters from Serial (USB) Port
String Ser_Rx(boolean HideEcho) {
// CUTTED OUT - the attachment MySketch.ino is complete...
}
// Controller Menue - change HiveContrl name
void Change_WiFi() {
if (Setup.valid == true) {
SerPri_ClScr();
Serial.println("You can change the Hive Controller WiFi setup now.");
Serial.println("");
Serial.print("WiFi SSID: ");
HCssid = Ser_Rx(false);
Serial.println("");
Serial.print("WiFi Key: ");
HCkey = Ser_Rx(false);
Password.toCharArray(Setup.pw, 41);
Hostname.toCharArray(Setup.name, 33);
HCssid.toCharArray(Setup.ssid, 33);
HCkey.toCharArray(Setup.key, 64);
Setup.valid = true;
cpu_flash.write(Setup);
Serial.println("");
Serial.println("");
Serial.println("The WiFi Setup was stored sucessfully!");
delay(2000);
MyNwCredetials(HCssid, HCkey);
WiFiConnectionHandler ArduinoIoTPreferredConnection(Setup.ssid, Setup.key);
SerPri_ClScr();
}
}
MySketch.ino (27 KB)