RANet firmware update

Expansion modules and attachments
Post Reply
rimai
Posts: 12857
Joined: Fri Mar 18, 2011 6:47 pm

RANet firmware update

Post by rimai »

I'd like to thank GiraffeCat for working with me on troubleshooting and finding out about this issue.
All RANet relay boxes and dimming modules will need this update.
Symptom: your RANet relay box blinks white/blue very fast and causes the relay box to go into fallback.
If you would like to see the symptom, upload this code:

Code: Select all

#include <SoftwareSerial.h>
#include <ReefAngel_Features.h>
#include <Globals.h>
#include <RA_Wifi.h>
#include <Wire.h>
#include <OneWire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <InternalEEPROM.h>
#include <RA_NokiaLCD.h>
#include <RA_ATO.h>
#include <RA_Joystick.h>
#include <LED.h>
#include <RA_TempSensor.h>
#include <Relay.h>
#include <RA_PWM.h>
#include <Timer.h>
#include <Memory.h>
#include <InternalEEPROM.h>
#include <RA_Colors.h>
#include <RA_CustomColors.h>
#include <Salinity.h>
#include <RF.h>
#include <IO.h>
#include <ORP.h>
#include <AI.h>
#include <PH.h>
#include <WaterLevel.h>
#include <Humidity.h>
#include <DCPump.h>
#include <PAR.h>
#include <ReefAngel.h>

void setup()
{
    ReefAngel.Init();  //Initialize controller
    ReefAngel.Use2014Screen();  // Let's use 2014 Screen 
    ReefAngel.AddRANet();  // RANet Add-On Module
    ReefAngel.Relay.On(Box1_Port2);
    ReefAngel.Relay.On(Box1_Port4);
}

void loop()
{
    ReefAngel.ShowInterface();
}
How to perform the firmware update:
1. Open the relay box/dimming module enclosure
2. Locate the RANet module inside.
RANet-Module.jpg
RANet-Module.jpg (9.09 KiB) Viewed 8754 times
3. Remove it from the expansion module
4. Detach the Xbee radio. Be careful not to damage the pins of the radio. Be very gentle.
5. Connect the USB-TTL cable to the RANet module
RANet-connection.jpg
RANet-connection.jpg (11.24 KiB) Viewed 8754 times
6. Download the attached code and open the code on Arduino or copy and paste it from the bottom of the post
8. Change the board to "Reef Angel Controller w/ optiboot"
9. Upload the code
10. Make sure the code uploads and Arduino says "Done uploading" without errors
11. Disconnect the USB-TTL cable
12. Attach the Xbee radio
13. Place the RANet module back into the expansion module
14. Close the enclosure
15. Change the board back to "Reef Angel Plus"
16. Upload the code above to confirm that the firmware fixed the issue

This is the latest firmware code:

Code: Select all

#include <Wire.h>
#include <avr/eeprom.h>

#define BLUE_LED    9
#define WHITE_LED   10

#define BLUE_INTENSITY   255
#define WHITE_INTENSITY  255

#define RANET_MAX_SIZE  68
#define DISCONNECT_TIMEOUT  10000

#define LastFallback0  100 // Memory location for fallback storage

#define RANet_Down    0
#define RANet_OK      1

#define LIGHTNING 1    // Trigger value for lightning strike
#define TRIGGER 62     // Position in payload of trigger byte

byte buffer_index;
byte buffer[128];
char buf[3];
byte bufint, bufsize;
byte RANetData[RANET_MAX_SIZE];
byte RANetCRC;
byte BlueChannel=0;
byte WhiteChannel=0;
byte RANet_Status=RANet_Down;
boolean cable_present=false;

unsigned long lastmillis=millis();
unsigned long lastcablecheck=millis();
char lastc=0;

