#include <SPI.h>
#include <WiFiNINA.h>
const int RSSI_MAX =-50;// define maximum strength of signal in dBm
const int RSSI_MIN =-100;// define minimum strength of signal in dBm
const int displayEnc=0;// set to 1 to display Encryption or 0 not to display
//char ssid[] = "Network Name"; // your network SSID (name)
//char pass[] = "Network Password"; // your network password (use for WPA, or use as key for WEP)
char ssid[] = "Network Name"; // your network SSID (name)
char pass[] = "Network Password"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress serverIP(10,190,17,38); // numeric IP for Google (no DNS)
//char server[] = "http://10.190.17.38:81/"; // name address for Google (using DNS)
IPAddress no_ip=(0,0,0,0);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
int pingResult;
int sensorValue=0;
int sensorPin=A0;
WiFiClient wifi_Client;
int status = WL_IDLE_STATUS;
int once=1;
int connectedS=0;
void setup() {
pinMode(LED_BUILTIN,OUTPUT);
Serial.begin(9600);
Serial.println("Attempting to connect to WPA network...");
Serial.print("SSID: ");
Serial.println(ssid);
IPAddress no_ip=WiFi.localIP();
IPAddress ip=WiFi.localIP();
printIPs();
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.disconnect();
listNetworks();
while (status != WL_CONNECTED && ip==no_ip) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
delay(3500);
ip = WiFi.localIP();
delay(500);
printIPs();
delay(1500);
Serial.println(status);
}
printIPs();
delay(1500);
Serial.println("Connected");
}
void printIPs()
{
Serial.println("my ip");
IPAddress ip = WiFi.localIP();
Serial.println(ip);
Serial.println("no ip");
Serial.println(no_ip);
}
void loop() {
Serial.println(status);
IPAddress ip = WiFi.localIP();
printIPs();
if ( ip!=no_ip)
{
digitalWrite(LED_BUILTIN,HIGH);
delay(100);
// tryToConnect();
sensorValue = analogRead(sensorPin);
// turn the ledPin on
//digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
Serial.println(sensorValue);
Serial.println("---");
delay(sensorValue);
if (sensorValue>400 )
{
once=0;
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
Serial.println(serverIP);
if (wifi_Client.connect(serverIP, 81)) {
Serial.println("connected to server");
// Make a HTTP request:
String strSensorValue=String(sensorValue);
wifi_Client.println("GET /?device=LF_Wave&sensorValue="+strSensorValue);
wifi_Client.println();
Serial.println("Logged Record");
}
delay(sensorValue);
/*
* String readableTime;
getReadableTime(readableTime);
*/
delay(15000);
}
delay(1000);
}
}
void getReadableTime(String &readableTime) {
unsigned long currentMillis;
unsigned long seconds;
unsigned long minutes;
unsigned long hours;
unsigned long days;
currentMillis = millis();
seconds = currentMillis / 1000;
minutes = seconds / 60;
hours = minutes / 60;
days = hours / 24;
currentMillis %= 1000;
seconds %= 60;
minutes %= 60;
hours %= 24;
if (days > 0) {
readableTime = String(days) + " ";
}
if (hours > 0) {
readableTime += String(hours) + ":";
}
if (minutes < 10) {
readableTime += "0";
}
readableTime += String(minutes) + ":";
if (seconds < 10) {
readableTime += "0";
}
readableTime += String(seconds) + " ago";
}
void tryToConnect()
{
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
IPAddress ip=WiFi.localIP();
Serial.println(no_ip);
while (status != WL_CONNECTED ) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
delay(1500);
ip = WiFi.localIP();
Serial.println(ip);
delay(1500);
Serial.println(status);
}
delay(1500);
Serial.println("Connected");
}
void listNetworks() {
Serial.println("Wifi scan started");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("Wifi scan ended");
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));// SSID
Serial.print(WiFi.RSSI(i));//Signal strength in dBm
Serial.print("dBm (");
Serial.print(dBmtoPercentage(WiFi.RSSI(i)));//Signal strength in %
Serial.print("% )");
if(WiFi.encryptionType(i) == ENC_TYPE_NONE)
{
Serial.println(" <<***OPEN***>>");
}else{
Serial.println();
}
delay(10);
if (WiFi.SSID(i)=="Network Name")
{
//digitalWrite(LED_BUILTIN,HIGH);
tryToConnect();
//digitalWrite(LED_BUILTIN,LOW);
}
}
}
Serial.println("");
// Wait a bit before scanning again
delay(500);
}// loop
/*
* Written by Ahmad Shamshiri
* with lots of research, this sources was used:
* https://support.randomsolutions.nl/827069-Best-dBm-Values-for-Wifi
* This is approximate percentage calculation of RSSI
* WiFi Signal Strength Calculation
* Written Aug 08, 2019 at 21:45 in Ajax, Ontario, Canada
*/
int dBmtoPercentage(int dBm)
{
int quality;
if(dBm <= RSSI_MIN)
{
quality = 0;
}
else if(dBm >= RSSI_MAX)
{
quality = 100;
}
else
{
quality = 2 * (dBm + 100);
}
return quality;
}//dBmtoPercentage
I've been having an issue recently with some of my arduino units and I'm unsure of how to troubleshoot this.
The purpose of the units I'm using is to recognize the throughput of product in comparison to an established goal. Using an outside sourced PowerApp, operators tell the arduino the assembly that they're running and the unit will then reflect the number on a separate PowerApp.
I'm using an IR Distance Sensor and its plugged into 5V, GND, and A0 on the Power and Analog ports.
Lately I've had an issue where the units won't accept an assembly and typically when this happens, I find that the LED, reference L is blinking.
If I restart the units, sometimes the Arduino behaves as expected but sometimes it doesn't, and the LED goes back into a blinking cycle.
The program only uses 34% of storage and 12% of memory. Does anyone have any insight on this issue?