Please post your full sketch. If possible, you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it's OK to add it as an attachment. After clicking the "Reply" button, you will see an "Attachments and other settings" link.
Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.
/*
MKR1000 WiFi Robot
This sketch use the WiFi server capability of a MKR1000 to post a web interface used to move a mini robot.
Required hardware:
- Arduino/Genuino MKR1000;
- MKR2UNO Shield adapter;
- Arduino Motor Shield
created 02 Nov 2016
by Arturo Guadalupi <a.guadalupi@arduino.cc>
*/
#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiMDNSResponder.h>
#include <Ultrasonic.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// Wifi Settings ///////
char ssid[] = "Mystior";
char pass[] = "19961006";
int keyIndex = 0; // your network key Index number (needed only for WEP)
char mdnsName[] = "WiFiSensor"; // the MDNS name that the board will respond to
// Note that the actual MDNS name will have '.local' after
// the name above, so "WiFiSensor" will be accessible on
// the MDNS name "WiFiSensor.local".
int status = WL_IDLE_STATUS;
// Create a MDNS responder to listen and respond to MDNS name requests.
WiFiMDNSResponder mdnsResponder;
WiFiServer server(80);
String readString;
const int pinDirA = 12;
const int pinDirB = 0; //pin 13 is wired to pin 0 on the shield adapter
const int pinPwmA = 3;
const int pinPwmB = 11;
const int pinBrakeA = 9;
const int pinBrakeB = 8;
void setup() {
//Initialize serial and wait for port to open:
//Put motor shield pins as output
pinMode(pinDirA, OUTPUT);
pinMode(pinPwmA, OUTPUT);
pinMode(pinBrakeA, OUTPUT);
pinMode(pinDirB, OUTPUT);
pinMode(pinPwmB, OUTPUT);
pinMode(pinBrakeB, OUTPUT);
brake();
Serial.begin(9600);
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
// attempt to connect to Wifi network:
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);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the status:
printWiFiStatus();
server.begin();
// Setup the MDNS responder to listen to the configured name.
// NOTE: You _must_ call this _after_ connecting to the WiFi network and
// being assigned an IP address.
if (!mdnsResponder.begin(mdnsName)) {
Serial.println("Failed to start MDNS responder!");
while (1);
}
Serial.print("Server listening at http://");
Serial.print(mdnsName);
Serial.println(".local/");
Serial.println();
}
void loop() {
// Call the update() function on the MDNS responder every loop iteration to
// make sure it can detect and respond to name requests.
mdnsResponder.poll();
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();// Read char by char HTTP request
readString += c;
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<head><title>WiFi Sensor</title></head>");
client.println("<center><hr/><p> Click the Buttons to view the charge point. <p/><hr/></center>");
client.println("<center><input type=button value='Detect' onmousedown=location.href='/?DETECT'></center>
");
client.println("<hr/>");
client.println("</body>");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
if (readString.indexOf("/?DETECT") > 0) {
Serial.println();
Serial.println("DETECT");
Serial.println();
Detect();
}
readString = "";// Clearing string for next read
Serial.println("client disconnected");
}
}
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
WiFiMDNSResponder.h is part of the WiFi101 library. This causes your code to attempt to use both the WiFiNina and the WiFi101 libraries at the same time, which results in a conflict.
I don't see that the WiFiNina library has any equivalent mDNS functionality. I do notice that Arduino has a library named ArduinoMDNS:
I've never used it, but it may be that you can use both the WiFiNina library and that library to reproduce the functionality of your previous sketch. This is not going to be as easy as just changing a file name in the #include directive. You are going to need to study the example sketches that come with the ArduinoMDNS library to see how to use it in your sketch. One thing to note is that, unlike WiFiNina and WiFi101, the ArduinoMDNS library is not pre-installed in the Arduino Web Editor. This means you will need to download the library: https://github.com/arduino-libraries/ArduinoMDNS/archive/master.zip
and then import it into the Arduino Web Editor. I'm not sure what we should infer from the fact that Arduino has not yet created a release version of ArduinoMDNS (or done any work on the library in years).
Maybe someone here with some experience in this subject will be able to suggest a better solution for doing mDNS with the WiFiNina library.