void setup()
{
  pinMode(BLUE_LED,OUTPUT);
  pinMode(WHITE_LED,OUTPUT);
  Serial.begin(57600);
  Wire.onReceive(NULL);
  Wire.onRequest(NULL);
  Wire.begin();
  for (int a=0;a<RANET_MAX_SIZE; a++) // Clear array
    RANetData[a]=0; 
  Wire.beginTransmission(0x68);
  Wire.write(0);
  int a=Wire.endTransmission();
  cable_present=(a==0);
  // setup PCA9685 for data receive
  // we need this to make sure it will work if connected ofter controller is booted, so we need to send it all the time.
  Wire.beginTransmission(0x40);
  Wire.write(0);
  Wire.write(0xa1);
  Wire.endTransmission();
}

void loop()
{
  if (cable_present)
  {
    BlueChannel=100;
    WhiteChannel=0;
    analogWrite(WHITE_LED,WHITE_INTENSITY*WhiteChannel/100);
    analogWrite(BLUE_LED,BLUE_INTENSITY*BlueChannel/100);
  }
  else  
  {
    BlueChannel=0;
    WhiteChannel=0;
    while(Serial.available())
    {
      UpdateWhiteChannel();
      char c = Serial.read(); // Read each incoming byte
      buffer[buffer_index]=c; // store in the buffer array
      if (c==10 && lastc==13) // if line feed we analyze the payload
      {
        if (buffer_index>25) // only need to analyse if buffer_index is greater than 25, otherwise the payload is broken or corrupt
          if (buffer_index==buffer[1]) // check if payload matches the length the controller sent
          {
            UpdateWhiteChannel();
            RANetCRC=0;
            for (int a=0; a<(buffer_index-2); a++) // calculate CRC
              RANetCRC+=buffer[a];
            UpdateWhiteChannel();
            if (RANetCRC==buffer[buffer_index-2]) // if CRC matches
            {
              UpdateWhiteChannel();
              for (int a=0; a<(buffer_index-2); a++) // Copy buffer to RANetData
                RANetData[a]=buffer[a];
              UpdateWhiteChannel();
              lastmillis=millis();
  //            Serial.print(millis());
  //            Serial.print("\t");
  //            Serial.println(RANetData[2]);
              for (int a=0;a<8;a++)
              {
                if (eeprom_read_byte((unsigned char *) LastFallback0+a)!=RANetData[10+a])
                {
                  eeprom_write_byte((unsigned char *) LastFallback0+a, RANetData[10+a]);
                }
                Wire.beginTransmission(0x38+a);
                Wire.write(~RANetData[2+a]);
                Wire.endTransmission();
              }
              for (int a=0;a<12;a=a+2)    // Step through the 12 bytes of dimming data
              {
                byte newdata=(RANetData[18+a]);    //LSB
                byte newdata1=(RANetData[18+a+1]);  //MSB
                Wire.beginTransmission(0x40);
                Wire.write(0x8+(4*(a/2)));  // Channels 0 through 5
                Wire.write(newdata);    // Write LSB
                Wire.write(newdata1);   // Write MSB
                Wire.endTransmission();
              }
              UpdateWhiteChannel();
              RANet_Status=RANet_OK;
            }
          }
        buffer_index=255; // reset buffer index
      }
      lastc=c;
      UpdateWhiteChannel();
      if (buffer_index++>=128) buffer_index=0; // increment index of buffer array. reset index if >=128
    }
    if (millis()-lastmillis>DISCONNECT_TIMEOUT)
    {
      lastmillis=millis();
  //    Serial.println("Disconnected");
  //    Serial.print(millis());
  //    Serial.print("\t");
  //    Serial.println(RANetData[10]);
      for (int a=0;a<8;a++)
      {
        Wire.beginTransmission(0x38+a);
        Wire.write(~eeprom_read_byte((unsigned char *) LastFallback0+a));
        Wire.endTransmission();
      }
      RANet_Status=RANet_Down;
    }
    if (RANet_Status==RANet_Down)
    {
        BlueChannel=0;
        WhiteChannel=millis()%2000<1000?0:100;
        analogWrite(WHITE_LED,WHITE_INTENSITY*WhiteChannel/100);
        analogWrite(BLUE_LED,BLUE_INTENSITY*BlueChannel/100);
    }
  }
  if (RANetData[TRIGGER]==LIGHTNING) Lightning();    // Look for lightning trigger
  RANetData[TRIGGER]=0;    // Clear trigger byte.
}

