Go Down

Topic: Deepsleep problem with modbus sensors (Read 1 time) previous topic - next topic

cortinamichele

Good morning all,

Relatively new to Arduino, I'm having some issues with my project of smart agricolture.

My configuration is: MKR 1000 to do wifi connection, MKR 485 mounted on top of the MKR 1000 and acting as MASTER, two Modbus soil moisture sensor (SLAVES) connected to MKR 485 by the X-Y terminations. The on-board 3-way DIP switch are configured as follows: A/B set to off, HALF/FULL set to HALF, X/Y set to on.

The program should read every (example) 5 minutes the modbus sensor and send data to my web server where a PHP script put the readings into a MYSQL database. All is powered by a 12V 7Ah battery with 20W solar panel and voltage regulator with USB 5V power output, which I use to power the MKR 1000 by its micro-usb input port.

I use successfully the ArduinoModbus.h library to initialize and read modbus sensor. The code works fine and consistently while using only the WiFi.maxLowPowerMode() function, but without the use of low power features of the SAMD21.

The troubles start when I try to use the ArduinoLowPower.h (LowPower.sleep) to put MKR 1000 in deep sleep, while not reading modbus sensor and sending data. Here, the routine runs once, then freezes and I cannot have any more readings from modbus sensor (message error 'failed to read').

NOTE: the same code, with WiFi.maxLowPowerMode() and LowPower.sleep, works fine without Modbus sensors, for example, if I do only analogic reading of voltage such as analogic sensor.

I have read that LowPower.sleep has troubles with serial communication, since the SAMD21 does not restart serial after deep sleep. Maybe the same issue with Modbus communication and the MKR 485 shield ?

This is the code:

Code: [Select]

#include <SPI.h>
#include <WiFi101.h>
#include <ArduinoModbus.h>
#include <ArduinoLowPower.h>

#define WIFI_SSID "XXXXXX"
#define WIFI_PASSWORD "YYYYYY"

char servername[] = "some.server.com";
int port = 80;

int status = WL_IDLE_STATUS;
WiFiClient client;

int sleeptime = 5L * 60L * 1000L;

int hum1, hum2;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

// SETUP
void setup()  {
 
  Serial.begin(9600);

  wifi_start();

  // START MODBUS RTU CLIENT
  if (!ModbusRTUClient.begin(9600)) {
    Serial.println("Failed to start Modbus RTU Client!");
  } else Serial.println("Modbus ok");

}


void loop() {

  // POWER MANAGEMENT: SAMD21 GO TO SLEEP
  LowPower.sleep(sleeptime);

   // RE-START MODBUS RTU CLIENT, AFTER WAKE UP
  ModbusRTUClient.begin(9600);

  void wifi_reconnect();
 
  if (status == WL_CONNECTED) {

    if (ModbusRTUClient.requestFrom(1, HOLDING_REGISTERS, 0x00, 1)) {
      hum1 = ModbusRTUClient.read() / 100.0;
    }

    if (ModbusRTUClient.requestFrom(2, HOLDING_REGISTERS, 0x00, 1)) {
      hum2 = ModbusRTUClient.read() / 100.0;
    }

    if (client.connect(servername, port))
    {
     // routine for sending data to web server is here - omitted
    }

  }

    WiFi.disconnect();
    status = WL_IDLE_STATUS;
    delay(5000);

}


void wifi_start()
{

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi not present");
    // don't continue:
    while (true);
  }

  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(WIFI_SSID);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(WIFI_SSID);
    // wait 5 seconds for connection:
    delay(5000);
  }
  // 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);


  WiFi.maxLowPowerMode();
}



void wifi_reconnect() {

  // Loop until we're reconnected
  status = WiFi.status();
  if ( status != WL_CONNECTED) {
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
    }
    Serial.println("Connected to AP");
  }

  WiFi.maxLowPowerMode();

}




Any feedback or support would be really appreciated.

Anybody using successfully modbus and low power wifi ?

Many thanks.
Michele

Go Up