Hi,
I have been searching for some example code that will display, an easier to read WiFi bar strength system, to show signal strength. At the moment, it currently shows a number, which is ok for me, but not for other users. I have not been able to locate a code that uses OLED (u8g2) with the Uno WiFi Rev2 (WiFiNiNa) to do this.
Here is my code (its basically a dust monitor that transmits data over WiFi with a local OLED screen that I would like for it display WiFi strength in bars):
/*
Upload Source Code
*/
#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0);
int measurePin = A0;
int ledPower = 12;
///////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)
unsigned int samplingTime = 280;
unsigned int deltaTime = 40;
unsigned int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup(){
Serial.begin(9600);
u8g2.begin();
while (status != WL_CONNECTED) {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_5x7_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(0,6,"Attempting to connect "); // write something to the internal memory
u8g2.setFont(u8g2_font_5x7_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(0,14,"to network named: "); // write something to the internal memory
u8g2.setFont(u8g2_font_luIS12_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.setCursor(0,28);
u8g2.print(ssid);
u8g2.sendBuffer(); // transfer internal memory to the display
delay(3000);
Serial.print("Attempting to connect to network named: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
server.begin();
long rssi = WiFi.RSSI();
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.print("signal strength (RSSI): ");
Serial.println(rssi);
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_5x7_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(0,6,"Connected to network: "); // write something to the internal memory
u8g2.setFont(u8g2_font_5x7_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.setCursor(0, 14);
u8g2.print(ssid);
u8g2.setFont(u8g2_font_5x7_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(0,22,"Signal Strength (RSSI): "); // write something to the internal memory
u8g2.setFont(u8g2_font_5x7_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.setCursor(0, 32);
u8g2.print(rssi);
u8g2.sendBuffer(); // transfer internal memory to the display
delay(4000);
pinMode(ledPower,OUTPUT);
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_fub20_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(8,29,"Welcome"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(800);
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_fub20_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(40,29,"To"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(800);
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_fub20_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(10,29,"Clean Air"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(2000);
}
void loop(){
long rssi = WiFi.RSSI();
digitalWrite(ledPower,LOW);
delayMicroseconds(samplingTime);
voMeasured = analogRead(measurePin);
delayMicroseconds(deltaTime);
digitalWrite(ledPower,HIGH);
delayMicroseconds(sleepTime);
calcVoltage = voMeasured*(5.0/1024);
dustDensity = 0.17*calcVoltage-0.1;
if ( dustDensity < 0)
{
dustDensity = 0.00;
}
Serial.println("Raw Signal Value (0-1023):");
Serial.println(voMeasured);
Serial.println("Voltage:");
Serial.println(calcVoltage);
Serial.println("Dust Density:");
Serial.println(dustDensity);
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_pcsenior_8f); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(0,6,"Dust Density "); // write something to the internal memory
u8g2.setFont(u8g2_font_fur20_tn); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.setCursor(0, 31);
u8g2.print(dustDensity);
u8g2.setFont(u8g2_font_pxplusibmvga9_tr); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(85,29,"ug/m3 ");
u8g2.setFont(u8g2_font_5x7_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.drawStr(83,18,"Wifi: "); // write something to the internal memory
u8g2.setFont(u8g2_font_5x7_tf); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.setCursor(113, 18);
u8g2.print(rssi);
u8g2.sendBuffer(); // transfer internal memory to the display
delay(1000);
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
client.print("Dust Density (ug/m3): ");
client.println(dustDensity);
client.print("Raw Signal Value (0-1023):");
client.println(voMeasured);
client.print("Voltage:");
client.println(calcVoltage);
client.print("signal strength (RSSI): ");
client.println(rssi);
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
client.stop();
Serial.println("client disonnected");
}
}
type or paste code here