void UpdateWhiteChannel()
{
  WhiteChannel=sin(radians((millis()%7200)/40))*255;
  BlueChannel=255-(sin(radians((millis()%7200)/40))*255);
  analogWrite(WHITE_LED,WhiteChannel);
  analogWrite(BLUE_LED,BlueChannel);
}

void Lightning()
{
  int a=random(1,5);    // Pick a number of consecutive flashes from 1 to 4.  
  for (int i=0; i<a; i++)
  {
    // Flash on
    int newdata=4095;
    Wire.beginTransmission(0x40);      // Address of the dimming expansion module
    Wire.write(0x8+(4*1));             // 0x8 is channel 0, 0x12 is channel 1, etc.  I'm using channel 1.
    Wire.write(newdata&0xff);          // Send the data 8 bits at a time.  This sends the LSB
    Wire.write(newdata>>8);            // This sends the MSB
    Wire.endTransmission();
    
    int randy=random(20,80);    // Random number for a delay
    if (randy>71) randy=((randy-70)/2)*100;    // Small chance of a longer delay
    delay(randy);                // Wait from 20 to 69 ms, or 100-400 ms
    
    // Flash off
    Wire.beginTransmission(0x40);    // Same as above
    Wire.write(0x8+(4*1));
    Wire.write(RANetData[20]);      // Return to previous value
    Wire.write(RANetData[21]);
    Wire.endTransmission();
    
    delay(random(30,50));                // Wait from 30 to 49 ms 
  }
}
Attachments
RANetReceiver.ino
(5.92 KiB) Downloaded 512 times
Roberto.
bencollinz
Posts: 140
Joined: Wed Mar 13, 2013 5:36 pm

Re: RANet firmware update

Post by bencollinz »

Is there a way to power up the board and test this without having to put it all back together? I have mine running off the USB cable right now and I really don't want to take it all apart just to not work again.
Image
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: RANet firmware update

Post by Sacohen »

Thanks for the update.

I did it and when I opened the relay module the wire that is connected to the RANet module was not connected to anything.
Image

I could not see where it should be connected.

I put the whole thing back together and it all seems to be working in terms of the ports all turn on and off remotely, but the Reef Angle logo doe not light up at all now.

It worked fine before.
rimai
Posts: 12857
Joined: Fri Mar 18, 2011 6:47 pm

Re: RANet firmware update

Post by rimai »

bencollinz wrote:Is there a way to power up the board and test this without having to put it all back together? I have mine running off the USB cable right now and I really don't want to take it all apart just to not work again.
You can power it with the power cord without putting it back together. Just be careful not to touch the socket pins.
Roberto.
rimai
Posts: 12857
Joined: Fri Mar 18, 2011 6:47 pm

Re: RANet firmware update

Post by rimai »

Sacohen wrote:Thanks for the update.

I did it and when I opened the relay module the wire that is connected to the RANet module was not connected to anything.
Image

I could not see where it should be connected.

I put the whole thing back together and it all seems to be working in terms of the ports all turn on and off remotely, but the Reef Angle logo doe not light up at all now.

It worked fine before.
The logo plugs into that connector
Roberto.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: RANet firmware update

Post by Sacohen »

I'll open it again, but the other end coming from that connection was not attached to anything.

Sent from my SM-G928P using Tapatalk
bencollinz
Posts: 140
Joined: Wed Mar 13, 2013 5:36 pm

Re: RANet firmware update

Post by bencollinz »

Sacohen wrote:I'll open it again, but the other end coming from that connection was not attached to anything.

