We begin with a WiFi scanning example from the Espressif 32 repository.
/* WiFi scan Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "sdkconfig.h"
#include <Arduino.h>
#include <WiFi.h>
void wifiScan() {
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
delay(10);
}
}
Serial.println("");
}
#if !CONFIG_AUTOSTART_ARDUINO
void arduinoTask(void *pvParameter) {
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
Serial.begin(115200);
delay(100);
while(1) {
wifiScan();
// Wait a bit before scanning again
delay(5000);
}
}
extern "C" void app_main()
{
// initialize arduino library before we start the tasks
initArduino();
xTaskCreate(&arduinoTask, "arduino_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
}
#else
void setup() {
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
Serial.begin(115200);
delay(100);
}
void loop() {
wifiScan();
// Wait a bit before scanning again
delay(5000);
}
#endif
There is a lot in this example, so let's consider a simple "hola, mundo" program instead:
#include <Arduino.h>
void setup() {
Serial.begin(115200);
delay(100);
}
void loop() {
delay(1000);
Serial.println("Hola, mundo!");
}
If you encounter problems, hit the "Interrupt" button to interrupt the current process. Failing that, hit the "Teardown sandbox" button, then run it again. Note that "Teardown sandbox" destroys the environment that you were using and allows other users to take a turn. You may need to wait a few seconds before another board becomes available.
Can you modify the example above to print a value that increases by 1 for each loop iteration? Documentation about Serial.println may be helpful.