Edit: I forgot to add that I connected my MKR 1010 to the Cloud thing. That updated the NINA firmware to the latest version for me so I did make sure of that.
Hey guys. Got my first Arduino with wifi built (MKR Wifi 1010) in and using the online editor tools and this thing is super cool!
Running into a problem, however. My project is working great when connected to the USB, but as soon as I disconnect the USB it drops off of the wireless (I can see this when looking in my router at connected devices). I plug the USB back in and it reconnects. Unplug and it disconnects.
I have the 1010 powered by a 3.7v LiPo battery through the VIN and GND. And there is a DHT22 temp/humidity sensor plugged in, but that is all. Testing the battery says it is putting out about 3.8v.
Is this an issue of not enough power maybe? I didn't want to use 2 of these batteries because I thought it might ruin the whole board!
I never restart the Arduino or reload the sketch. I simply plug it into USB and it shows on my router, then unplug and it drops off of my router. Plug it in and it connects, repeat. The sketch is running the entire time in its loop.
Here is my code. Any help is appreciated. I am so excited to get this project working and getting so close after a crazy amount of hours that I shouldn't admit publicly lol. Lots to learn. =)
/*
Repeating WiFi Web Client
This sketch connects to a a web server and makes a request
using a WiFi equipped Arduino board.
created 23 April 2012
modified 31 May 2012
by Tom Igoe
modified 13 Jan 2014
by Federico Vanzati
http://www.arduino.cc/en/Tutorial/WifiWebClientRepeating
This code is in the public domain.
*/
#include <SPI.h>
#include <WiFiNINA.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
// Set DHT pin:
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302)
// Initialize DHT sensor for normal 16mhz Arduino:
DHT dht = DHT(DHTPIN, DHTTYPE);
///////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)
int keyIndex = 0; // your network key index number (needed only for WEP)
int status = WL_IDLE_STATUS;
// Initialize the WiFi client library
WiFiClient client;
// server address:
char server[] = "192.168.50.211";
//IPAddress server(192,168,50,211);
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
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();
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 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();
//Initialize sensor
dht.begin();
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
while (client.available()) {
char c = client.read();
Serial.write(c);
}
// 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();
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the NINA module
client.stop();
//Read the humidity
float h = dht.readHumidity();
// Read the temperature as Fahrenheit:
float f = dht.readTemperature(true);
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
String body = "{\n \"Humidity\": \"" + String(h) + "\",\n \"Temperature\": \"" + String(f) + "\",\n \"DataSourceId\": \"1\"\n}";
postData(body);
// note the time that the connection was made:
lastConnectionTime = millis();
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board'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");
}
void postData(String body) {
// send HTTP request header
client.println("POST /api/DataSensors/AddDht22SensorData HTTP/1.1");
Serial.println("POST /api/DataSensors/AddDht22SensorData HTTP/1.1");
client.println("Host: " + String(server));
Serial.println("Host: " + String(server));
client.println("Content-Type: application/json");
Serial.println("Content-Type: application/json");
client.println("Accept: */*");
Serial.println("Accept: */*");
client.println("User-Agent: ArduinoWiFi/1.1");
Serial.println("User-Agent: ArduinoWiFi/1.1");
client.println("Cache-Control: no-cache");
Serial.println("Cache-Control: no-cache");
client.println("Accept-Encoding: gzip, deflate");
Serial.println("Accept-Encoding: gzip, deflate");
client.println("Accept-Language: en-us");
Serial.println("Accept-Language: en-us");
client.println("Content-Length: " + String(body.length())); //ANSWER: body.length() needs to be wrapped as a string, see above
Serial.println("Content-Length: " + String(body.length()));
client.println("Connection: close");
Serial.println("Connection: close");
client.println(); // end HTTP request header
Serial.println();
client.println(body);
Serial.println(body);
}