Hi,
I am trying to put in place a ModBus Communication via Wifi thanks to a wifly shield. I am using the ModbusMaster.h library. However it is used to serial communication.
How can I make it with WiFi ?
My code is following :
//Librairies
#include "WiFly.h"
#include "Credentials.h"
#include <ModbusMaster.h>
//Parametrage
// WiFly
boolean pWiFly = HIGH;
Server server(80);
long startTime;
//ModBus
boolean pModbus = HIGH;
ModbusMaster node(2);
void setup() {
// Connection du WiFly
if (pWiFly == HIGH) {
Serial.begin(9600); //Serial.println("Debut du programme");
server.begin(); //Serial.println("Demarrage serveur");
SpiSerial.begin(); //Serial.println("Connection au SPI UART.");
SpiSerial.print("$$"); //Serial.println("Passage en mode commande");
WiFly.begin(); //Serial.println("Demarrage WiFly");
// Connection au reseau WiFi
if (!WiFly.join(ssid)) {
while (1) {
//Serial.println("Connection impossible");
}
}
//Affichage de l'adresse IP
Serial.print("IP: ");
Serial.println(WiFly.ip());
}
//Connection du ModBus
node.begin(9600); //Serial.println("Modbus : OK");
}
void loop() {
//Envoi des données analogiques par wifi sur une page web
if (pWiFly == HIGH) {
//fWiFly();
}
//Communication Modbus
if (pModbus == HIGH) {
fModbus();
}
}
//Fonction ModBus
void fModbus() {
static uint32_t i;
uint8_t j, result;
uint16_t data[6];
i++;
// set word 0 of TX buffer to least-significant word of counter (bits 15..0)
node.setTransmitBuffer(0, lowWord(i));
// set word 1 of TX buffer to most-significant word of counter (bits 31..16)
node.setTransmitBuffer(1, highWord(i));
// slave: write TX buffer to (2) 16-bit registers starting at register 0
result = node.writeMultipleRegisters(0, 2);
// slave: read (6) 16-bit registers starting at register 2 to RX buffer
result = node.readHoldingRegisters(2, 6);
// do something with data if read is successful
if (result == node.ku8MBSuccess)
{
for (j = 0; j < 6; j++)
{
data[j] = node.getResponseBuffer(j);
}
}
}