Tmathews1988
Im using a Nano IOT communicating via Modbus TCP to Home Assistant. is there a function in Open PLC that IF it loses WIFI connection i can toggle a internal bit?
 
Basically the nano is controlling a heating device, im using home assistant to tell the nano when to turn on/off the heating device based on temperature sensors and if i lose WIFI connection and the Nano doesn't receive the off command i dont want my house getting 107 degrees lol.  
Reply 0 0
fg71
Simple approach is to see how Arduino do it. You have 2 functions setup() and loop()
The setup() function only running once and loop() function is running forever.
 
What you only need to do is to have logic in loop() function that check the status of your connection.
 
Here is simple example in Arduino IDE.
If your Nano is connected to to your router it will print Set the heater to input
And if you change your SSID or PASSWORD so it wouldn't connect to your router it will print Set the heater to normal temp
#include <WiFi.h>

const char* ssid     = "SSID";
const char* password = "PASSWORD";

void setup() {
  Serial.begin(115200);
  delay(2000);
  WiFi.begin(ssid, password);

  if (WiFi.status() != WL_CONNECTED) {
        delay(500);
    }
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Set the heater to normal temp");
  } else {
    Serial.println("Set the heater to input");
  }

}
 
Reply 1 0
thiagoralves
I guess the main point should be: do you need WiFi to read your temperature sensors? This is an odd architecture in my views. Sensors should be local, so that all decision making is done locally on the device itself. WiFi should only be required for monitoring, like setting new temperature set points, switching between heating / cooling, but not for decision making.
 
Anyway, answering your specific question, I think that indeed the only way would be through an Arduino Extension. The Modbus TCP enabled already gives you a working connection. You can use that to ping other servers to ensure you’re connected
Reply 1 0
Tmathews1988
I guess the main point should be: do you need WiFi to read your temperature sensors? This is an odd architecture in my views. Sensors should be local, so that all decision making is done locally on the device itself. WiFi should only be required for monitoring, like setting new temperature set points, switching between heating / cooling, but not for decision making.
 
Anyway, answering your specific question, I think that indeed the only way would be through an Arduino Extension. The Modbus TCP enabled already gives you a working connection. You can use that to ping other servers to ensure you’re connected
i decided to add a temperature sensor DS18B20, taking your advice and keeping the control local and adjusting setpoints remotely. i am assuming because there is a DS18B20 function block that there is native support for these sensors and i shouldn't have to do much? i tried using this on D2 on the NANO but i cannot seem to get any reading? are there any additional steps i need to take?
Reply 0 0
thiagoralves
The block should work just fine. Just remember to use the actual pin number value (like 2, or 3, etc) as input for the block, instead of PLC locations like %IX0.0, %IX0.1, etc. Also, remove the pin you're using with the DS18B20 block from the list at the I/O Config screen on the upload dialog.
Reply 1 0
Tmathews1988
The block should work just fine. Just remember to use the actual pin number value (like 2, or 3, etc) as input for the block, instead of PLC locations like %IX0.0, %IX0.1, etc. Also, remove the pin you're using with the DS18B20 block from the list at the I/O Config screen on the upload dialog.
That was my issue. Thanks again...
Reply 0 0
Tmathews1988
The block should work just fine. Just remember to use the actual pin number value (like 2, or 3, etc) as input for the block, instead of PLC locations like %IX0.0, %IX0.1, etc. Also, remove the pin you're using with the DS18B20 block from the list at the I/O Config screen on the upload dialog.
i got everything working like i want except i still keep dropping wifi and the NANO doesn't want to reconnect. it will continue to run even when not connected but id like to get this working. i tried using Arduino extension and a loop sketch i found to keep connecting to wifi if wifi drops but i get a error every time i try to use any Arduino extension. attached is my log and program. Any idea what i can do?
Reply 0 0
thiagoralves
I don’t have much experience with the nano, but try using a regular ESP32 board. Maybe the WiFi on the nano is not very reliable
Reply 0 0
fg71
got everything working like i want except i still keep dropping wifi and the NANO doesn't want to reconnect.

You need a function to check if you are connected, and reconnect if not connected else passthru. You need this function to be running in loop() function. When you have the function logic correct and still have problem you should check if you have the latest firmware. When you say Nano IoT I guessing you mean Nano 33 Iot you could se Arduino Ide to update the firmware.

Use the Firmware Updater in Arduino IDE – Arduino Help Center

#include <WiFi.h>

// WiFi credentials
const char* ssid = "SSID";
const char* password = "PASS";

// Variables for millis() timing
unsigned long previousMillis = 0;
const unsigned long interval = 1000; // Check every 1 second

