Hello. I have a problem with the arduino matlab wifi connection.
The arduino hardware setup MKR1000 board works fine when usb connected.
And search with a=arduino ('192.168.1.62', 'MKR1000', 9500), I can find information.
However, if I remove the cable connected to the board and laptop and connect the external battery, I cannot find the Arduino.
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project See About the Installation & Troubleshooting category.
I suggest that you show your sketch. One point might be that you're using while!Serial) in your setup() which will block till a terminal program connects to the Arduino; but we don't know for sure as we have not seen your sketch.
I am having the same problem with the MKR1000. When I hook to external power I get a "While not serial" loop check. I believe this stems from the "while(!Serial);" at the beginning of the sketch. I borrowed the code from a basic WiFi101 example but I am can not get it to work without because then I get"
SerialUSB' does not name a type #define Serial SerialUSB
Has anyone figured out a way to get the MKR1000 to work outside of a computer connection?
Take the while(!Serial) out. There is no need for it if you don't connect via USB to the computer. The reason that it's there is to prevent you from missing the first serial prints while e.g. serial monitor is not open.
Note: no experience with your board but it's common for boards with native USB.
Ok, maybe I have the wrong library setup or something, cause when I comment while(!Serial) out I get:
In file included from /home/builder/.arduino15/packages/arduino/hardware/samd/1.8.13/cores/arduino/Arduino.h:51:0,
from /tmp/arduino-build-23EDFD9D743A33E9FF7EDED671C9F36E/sketch/WPA_Mqtt_noWHILESERIAL.ino.cpp:1:
/home/builder/.arduino15/packages/arduino/hardware/samd/1.8.13/variants/mkr1000/variant.h:210:37: error: 'SerialUSB' does not name a type
#define Serial SerialUSB
^
/tmp/552964444/WPA_Mqtt_noWHILESERIAL/WPA_Mqtt_noWHILESERIAL.ino:33:3: note: in expansion of macro 'Serial'
Serial.print("Attempting to connect to WPA SSID: ");
^~~~~~
/home/builder/.arduino15/packages/arduino/hardware/samd/1.8.13/variants/mkr1000/variant.h:211:37: error: 'SerialUSB' does not name a type
#define Serial SerialUSB
^
/tmp/552964444/WPA_Mqtt_noWHILESERIAL/WPA_Mqtt_noWHILESERIAL.ino:34:3: note: in expansion of macro 'Serial'
Serial.println(ssid);
^~~~~~
/tmp/552964444/WPA_Mqtt_noWHILESERIAL/WPA_Mqtt_noWHILESERIAL.ino:35:3: error: expected unqualified-id before 'while'
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
^~~~~
Here's the portion of the While(!Serial) code:
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
}
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(5000);
}
Serial.println("You're connected to the network");
Serial.println();
I am pretty much a coding novice so please forgive my ignorance. But the only libraries I am using is Wifi101 and ArduinoMqttClient. I have the HR-sr204 setup to measure distance and report its findings to a MQTT topic. The Sketch works great as long as it's plugged into a computer, which is not very practical. I finally figured it was 'While(!Serial) ' that was causing the MKR1000 to loop when it could not open that port but when I just comment that line out (like in the code below) I get the errors you can see in last post.
Thank you for any and all help you may be able to provide! This has been a weeks worth of tinkering and google searching.
#include <ArduinoMqttClient.h>
#include <WiFi101.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "mybrokerIP";
const char mqtt_user[] = "myuser";
const char mqtt_pass[] = "mypass";
int port = 1883;
const char topic[] = "my topic";
const char topic2[] = "things";
const char topic3[] = "other things";
//set interval for sending messages (milliseconds)
const long interval = 8000;
unsigned long previousMillis = 0;
int count = 0;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
//REMOVED THIS//while (!Serial) {
// ; // wait for serial port to connect. Needed for native USB port only
}
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(5000);
}
Serial.println("You're connected to the network");
Serial.println();
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
mqttClient.setUsernamePassword(mqtt_user, mqtt_pass);
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
}
void loop() {
// call poll() regularly to allow the library to send MQTT keep alives which
// avoids being disconnected by the broker
mqttClient.poll();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;
//record random value from A0, A1 and A2
int Rvalue = analogRead(A0);
int Rvalue2 = analogRead(A1);
int Rvalue3 = analogRead(A2);
Serial.print("Sending message to topic: ");
Serial.println(topic);
Serial.println(Rvalue);
Serial.print("Sending message to topic: ");
Serial.println(topic2);
Serial.println(Rvalue2);
Serial.print("Sending message to topic: ");
Serial.println(topic2);
Serial.println(Rvalue3);
// send message, the Print interface can be used to set the message contents
mqttClient.beginMessage(topic);
mqttClient.print(Rvalue);
mqttClient.endMessage();
mqttClient.beginMessage(topic2);
mqttClient.print(Rvalue2);
mqttClient.endMessage();
mqttClient.beginMessage(topic3);
mqttClient.print(Rvalue3);
mqttClient.endMessage();
Serial.println();
}
}
void setup()
{
//Initialize serial and wait for port to open:
Serial.begin(9600);
//REMOVED THIS//while (!Serial) {
^
|
// ; // wait for serial port to connect. Needed for native USB port only
}
^
|
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to WPA SSID: ");
When you removed the while(!Serial), you left the closing }.
Oh. My. God. Something so simple, has caused me so many headaches. I feel very dumb, but thank you so much @sterretje for helping me with this and pointing that out.
I can confirm that removing while (!Serial) allows the MKR1000 to function correctly on external power.