Time for additional Ethernet add-on with PWM output

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

Re: Time for additional Ethernet add-on with PWM output

Post by rimai »

topjimmy wrote:I think I am going to have to upgrade. Is it just a swap out of the board?
yes :)
Roberto.
topjimmy
Posts: 146
Joined: Tue May 08, 2012 8:16 am

Re: Time for additional Ethernet add-on with PWM output

Post by topjimmy »

Well I am just going to pic a few of the wave functions for now.

Roberto, what library can I look at to see the i2c functions? I know obviously wire.h but do you have some other written that would help with communications?

No sense reinventing the wheel.
Image
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Time for additional Ethernet add-on with PWM output

Post by rimai »

RA_PWM.cpp
And this is what I load in the dimming module for your reference:

Code: Select all

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

byte PWMports[] ={
  3,5,6,9,10,11};
byte ChannelValue[] = {
  0,0,0,0,0,0};

byte cmdnum=255;
byte datanum=255;
void setup()
{
  Serial.begin(57600);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(NULL);
  Wire.begin(8);
  randomSeed(analogRead(0));
  pinMode(3,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  wdt_enable(WDTO_1S);
}

void loop()
{
  wdt_reset();
  //Serial.println(ChannelValue[0],DEC);
  if (cmdnum!=255)
  {
    ProcessCMD(cmdnum,datanum);    
    cmdnum=255;
    datanum=255;
  }
}

void receiveEvent(int howMany) {
  wdt_reset();
  if (howMany==5)
  {
    byte cmd1, cmd2, cmd3, cmd4, cmd5;
    cmd1=Wire.read();
    cmd2=Wire.read();
    cmd3=Wire.read();
    cmd4=Wire.read();
    cmd5=Wire.read();
    if (cmd1=='$' && cmd2=='$' && cmd3=='$')
    {
      cmdnum=cmd4;
      datanum=cmd5;
      //Serial.println(cmd4,DEC);
      //Serial.println(cmd5,DEC);
    }
  }
  else
  {
    for (int a=0;a<howMany;a++)
    {
      Wire.read();
    }
  }  
}

void ProcessCMD(byte cmd, byte data)
{
  wdt_reset();
  // Individual Channel
  if (cmd>=0 && cmd<=5)
  {
    ChannelValue[cmd]=data;
    analogWrite(PWMports[cmd],data);
  }
}
Roberto.
topjimmy
Posts: 146
Joined: Tue May 08, 2012 8:16 am

Re: Time for additional Ethernet add-on with PWM output

Post by topjimmy »

Thanks!
Image
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

Very cool...that's super helpful :)
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
topjimmy
Posts: 146
Joined: Tue May 08, 2012 8:16 am

Re: Time for additional Ethernet add-on with PWM output

Post by topjimmy »

I also managed to get more pump functions to fit, once I realized that they were written for two pumps, using both daylight and actinic channels :oops:

I only have one pump...... :lol:
Image
topjimmy
Posts: 146
Joined: Tue May 08, 2012 8:16 am

Re: Time for additional Ethernet add-on with PWM output

Post by topjimmy »

Quick update: I loaded the code Roberto posted for the PWM expansion onto the Uno\Ethernet. I soldered the SDA , Clock, and ground to the 4a,5a and grn. I used 10k resistors connected to the +5v header to the clock and sda connections. The relay and the head unit did not like it. The relay buzzed and the head unit would randomly reset.

I did set up the head units jumpers, and used a fresh wizard with pwm exp. enabled.

I don't know if I can use that 5v header. I figured it was ok but maybe I'm mistaken. I can post the hybrid code that is part Drews ethernet, part PWM expansion if anyone is interested in looking it over.

Edit: Added mish mash of code.

Code: Select all

#include <SPI.h>

#include <Wire.h>
#include <avr/wdt.h>
#include <Ethernet.h>


byte PWMports[] ={
  3,5,6,9,10,11};
byte ChannelValue[] = {
  0,0,0,0,0,0};

byte cmdnum=255;
byte datanum=255;


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //YOUR MAC off sticker
IPAddress ip(192,168,1, 110); //YOUR STATIC IP HERE
IPAddress remoteserver(198,171,134,6); // forum.reefangel.com
EthernetClient clientout;
unsigned long lastmillisout=millis();
unsigned long lastmillisin=millis();
boolean sending=false;
char strout[400];

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

/*||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ETHERNET MODULE  |||||||||||||||||||||||||||||||||||||||||||||||*/

void ethernetmodule() //create ethernet function for loop
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      while (client.available()) {
        char c = client.read();
        if (sending==false) Serial.write(c);
        lastmillisin=millis();        
      }
      while (Serial.available()){
        memset(strout,0,sizeof(strout));
        int b=Serial.readBytesUntil('>',strout,sizeof(strout));
        client.print(strout);
        client.print(">");
        if (millis()-lastmillisin>2000)
        {

          lastmillisin=millis();
          client.stop();
        }
      }
      if (millis()-lastmillisin>2000)
      {

        lastmillisin=millis();
        client.stop();
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();

  }
  else
  {
    if (Serial.available())
    {
      memset(strout,0,sizeof(strout));
      int b=Serial.readBytesUntil('\n',strout,sizeof(strout));

      if (b>10)
      {
        sending=true;
        Serial.print(strout);
        Serial.println(" HTTP/1.0");
        Serial.println();
        if (clientout.connect(remoteserver, 80)) 
        {

          clientout.print(strout);
          clientout.println(" HTTP/1.0");
          clientout.println();
          lastmillisout=millis();
        }
      }
    }
    while (clientout.available()) {
      char c = clientout.read();
      Serial.print(c);
      if (millis()-lastmillisout>8000)
      {
        lastmillisout=millis();
        clientout.stop();
        sending=false;
      }
    }
    if (!clientout.connected())
    {
      clientout.stop();
      sending=false;
    }
  }
  if (millis()-lastmillisout>8000)
  {
    lastmillisout=millis();
    clientout.stop();
    sending=false;
  }



  //GET /status/submitp.aspx?t1=806&t2=0&t3=0&ph=441&id=test&em=0&rem=0&key=&atohigh=0&atolow=0&r=0&ron=0&roff=255 HTTP/1.0

  //GET /status/submitp.asp?t1=806&t2=0&t3=0&ph=441&id=test&em=0&rem=0&key=&atohigh=0&atolow=0&r=0&ron=0&roff=255 HTTP/1.0
  //Host: forum.reefangel.com
  //
  //GET /status/submitp.aspx?t1=806&t2=0&t3=0&ph=441&id=test&em=0&rem=0&key=&atohigh=0&atolow=0&r=0&ron=0&roff=255 HTTP/1.0
  //Host: forum.reefangel.com
  //Connection: Keep-Alive



}
/*||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| END ETHERNET MODULE  |||||||||||||||||||||||||||||||||||||||||||||||*/



