消費電力はザックリ以下のような感じです(Vin = 6V)。
・WiFi ON:50 mA
・WiFi OFF:22 mA
・WiFi ON & スリープ:30mA
・WiFi OFF & スリープ:15mA
Arduino IDE コード
#include <WiFiNINA.h>
#include <WiFiUdp.h>
#include "DHT.h"
#include "ArduinoLowPower.h"
#include <BMP280_DEV.h>
int status = WL_IDLE_STATUS;
#include "arduino_secrets.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)
int keyIndex = 0; // your network key index number (needed only for WEP)
unsigned int localPort = 2390; // local port to listen on
char ReplyBuffer[100] = ""; // a string to send back
WiFiUDP Udp;
#define DHTPIN 8
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float pressure;
BMP280_DEV bmp280;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
bmp280.begin(BMP280_I2C_ALT_ADDR);
bmp280.setPresOversampling(OVERSAMPLING_X4); // Set the pressure oversampling to X4
bmp280.startForcedConversion();
bmp280.getCurrentPressure(pressure);
// 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);
WiFi.lowPowerMode(); // ENABLE WiFi Low Power Mode
// wait 10 seconds for connection:
delay(5000);
}
Serial.println("Connected to WiFi");
printWifiStatus();
Serial.println("\nStarting connection to server...");
}
void loop() {
while (status != WL_CONNECTED) {
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
WiFi.lowPowerMode();
delay(500);
Serial.println(status);
}
dht.begin();
bmp280.startForcedConversion();
bmp280.getCurrentPressure(pressure);
delay(100);
float Vb = analogRead(ADC_BATTERY) * (4.3 / 1023.0);
float Vs = analogRead(A1)/1023.0*(69.0+10.0)/10.0*3.3;
float h = dht.readHumidity();
float t = dht.readTemperature();
sprintf(ReplyBuffer,"Vbat:%.1f V, Vsol:%.2f V, %.1f hPa, Temp.:%.2f deg, Humi.:%.1f",Vb, Vs, pressure, t, h);
Udp.begin(localPort);
Udp.beginPacket("192.168.0.255", 2390);
Udp.write(ReplyBuffer);
Udp.endPacket();
delay(100);
status = WL_IDLE_STATUS;
WiFi.end();
LowPower.deepSleep(60000 * 10);
}
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");
}