Hi, I currently working on a project whereby I'll use modbus to collect data and then update through HTTP to Google sheets.
This are the boards I'm using
MKR WiFi1010
Arudino Shield
Modbus
Breadboard
Jumper Wires
This are the software I using to capture data
Google App Script
Below is my sketch
#include <ArduinoModbus.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <Adafruit_SleepyDog.h>
float temperature;
float humidity;
int count = 0;
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[x] = "8******";
char pass[] = "*****";
int status = WL_IDLE_STATUS;
// Initialize the Ethernet client library
WiFiSSLClient client;
//server address
char server[] = "google url";
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()
{
Serial.begin(9600);
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
// 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);
}
// Print to serial monitor to display successful connection
Serial.println("Connected to WiFi");
Serial.println("Modbus Temperature Humidity Sensor");
// start the Modbus RTU client
if (!ModbusRTUClient.begin(9600))
{
Serial.println("Failed to start Modbus RTU Client!");
while (1);
}
}
void loop()
{
// if there's incoming data from the net connection.
while (client.available())
{
char c = client.read();
Serial.write(c);
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval)
{
//watchdog timer
if(WiFi.status() != WL_CONNECTED)
{
int i=0;
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while(WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass);
Serial.println(i);
i++;
delay(10000);
if(i>=6)
{
Serial.println("Resetting");
Watchdog.enable(15);
delay(500);
}
}
}
DataRequest();
httpGASRequest();
}
}
//this is to collect the data
void DataRequest()
{
// send a Holding registers read request to (slave) id 1, for 2 registers
if (!ModbusRTUClient.requestFrom(1, INPUT_REGISTERS, 0x01, 2))
{
Serial.print("failed to read registers! ");
Serial.println(ModbusRTUClient.lastError());
}
else
{
// If the request succeeds, the sensor sends the readings, that are
// stored in the holding registers. The read() method can be used to
// get the raw temperature and the humidity values.
short rawtemperature = ModbusRTUClient.read();
short rawhumidity = ModbusRTUClient.read();
// To get the temperature in Celsius and the humidity reading as
// a percentage, divide the raw value by 10.0.
temperature = rawtemperature / 10.0;
humidity = rawhumidity / 10.0;
Serial.print("Temperature is ");
Serial.println(temperature);
Serial.print("Humidity is ");
Serial.println(humidity);
}
}
// this method makes a HTTP connection to the server:
void httpGASRequest()
{
// close any connection before send a new request.
// This will free the socket on the NINA module
client.stop();
// if there's a successful connection:
if (client.connect(server, 443))
{
Serial.println("connecting...");
// send the HTTP PUT request:
client.println("GET https://script.google.com/macros/s/AKfycbxOztgS7E7In4z95oiFGRVBkfGCTWoWNrlYa5yEX1EE6Dp_at9NDiq-LN08F6FmXz3pug/exec?""temp=" + String(temperature) + "&hum=" + String(humidity));
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else
{
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
‘
‘
Whenever I view my serial monitor, it only update a few time to my google sheet, afterwards it does not update anymore. Can anyone help me?