void setup()
{
  Serial.begin(57600);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(NULL);
  Wire.begin(8);
  randomSeed(analogRead(0));
  pinMode(3,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(9,OUTPUT);
//  pinMode(10,OUTPUT);
//  pinMode(11,OUTPUT);
  wdt_enable(WDTO_1S);
  
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  // Ethernet connection ok and server started
}


  
void loop()
{

  wdt_reset();
  //Serial.println(ChannelValue[0],DEC);
  if (cmdnum!=255)
  {
    ProcessCMD(cmdnum,datanum);    
    cmdnum=255;
    datanum=255;
  }
}

void receiveEvent(int howMany) {
  wdt_reset();
  if (howMany==5)
  {
    byte cmd1, cmd2, cmd3, cmd4, cmd5;
    cmd1=Wire.read();
    cmd2=Wire.read();
    cmd3=Wire.read();
    cmd4=Wire.read();
    cmd5=Wire.read();
    if (cmd1=='$' && cmd2=='$' && cmd3=='$')
    {
      cmdnum=cmd4;
      datanum=cmd5;
      //Serial.println(cmd4,DEC);
      //Serial.println(cmd5,DEC);
    }
  }
  else
  {
    for (int a=0;a<howMany;a++)
    {
      Wire.read();
    }
  }  
}

void ProcessCMD(byte cmd, byte data)
{
  wdt_reset();
  // Individual Channel
  if (cmd>=0 && cmd<=5)
  {
    ChannelValue[cmd]=data;
    analogWrite(PWMports[cmd],data);
  }
}
Image
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

Show me some pics of what you're up to.
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
topjimmy
Posts: 146
Joined: Tue May 08, 2012 8:16 am

Re: Time for additional Ethernet add-on with PWM output

Post by topjimmy »

DrewPalmer04 wrote:Show me some pics of what you're up to.
I don't have a pic but I'll try to explain better

On the 5v header I have two 10k resistors soldered to the pin, one goes to the pin for SDA, one to the SCL. Then from the relay unit I use the Data - , Data + and Grd, Each of those is soldered to the appropriate pin.

I assume that once power is supplied to the board. That all 5v pins are now "hot" and that there is no need to bring in any other 5v feed. The RA head unit is powering the Uno. So power is then dispersed through the regulator to all the +vcc pins.

Does it matter that I used the pin on the board as the +5v pull up for the resistor? Or does it have to be on the line coming in to the board?
Image
topjimmy
Posts: 146
Joined: Tue May 08, 2012 8:16 am

Re: Time for additional Ethernet add-on with PWM output

Post by topjimmy »

edited earlier post to explain it better.
Image
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

So you have a resistor between the RA and the SDA on the uno? I would suggest powering the uno from a solo power source and trying to just use only the SDA/SCL for communication. Start simple...once it's communicating, then attempt to power the uno from the RA...:)
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
topjimmy
Posts: 146
Joined: Tue May 08, 2012 8:16 am

Re: Time for additional Ethernet add-on with PWM output

Post by topjimmy »

DrewPalmer04 wrote:So you have a resistor between the RA and the SDA on the uno? I would suggest powering the uno from a solo power source and trying to just use only the SDA/SCL for communication. Start simple...once it's communicating, then attempt to power the uno from the RA...:)
There are two 10k pull up resistors.

Image

Not my pic but exactly what I did, minus the 5v connection from the usb. I snipped that off.
Image
topjimmy
Posts: 146
Joined: Tue May 08, 2012 8:16 am

Re: Time for additional Ethernet add-on with PWM output

Post by topjimmy »

Success!

I re soldered the connections,and I am now using the shield to act as the pwm expansion. I ordered one of the Chinese combos and as of now, it is the pwm expansion and the other shield is the ethernet. I am going to try and combine them, I should just have to connect the tx rx and ground to make the wifi part work also on the combo

Much thanks to Roberto for the code,and to Drew for the original ethernet work
Last edited by topjimmy on Wed May 01, 2013 4:44 am, edited 1 time in total.
Image
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

Cool. Give us a breakdown of your complete methods if you'd like. Start a thread. I'm sure others might enjoy this.
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
topjimmy
Posts: 146
Joined: Tue May 08, 2012 8:16 am

Re: Time for additional Ethernet add-on with PWM output

Post by topjimmy »

I need to do some more testing, then I will. I am an hour off on time so I don't think it's syncing with the RTC. Not sure though. I might work on it this weekend.
Image
topjimmy
Posts: 146
Joined: Tue May 08, 2012 8:16 am

Re: Time for additional Ethernet add-on with PWM output

Post by topjimmy »

Roberto,
Any reason as to why the PWM signal would start an hour early? I assumed that the expansion sets it's time with the head unit.

I must have jiggered something up....
Image
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Time for additional Ethernet add-on with PWM output

Post by rimai »

You are probably running 2 time syncs.
I use RTC from the head unit and I think you are syncing with the NTP from internet.
Make sure you account for daylight savings.
Roberto.
topjimmy
Posts: 146
Joined: Tue May 08, 2012 8:16 am

Re: Time for additional Ethernet add-on with PWM output

Post by topjimmy »

I removed the time sync that drew had put in. I'll have to get some serial monitor on it and see what's going on.
Image
User avatar
DrewPalmer04
Posts: 818
Joined: Tue May 29, 2012 2:12 pm
Location: Christopher, IL

Re: Time for additional Ethernet add-on with PWM output

Post by DrewPalmer04 »

There will be a conflict of using NTP and I2C. NTP defines the time based on the packets received from the NTP server. Then youre also trying to define time from the RTC. So they are trying to define the time far too often together. Good move just getting rid of ANYTHING NTP related. Then start fresh with RTClib and Wire.h :)
Out for now...but not over.