// Function to check WiFi connection with millis()
void checkWiFiConnection() {
  while (WiFi.status() != WL_CONNECTED) { // Repeat until connected
    unsigned long currentMillis = millis();

    // Check if interval has passed
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis; // Update the previousMillis
      
      Serial.println("Still trying to connect to WiFi...");
      
      // Attempt to reconnect if needed (optional)
      if (WiFi.status() == WL_DISCONNECTED) {
        WiFi.begin(ssid, password); // Restart WiFi connection
      }
    }
  }

  // Once connected
  Serial.println("WiFi Connected!");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(115200);
  
  // Start WiFi connection
  WiFi.begin(ssid, password);
  Serial.println("Connecting to WiFi...");
}

void loop() {
  // Check WiFi connection in the loop
  checkWiFiConnection();
  
  // Add your other main logic here after WiFi is connected
  Serial.println("Connected! Running main logic...");
  delay(5000); // Example: Just to demonstrate once connected
}
 
Reply 0 0
Tmathews1988

You need a function to check if you are connected, and reconnect if not connected else passthru. You need this function to be running in loop() function. When you have the function logic correct and still have problem you should check if you have the latest firmware. When you say Nano IoT I guessing you mean Nano 33 Iot you could se Arduino Ide to update the firmware.

Use the Firmware Updater in Arduino IDE – Arduino Help Center

  1. #include <WiFi.h>
  2. // WiFi credentials
  3. const char* ssid = "SSID";
  4. const char* password = "PASS";
  5. // Variables for millis() timing
  6. unsigned long previousMillis = 0;
  7. const unsigned long interval = 1000; // Check every 1 second
  8. // Function to check WiFi connection with millis()
  9. void checkWiFiConnection() {
  10.   while (WiFi.status() != WL_CONNECTED) { // Repeat until connected
  11.     unsigned long currentMillis = millis();
  12.     // Check if interval has passed
  13.     if (currentMillis - previousMillis >= interval) {
  14.       previousMillis = currentMillis; // Update the previousMillis
  15.       
  16.       Serial.println("Still trying to connect to WiFi...");
  17.       
  18.       // Attempt to reconnect if needed (optional)
  19.       if (WiFi.status() == WL_DISCONNECTED) {
  20.         WiFi.begin(ssid, password); // Restart WiFi connection
  21.       }
  22.     }
  23.   }
  24.   // Once connected
  25.   Serial.println("WiFi Connected!");
  26.   Serial.print("IP Address: ");
  27.   Serial.println(WiFi.localIP());
  28. }
  29. void setup() {
  30.   Serial.begin(115200);
  31.   
  32.   // Start WiFi connection
  33.   WiFi.begin(ssid, password);
  34.   Serial.println("Connecting to WiFi...");
  35. }
  36. void loop() {
  37.   // Check WiFi connection in the loop
  38.   checkWiFiConnection();
  39.   
  40.   // Add your other main logic here after WiFi is connected
  41.   Serial.println("Connected! Running main logic...");
  42.   delay(5000); // Example: Just to demonstrate once connected
  43. }
 
I tried using a couple different Arduino sketches like the one you attached but I kept getting a error when I tried to add it with OpenPLC via Arduino extensions. I'm not familiar with Arduino IDE code I am familiar with PLC ladder. I am learning lol.
Reply 0 0
fg71
When you update the WiFi firmware of your board it will update WiFi module. And when you upload a program to your board you are updating the bootloader.
That is not the same thing.
 
Thats why I said have you checked your Nano IoT board if you use the latest WiFi firmware for your board.
If there is issues with WiFi its the firmware that need to be updated and not the program.
 
So that is your next option.
Reply 1 0
fg71
Here is the link to Arduino WiFi firmware update for your board I believe.
Reply 1 0
Tmathews1988
Here is the link to Arduino WiFi firmware update for your board I believe.
Thanks... I will try this tonight.
Reply 0 0
Tmathews1988
Here is the link to Arduino WiFi firmware update for your board I believe.
i tried the firmware and that didn't seem to help i kept dropping connection and it did not want to reconnect. i may have found the issue today, i left my laptop plugged into it and it didn't drop connection once in 8 hours so I'm thinking its a power supply issue at this point, i soldered in some capacitors and put it on its on power supply instead of robing it from another board. i guess we will see. i still don't like that it wont reconnect after it drops WIFI. i guess if i have to ill use a raspberry pi instead, i was hoping to stay cheep.
Reply 0 0
fg71
You could check if your Router channel for 2.4GHz band and use other to make the signal stronger
It could be that its not Nano IoT that is the problem but your signal for the 2.4 GHz band.
And modern Router today do not come with 1 antenna and I guess that is not enough. 🙂
 
I have 3 on my Router, and the only problem I have with Nano Esp32 is connection delay of almost 1 seconds.
But I could solve that issue by software code there other boards do not need it.
Reply 1 0
Reply