Go Down

Topic: Arduino-Yun Wifi - Internet Commands? (Read 7328 times) previous topic - next topic

stgeorge

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...

ShapeShifter

Take a look at the Bridge Library Process Tutorial.

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.

stgeorge

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

ShapeShifter

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.

stgeorge

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....

DarkSabre

...
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.

ShapeShifter

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, 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:
  • Serial Monitor: a serial terminal emulator window that is part of the Arduino IDE which allows you to connect to a serial port and send commands out that port and see the responses from that port.
  • Serial Port: a physical serial communications port that can send and receive data. In this context, they are typically USB ports: one on the Arduino, and one on the host computer, which are connected by a cable. You use the Serial class in your sketch to control the Arduino's serial port, and you select the corresponding host computer serial port for use with the IDE's Monitor window.
  • Console Port: a networked version of a serial port. In the sketch, you use the Bridge library's Console class to communicate over this port. In the IDE, if you have a network address selected in the Port menu, the Serial Monitor will communicate over the network with the Console class in the sketch. It works much like the serial port, but over the network and without a USB connection.

stgeorge

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.

ShapeShifter

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.

stgeorge

Thanks again.

Where do you typically source yours?  I typically troll Amazon and/or Ebay.

ShapeShifter

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.

stgeorge

#11
Aug 16, 2016, 02:07 am Last Edit: Aug 16, 2016, 02:24 am by stgeorge
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();
}

stgeorge

Question:  When an Arduino sketch fails to compile....how does one trace back to the problem?  i'm not seeing it!

DarkSabre

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.

ShapeShifter

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.

Go Up