Sent from my SM-G928P using Tapatalk
It's a very tiny LED. I thought the same thing. It's supposed to be glued/taped to the closest end of the RA logo to that connector. I used electrical tape.
Image
bencollinz
Posts: 140
Joined: Wed Mar 13, 2013 5:36 pm

Re: RANet firmware update

Post by bencollinz »

Sacohen wrote:I'll open it again, but the other end coming from that connection was not attached to anything.

Sent from my SM-G928P using Tapatalk
20160207_081013.jpg
20160207_081013.jpg (556.95 KiB) Viewed 8710 times
side note; I love how upload size is limited to 2MB then you try to upload it after resizing and the limit changes to 1MB. something odd happening there.
Image
bencollinz
Posts: 140
Joined: Wed Mar 13, 2013 5:36 pm

Re: RANet firmware update

Post by bencollinz »

rimai wrote:I'd like to thank GiraffeCat for working with me on troubleshooting and finding out about this issue.
All RANet relay boxes and dimming modules will need this update.
Symptom: your RANet relay box blinks white/blue very fast and causes the relay box to go into fallback.
If you would like to see the symptom, upload this code:

Code: Select all

#include <SoftwareSerial.h>
#include <ReefAngel_Features.h>
#include <Globals.h>
#include <RA_Wifi.h>
#include <Wire.h>
#include <OneWire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <InternalEEPROM.h>
#include <RA_NokiaLCD.h>
#include <RA_ATO.h>
#include <RA_Joystick.h>
#include <LED.h>
#include <RA_TempSensor.h>
#include <Relay.h>
#include <RA_PWM.h>
#include <Timer.h>
#include <Memory.h>
#include <InternalEEPROM.h>
#include <RA_Colors.h>
#include <RA_CustomColors.h>
#include <Salinity.h>
#include <RF.h>
#include <IO.h>
#include <ORP.h>
#include <AI.h>
#include <PH.h>
#include <WaterLevel.h>
#include <Humidity.h>
#include <DCPump.h>
#include <PAR.h>
#include <ReefAngel.h>

void setup()
{
    ReefAngel.Init();  //Initialize controller
    ReefAngel.Use2014Screen();  // Let's use 2014 Screen 
    ReefAngel.AddRANet();  // RANet Add-On Module
    ReefAngel.Relay.On(Box1_Port2);
    ReefAngel.Relay.On(Box1_Port4);
}

void loop()
{
    ReefAngel.ShowInterface();
}
How to perform the firmware update:
1. Open the relay box/dimming module enclosure
2. Locate the RANet module inside.
RANet-Module.jpg
3. Remove it from the expansion module
4. Detach the Xbee radio. Be careful not to damage the pins of the radio. Be very gentle.
5. Connect the USB-TTL cable to the RANet module
RANet-connection.jpg
6. Download the attached code and open the code on Arduino or copy and paste it from the bottom of the post
8. Change the board to "Reef Angel Controller w/ optiboot"
9. Upload the code
10. Make sure the code uploads and Arduino says "Done uploading" without errors
11. Disconnect the USB-TTL cable
12. Attach the Xbee radio
13. Place the RANet module back into the expansion module
14. Close the enclosure
15. Change the board back to "Reef Angel Plus"
16. Upload the code above to confirm that the firmware fixed the issue

This is the latest firmware code:

Code: Select all

#include <Wire.h>
#include <avr/eeprom.h>

#define BLUE_LED    9
#define WHITE_LED   10

#define BLUE_INTENSITY   255
#define WHITE_INTENSITY  255

#define RANET_MAX_SIZE  68
#define DISCONNECT_TIMEOUT  10000

#define LastFallback0  100 // Memory location for fallback storage

#define RANet_Down    0
#define RANet_OK      1

#define LIGHTNING 1    // Trigger value for lightning strike
#define TRIGGER 62     // Position in payload of trigger byte

