Arduino Forum

Products => Arduino Yún => Topic started by: stgeorge on Aug 10, 2016, 06:00 pm

Title: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 10, 2016, 06:00 pm
Hello-

I've got a standard push button program connected to a NO relay which closes when the 'button' is pushed, thereby connecting my Arduino's 5v pin to pin #3.

This standard button program constantly monitors the #3 pin state.

On my Arduino is a Yun wifi shield already connected to my LAN.  Because of the Yun shield setup, I don't really have an easy method of monitoring the shield via a serial monitor (nor do I think I need one).  What I do want to do though is insert appropriate programming commands to send a call to this API at an internet site (if/when the button is pushed and the pin state = High).  Here are the instructions (from the internet site) as to how to send the call appropriately so that I'll be notified by the website:


PushingBox API is really simple, to launch a scenario of notifications you can send an HTTP request or an email. The only argument you should attach is the DeviceID. This is the unique key that identify the scenario you want to launch. The DeviceID can be found on the Scenario Page.
 You can also put more arguments to define customs notifications text using your own variables.

HTTP API

Using command line in a terminal

GET Method curl "http://api.pushingbox.com/pushingbox?devid=v0123456789ABCDE"

POST Method curl -d "devid=v0123456789ABCDE" http://api.pushingbox.com/pushingbox

With PHP
$ch = curl_init('http://api.pushingbox.com/pushingbox?devid=v0123456789ABCDE');
curl_exec ($ch);
curl_close ($ch);


