Upload sketch through WIFI

Hi,
I am using arduino nano 33 iot and I would like to upload sketches through wifi(assuming it is already connected), specifically changing the wifi network(ssid and pass).
I'm using the basic sketch where the credentials are in a seperate header file named "arduino_secrets":

#define SECRET_SSID "WIFI_SSID"
#define SECRET_PASS "WIFI_PASSWORD"

Is it possible?
Thanks

use my ArduinoOTA library. (or the WiFi101OTA library with modified include)

I am using arduino nano 33 iot and I would like to upload sketches through wifi(assuming it is already connected), specifically changing the wifi network(ssid and pass).

Currently, there is no official Arduino OTA library for the WiFi Nina of the Nano 33 IoT.

Are there any modification that I can do with other libraries for it to work?
I tried running WiFi101_OTA example of ArduinoOTA library and it stopped in not finding the wifi module:
"WiFi shield not present"
Thanks

and did you change the include to WiFiNINA?

Juraj:
and did you change the include to WiFiNINA?

Yes, now I did and it works, thanks.
I have a small issue with this though not sure how to solve it. I uploaded a code to one board with the default password and wrote it when asked for password while uploading through OTA. It didn't work out and switched things in the code and now i'm unable to upload code through OTA no matter what.
How can I reset this thing in the IDE so the process will begin as in nothing happend?
When doing the process correctly to a new board everything worked fine, but still want to use the other one.
Thanks

omri1:
Yes, now I did and it works, thanks.
I have a small issue with this though not sure how to solve it. I uploaded a code to one board with the default password and wrote it when asked for password while uploading through OTA. It didn't work out and switched things in the code and now i'm unable to upload code through OTA no matter what.
How can I reset this thing in the IDE so the process will begin as in nothing happend?
When doing the process correctly to a new board everything worked fine, but still want to use the other one.
Thanks

I don't understand what you wrote, but the uploaded sketch must support OTA too for the next upload.

Juraj:
I don't understand what you wrote, but the uploaded sketch must support OTA too for the next upload.

Ok, I just did a reset to the IDE and things worked out.
Thanks!

Hi,
In case of wifi disconnecting, what is the right procedure with ArduinoOTA(how to release resources and restart it)? I have an issue that wifi disconnects after about an hour and is not able to connect again(doesn’t happen when commenting ArduinoOTA related code).
Thanks
This is my code:

#include <SPI.h>
#include <WiFiNINA.h>
#include <Adafruit_SHT31.h>
#include <Wire.h>
#include <Digital_Light_TSL2561.h>
#include <ArduinoOTA.h>
#include "arduino_secrets.h" 

char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

String deviceID;
float temperature;
float humidity;
int light;
float weight;
int smA;
int smB;

unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 90000; // delay between updates

void setup() {
  Serial.begin(9600);
  delay(2000);
  Serial1.begin(9600);
  Serial.println("Plants");
  pinMode(LED_BUILTIN, OUTPUT);

  wifiTask();

}

void loop() {
  #define INTERVAL 5 * 1000
  static unsigned long lastDataRecord = 0;
  
  wifiTask();

  if ( millis() - lastDataRecord > INTERVAL )
  {
    updateSensors();
    prints();
    lastDataRecord = millis();
  }

  getWeight();

}

void updateSensors() { 
  getTemperatureHumidity();
  getSm();
  getLight();
}


int httpRequest() {
  static WiFiClient client;
  static const char WEBSITE[] = "api.pushingbox.com";
  static const String devid = "v387AFB20A3CADC6";

  client.stop();

  if (client.connect(WEBSITE, 80)) {
    Serial.println("connecting...");
    client.print("GET /pushingbox?devid=" + devid
       + "&deviceID=" + (String) deviceID
       + "&humidity=" + (String) humidity
       + "&temperature="      + (String) temperature
       + "&light="      + (String) light
       + "&weight="      + String(weight, 3)
       + "&smA="      + (String) smA
       + "&smB="      + (String) smB
         );

    
      client.println(" HTTP/1.1"); 
      client.print("Host: ");
      client.println(WEBSITE);
    client.println("User-Agent: ArduinoWiFi/1.1");
    client.println("Connection: close");
    client.println();
    Serial.println("Sent data");
    
    lastConnectionTime = millis();
    return 1;
  } else {
    Serial.println("failed to connect to http");
    return 0;
  }
}

void getTemperatureHumidity() {
  static Adafruit_SHT31 sht31 = Adafruit_SHT31();
  static bool shtValid = false;
  if (shtValid) {
    temperature = sht31.readTemperature();
    humidity = sht31.readHumidity();
    if (isnan(temperature) || isnan(humidity))
      {
          temperature = 0;
          humidity = 0;
          Serial.println("Failed to read from DHT");
          shtValid = false;
      }
  }
  else {
    if (!sht31.begin(0x44)) {
      Serial.println("Couldn't find SHT31");
    }
    else {
      shtValid = true;
    }
  }
}

void getLight() {
  static bool lightVaild = false;
  if (isTSLconnected()) {
    if (lightVaild) {
      light = TSL2561.readVisibleLux();
    }
    else {
      TSL2561.init();
      lightVaild = true;
    }
  }
  else { 
    Serial.println("TSL2561 not connected");
    light = -1;
    lightVaild = false;
  }
}

bool isTSLconnected() {
  Wire.beginTransmission(0x29);
  int error = Wire.endTransmission();
  if (error == 0) {
    return true;
  }
  else {
    return false;
  }
}