byte buffer_index;
byte buffer[128];
char buf[3];
byte bufint, bufsize;
byte RANetData[RANET_MAX_SIZE];
byte RANetCRC;
byte BlueChannel=0;
byte WhiteChannel=0;
byte RANet_Status=RANet_Down;
boolean cable_present=false;

unsigned long lastmillis=millis();
unsigned long lastcablecheck=millis();
char lastc=0;

void setup()
{
  pinMode(BLUE_LED,OUTPUT);
  pinMode(WHITE_LED,OUTPUT);
  Serial.begin(57600);
  Wire.onReceive(NULL);
  Wire.onRequest(NULL);
  Wire.begin();
  for (int a=0;a<RANET_MAX_SIZE; a++) // Clear array
    RANetData[a]=0; 
  Wire.beginTransmission(0x68);
  Wire.write(0);
  int a=Wire.endTransmission();
  cable_present=(a==0);
  // setup PCA9685 for data receive
  // we need this to make sure it will work if connected ofter controller is booted, so we need to send it all the time.
  Wire.beginTransmission(0x40);
  Wire.write(0);
  Wire.write(0xa1);
  Wire.endTransmission();
}

void loop()
{
  if (cable_present)
  {
    BlueChannel=100;
    WhiteChannel=0;
    analogWrite(WHITE_LED,WHITE_INTENSITY*WhiteChannel/100);
    analogWrite(BLUE_LED,BLUE_INTENSITY*BlueChannel/100);
  }
  else  
  {
    BlueChannel=0;
    WhiteChannel=0;
    while(Serial.available())
    {
      UpdateWhiteChannel();
      char c = Serial.read(); // Read each incoming byte
      buffer[buffer_index]=c; // store in the buffer array
      if (c==10 && lastc==13) // if line feed we analyze the payload
      {
        if (buffer_index>25) // only need to analyse if buffer_index is greater than 25, otherwise the payload is broken or corrupt
          if (buffer_index==buffer[1]) // check if payload matches the length the controller sent
          {
            UpdateWhiteChannel();
            RANetCRC=0;
            for (int a=0; a<(buffer_index-2); a++) // calculate CRC
              RANetCRC+=buffer[a];
            UpdateWhiteChannel();
            if (RANetCRC==buffer[buffer_index-2]) // if CRC matches
            {
              UpdateWhiteChannel();
              for (int a=0; a<(buffer_index-2); a++) // Copy buffer to RANetData
                RANetData[a]=buffer[a];
              UpdateWhiteChannel();
              lastmillis=millis();
  //            Serial.print(millis());
  //            Serial.print("\t");
  //            Serial.println(RANetData[2]);
              for (int a=0;a<8;a++)
              {
                if (eeprom_read_byte((unsigned char *) LastFallback0+a)!=RANetData[10+a])
                {
                  eeprom_write_byte((unsigned char *) LastFallback0+a, RANetData[10+a]);
                }
                Wire.beginTransmission(0x38+a);
                Wire.write(~RANetData[2+a]);
                Wire.endTransmission();
              }
              for (int a=0;a<12;a=a+2)    // Step through the 12 bytes of dimming data
              {
                byte newdata=(RANetData[18+a]);    //LSB
                byte newdata1=(RANetData[18+a+1]);  //MSB
                Wire.beginTransmission(0x40);
                Wire.write(0x8+(4*(a/2)));  // Channels 0 through 5
                Wire.write(newdata);    // Write LSB
                Wire.write(newdata1);   // Write MSB
                Wire.endTransmission();
              }
              UpdateWhiteChannel();
              RANet_Status=RANet_OK;
            }
          }
        buffer_index=255; // reset buffer index
      }
      lastc=c;
      UpdateWhiteChannel();
      if (buffer_index++>=128) buffer_index=0; // increment index of buffer array. reset index if >=128
    }
    if (millis()-lastmillis>DISCONNECT_TIMEOUT)
    {
      lastmillis=millis();
  //    Serial.println("Disconnected");
  //    Serial.print(millis());
  //    Serial.print("\t");
  //    Serial.println(RANetData[10]);
      for (int a=0;a<8;a++)
      {
        Wire.beginTransmission(0x38+a);
        Wire.write(~eeprom_read_byte((unsigned char *) LastFallback0+a));
        Wire.endTransmission();
      }
      RANet_Status=RANet_Down;
    }
    if (RANet_Status==RANet_Down)
    {
        BlueChannel=0;
        WhiteChannel=millis()%2000<1000?0:100;
        analogWrite(WHITE_LED,WHITE_INTENSITY*WhiteChannel/100);
        analogWrite(BLUE_LED,BLUE_INTENSITY*BlueChannel/100);
    }
  }
  if (RANetData[TRIGGER]==LIGHTNING) Lightning();    // Look for lightning trigger
  RANetData[TRIGGER]=0;    // Clear trigger byte.
}