Can someone assist me with inserting language in my void loop (below) such that my Yun shield, which is already connected to the internet via my LAN, will execute this call (above):

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Thanks in advance for any help you can provide...  I'm thinking the POST method might be most effective, but I'm not certain how to set it up...
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 10, 2016, 09:04 pm
Take a look at the Bridge Library Process Tutorial (https://www.arduino.cc/en/Tutorial/Process).

That example shows using the curl command to retrieve an ASCII text file. You will need to change it to use the required command for your API service.

For example, something like:
Code: [Select]
 Process p;          
  p.begin("curl");    
  p.addParameter("-d");
  p.addParameter("\"devid=v0123456789ABCDE\"");
  p.addParameter("http://api.pushingbox.com/pushingbox");
  p.run();


I did not try to compile or run that, so you may have to make some changes to get it to work, but it's a starting point. Also, as mentioned in the tutorial, you will need to include process.h and call Bridge.begin().

Note that the parameter that includes the devid string includes quote characters inside the string, which are escaped with a backslash character. When updating the devid string, be careful not to delete the backslash that is escaping the ending quote character.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 10, 2016, 09:51 pm
Thanks for the assistance!

I think you're right- I have a lot to learn as it relates to the bridge stuff...

For starters, I'm curious what exactly it means when it says ...

In setup(), you'll want to initialize Bridge and start a serial connection. Before running the rest of setup() wait for a serial connection to become active.

So when you add the Bridge.begin(); and Serial.begin(9600); under void setup(), does that mean that a serial monitor window will randomly pop up or is there something else I'll need to do to get a serial monitor session working?

Also- is this the standard m.o. for serial monitoring with the Dragino Yun?

Thanks
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 10, 2016, 10:18 pm
In setup(), you'll want to initialize Bridge and start a serial connection. Before running the rest of setup() wait for a serial connection to become active.
The key is that you need to call Bridge.begin() before doing anything with a Process class object (or any of the other classes that are in the Bridge Library.

Opening up the serial port is not a required part of the using the Process class. That example has you open the serial port because it is using the Process class to call the curl command to retrieve a text file from an HTTP server. It then displays the retrieved data on the serial port. For your use, you do not need the serial port, unless you want to use it for debugging purposes or to see any error messages returned by the curl request.

Quote
So when you add the Bridge.begin(); and Serial.begin(9600); under void setup(), does that mean that a serial monitor window will randomly pop up or is there something else I'll need to do to get a serial monitor session working?
It will not automatically open a serial monitor window. You will need to manually open the Arduino Serial Monitor from within the Arduino IDE, or you will have to use some other serial terminal emulator program. That is, of course, if you decide you want to use the serial output.

Because this example sketch immediately loads the remote text file, and only does it once, it includes this line of code:
Code: [Select]
 while (!Serial);

This causes the sketch to wait until a USB serial connection is established - in other words, for the Serial Monitor to be opened. This way, the output from the Process command will be visible. Without this delay until a connection is made, the output would likely be displayed before you had a chance to open the serial monitor, and you would never see the output.

While that bit of code can be handy for debugging, you will probably not want it in your final sketch - if you include it, you would need to open a serial monitor each time you power up or restart your sketch. That's generally not a normal operating scenario for a finished product.

Quote
Also- is this the standard m.o. for serial monitoring with the Dragino Yun?
No, that's the standard debug output method for the official Arduino Yun board. That board uses an ATmega32U processor which has a USB serial port (Serial) for use by the Serial Monitor over the USB connection, and a second hardware serial port on pins 0 and 1 (Serial1) that talks to the Linux processor using the Bridge library.

The Dragino Yun Shield is different - it doesn't have an AVR processor to run a sketch, instead it is a shield to connect to an Arduino processor board. Since there are different boards it can be used with, and they have different processors, the ability to use a serial port for debugging and the Serial Monitor will vary. For example, if you are connecting the shield to an Uno, the only serial port available is connected to pins 0 and 1, which will need to be used for Linux communications with the Yun Shield. That means that the USB serial port needs to be disabled and Serial Monitor communications are not possible. If you are using it with an Arduino Leonardo, then that will be much closer to the official Yun, and you will have the USB serial port that you can use with the Serial Monitor. Other Arduino boards will have different limitations and abilities when used with the Dragino Yun Shield.

The Dragino site will be the prime place to go for information specific to the Dragino Yun Shield. That shield is similar to an official Yun, especially when connected to a Leonardo, but there are differences. Any Bridge library examples that come with the Arduino IDE or from this site will assume that you are using an official Arduino Yun, so some changes and allowances will have to be made to accommodate the differences from the Dragino Yun Shield. I have no direct experience with that shield, so I can't give you specific changes that will need to be made.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 10, 2016, 10:45 pm
Thanks- this might be getting to the bottom of a long standing problem that I've been having.  That is, with my Dragino Yun wifi shield connected to an Arduino Mega2560 board, I simply cannot seem to get the serial monitor to work with any of the examples from the Arduino IDE.  There must be something simple that I'm not doing or some programming language imbedded in the standard sketches which is causing my board to not communicate properly.  Odd because I thought this combination was going to be a very easy and capable setup to use.

Also, I find it quite confusing when people call it Serial Monitor, Serial Port, and Console Port...?  I've been to the Dragino website quite a bit and can't quite seem to figure this thing out.  None of the examples really seem to work when it comes to monitoring what is actually going on.  If it is a simple blink the led or something, that does work, but not much more....
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: DarkSabre on Aug 11, 2016, 02:56 am
...
Also, I find it quite confusing when people call it Serial Monitor, Serial Port, and Console Port...?
If it helps, the Console is somewhat different than the Serial Monitor.
The Serial Monitor is only going to work if you have the Arduino side physically connector to your computer.

I find the Console a lot more useful since it communicates with your computer over the local network (wireless or wired). The syntax is very similar ( Console.begin() , Console.println() , etc ). You will have to include "Console.h" and have Console.being() after the Bridge has started.

If the Arduino side is physically connected to the computer, Ctrl+Shift+M will open the Serial Monitor.
If the Arduino side is not connected to the computer and you're communicating via the IP address, Ctrl+Shift+M will open the Console.

I constantly use Console.println() to send debug information back to my computer when I'm working on a program.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 11, 2016, 01:54 pm
That is, with my Dragino Yun wifi shield connected to an Arduino Mega2560 board, I simply cannot seem to get the serial monitor to work with any of the examples from the Arduino IDE.
That is to be expected. If you take a closer look at the Yun Shield's Mega2560 installation guide (http://wiki.dragino.com/index.php?title=Yun_Shield#Connect_to_Arduino_Mega2560), it talks about a conflict between the shield communications and the Arduino's USB communications: the first step is to disable the USB serial port. None of the Arduino example sketches that make use of the Serial communications class or the Serial Monitor will work with your setup.

Quote
Odd because I thought this combination was going to be a very easy and capable setup to use.
Capable: yes. Easy: not so much. Any examples that come with the Arduino IDE will assume you are using an official Arduino board or one that is fully compatible. This is especially true of the Bridge library examples: they assume you are using a genuine Arduino Yun board. Dragino states that their shield gives you similar capabilities to the Yun, but it is not exactly the same. There are some subtle and not so subtle differences, and dealing with those differences are a more advanced topic requiring some experience and understanding of the issues.

If you want an easy to use system, one where all of the examples will work without modification, then use an actual Arduino Yun board, not a clone. Once you understand the way things work and know the differences and their implications, then go with the shield coupled with a more advanced processor board like the Mega2560. But be aware that it will require more effort and finesse - a trade-off for the greater power and flexibility. That's just my opinion.

To be fair, I have no personal experience with the Dragino Yun Shield. But I've seen many people come here with various issues that make many of the subtle differences apparent. And I've studied Dragino's wiki on the shield, as well as the schematics, so I  think I understand what is causing many of these differences. The Yun Shield is quite capable, and it does much of the same things, but it is not exactly the same as an actual Yun. 

Quote
Also, I find it quite confusing when people call it Serial Monitor, Serial Port, and Console Port...?
Some people use the terms loosely or improperly, but basically:
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 11, 2016, 11:36 pm
Hey- thanks for your thoughtful explanation- very helpful!

I guess what attracted me to Dragino's version is the wifi aspect of it since I don't have an easy ethernet solution to where I'd prefer for my Mega2560 to be located.  That being said, and given your explanation, I'm now thinking of getting a Yun and working out an ethernet cable to that spot.

Sadly, I get the sense too that the Dragino is very capable, but unfortunately, the folks at Dragino are very Chinese if you know what I mean- their support needs a bit of western interpretation help.  If I could communicate with someone there who could talk to me in western fashion (i.e. not just English, but with helpful explanation of what my troubles are etc), I'd probably stick it out.  Until then, I'll probably hook up a Yun if I can find one that is not too expensive.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 12, 2016, 12:00 am
That being said, and given your explanation, I'm now thinking of getting a Yun and working out an ethernet cable to that spot.
The official Arduino Yun does have the same WiFi abilities as the Dragino Shield. It has both a WiFi interface, and wired Ethernet interface. I routinely use one or the other, or even both at times.

Quote
Sadly, I get the sense too that the Dragino is very capable, but unfortunately, the folks at Dragino are very Chinese if you know what I mean- their support needs a bit of western interpretation help.
Yes, I fully understand. It's that way with a lot of products that come out the Shanzhai product copying culture. It's common to duplicate a product and undercut a price - they can afford to do that since they didn't spend a lot of money on the initial engineering development - they just copied an existing product. Support often suffers on such products. To be fair, the Dragino Yun Shield does have some original work behind it to remove the AVR processor and turn it into a shield. But it is those changes that cause the potential problems due to the new incompatibilities with the original product. And I think that their support may be lacking not only because of the language communications issues, but also because they don't have the original engineering experience that went into the original product, and they simply might not understand the product as well.

Quote
Until then, I'll probably hook up a Yun if I can find one that is not too expensive.
Yes, the official Arduino Yun boards are expensive, and can be hard to find. But mine have been rock solid performers, with great support from the Arduino IDE (everything just plain works) and from this community. That's priceless, in my mind.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 12, 2016, 12:16 am
Thanks again.

Where do you typically source yours?  I typically troll Amazon and/or Ebay.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 12, 2016, 01:00 pm
I bought a bunch of them from SparkFun when they were a relatively new product. A little more expensive than some sellers, but easy to get free shipping, and I like to support them since they do so much for the open source and maker communities.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 16, 2016, 02:07 am
OK- now I'm cooking with gas, *but* the following sketch fails to compile....any assistance would be appreciated!

Setup is Arduino Leonardo with a Dragino Yun shield on top

#include <process.h>

void setup() {
Bridge.begin();
Serial.begin(9600);
while (!Serial);
runCurl();
 runCpuInfo();
}

void loop() {

}

void runCurl() {
 Process p;            
 p.begin("curl");
 p.addParameter("-d");
 p.addParameter("\"devid=vBECAF1F4A7D765"");
 p.addParameter("http://api.pushingbox.com/pushingbox");
 p.run();

 while (p.available()>0)
   char c = p.read();
   Serial.print(c);
 Serial.flush();
}
void runCpuInfo() {
 Process p;
 p.begin("cat");      
 p.addParameter("/proc/cpuinfo");
 p.run();

 while (p.available()>0) {
   char c = p.read();
   Serial.print(c);
 }
 Serial.flush();
}
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 16, 2016, 02:14 am
Question:  When an Arduino sketch fails to compile....how does one trace back to the problem?  i'm not seeing it!
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: DarkSabre on Aug 16, 2016, 03:21 am
Compare this line by line to what you had.
You were missing several closing }'s.
You had a ; after a while statement instead of a {.
I don't know exactly what format this is supposed to be:
  p.addParameter("\"devid=vBECAF1F4A7D765\"");
but I added another \ to escape the other ".
Bridge needed to be included.


Code: [Select]
#include <Bridge.h>
#include <Process.h>

void setup() {
  Bridge.begin();
  Serial.begin(9600);
  while (!Serial) {
    runCurl();
    runCpuInfo();
  }
}

void loop() {

}

void runCurl() {
  Process p;
  p.begin("curl");
  p.addParameter("-d");
  p.addParameter("\"devid=vBECAF1F4A7D765\"");
  p.addParameter("http://api.pushingbox.com/pushingbox");
  p.run();

  while (p.available() > 0) {
    char c = p.read();
    Serial.print(c);
    Serial.flush();
  }

}

void runCpuInfo() {
  Process p;
  p.begin("cat");
  p.addParameter("/proc/cpuinfo");
  p.run();

  while (p.available() > 0) {
    char c = p.read();
    Serial.print(c);
  }
  Serial.flush();
}


The errors all appear at the bottom of the screen, but you have to troubleshoot them pretty much 1 at a time.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 16, 2016, 02:00 pm
Question:  When an Arduino sketch fails to compile....how does one trace back to the problem?  i'm not seeing it!
The error message will include a (sometimes cryptic) description of the problem, and a line number. Look carefully at that indicated line, and see if there is anything wrong the actual error could possibly be on the line or two above the line mentioned in the message. It could be a simple spelling mistake, a missing comma, semicolon, curly brace, or something like that. It could be using the wrong data type, the wrong number of parameters in a function call, or many other details. The compiler is very picky, and wants things to be just so - being close to the right syntax is not good enough, you've got to pay attention to all of the little details.

Start with the first error message and work down the list. Often times, an error in one place can trigger another error later in the code. For that reason, don't start by looking at messages in the middle or end of the list: that code might be perfectly fine, and it was an earlier error that caused the message. For example, an error in a line of code declaring a variable will cause the variable to not actually be declared, or declared incorrectly. Any later line of code that references that variable might have an error message because of that, but there isn't anything to fix on that line, and that error will go away once the original error is fixed.

I start at the beginning of the error message list and work down. If a later message looks like it may have been caused by a previous error, I skip it for now (like if it says a variable is undefined, and I just fixed the declaration statement a moment ago.) If a later message just doesn't make sense, and I fixed a previous problem, I stop looking at messages and recompile (verify in Arduino IDE parlance) to get a new list of messages. By fixing the earlier problem, it often eliminates later messages, or changes them into something more meaningful.

Fixing compilation errors, and debugging code is as much an art as it is a science. It's a learned skill that's difficult to learn, but gets better with experience.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 16, 2016, 03:46 pm
I'm now back at my computer (was using a tablet for the post above) and I tried compiling your sketch.

At first, I only got one error: "fatal error: process.h: No such file or directory" and pointing at the first line where process.h is being included. I changed "process" to "Process" and it compiles past that point. Very curious: I'm on Windows and I thought file names were not case sensitive? But here, it seems to make a difference (looking at the examples, they all seem to use Process with a capital "P" when including the file.)

Fixing that, and recompiling, there are more errors:


This last error is a common one. This is the code in question:
Code: [Select]

 while (p.available()>0)
   char c = p.read();
   Serial.print(c);
 Serial.flush();

You have it indented where it appears that you want both the read() and print() statements to be part of the while loop. That would make sense. In some languages, like Python, doing that indenting is enough to make the indented statements part of the while loop. But C++ (which is what the Arduino is using) totally ignores indenting - doing the indenting is just to make it more readable for you. This is a very common error. As far as the compiler is concerned, what it's really doing is this: (using the Auto Format command from the Tools menu will reformat it to look like this, making the problem more obvious)
Code: [Select]

 while (p.available()>0)
   char c = p.read();
 Serial.print(c);
 Serial.flush();

Here, it's clear that only the read() statement is in the loop. It will read all characters, and essentially throw them away. You reach the print() statement only when the loop finishes, and at that point the variable c (as well as all of the characters that have been read) are gone and no longer accessible. The fix is to add the curly braces:
Code: [Select]

 while (p.available()>0)
 {
   char c = p.read();
   Serial.print(c);
 }
 Serial.flush();

This code will work as you expect. The variable declaration, and the read() and print() calls are all inside of the while loop, and it will repeatedly read and print character as long as they are available.

Another common error is to add a semicolon when one isn't needed. Many people automatically add a semicolon at the end of each line, thinking it is a line terminator. In reality, the semicolon is a statement separator. Consider this code, which is the same as the code above with the addition of a semicolon after the while statement:
Code: [Select]

 while (p.available()>0);
 {
   char c = p.read();
   Serial.print(c);
 }
 Serial.flush();

In this case, that added semicolon causes lots of problems. It is actually putting an empty NULL statement after the while statement, separating that NULL statement from the block of statements in the curly braces. What this is doing is telling the while statement to do nothing as long as characters are available. Once characters are no longer available, it will enter the block of statements denoted by the curly braces. It will create a variable c, read a single character into it, print the character, and then end the block. That will destroy the variable c, making it no longer accessible. That block will only be executed once, because it is no longer associated with the while statement - that semicolon has told the while statement to do nothing. In this case, the problems isn't really that the block statement will only be executed once. In reality, it will never be executed: since nothing is done inside the while statement to consume characters from p, the loop will never end. That infinite loop will run forever, and the sketch will never get past that line.

So that gives an example of type of things you need to look for when trying to get a sketch to compile. It's the details that count - every little character could be important. When you see an error message, you've got to look at the smallest of details to see what might be wrong. In the discussion above, I show three errors: a missing backslash which needs to be added, an error on the next line that isn't really an error and which will be fixed by fixing the first error, and some missing curly braces to make the loop do what you want it to do. Fix those errors, and then try to verify the sketch again. You could very well end up with a new set of errors. This can be an iterative process where you have to go through several cycles of fixing errors and verifying again.

Once you have the sketch successfully verified, then the hard work begins: debugging the code and getting it to do what you want it to do!
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 16, 2016, 04:41 pm
Thanks to both of you - ShapeShifter and DarkSabre- very informative and educational.

For starters, I'm curious if this language (C++ ?) cares about blank lines and/or indents- sounds like it doesn't, but it does seem to care about capitalization, right?  Odd because there are some strange ways of capitalizing certain words and not others...

Secondly, more related to what I'm trying to do here- I'm not seeing that it is going to runCurl nor runCpuInfo unless and until I tell it to from within the void loop() section, is that right?

Basically I'm trying to merge two fairly simple tasks, and I know that the serial monitoring bit isn't really appropriate for the finished product, but would love to sort of monitor what the MCU is doing while I tweak this code...maybe that's sort of foolish, not sure.

Anyway- I'm trying to merge the standard 'button' sketch on an if/then loop to where it can monitor a button (my doorbell) and then, if it is pressed, shoot pushingbox my device id # which will notify my cell phone.  I've perfected the button side of things, so now I'm trying to figure out how to get this Dragino Yun in combo with a Leonardo Arduino board to send the curl call (?) to pushingbox's api.  I've (obviously) not quite got the curl section figured out yet....but it's been an interesting journey getting there...

BTW- in case it matters to anyone else reading this now or in the future...the device id listed herein is not accurate on purpose.  The id has been changed to protect the innocent!  Thx
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 16, 2016, 05:21 pm
For starters, I'm curious if this language (C++ ?) cares about blank lines and/or indents- sounds like it doesn't, but it does seem to care about capitalization, right?  Odd because there are some strange ways of capitalizing certain words and not others...
Whitespace is generically defined as characters that don't make a printed mark on the page: this includes spaces, tabs, carriage return, new line, etc. C++ ignores all whitespace (or more properly, takes all sequential whitepsace and considers it a single separator. Some places you need a separator (like between a type name and variable name in a variable declaration) and some times it's optional (like between a number and an operator.) Anywhere whitespace is required or optional, you can use any combination of whitespace" a single space works the same as 10 spaces, which works the same as a combination of spaces and tabs, as well as any combination of spaces, tabs, carriage returns, etc. All of the following are identical as far as the compiler is concerned:
Code: [Select]
char c;
Code: [Select]
    char c;
Code: [Select]
char      c;
Code: [Select]
char c     ;
Code: [Select]
char
            c
    ;

Code: [Select]
char
c
;

Code: [Select]
char





c





                           ;


Names are case sensitive - this goes for function names, class names, type names, variable names, etc. The following code defines three different variables, all of which are valid and unique variables, even if some languages that are not case sensitive (like BASIC or Python) would treat them as all the same variable (and complain about duplicate definitions:
Code: [Select]
byte variable;
byte Variable;
byte vAriAblE;


Arduino sketches are based on C++. There's a little pre-processing performed by the IDE to make it a little simpler, but the language that you are writing is regular C++. It sounds like you should read up on some C++ programming tutorials - while there are some slight differences in the way you set up a C++ program vs a sketch (like the sketch sets up the main() entry point for you) most of what you read about C++ will turn out to be very valuable.

If you want the definitive word on C++, that would be the  book The C++ Programming Language, by Bjarne Stroustrup (https://www.amazon.com/C-Programming-Language-4th/dp/0321563840/). He's the creator of the language, and that book is his official description of the language. It's not necessarily beginner material, however, so you are probably better off looking for an online introductory tutorial. But if you are serious about programming, you may eventually want it.

Quote
Secondly, more related to what I'm trying to do here- I'm not seeing that it is going to runCurl nor runCpuInfo unless and until I tell it to from within the void loop() section, is that right?
It's valid to put them in setup(), and they will get called. But they will only be called once. So that's valid for a one-time setup type of operation, but not particularly useful for what you are planning on doing.

Instead, you will want the code to be in your loop() function, which gets called over and over again, forever. That code will look for a button press, and if it finds one, it will call runCurl(). You say you have the button press code already working - are you debouncing the button press? When you press the button, it can actually make several make/break actions for a few milliseconds, both when you press the button and when you release it. The Arduino is fast enough to be able to detect these multiple activations. If you are just using it to light an LED, it doesn't matter, because while it will cause the LED to flicker when the button is being pressed or released, your eye won't see it. But your code will probably see it, and may call runCurl() multiple times for a single button press, causing multiple notifications to be sent. If you haven't already, it's worth doing some research on button debouncing.

To really perfect the button code, you want it to the point where you can print out a message on the serial port saying that the button is pressed, and that message is only ever printed once per press, regardless of how hard or soft you mash the button, and how long you hold it. When you have it to the point where it only prints once per press, then you want to add the call to runCurl() where you are printing out that message.

Quote
Basically I'm trying to merge two fairly simple tasks, and I know that the serial monitoring bit isn't really appropriate for the finished product, but would love to sort of monitor what the MCU is doing while I tweak this code...maybe that's sort of foolish, not sure.
No, not at all foolish! That serial output will be invaluable while debugging the code. It's very common to add a lot of Serial print statements when developing code, so that you can see what's going on, and hopefully figure out how to fix it when it doesn't do what you want.

The part that will be particularly helpful is your while available() loop where you echo any output from the Process object to the serial port. If the curl command generates any kind of error message, that will let you see what it is. If it doesn't work, it will be VERY hard to figure out why if you can't see the error messages.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 16, 2016, 06:56 pm
Well...for some reason, it's not sending the device id to the website.  I don't know curl well at all, so I'm curious if you think the http (i.e. the website) call should be prior to calling out the "-d" and the device id # ?

Oddly, I'm not seeing anything in the monitor either, except that it has 'connected' !?

I've sent the runCurl() code to pushingbox to see if they have any thoughts as to why it's not working.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 16, 2016, 07:29 pm
I'm not an expert on curl.

When I'm experimenting with something like that, I will first SSH into the Yun, and try the commands out on the command line. That often allows more visibility to any error messages and status. It also allows tring a variety of options much faster than it could be done by updating/loading/running a sketch each time. If it doesn't work by typing the command directly on the Linux command line, there is virtually no chance of getting it to work with a Process object in a sketch.

Two options to consider using while testing, which may give you more information about why it's not working:
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 16, 2016, 08:06 pm
That's a good idea- you mean from a bash prompt on root?  When I open a terminal and SSH into the Yun, I login as root and get to a prompt...do you then just start typing each line of that runCurl code?

How do you use the v and/or trace?  Do you mean, try putting them in the sketch somehow?

Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 16, 2016, 08:20 pm
I login as root and get to a prompt...do you then just start typing each line of that runCurl code?
Yes, that's exactly what I mean. But just to be accurate (in case you go searching the web for help) it's not a bash prompt, it's ash. It's mostly the same, but there are some differences that could catch you.

Quote
How do you use the v and/or trace?  Do you mean, try putting them in the sketch somehow?
I would use them when you're experimenting on the command line. Just add the options to whatever command you're testing. For example, your basic curl command is:
Code: [Select]
curl -d "devid=v0123456789ABCDE" http://api.pushingbox.com/pushingbox

To add the verbose command:
Code: [Select]
curl -v -d "devid=v0123456789ABCDE" http://api.pushingbox.com/pushingbox

To add the trace command:
Code: [Select]
curl --trace -d "devid=v0123456789ABCDE" http://api.pushingbox.com/pushingbox

Each of those are the command as you would type it on the command line.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 16, 2016, 09:56 pm
Thanks- before reading your last comment, I tried that line (without the -v or --trace) and am getting a bigger problem...that is, I'm getting: 

curl: (6) Couldn't resolve the host 'api.pushingbox.com'

So on a hunch, I went into my admin controls on the Yun and looked at my wifi setup, thinking that since I had locked down the IP address on my LAN for this Arduino, I may have failed to properly set the DNS address.  I had indeed left it blank thinking that it would then default to the router's DNS settings, so I added the router's address in that blank (aka the gateway address).

Retried it via command line and I'd love to tell you that it worked, but alas, same problem.... ugh!

Any thoughts?
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 16, 2016, 10:56 pm
Any thoughts?
To get you past this current stumbling block, I would try substituting the IP address 213.186.33.19 in place of api.pushingbox.com. (I got that address just now by running "nslookup api.pushingbox.com" on my computer.) You're better off using the name rather than the numeric address, in case the address ever changes in the future.

In the long run, you will have to sort out your DNS issues, but this should work as temporary work-around.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 16, 2016, 11:55 pm
Thanks- tried that too- now I'm getting lots of fun gobbledy gook!  I think I need to have a chat with the pushingbox folks....
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 17, 2016, 12:11 am
I think I need to have a chat with the pushingbox folks....
Good plan. This is the place for support to figure out how to call curl on the Yun. But they are the place for help on what parameters to use when calling curl.

Once you work with them and figure out a curl command that works when typed on the Yun's command line, we can make it work in the sketch.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 17, 2016, 03:55 pm
Breakthrough! 

Ok- firstly, I have sent a note to the French Pushingbox folks, but they must be on vacation or something because no response yet.

But- then I traded a few emails with a very helpful technician with Dragino Yun over in China and it seems I had two network problems.  First, I mistakenly thought the 'broadcast' address was the address that would be broadcast out over the LAN (similar to broadcast ssid) but that is obviously wrong, so we got that set correctly.  Second, which was probably the real issue, the Admin GUI imbedded in the Yun where I set my fixed IP address on the LAN has a field entitled, "Use Custom DNS Server(s): __________" and I had left that blank because my experience with other networked devices has been that, in the absence of a customized dns setting, the device will look to the router's dns settings.  This goes back to the language / cultural misunderstandings that I discussed earlier.  Anyway, I set the 'custom' dns to my router's address and boom, it works great!

I executed the curl command from an ash prompt and pow- straight through successfully!

Now that I have it communicating properly with the outside world, I need to go back and experiment with my various iterations of this sketch...
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 17, 2016, 04:15 pm
Congratulations! Great work chewing your way through the issues.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 17, 2016, 04:29 pm
This issue / misunderstanding is causing a lot of people to have the same / similar troubles.  If you google Arduino couldn't resolve... many of them pop up- most old, but I'm wondering if there's not a way to somehow post this as a common misunderstanding with the Yun's GUI...
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 17, 2016, 05:39 pm
So I'm now trying to make this as simple as possible....

As we have confirmed- when I SSH into the Dragino Yun on top of an Arduino Leonardo board, and type the following at a command prompt:

curl -d "devid=vBECBF1F4A7D765" http://api.pushingbox.com/pushingbox

...it works like a champ.

Now, attempting to merge that curl command with a fairly typical (and working) button check, I have the following sketch:

// set pin number:
const int buttonPin = 3;     // the number of the doorbell input
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

#include <Bridge.h>
#include <Process.h>

void setup() {
  // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);
 Bridge.begin();
}

void loop() {
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);

 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 if (buttonState == HIGH) {
   // turn LED on:
   digitalWrite(ledPin, HIGH);
   runCurl();
 } else {
   // turn LED off:
   digitalWrite(ledPin, LOW);
 }
}

void runCurl() {
Process p;            
p.begin("curl");
p.addParameter("-d");
p.addParameter("\"devid=vBECBF1F4A7D765\"");
p.addParameter("http://api.pushingbox.com/pushingbox");
p.run();
}


And again, it compiles fine, and loads fine, and the button works properly (i.e. the LED on the Arduino lights up when the button is pressed etc), but the call (listed at top) is not effectively executed after the LED lights up.

Do you see anything that would otherwise cause it to not work correctly here?  Thanks
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 17, 2016, 06:25 pm
Try explicitly giving the full path to curl. Inside runCurl(), change the p.begin() call to:

Code: [Select]
p.begin("/usr/bin/curl");

The command shell will normally search for the right executable on the path, but here you are not using a command shell and calling the file directly. It may be that it can't find the executable file.

If you had used p.runShellCommand(), then it would launch a command shell, which would then search for the file, and execute it. Instead, you are using p.run(), which does not launch a command shell. Since we already know the location of the curl command on the system, and don't need the command processor's help, there is no need to add the extra overhead of runShellCommand() - I think run() is the proper choice in this case.

If that doesn't work, put in a loop that echos the output from the command. It's probably returning an error string that might give a good clue to the problem:
Code: [Select]

 while (p.available()>0)
 {
   char c = p.read();
   Serial.print(c);
 }


Of course, you will need to call Serial.begin() in setup().

In looking at your code, you are calling runCurl(), any time that the button is high. Done this way, runCurl() will likely get called multiple times when the button is pressed, and will keep getting called over and over again as long as the button is pressed. It doesn't matter if you turn on the LED thousands of times while the button is pressed, but you probably don't want to be inundated with countless notifications for each press.

Take a look at the Debounce Example (https://www.arduino.cc/en/Tutorial/Debounce) and try to understand what it is doing. You will almost certainly need to do something similar. You will want to call runCurl() where the example code is toggling the LED state.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 17, 2016, 06:42 pm
Thanks

Re-Debounce- I'm sure that is going to be a problem that I'll need to deal with, but I'm trying to stay focused here first to get it actually working, plus that debounce (toggle on/off) was hurting by brain looking at, so for now, I'm putting that on the back burner.  I agree though, once I get this working, I'm going to need a simple, effective way to just shoot one call out every time it is actually pressed.

So, I replaced "curl" with the "/usr/bin/curl" and connected the pin (i.e. pressed the button), and nota.

For the echo - do I put the while statement just below runCurl(); in the loop and yes, call Serial.begin() in setup...is that all? 

Also- why #include <Bridge.h> and <Process.h> at the start- I sort of figured that needed to be there, but I don't really understand what they are doing.

Thank you


Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 17, 2016, 06:54 pm
Also- the first draft yielded the following on Serial.begin();


no matching function for call to 'Serial_::begin()'

I think I'll need to add 9600 baud rate?

Also- p.available needs to be called out ahead someplace, right?  It's getting hung on that too.

Thanks
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 17, 2016, 08:44 pm
I agree though, once I get this working, I'm going to need a simple, effective way to just shoot one call out every time it is actually pressed.
The issue is that you are probably going to be making a bunch of calls, one right after the other, and that could possibly cause issues that would not show up if you just made a single call. As a compromise until you get proper debouncing working, you could add a delay() call immediately after calling runCurl(). For a "production" system, this would not be a good thing, but as a quick proof of concept it should work. If you add a delay(1000); after the runCurl() call, then it will call the function only once if you press the button less than a second, and will call it once a second if you hold the button down. You could make the delay longer if you want to make sure it's only called once for a longer press. But this delay will tend to make the program unresponsive, so you don't want it to be longer than necessary.

Quote
So, I replaced "curl" with the "/usr/bin/curl" and connected the pin (i.e. pressed the button), and nota.
Well, it was worth a try.

Quote
For the echo - do I put the while statement just below runCurl(); in the loop and yes, call Serial.begin() in setup...is that all?  
No, put it inside runCurl() after the p.run() call. If you put it outside of runCurl(), the p object will not be defined. Not only will this cause a compile error, but even if you could call it from there, the p object will be automatically destroyed when runCurl() exits, along with any data you would be trying to read.

Quote
Also- why #include <Bridge.h> and <Process.h> at the start- I sort of figured that needed to be there, but I don't really understand what they are doing.
Those files define the Bridge and Process classes, respectively. It's telling the compiler that the classes exist, and what their interface is - the functions and data that the class provides. This lets the compiler check that you are calling any functions properly, and it tells the compiler how to generate the proper code to call those class functions.

Without those include statements, it won't know about the Bridge object and how to call Bridge.begin(), and it won't know about the Process class and won't know how to create the p object of that class, nor what functions can be called in that class.

I think I'll need to add 9600 baud rate?
Yes, you will need to add some baud rate, and it generally must match what you are using in the Serial Monitor. The exception is when the USB port is managed directly by the AVR chip, like when using a '32U4 processor that is on a Leonardo, Pro Micro, or genuine Yun board - in that case, the baud rate is meaningless and can be anything, but something must still be provided.

Quote
Also- p.available needs to be called out ahead someplace, right?  It's getting hung on that too.
You probably went ahead and put the p.available() loop in your loop() function after calling runCurl(). The compiler does know about the available() function as it applies to the Process class (because you did the #include of Process.h.) What it's no doubt complaining about is that it doesn't know what the "p" object is. Inside the runCurl() function it's defined to be an object of the Process class, but that variable declaration does not extend outside of the runCurl() function. Therefore, the compiler doesn't know what type it is, doesn't know that it's a Process object, and therefore doesn't know that the available() function is available for that object.

The answer is to put that available loop at the end of runCurl(), after the p.run() command, and before the function's closing curly brace.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 17, 2016, 10:45 pm
Well...fits & starts I guess.  I made those various changes (see below), and it compiles fine, but not only does it not make the 'call' to the website, now it doesn't even light up the LED when I 'press the button' ?!

Maybe you can look and see what I've done wrong?  I can't get the serial monitor to connect either, so I have no idea.

I do think that delay idea is a good one temporarily- in fact, I thought it was possible that due to multiple calls, the website wasn't reacting, so I put in a 1200 ms delay and nope.

Anyway- here's what I have currently...

// set pin number:
const int buttonPin = 3;     // the number of the doorbell input
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

#include <Bridge.h>
#include <Process.h>

void setup() {
   // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Bridge.begin(9600);
  Serial.begin(300);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    runCurl();
    delay(1200);
 
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

void runCurl() {
 Process p;           
 p.begin("curl");
 p.addParameter("-d");
 p.addParameter("\"devid=vBECBF1F4A7D765\"");
 p.addParameter("http://api.pushingbox.com/pushingbox");
 p.run();
 while (p.available()>0)
 {
   char c = p.read();
   Serial.print(c);
 }
}
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 17, 2016, 11:09 pm
This doesn't look right:

Code: [Select]

  Bridge.begin(9600);
  Serial.begin(300);



Bridge.begin() is normally called with no arguments. It can be called with a single numeric argument, like you now have it, and that would be the baud rate that the serial port talking to Linux should use. It's there in case you have a special requirement to use a different baud rate. But you can't change it here unilaterally - if you provide a custom baud rate here, you need to go into Linux and change the baud rate there as well. If you change it only in the sketch, then the two processors will not be able to talk to each other. Bridge.begin() will not be able to establish communications with the Linux side, and it will likely never return. This is probably why your LED is no longer working, because Bridge.begin() has not returned while it tries over and over again to establish communications.

Then, you have the baud rate in the Serial.begin() call. 300 baud is a valid speed, but it is very slow. I doubt you really want to use that low of a speed. The only reason to do so is if you have a VERY long serial cable (hundreds of feet?) or you are using a dial-up acoustic coupler modem (has anybody used them in the last 40 years or more? The last time I used 300 baud was in the mid-'70s with an acoustic coupler - where you manually dialed the telephone and placed the phone handset in the rubber cups of the modem. This was when ARPAnet, the precursor to the Internet, only had about a dozen computers connected to it, and I had to dial into Rutgers to get onto it, because my university wasn't connected directly.)

(http://www.coqdiddles.com/wp-content/uploads/2011/03/acoustic-coupler.jpg)

Cool!  I found a picture of the same Anderson Jacobson modems I used in the '70s. 8)
Talk about a trip down memory lane...


Anyway, back to the topic... I think you need to take the parameter out of the Bridge.begin() call, and use a more appropriate value for Serial.begin(), like 9600, 19200, or 115200.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 17, 2016, 11:13 pm
BTW, please use code tags when posting code. It makes it much easier to read, and prevents silly formatting issues that can sometimes happen.

There are two ways:
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 17, 2016, 11:18 pm
OK- yes that's quite a trip!  Interestingly though, when the Arduino IDE's serial monitor pops up, the default rate is 300 baud ?!  So that's why I chose it for the Serial call and as far as the bridge, I thought you had said that I should call out some baud, but it doesn't really matter since I'm using an underlying Leonardo board- but I had seen someplace else online where they recommended 115,200 so that's what I used.  Oh well...let me try nothing for the Bridge begin call and 9600 for the serial call and I'll report back...
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 17, 2016, 11:29 pm
OK- so I changed the Bridge.begin call to () and the Serial.begin call to (9600) and now the led light is indicating a 'pressed button' so that's good, but still no serial communication and definitely no runCurl command execution...ugh!

Not sure exactly what you mean by "code tags" ?  I guess I'm slow, but don't follow that one...
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 18, 2016, 03:04 am
You're right, when using a Leonardo the baud rate makes no difference: either in Serial.begin or in the Serial Monitor. There are a couple recent Yun Shield discussions going: one using a Leonardo, and one using a Mega2560 - and I couldn't immediately remember which one you were using.

Interestingly though, when the Arduino IDE's serial monitor pops up, the default rate is 300 baud ?!
I'm guessing you changed it at some point in the past and forgot about it. When the Serial Monitor opens, it remembers the settings you had used last time.

One thing to be aware of with the Leonardo is that the USB serial port is created by the sketch software itself. Every time you load new code, or reset the Leonardo, the serial port goes away briefly, and then appears again. I found that I generally have to close and reopen the serial terminal emulator to see any output every time that the sketch restarts. That may be what is happening here: try starting the sketch, and THEN open the Serial Monitor and see if that makes any difference. That's why so many example sketches have this:

Code: [Select]
while (!Serial)
   ;


When converted to a boolean, Serial will initially be false until a USB serial connection is made. Since the while loop checking this does nothing (has just an empty semicolon) the sketch will wait until a connection is made. This is because on these systems, the connection cannot be established until after the sketch starts and Serial.begin() has been called. This construct is used when setup() wants to print out some initial information that you want to capture on the Serial Monitor. By waiting for a connection, those initial messages aren't printed out before there is a chance to make a connection.

That's not really a concern in your case, since there are no other messages printed at startup. The only thing you're looking for is any messages printed when you press the button.

Not sure exactly what you mean by "code tags" ?  I guess I'm slow, but don't follow that one...
Read this... (http://forum.arduino.cc/index.php/topic,148850.msg1118324.html#post_codetags)
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 18, 2016, 12:55 pm
Very educational and helpful- thanks!  I'll try to improve my posts going forward   :smiley-grin: .

One thought I had overnight was that, the *only* time I've actually seen the serial monitor work properly has been when I have run the ConsoleRead example sketch.

Again, since I'm going over wifi, I wonder if it wouldn't make sense to replace all the serial monitor stuff with console read stuff and give that a try.  If it works, I'd be able to actually see what the mcu is doing and/or if there are any errors when it attempts to run this curl command.

I can go back and look at that ConsoleRead sketch and see if I can figure out how to replace the serial monitor coding with console coding, but if you have any thoughts on this either way and/or if you have any advice as to console coding requirements, please let me know.  Thanks.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 18, 2016, 01:30 pm
Again, since I'm going over wifi, I wonder if it wouldn't make sense to replace all the serial monitor stuff with console read stuff and give that a try.
I don't think you mentioned that you were trying to use the Serial Monitor over WiFi, or if you did, I missed it. Yes, if you are using a strictly WiFi connection, you MUST use the Console class. The Serial class works ONLY over the USB serial connection. The Console class works ONLY over a network connection (WiFi or Ethernet.) The fact that we were talking about the Serial class made me assume we were talking about USB.

Quote
...see if I can figure out how to replace the serial monitor coding with console coding,
Serial and Console both derive from the same Stream class, so they have the same interface and work much the same way. Switching your sketch over is easy: first, you need to add #include <Console.h> at the beginning of your sketch. Then, change your Serial.begin() statement to Console.begin() with no arguments, and make sure it is s called after Bridge.begin() is called. Finally, change every occurrence of Serial in your sketch to Console. All of the print(), println() and other functions are the same and will not need any changes.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 18, 2016, 03:23 pm
I figured!  I had this initial hunch that I needed to be working over Console due to wifi, but what had been throwing me off is that, on occasion, I'd see 'connected' in the serial monitor- but then it wouldn't actually communicate, so I knew something was wrong.

Anyway- I'll be working on a conversion to console for a bit and report back what I'm seeing/hearing from the mcu.  I'm really interested to learn why this runCurl loop isn't working.

I assume you don't need to call out any baud rate on either the bridge or the console, right?

Does it matter the order in which you list the 'include' libraries (?) - I'm guessing not.

Finally...since I've run across 'boolean' I'm curious if you can shed light on what that does and/or what the proper use is for it?  I've certainly heard of Boolean search nomenclature et al but haven't run across it in programming code until recently.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 18, 2016, 03:54 pm
I assume you don't need to call out any baud rate on either the bridge or the console, right?
Correct.

Bridge.begin() can accept an optional baud rate, because it is actually a serial port connection to the Linux processor. They give you the option of changing that baud rate if you have a special need.

Console.begin() expects no parameters. There is no concept of a baud rate for a network connection. While a network connection does have a physical data signalling speed, that is a function of the physical hardware interface and not something that can be changed in software. (Or at least not by the Console class.)

Quote
Does it matter the order in which you list the 'include' libraries (?) - I'm guessing not.
Sometimes it matters, usually it doesn't. There may be a case where an include file needs to use information from another include file, and therefore must be included after the other one. But usually, that include file will just include the specific file it needs, eliminating the need to include them in a specific order. (Wow, talk about some convoluted logic, any chance you can follow that?)

In the case of these include files, it doesn't really matter. I like to include Bridge.h first, as it is the main include file of the Bridge library, and everything else depends on it. However, because everything else depends on it, it is automatically included by the other include files. So, if you happen to include Console.h first, it will include Bridge.h internally to it. Then, when you get to the point where you are including it in your sketch, the compiler will realize that it's already been included and not include it a second time.

But the short answer is no, it usually doesn't matter. In general, I tend to include files in alphabetical order: sometimes there is a long list of includes in a source file, and having them in order makes it easier to find a file in the list when reading the code.

Quote
Finally...since I've run across 'boolean' I'm curious if you can shed light on what that does and/or what the proper use is for it?
A boolean is usually another name for an unsigned char (byte.)

It represents a logical value: either true or false. The results of the comparison operators (==, >, <, <=, >=, etc) is a logical value. There are logical operators that take logical values and return logical results like and (&) or (|) exclusive-or (^) and not (!). There are statements that expect logical expressions, like if and while.

C++ uses the numeric value 0 to represent false, and any non-zero numeric value is considered true. The result of a comparison or logical operator is usually 1 when it wants to signal true.

A logical value can be stored in just about any integral numeric variable type, although a single byte value is usually used for efficiency. "boolean" is just another form of unsigned character value (just like "byte") that can be used to store any 8-bit value, but by using the type "boolean" you are giving the reader a hint that you intend to use it for true/false values. (Although the compiler really doesn't care, it treats "byte", "boolean", and "unsigned char" as the same thing: an unsigned 8-bit value.)
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 18, 2016, 04:25 pm
Thanks for that.

OK- so I've converted my little sketch to console and the first time around, it showed 'connected' in the serial monitor, but now I'm not often even seeing that.

I'm now wondering if I have my network setup correctly for this console communicating.  Because it worked this way for the consoleread example sketch, I'm again thinking that it is fine, but now maybe not. 

I read in the consoleread sketch intro that you either telnet to the board or port forward the board and open a serial monitor to connect and see print and other feedback.  I port forwarded the board's ip on my lan to 6571 and again, since it worked, I have left it that way, but what is odd to me is that port forwarding is really for outside the lan access to a device on the lan, so this doesn't really make a lot of sense to me because I'm trying to monitor this board's mcu from within my lan.  Is there a better way to set this up and/or to open or connect to the console?

Here's my code currently:

Code: [Select]

// set pin number:
const int buttonPin = 3;     // the number of the doorbell input
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

#include <Bridge.h>
#include <Process.h>
#include <Console.h>

void setup() {
   // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Bridge.begin();
  Console.begin();
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    runCurl();
    delay(750);
 
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

void runCurl() {
 Process p;           
 p.begin("/usr/bin/curl");
 p.addParameter("-d");
 p.addParameter("\"devid=vBECBF1F4A7D765\"");
 p.addParameter("http://api.pushingbox.com/pushingbox");
 p.run();
 while (p.available()>0)
 {
   char c = p.read();
   Console.print(c);
 }
}


The led light and button pressing etc all works fine, including the delay, but no serial monitor communication and definitely no success with the runCurl loop.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 18, 2016, 04:43 pm
I've not used the Console much, I usually use the USB port for debug messages during development, and then I don't use any type of serial stream communications once the project is finished.

The few times I used the Console class, I did nothing special with a network configuration: I just selected the Yun's network address in the Port menu, and opened the Serial Monitor. I've read where you have to do some extra effort to use a standard Telnet client to connect with the Console class, but that shouldn't be necessary when using the Arduino IDE's Serial Monitor window. Of course, my experience is with an official Arduino Yun board - there are some subtle and no so subtle differences introduced by Dragino in their Yun Shield, and this may be different for you.

I agree, port forwarding should only be necessary for connections coming in from the WAN (Internet) to your LAN (local network), it should not be necessary for a LAN to LAN connection. Assuming your Yun Shield and your computer are connected to the same logical network, your router should not be involved in any communications between your Yun Shield and computer, it's only involvement is to assign IP addresses to them.

Now, if your Yun Shield is connected by WiFi, and your computer is on wired Ethernet, and those are two logically distinct networks that must pass through your router, then you may have to set up some special routing rules. But that would be an unusual setup, and is not the way that most home networks are configured.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 20, 2016, 11:32 pm
So finally got it working properly- interestingly, here is where I found the problem- this line:


 p.addParameter("\"devid=vBECBF1F4A7D765\"");


The curl command I have been trying to execute is:

curl -d "devid=vBECBF1F4A7D765" http://api.pushingbox.com/pushingbox/

and my runCurl(); loop had previously been:

Code: [Select]
void runCurl() {

 Process p;           
 p.begin("curl");
 p.addParameter("-d");
 p.addParameter("\"devid=vBECBF1F4A7D765\"");
 p.addParameter("http://api.pushingbox.com/pushingbox/");
 p.run();
 while (p.available()>0)
 {
   char c = p.read();
   Console.print(c);
 }
}


...which wasn't working.  After many, many experiments, I finally figured out that all I needed to do was literally have the devid in quotes.

Now it works beautifully...

Here is the final version which works...

Code: [Select]
void runCurl() {

 Process p;           
 p.begin("curl");
 p.addParameter("-d");
 p.addParameter("devid=vBECBF1F4A7D765");
 p.addParameter("http://api.pushingbox.com/pushingbox/");
 p.run();
 while (p.available()>0)
 {
   char c = p.read();
   Console.print(c);
 }
}
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 20, 2016, 11:39 pm
Now it works beautifully...
CONGRATULATIONS!  It's been a long hard battle...

Interesting that it doesn't actually need the double quote character in the parameter string. I guess it had problems passing it through properly? So, I suppose the curl command would also work from the SSH command line without the quotes around the devid string? I wonder why they suggested using them in the first place?
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 21, 2016, 01:44 am
Thanks.  I'm guessing that the pushingbox website may be a bit overrated, but I'm grateful for the service they provide because it is a very inexpensive way to push notification out to me through this Arduino.
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: stgeorge on Aug 21, 2016, 10:30 pm
Anyone have experience working with Console ascii table?

It's so strange- my little runCurl(); loop is working well, but as soon as I add the console to it to try to see the output from the MCU, I can't get any activity to show up in the monitor.

I can see that it's communicating with my Arduino because firstly, I see that the read/transmit lights are lit up and secondly, the base program doesn't start up until I open the Console (aka Serial Monitor), and then it works correctly, but nothing pops up in the console monitor. 

Here is the current language for my loop:

Code: [Select]
void runCurl() {

 Process p;           
 p.begin("curl");
 p.addParameter("-d");
 p.addParameter("devid=vBECBF1F4A7D765");
 p.addParameter("http://api.pushingbox.com/pushingbox/");
 p.run();
 while (p.available()>0)
 {
   char c = p.read();
   Console.print(c);
 }
}


And yes, I've properly included the console, and started the bridge and console ahead of my while statement- all just like I do in either the Cosole Read example sketch or the Console ASCII Table print, both of which work and show up in the console correctly, but this process doesn't show anything.  Is it possible that there's nothing showing up that it can print out?
Title: Re: Arduino-Yun Wifi - Internet Commands?
Post by: ShapeShifter on Aug 21, 2016, 11:05 pm
Is it possible that there's nothing showing up that it can print out?
I suppose it's possible. It's also possible p.run() isn't returning?

Easy way to tell is the add some additional messages, just for testing:

Code: [Select]
void runCurl() {

 Process p;          

 Console.println("Calling curl");

 p.begin("curl");
 p.addParameter("-d");
 p.addParameter("devid=vBECBF1F4A7D765");
 p.addParameter("http://api.pushingbox.com/pushingbox/");
 p.run();

 Console.println("Start of output:");

 while (p.available()>0)
 {
   char c = p.read();
   Console.print(c);
 }

 Console.println("End of output");
}


This way, you should at least see something...