Get Values from Numeric DropDown Button

Support forum for MegunoLink
Post Reply
nigel91
Posts: 2
Joined: Sat Jul 18, 2015 2:56 pm

Wed Jul 22, 2015 4:17 pm

Hie . IM still new to megunolink . My problem is somewjhat small but cant work my way around it . i am trying to get the values from 4 dropdown buttons numbered 1-4 . i have in my arduino code a script that reads which number is on each drp Down button . My problem is when being read they are all jumbled . and not inorder . And sometimes it does not register until i press. i attempted using the GET command but still same issue . Below is my arduino code and megunolink program in a zip file
Attachments
megg - Copy.zip
(4.18 KiB) Downloaded 1070 times
Paul
Posts: 33
Joined: Wed Jun 10, 2015 10:35 pm

Fri Jul 24, 2015 10:50 am

Hi,

I could see two problems. First, you'd set a channel on the message processor, but weren't using that channel in the Arduino code. And second MegunoLink is going to send a response like {UI|set|NumberControl1.Value=3} for the get command, but you were looking for an integer.

I made an alternative Arduino program that might be a bit more straight forward. It uses our CommandHandler library (http://www.megunolink.com/documentation ... d-handler/) to listen for serial commands to change the priorities. So when you sent !SetPriority 1 3\r\n, it will change the value for Priority1 to 3. Then I made another command (RunRelays) the runs the relay sequence according to your priorities.

I updated the interface panel in MegunoLink Pro so that it sends the SetPriority commands when you change the number controls. If you hit the Send All button, it will send all of them at once. Another button sends the Run Relay command.

I've attached, and copied the code below. The updated MegunoLink Pro file is also in the zip archive. I did have trouble compiling with verision 1.6.4 of the Arduino IDE, but 1.6.5 (the latest) was fine.

Best of luck,
Paul.

Here's the modified Arduino program:

Code: Select all

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3
#include "MegunoLink.h"
#include "CommandHandler.h"

#define RELAY1  2 
#define RELAY2  3                        
#define RELAY3  4                        
#define RELAY4  5

int load1;            // Store state of selection
int load2;             // Store state of selection
int load3;            // Store state of selection
int load4 ;            // Store state of selection
int Priority1,Priority2,Priority3,Priority4;

InterfacePanel maximumdemand;

// -------------------------------------------------------------------
// Handle Serial Commands using the MLP Serial Command Handler Library
// See: http://www.megunolink.com/documentation/arduino-libraries/serial-command-handler/
CommandHandler<> SerialCommandHandler;

void Cmd_SetPriority(CommandParameter &p)
{
  int PriorityLevel = p.NextParameterAsInteger();
  int PriorityValue = p.NextParameterAsInteger();
  
  switch(PriorityLevel)
  {
    case 1:
      Priority1 = PriorityValue;
      break;
      
    case 2:
      Priority2 = PriorityValue;
      break;
      
    case 3:
      Priority3 = PriorityValue;
      break;
      
    case 4:
      Priority4 = PriorityValue;
      break;
      
    default:
      Serial.println("Unknown priority level");
      return;
  }
  
  Serial.print("Priority "); 
  Serial.print(PriorityLevel); 
  Serial.print(" = ");
  Serial.println(PriorityValue);
}

void Cmd_DoRelaySequence(CommandParameter &p)
{
   Serial.println("Running relay sequence...");
   
   // Activate the outputs. 
   digitalWrite( Priority4,HIGH);
   delay (1000);
   digitalWrite( Priority3,HIGH);
   delay (1000);
   digitalWrite( Priority2,HIGH);
   delay (1000);
   digitalWrite( Priority1,HIGH);
   delay (1000);
   
   // Deactivate outputs. 
   digitalWrite(Priority4, LOW);
   digitalWrite(Priority3, LOW);
   digitalWrite(Priority2, LOW);
   digitalWrite(Priority1, LOW);
   
   Serial.println("Done.");
}

void Cmd_Unknown()
{
  Serial.println("Unknown command!");
}

// -------------------------------------------------------------------

 void setup()
{  
 Serial.begin(9600);
 

   pinMode(RELAY1, OUTPUT);       
   pinMode(RELAY2, OUTPUT);
   pinMode(RELAY3, OUTPUT);
   pinMode(RELAY4, OUTPUT);
   
   // Wasn't sure what was happening here. I just commented it out
   // to speed up testing. 
//   digitalWrite(RELAY1,LOW);           // Turns ON Relays 1
//   delay(2000); 
//   digitalWrite(RELAY1,LOW);           // Turns ON Relays 1
//   delay(2000);
//   digitalWrite(RELAY2,LOW);           // Turns ON Relays 2
//   delay(2000);
//   digitalWrite(RELAY3,LOW);           // Turns ON Relays 3
//   delay(2000);
//   digitalWrite(RELAY4,LOW);           // Turns ON Relays 4
//   delay(2000);  

   
  // These probably aren't needed. I think it would do the same 
  // thing as the pinMode above. 
//  pinMode( Priority1,OUTPUT);
//  pinMode( Priority2,OUTPUT);
//  pinMode( Priority3,OUTPUT);
//  pinMode( Priority4,OUTPUT);

  // when serial messages are received, the serial command handler
  // will call the functions registered with AddCommand. 
  SerialCommandHandler.AddCommand(F("SetPriority"), Cmd_SetPriority);
  SerialCommandHandler.AddCommand(F("RunRelays"), Cmd_DoRelaySequence);
  SerialCommandHandler.SetDefaultHandler(Cmd_Unknown);
 
  Serial.println("Relay Sequencer Ready...");
}
 
void loop()
{
  // when serial messages are received, the serial command handler
  // will call the functions registered with AddCommand. 
  SerialCommandHandler.Process();
}
Attachments
megg.zip
Updated code and MegunoLink Pro file.
(4.75 KiB) Downloaded 1019 times
bobtarizona
Posts: 3
Joined: Sun Jan 21, 2018 5:26 pm

Sun Jan 21, 2018 11:15 pm

Please help!
I am new using the MeguinoLink Interface and sending data to control the Arduino pins.
I want to send a digital pin number to the Arduino and use that pin number to select the right pin. Next I want to use the Command button ON/OFF to turn that pin HIGH or LOW.
I have down loaded the Library example for turning a LED on and off but it is limited to one pre defined pin in the Ardiuno.

Next I downloaded the Ardunio sketch for turning on various relays that are connect to Arduino digital output pins. The ardunio download and compiles and uploads OK.

However the attached zip file that is supposed to be the correct MeguinoLinl Interface Panel ( Titled- Part1.mix) for this Ardunio program but does not have any Command buttons on the Interface panel. It only shows a Time Plot Panel and a Serial Connection Panel.



Can someone send the correct Meguino Interface to run this relay example.

Or show me the Interface Panel that can send the digital pin number and ON/OFF info th the Arduino and the Arduino code that will receive the digital pin number value, select that pin then receive the ON/OFF command and set that pin HIGH or LOW.

Thanks! bob T
philr
Posts: 446
Joined: Mon May 26, 2014 10:58 am

Mon Jan 22, 2018 4:15 am

Hi Bob, I'll work on a few extra examples to address some of these. I've added one to reconfigure the IO pin. These can be grabbed from github and will be included in a future release of the library.
https://github.com/Megunolink/MLP/blob/ ... putPin.ino

If you download the zip from github you can update your files manually. There is also a megunolink interface to control the output pin.

What sketch did you download for controlling relays? Can you send me the URL?

Cheers
Phil
bobtarizona
Posts: 3
Joined: Sun Jan 21, 2018 5:26 pm

Mon Jan 22, 2018 7:21 pm

Thanks Phil for your help. I have finally come up (after pulling my hair out for 3 days) with a very much simpler Ardunio code to turn on and off the digital output pins.

Phil I have attached two files for my project.

Please let me know what you think or added ideas.

Thnaks

Bobtarizona
Attachments
1-22-2018_Serial_Code_to_select_digital_pins_Ver_3.zip
MeguinoLink Interface Panel
(1.63 KiB) Downloaded 535 times
Works with Arduino turn digitl pins ON or OFF Ver 2.zip
Arduino Code for projet
(2.53 KiB) Downloaded 512 times
philr
Posts: 446
Joined: Mon May 26, 2014 10:58 am

Tue Jan 23, 2018 2:40 am

Hi Bob, nice work. That will do the job. Is it working reliably for you?

Cheers
Phil
bobtarizona
Posts: 3
Joined: Sun Jan 21, 2018 5:26 pm

Wed Jan 24, 2018 8:16 pm

Yes Phil that code is working OK. Of course it is just a beginning and I have already added a DHT11 Temp/Humidity sensors to my Arduino test brd and sketch. Then On the MegunoLink Panel I have added the Table to display those DTH11 values in real time.

Thanks for your comments. Some of this is a long learning path but being retired I have lots of free time to work with these kind of projects.
philr
Posts: 446
Joined: Mon May 26, 2014 10:58 am

Fri Jan 26, 2018 3:05 am

Great! I look forward to seeing how it all progresses. Post any of your questions here in the forum and I'll try my best to help out.
Phil
Post Reply