void UpdateWhiteChannel()
{
  WhiteChannel=sin(radians((millis()%7200)/40))*255;
  BlueChannel=255-(sin(radians((millis()%7200)/40))*255);
  analogWrite(WHITE_LED,WhiteChannel);
  analogWrite(BLUE_LED,BlueChannel);
}

void Lightning()
{
  int a=random(1,5);    // Pick a number of consecutive flashes from 1 to 4.  
  for (int i=0; i<a; i++)
  {
    // Flash on
    int newdata=4095;
    Wire.beginTransmission(0x40);      // Address of the dimming expansion module
    Wire.write(0x8+(4*1));             // 0x8 is channel 0, 0x12 is channel 1, etc.  I'm using channel 1.
    Wire.write(newdata&0xff);          // Send the data 8 bits at a time.  This sends the LSB
    Wire.write(newdata>>8);            // This sends the MSB
    Wire.endTransmission();
    
    int randy=random(20,80);    // Random number for a delay
    if (randy>71) randy=((randy-70)/2)*100;    // Small chance of a longer delay
    delay(randy);                // Wait from 20 to 69 ms, or 100-400 ms
    
    // Flash off
    Wire.beginTransmission(0x40);    // Same as above
    Wire.write(0x8+(4*1));
    Wire.write(RANetData[20]);      // Return to previous value
    Wire.write(RANetData[21]);
    Wire.endTransmission();
    
    delay(random(30,50));                // Wait from 30 to 49 ms 
  }
}
This fixed the issue! Thank you very much. Any idea why it started?
Image
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: RANet firmware update

Post by Sacohen »

bencollinz wrote:
Sacohen wrote:I'll open it again, but the other end coming from that connection was not attached to anything.

Sent from my SM-G928P using Tapatalk
It's a very tiny LED. I thought the same thing. It's supposed to be glued/taped to the closest end of the RA logo to that connector. I used electrical tape.
Thanks. I thought it was something like that.

Sent from my SM-G928P using Tapatalk
User avatar
GiraffeCat
Posts: 61
Joined: Sun Jun 08, 2014 12:55 pm
Location: Firestone

Re: RANet firmware update

Post by GiraffeCat »

rimai wrote:I'd like to thank GiraffeCat for working with me on troubleshooting and finding out about this issue.
All RANet relay boxes and dimming modules will need this update.
Symptom: your RANet relay box blinks white/blue very fast and causes the relay box to go into fallback.
If you would like to see the symptom, upload this.....

I would like to thank Roberto, because where else are you going to find this kind of tech support? (Check the time he posted this update thread if you don't believe me.) So, Thank You!

It has been a while since I changed the code on the boards, and haven't seen the issue return. (I waited to reply until I was sure.) I am going to start messing with stuff again soon, and I hope to not find any more bugs.

Thanks again for getting this figured out,
Dan
.....Your forefathers wisely set aside their compassion - Steeled themselves for what needed to be done.......
Post Reply