VISIT: Ethernet Module/Wifi Alternative
Francois
Posts: 30
Joined: Tue Aug 30, 2011 2:08 am
Location: South Africa

Re: Time for additional Ethernet add-on with PWM output

Post by Francois »

Thank you DrewPalmer04 for all your hard work and setting up the code for us....stunning work man! but one question....How do I get the ethernet board to communicate with the android app? The web portal and the iphone app works perfectly. I'm getting Error 23: XML SAX Parser the whole time with the android app. but can still toggle the ports on and off....but can't retrieve data. Maybe Curt can assist?
Image
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Time for additional Ethernet add-on with PWM output

Post by binder »

Francois wrote:Thank you DrewPalmer04 for all your hard work and setting up the code for us....stunning work man! but one question....How do I get the ethernet board to communicate with the android app? The web portal and the iphone app works perfectly. I'm getting Error 23: XML SAX Parser the whole time with the android app. but can still toggle the ports on and off....but can't retrieve data. Maybe Curt can assist?
It could have something to do with the version of software you are running on both the android app and the controller. Sometimes there are xml tags that are sent from the controller that cause problems with the app processing. It can also be that you are communicating too fast with the controller or something else is interfering with the data. On one of my test builds of the android app, I noticed the xml parse error happening with some older library versions. I unfortunately dont have a good answer of something to try. I will have to try looking into it further. I will say that some people have experienced problems if the flush value or timeouts were set to small or low.


Sent from my iPad mini
Post Reply