Hi, me too am having the wifi connection issue. I've been searching the internet for days now without success.
Configuration:
Firmware version: 19.4.4
Arduino IDE:1.8.0
WiFi101: 0.13.0
Mac OS 10.12.3
MKR1000 connects to a server (Raspberry PI) with Node-Red.
This is my source code:
#define DEBUG
#define ONE_WIRE_BUS 7
#include <RTCZero.h>
#include <SPI.h>
#include <WiFi101.h>
#include <OneWire.h>
#include <DallasTemperature.h>
RTCZero rtc;
IPAddress server(192,168,1,33);
WiFiClient client;
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
char ssid[] = "mySSID"; // your network SSID (name)
char pass[] = "MyPassword"; // your network password
int status = WL_IDLE_STATUS; // the WiFi radio's status
bool flag = false;
const byte seconds=0;
byte minutes;
byte hours;
const byte sleepMinutes = 1;
const byte sleepSeconds = 30;
const byte maxReadings = 14;
float vorigeTemp = 0.0;
float tempSensorA;
float tempSensorB;
float temperatuur;
byte teller;
bool verbonden;
void setup() {
// wacht om door te gaan om sleep mode te kunnen omzeilen
delay(10000);
//Initialize serial and wait for port to open:
Serial.begin(9600);
sensors.begin();
rtc.begin();
rtc.setTime(0, 0, 0);
rtc.setDate(1, 1, 2000);
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
while(true);
}
/*Serial.print("Firmware version: ");
Serial.println(WiFi.firmwareVersion());*/
}
void loop() {
//makeWifiConnection();
getTemperatuur();
delay(15000);
//goDeepSleep();
}
void getTemperatuur() {
// if you get a connection, report back via serial:
sensors.requestTemperatures();
delay(200);
tempSensorA = sensors.getTempCByIndex(0);
tempSensorB = sensors.getTempCByIndex(1);
temperatuur = (tempSensorA + tempSensorB) / 2;
// Temperatuur verzenden
Serial.println("make connection");
// Maak WiFi Connection
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
//Serial.println("WL_NO_SHIELD");
// don't continue:
while (true);
}
//status = WiFi.begin(ssid, pass);
// attempt to connect to WiFi network:
teller = 0;
while (teller++ < 5) {
Serial.println("wait for connection...");
Serial.println(teller);
while ( status != WL_CONNECTED) {
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
//delay(4000);
}
delay(4000);
if ( status = WL_CONNECTED ) {
teller = 6;
}
}
Serial.println("Sending temp");
verbonden = false;
teller = 0;
while (teller++ < 5 ) {
Serial.println("wait for client connection...");
Serial.println(teller);
verbonden = client.connect(server, 2000);
if (verbonden) {
teller = 6;
}
delay(2000);
}
if (!verbonden) {
Serial.println("Client connect failed");
}
String cmd = "{\"kamer\":\"K00\", \"temperatuur\":";
String temp = cmd + temperatuur + "}";
Serial.println(temp);
client.print(temp);
//delay(200);
client.stop();
WiFi.end();
Serial.println("Wifi closed");
}
// put arduino in deep sleep mode
void goDeepSleep() {
//Serial.println("goDeepSleep - start");
//hours = rtc.getHours();
//minutes = rtc.getMinutes();
rtc.setTime(0, 0, 0);
rtc.setAlarmTime(00, 0, sleepSeconds); //set alarm time to go off in 10 seconds
//following two lines enable alarm, comment both out if you want to do external interrupt
rtc.enableAlarm(rtc.MATCH_HHMMSS); //set alarm
rtc.attachInterrupt(wakeUp); //creates an interrupt that wakes the SAMD21 which is triggered by a FTC alarm
//comment out the below line if you are using RTC alarm for interrupt
// extInterrupt(A1); //creates an interrupt source on external pin
//puts SAMD21 to sleep
rtc.standbyMode(); //library call
//samSleep(); //function to show how call works
}
void wakeUp() {
// wakker geworden
//Serial.println("wakeUp - Start");
//WiFiClient client;
}
Running this script gives the following output on the serial monitor
make connection
wait for connection...
1
Sending temp
wait for client connection...
1
{"kamer":"K00", "temperatuur":22.53}
Wifi closed
make connection
wait for connection...
1
Sending temp
wait for client connection...
1
wait for client connection...
2
wait for client connection...
3
wait for client connection...
4
wait for client connection...
5
Client connect failed
{"kamer":"K00", "temperatuur":22.56}
Wifi closed
The temperature never arrives at the server, and the connection to the server seems to fail.
What I also notice on de Node-Red side is that the connections don't seem to close (see image in attach)
When I comment out the Wifi.end() all works well.
Than the connections at Node-Red side open and close properly.
What I find strange is that it worked for about a week with the doDeepSleep active. Any suggestion are more than welcome. I'm a bit desperate on this one.
