Is wifimanager allowed for IoT cloud? How do I connect to wifi using wifimanager instead of using the secrets tab?
I figured it out. Here's my code:
/*
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
int random_value;
bool led_switch;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
int LED = 1;
char ssid[32]; // SSID char limit
char pass[63]; // PASS char limit
void captivePortal();
void setup() {
// Set pin mode
pinMode(LED,OUTPUT);
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Start WiFiManager
captivePortal();
// Defined in thingProperties.h
initProperties();
preferredConnectionHandler(ssid, pass);
Serial.println("Connecting to WiFi");
Serial.println(SSID);
Serial.println(PASS);
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
random_value = random(0, 500);
delay(500);
}
/*
Since LedSwitch is READ_WRITE variable, onLedSwitchChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLedSwitchChange() {
// Add your code here to act upon LedSwitch change
if(led_switch){
digitalWrite(LED, HIGH);
}
else{
digitalWrite(LED, LOW);
}
}
void captivePortal(){
// put your setup code here, to run once:
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
// it is a good practice to make sure your code sets wifi mode how you want it.
//WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wm;
// reset settings - wipe stored credentials for testing
// these are stored by the esp library
wm.resetSettings();
// Automatically connect using saved credentials,
// if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
// if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
// then goes into a blocking loop awaiting configuration and will return success result
bool res;
// res = wm.autoConnect(); // auto generated AP name from chipid
// res = wm.autoConnect("AutoConnectAP"); // anonymous ap
res = wm.autoConnect("Fire Alarm","password"); // password protected ap
if(!res) {
Serial.println("Failed to connect");
// ESP.restart();
}
else {
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
String str_ssid = wm.getWiFiSSID(false);
int len = str_ssid.length() + 1;
str_ssid.toCharArray(ssid,len);
Serial.println(ssid);
String str_pass = wm.getWiFiPass(false);
len = str_pass.length() + 1;
str_pass.toCharArray(pass,len);
Serial.println(pass);
}
}
thingProperties.h
// Code generated by Arduino IoT Cloud, DO NOT EDIT.
#include <WiFiManager.h>
#include <ArduinoIoTCloud.h>
#include <Arduino_ESP32_OTA.h>
//#include <Arduino_ConnectionHandler.h>
const char DEVICE_LOGIN_NAME[] = "<replace with your own>";
char SSID[32]; // Network SSID (name)
char PASS[63]; // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[] = "<replace with your own>"; // Secret device password
void onLedSwitchChange();
int random_value;
bool led_switch;
void initProperties(){
ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
ArduinoCloud.addProperty(random_value, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(led_switch, READWRITE, ON_CHANGE, onLedSwitchChange);
}
void preferredConnectionHandler(char* ssid, char* pass) {
strcpy(SSID, ssid);
strcpy(PASS, pass);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
platformio.ini
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 9600
lib_ignore = WiFiNINA
lib_deps =
https://github.com/tzapu/WiFiManager.git#v2.0.15-rc.1
arduino-libraries/ArduinoIoTCloud@^1.10.0
arduino-libraries/Arduino_ESP32_OTA@^0.1.0
5 Likes
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.