MKR 1010 turning LED on and off via WiFi

I recently purchased an Arduino MKR 1010. I am attempting to use MIT App Inventor to turn a LED on and off via WiFi. The string request in the void loop() contains "ON" but the LED does not light.
I have attached two png files and the ino file. One png file shows the MIT App Inventor block and the other png file shows the serial monitor when the MIT App Inventor ButtonOn is clicked. Your insight is appreciated.

MitBlock.png

ArduinoCode.ino (2.9 KB)

please learn how to use the forum.


the images

MIT BLOCK
MitBlock.png

Serial Monitor (why post an image of text...)

the arduino code

/*
  This example connects to an unencrypted Wifi network.
  Then it prints the  MAC address of the Wifi module,
  the IP address obtained, and other network details.

  created 13 July 2010
  by dlf (Metodo2 srl)
  modified 31 May 2012
  by Tom Igoe
*/
#include <SPI.h>
#include <WiFiNINA.h>

WiFiServer ESPserver(80);//Service Port
boolean alreadyConnected = false; //whether or not the client was connected

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "";        // your network SSID (name)
char pass[] = "";    // your network password (use for WP
int status = WL_IDLE_STATUS;     // the Wifi radio's status

int delimiter, delimiter_1;
//String request = " ";


void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);


  //  digitalWrite(relayPin, LOW);// Start the server
  ESPserver.begin();
  Serial.println("Server started");

  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();
  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(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

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

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

void loop() {
  // Check if a client has connected
  WiFiClient client = ESPserver.available();
  if (!client)
  {
    return;
  } //end (!client)
  // Wait until the client sends some data
  Serial.println("New Client");
  while (!client.available())
  {
    delay(1);
  } // end while(!client.available()

  delimiter = 0;
  delimiter_1 = 0;

  // Read the first line of the request

  String request = client.readStringUntil('HTTP');

  int relayPin = 5;
  pinMode(relayPin, OUTPUT);
  Serial.print("request = ");
  Serial.println(request);
  delimiter = request.indexOf("%");
  Serial.print("delimiter = ");
  Serial.println(delimiter);
  delimiter_1 = request.indexOf("%", delimiter + 1);
  Serial.print("delimiter_1 = ");
  Serial.println(delimiter_1);
  request = request.substring(delimiter + 1, delimiter_1);
  Serial.print("substring request= ");
  Serial.println(request);


  if ((request) = "ON") {
    digitalWrite(relayPin, HIGH);
  }

  if (request = "OFF") {
    digitalWrite(relayPin, LOW);
  }
  client.stop();
}

this won't work

   String request = client.readStringUntil('HTTP');

as you should use double quotes for a string, not simple quotes. the compiler likely complained to you...

using = is not the way to test for equality, this is an assignment. testing is ==, so these won't do what you expect.
  if ((request) = "ON") {

  if (request = "OFF") {

Is there a way to connect the MIT app inventor to the arduino MKR without using the exact IP address of the MKR ? The reason is that I want to use it not on the same WIFI network but via the arduino cloud

It’s a routing question... the IP address is what uniquely identifies your device on the network. It can be static and locally declared or dynamic but could be pre-allocated from the router to be always the same one. There are also further techniques like zeroconf or bonjour or using mDNS that let a device advertise its services on the network (if not blocked by the network admin). It all depends on your network configuration

This example uses the mDNS approach

Once the board is connected to your WiFi network you just have to type WiFiRobot.local in your web browser and that's it! You can now play with your WiFi robot!

The other option is to have the MKR create its own WiFi network in which case you join that network from the phone to discuss with the MKR. In that environment the MKR will impose which address it is using and will ne always the same, the clients will adapt.

I understand what you are saying and mDNS is a interesting option but
The arduino cloud somehow connects to the MKR via my local network, what means its "knows" the current IP address (or using much higher protocol to connect) and I can connect to the device using the arduino dashboards even when I'm not on the same wifi network.
So my question is there a more direct way to connect the MIT App builder to the arduino cloud ?

The arduino cloud somehow connects to the MKR via my local network, what means its "knows" the current IP address (or using much higher protocol to connect) and I can connect to the device using the arduino dashboards even when I'm not on the same wifi network.

This is not how it works...

  • the MKR connects frequently to the "arduino cloud" to check if there is a message to handle

  • when you want to talk to your MKR, you connect to the "arduino cloud" and leave a message for your MKR (identified with a unique thing ID). That message stays on the arduino cloud until it's picked-up

==> The connexion is not initiated from the cloud to your MRK. The cloud is just a broker there, connecting the parties. It's living in a very well known location so that both parties know where to go to drop or pick up a message.

(that would be a massive breach of any network security if something could reach out deep down in your private network from the outside...)

if you are using their cloud, you could post the local IP address in the arduino cloud and have your MIT script read it before trying to connect... (but each "thing" being uniquely identified, I don't know if that would solve your general problem).

if you want to reach out to your MKR from the local network without knowing it's IP address, there are no magic there, the MKR needs to advertise it locally. that could be done out of band, through bluetooth for example...

It makes sense that this is the mechanism.
How do you recommend to post the IP automatically to the cloud and from there to the MIT script ?

lightov:
How do you recommend to post the IP automatically to the cloud and from there to the MIT script ?

do you have a public cloud infrastructure access ? somewhere where you can run a simple PHP script that would be accessible via internet ? that would be the easiest way (not very secure if you do it simply)

I don't know the exact capability of the MIT stuff, I don't run Android (too many security flaws for my own taste) but I assume they have a way to pull a file from the internet and look at its content..