mkr wifi 1010 and usb charger as power supply

Hi,

I want to built a moisture and temperature sensor which has a stationary power supply.
I’m using a MKR WIFI 1010 and a MKR ENV Shield. The Sketch works fine as long as it is attached to my mac. The place where I want to use it has a regular 220V socket. So I thought I could use a usb charger like this one

It seems like the board starts properly but I cannot see it connect to the wifi.

Here is my sketch which works just fine when attached to my laptop.
Is it even possible to use these usb chargers as a power supply?
Is the Serial interface gonna be a problem when no usb host is connected?
Do I need to apply a 5V power supply to the 5V and GND pin to get this running?

#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
#include <Arduino_MKRENV.h>
#include <WiFiUdp.h>

int status = WL_IDLE_STATUS;                     // the Wifi radio's status
int backoff = 5000;

// Specify IP address or hostname
int humidity, lux, pressure, uva;
float temperature;

unsigned int localPort = 2390;      // local port to listen on
unsigned int remotePort = 2392;
unsigned int timelocalPort = 2391;      // local port to listen on
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte timepacketBuffer[ NTP_PACKET_SIZE];
WiFiUDP measDataUdp;
WiFiUDP TimeUdp;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  Serial.println("Current Firmware:");
  Serial.println(fv);
  Serial.println("Required Firmware");
  Serial.println(WIFI_FIRMWARE_LATEST_VERSION);
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(SECRET_SSID);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(SECRET_SSID, SECRET_PASS);

    delay(backoff);
  }

  // you're connected now, so print out the data:
  Serial.println("You're connected to the network");
  printCurrentNet();
  printWiFiData();

  //setup sensors
  if (!ENV.begin()) {
    Serial.println("Failed to initialize MKR ENV shield!");
    while(1);
 }

  //start udp
  Serial.println("Starting connection to data server...");
  // if you get a connection, report back via serial:
  measDataUdp.begin(localPort);

  Serial.println("Starting ntp udp socket...");
  TimeUdp.begin(timelocalPort);
}

void loop() {
  String meas_string, time_string, str_seconds = "";
  String hours, minutes, seconds = "";
  // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
  const unsigned long seventyYears = 2208988800UL;
  unsigned long secsSince1900, lowWord, highWord, epoch;

  Serial.println("Retrieving Time");
  sendNTPpacket(); // send an NTP packet to a time server
  // wait to see if a reply is available
  delay(1000);
  if (TimeUdp.parsePacket()) {
    Serial.println("packet received");
    // We've received a packet, read the data from it
    TimeUdp.read(timepacketBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:

    highWord = word(timepacketBuffer[40], timepacketBuffer[41]);
    lowWord = word(timepacketBuffer[42], timepacketBuffer[43]);
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    secsSince1900 = highWord << 16 | lowWord;
    Serial.print("Seconds since Jan 1 1900 = ");
    Serial.println(secsSince1900);

    // now convert NTP time into everyday time:
    Serial.print("Unix time = ");
    // subtract seventy years:
    epoch = secsSince1900 - seventyYears;
    // print Unix time:
    Serial.println(epoch);

    // print the hour, minute and second:
    hours = String((epoch  % 86400L) / 3600 + 2);
    minutes = String((epoch % 3600) / 60);
    seconds = String(epoch % 60);
    time_string = hours + ":" + minutes + ":" + seconds;
    Serial.println(time_string);
  }

  // wait ten seconds before asking for the time again
  delay(10000);
  Serial.println("Reading sensors...");
  humidity = int(ENV.readHumidity()); //in percent
  lux = int(ENV.readIlluminance());
  pressure = int(ENV.readPressure()); //in kPa
  temperature = ENV.readTemperature();
  uva = int(ENV.readUVA());

  Serial.print("Humidity relative[%]: ");
  Serial.println(humidity);
  Serial.print("Lux : ");
  Serial.println(lux);
  Serial.print("Pressure [kPa]: ");
  Serial.println(pressure);
  Serial.print("Temperature [C]: ");
  Serial.println(temperature);
  Serial.print("uva : ");
  Serial.println(uva);
  Serial.println("--------------");

  // send a reply, to the IP address and port that sent us the packet we received
  measDataUdp.beginPacket("buechse", remotePort);
  meas_string = String(epoch) + ";" + String(humidity) + ";" + String(lux) + ";" + String(pressure) + ";" + String(temperature) + ";" + String(uva) + ";" + 999; //999 is a placeholder for the infrared sensor
  measDataUdp.println(meas_string);
  measDataUdp.endPacket();
  delay(10000);
}

void printWiFiData() {
  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP address : ");
  Serial.println(ip);

  Serial.print("Subnet mask: ");
  Serial.println((IPAddress)WiFi.subnetMask());

  Serial.print("Gateway IP : ");
  Serial.println((IPAddress)WiFi.gatewayIP());

  // print your MAC address:
  byte mac[6];
  WiFi.macAddress(mac);
  Serial.print("MAC address: ");
  printMacAddress(mac);
}

void printCurrentNet() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the MAC address of the router you're attached to:
  byte bssid[6];
  WiFi.BSSID(bssid);
  Serial.print("BSSID: ");
  printMacAddress(bssid);
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI): ");
  Serial.println(rssi);

  // print the encryption type:
  byte encryption = WiFi.encryptionType();
  Serial.print("Encryption Type: ");
  Serial.println(encryption, HEX);
  Serial.println();
}

void printMacAddress(byte mac[]) {
  for (int i = 5; i >= 0; i--) {
    if (mac[i] < 16) {
      Serial.print("0");
    }
    Serial.print(mac[i], HEX);
    if (i > 0) {
      Serial.print(":");
    }
  }
  Serial.println();
}

// send an NTP request to the time server at the given address
void sendNTPpacket() {
  //Serial.println("1");
  // set all bytes in the buffer to 0
  memset(timepacketBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  //Serial.println("2");
  timepacketBuffer[0] = 0b11100011;   // LI, Version, Mode
  timepacketBuffer[1] = 0;     // Stratum, or type of clock
  timepacketBuffer[2] = 6;     // Polling Interval
  timepacketBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  timepacketBuffer[12]  = 49;
  timepacketBuffer[13]  = 0x4E;
  timepacketBuffer[14]  = 49;
  timepacketBuffer[15]  = 52;

  //Serial.println("3");

  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:
  TimeUdp.beginPacket("pool.ntp.org", 123); //NTP requests are to port 123
  //Serial.println("4");
  TimeUdp.write(timepacketBuffer, NTP_PACKET_SIZE);
  //Serial.println("5");
  TimeUdp.endPacket();
  //Serial.println("6");
}
while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

What do you think this does when there is no computer connected to the usb port?

Yes you can power from a usb power supply.

Thx a lot man, I should spend more time on reviewing copied & pasted code to avoid such stuff.
That fixed the issue.

daeda:
Thx a lot man, I should spend more time on reviewing copied & pasted code to avoid such stuff.
That fixed the issue.

You are welcome. Yes, please read the code more carefully, the answer was there in the comment.