Go Down

Topic: Modbus and Wifly (Read 412 times) previous topic - next topic

yeti195

Hello,

I have been playing around with this modbus lib for a bit now with a Ethernet shield. 

https://code.google.com/p/mudbus/

Now, I'm wanting to see if I can get it working with a WiFly shield from Sparkfun. 

I just cant see to figure it out.  Here's the code I use for the Ethernet shield.  It's a voltmeter for a 48v battery array.  Works pretty well.

Code: [Select]


// include the library code:
#include <LiquidCrystal.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Mudbus.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 5, 9, 1, 0);
int backLight = 6;    // pin 6 will control the backlight
int LedPin = 3;

float VRef = 4.97;  // Arduino voltage ref.

//Battery 1
int battery1 = 0;
float vout1 = 0.0;
float vin1 = 0.0;
float R1 = 21680; // resistance of R1
float R2 = 9790; // resistance of R2
int value1 = 0;

//Battery2
int battery2 = 1;
float vout2 = 0.0;
float vin2 = 0.0;
float R3 = 55740; // resistance of R3
float R4 = 9820; // resistance of R4
int value2 = 0;

//Battery3
int battery3 = 2;
float vout3 = 0.0;
float vin3 = 0.0;
float R5 = 98700; // resistance of R5
float R6 = 9810; // resistance of R6
int value3 = 0;

//Battery4
int battery4 = 3;
float vout4 = 0.0;
float vin4 = 0.0;
float R7 = 118900; // resistance of R7
float R8 = 9810; // resistance of R8
int value4 = 0;

Mudbus Mb;

void setup()
{
  uint8_t mac[]     = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x06 };
  uint8_t ip[]      = { 192, 168, 1, 141 };
  uint8_t gateway[] = { 192, 168, 1, 1 };
  uint8_t subnet[]  = { 255, 255, 255, 0 };
  Ethernet.begin(mac, ip, gateway, subnet);
  //Avoid pins 4,10,11,12,13 when using ethernet shield

  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.

  //delay(5000);  //Time to open the terminal
   
  pinMode(LedPin, OUTPUT);
  pinMode(battery1, INPUT);
  pinMode(battery2, INPUT);
  pinMode(battery3, INPUT);
  pinMode(battery4, INPUT);
  lcd.begin(20, 4);
  lcd.print("  Modbus Voltmeter  ");
  lcd.setCursor(0, 1);
  lcd.print("    Version: 3.0    ");
  lcd.setCursor(0, 3);
  lcd.print("Sight & Sound Theat.");
  delay(5000);
  lcd.clear();
   
  Mb.R[0] = 0.00;
  Mb.R[1] = 0.00;
  Mb.R[2] = 0.00;
  Mb.R[3] = 0.00;
}


void loop() {
 
   //modbus TCP update
   Mb.Run();
   
   // read the value of battery 1
   value1 = analogRead(battery1);
   vout1 = (value1 * VRef) / 1024.0;
   vin1 = vout1 / (R2/(R1+R2));
   if (vin1<0.09)   
   vin1=0.0; //statement to quash undesired reading !
   
   // read the value of battery 2
   value2 = analogRead(battery2);
   vout2 = (value2 * VRef) / 1024.0;
   vin2 = vout2 / (R4/(R3+R4));
   if (vin2<0.09)   
   vin2=0.0; //statement to quash undesired reading !
   
   // read the value of battery 3
   value3 = analogRead(battery3);
   vout3 = (value3 * VRef) / 1024.0;
   vin3 = vout3 / (R6/(R5+R6));
   if (vin3<0.09)   
   vin3=0.0; //statement to quash undesired reading !
   
   // read the value of battery 4
   value4 = analogRead(battery4);
   vout4 = (value4 * VRef) / 1024.0;
   vin4 = vout4 / (R8/(R7+R8));
   if (vin4<0.09)   {
   vin4=0.0; //statement to quash undesired reading !
   
}

// This section writes the values of each battery to the ModBus registers

Mb.R[0] = (vin1 * 100);
Mb.R[1] = (vin2 * 100);
Mb.R[2] = (vin3 * 100);
Mb.R[3] = (vin4 * 100);

// This section writes the values of each battery to the LCD display

lcd.setCursor(0, 0);
lcd.print("1: ");
lcd.print(vin1);
lcd.setCursor(10, 0);
lcd.print("|");
lcd.setCursor(13, 0);
lcd.print("Total");
lcd.setCursor(0, 1);
lcd.print("2: ");
lcd.print(vin2 - vin1);
lcd.setCursor(10, 1);
lcd.print("|");
lcd.setCursor(13, 1);
lcd.print(vin4);
lcd.setCursor(0, 2);
lcd.print("3: ");
lcd.print(vin3 - vin2);
lcd.setCursor(10, 2);
lcd.print("|");
lcd.setCursor(0, 3);
lcd.print("4: ");
lcd.print(vin4 - vin3);
lcd.setCursor(10, 3);
lcd.print("|");
lcd.setCursor(12, 3);
lcd.print("70 Amps");

delay(350);

// This section is the ModBus coil write to light up the LED fron Indusoft

digitalWrite(LedPin, Mb.C[0]), HIGH;
}




I have the Wifly shield configured with an IP address, and I see it on the network.  I just don't see how to have this sketch communicate with the WiFly.

Any help would be greatly appreciated.

Go Up