void getWeight() {
  #define TIME_OUT 5000
  static String readString;
  static unsigned long lastTimeWeightActivated = 0;
  if (Serial1.available())
  {
    char c = Serial1.read();
    readString += c;
    if (c == 'g' ) {
      lastTimeWeightActivated = millis();
      int strLen =  readString.length(); 
      
      //Serial.print("strlen: ");Serial.println(strLen);
      //Serial.print("readString: ");Serial.println(readString);

      if (strLen == 12) { //DEBORAH
        weight = readString.substring(5,10).toFloat();
      }
      else if (strLen == 19) {
        if (readString.charAt(6) == 'N') { // NINA
          weight = readString.substring(11,17).toFloat();
          weight /= 1000.0;
        }
        else { //PETER
          weight = readString.substring(10,16).toFloat(); 
        }  
      }
      readString = "";
    }
    //Serial.print("weight: "); Serial.println(weight,3);
  }
  else if (millis() - lastTimeWeightActivated > TIME_OUT) {
      weight = 0;
  }
}

void getSm() {
  #define SMAPIN A0
  #define SMBPIN A2
  smA=analogRead(SMAPIN);
  smB=analogRead(SMBPIN);
}

void wifiTask() {
    static int state = 0;
    static int wifiConnectTry = 0;
    static int wifiStatus = WL_IDLE_STATUS;
    static int httpStatus = 0;
    static int httpConnectTry = 0;
    static unsigned long previousMillis = 0;
    static bool otaConnected = false;
    unsigned long currentMillis = 0;

    #define WIFI_CONNECT_TIMEOUT 10000 // seconds waiting between re-connection attempts
    #define HTTP_CONNECT_TIMEOUT 5000
    #define HTTP_CONNECTED 1
    #define HTTP_DISCONNECTED 0

enum WIFI_STATE_TYPE { WIFI_CONNECT, // Case 0
                       HTTP_CONNECT,
                       HTTP_POLL,
                       WIFI_STATE_RESTART = 255
                     };


switch ( state )
{
  case WIFI_CONNECT:
    if ( wifiStatus == WL_CONNECTED )
    {
        Serial.println("WIFI Connected");
        printWifiStatus();
        state++;
        digitalWrite( LED_BUILTIN, HIGH );
        if (!otaConnected) {
          otaConnected = true;
          ArduinoOTA.begin(WiFi.localIP(), "Arduino", "12345", InternalStorage);
        }
        break;
    }
    if ( millis() - previousMillis < WIFI_CONNECT_TIMEOUT && wifiConnectTry > 0 )
    {
      break;
    }

    if ( wifiConnectTry > 10 )
    {
      state = WIFI_STATE_RESTART;
      break;
    }

    wifiStatus = WiFi.begin( ssid, pass );
    previousMillis = millis();
    wifiConnectTry++;
    Serial.print( "Try: " );
    Serial.print( wifiConnectTry );
    Serial.print( " Status: " );
    Serial.println( wifiStatus );
    break;
  case HTTP_CONNECT:
    if (httpStatus == HTTP_CONNECTED) {
      state++;
      break;
    }
    if ( millis() - previousMillis < HTTP_CONNECT_TIMEOUT && httpConnectTry > 0 )
    {
      break;
    }

    if ( httpConnectTry > 10 )
    {
      state = WIFI_STATE_RESTART;
      break;
    }
    httpStatus = httpRequest();
    previousMillis = millis();
    httpConnectTry++;
    Serial.print( "http Try: " );
    Serial.print( httpConnectTry );
    Serial.print( " http Status: " );
    Serial.println( httpStatus );
    break;
  case HTTP_POLL:
    if (millis() - lastConnectionTime > postingInterval) {
        httpStatus = httpRequest();
        if (httpStatus == HTTP_DISCONNECTED) {
          if (WiFi.status() == WL_CONNECTED) {
              state = HTTP_CONNECT;
              break;
          }
          else {
              state = WIFI_STATE_RESTART;
              break;
          }
        }
    }
    break;
  default:
    state = 0;
    wifiConnectTry = 0;
    wifiStatus = WL_IDLE_STATUS;
    httpConnectTry = 0;
    httpStatus = HTTP_DISCONNECTED;
    WiFi.disconnect();
    WiFi.end();
    digitalWrite(LED_BUILTIN, LOW);
    Serial.println( "WiFi restart" );
    break;
}

if ( wifiStatus == WL_CONNECTED ) {
  ArduinoOTA.poll();       
}

}

void printWifiStatus() {
  static byte mac[6];
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  
  
  WiFi.macAddress(mac);
  String mac0,mac1,mac2,mac3,mac4,mac5;
  mac0 = String(mac[0],HEX);
  mac1 = String(mac[1],HEX);
  mac2 = String(mac[2],HEX);
  mac3 = String(mac[3],HEX);
  mac4 = String(mac[4],HEX);
  mac5 = String(mac[5],HEX);
  deviceID = String(mac5 + ":" + mac4 + ":" + mac3 + ":" + mac2 + ":" + mac1 + ":" + mac0);
  Serial.print("mac address: "); Serial.println(deviceID);
}

omri1:
Hi,
In case of wifi disconnecting, what is the right procedure with ArduinoOTA(how to release resources and restart it)? I have an issue that wifi disconnects after about an hour and is not able to connect again(doesn't happen when commenting ArduinoOTA related code).
Thanks

I didn't test this, but call ArduinoOTA.end() before repeating begin().

I'll try, thanks. Is there a problem calling ArduinoOTA.poll() when wifi is disconnected?

omri1:
I'll try, thanks. Is there a problem calling ArduinoOTA.poll() when wifi is disconnected?

no.