possible environment bug w/ arduino IDE 1.8.5

Hello,

I'm seeing some surprising behaviour from the IDE when compiling for my Yun. The sketch I'm trying to run is from the hackair project, but the problem seems likely to be with the IDE itself.

The sketch I'm trying is this one (irrelevant stuff stripped out):

/**
 * @file Serial Sensor example
 */

#include "hackair.h"

// Specify your sensor
hackAIR sensor(SENSOR_SDS011);

void setup() {
  // Initialize the sensor
  sensor.begin();

  // Open serial port
  Serial.begin(9600);
}

void loop() {
  pass

  // Wait a bit so the data is readable
  delay(2500);
}

The error is:

Error compiling for board Arduino Yún.
/home/lumbar/Arduino/libraries/hackAir-Arduino/src/hackair_ethernet.cpp:25:10: fatal error: SPI.h: No such file or directory
 #include <SPI.h>
          ^~~~~~~

But if I add this include line to the top of the sketch, I get a new error:

/home/lumbar/Arduino/libraries/hackAir-Arduino/src/hackair_ethernet.cpp:26:10: fatal error: Ethernet.h: No such file or directory
 #include <Ethernet.h>
          ^~~~~~~~~~~~

And if I add /that/ include line, the compilation finishes with errors and the sketch gets uploaded but doesn't work properly.

In file included from /usr/share/arduino/libraries/Ethernet/src/Dns.cpp:7:0:
/usr/share/arduino/libraries/Ethernet/src/Dns.cpp: In member function 'uint16_t DNSClient::BuildRequest(const char*)':
/usr/share/arduino/libraries/Ethernet/src/utility/util.h:4:24: warning: result of '(256 << 8)' requires 18 bits to represent, but 'int' only has 16 bits [-Wshift-overflow=]
 #define htons(x) ( ((x)<< 8 & 0xFF00) | \
                     ~~~^~~~
/usr/share/arduino/libraries/Ethernet/src/Dns.cpp:183:21: note: in expansion of macro 'htons'
     twoByteBuffer = htons(QUERY_FLAG | OPCODE_STANDARD_QUERY | RECURSION_DESIRED_FLAG);
                     ^~~~~

What's nice is that if I then delete the extra include lines, leaving the sketch as it was originally, then everything works perfectly: the sketch compiles without errors, is uploaded, and works as expected.

So it looks like something's blown in the environment, though I could easily be wrong about that. Any ideas?

(I'm running the arduino packaged for fedora 27.)

Error compiling for board Arduino Yún.
/home/lumbar/Arduino/libraries/hackAir-Arduino/src/hackair_ethernet.cpp:25:10: fatal error: SPI.h: No such file or directory
 #include <SPI.h>
          ^~~~~~~

You need this library to correct the error.

/home/lumbar/Arduino/libraries/hackAir-Arduino/src/hackair_ethernet.cpp:26:10: fatal error: Ethernet.h: No such file or directory
 #include <Ethernet.h>
          ^~~~~~~~~~~~

You do not need the ethernet.h library since you are not using the RJ-45 style connection.

In file included from /usr/share/arduino/libraries/Ethernet/src/Dns.cpp:7:0:
/usr/share/arduino/libraries/Ethernet/src/Dns.cpp: In member function 'uint16_t DNSClient::BuildRequest(const char*)':
/usr/share/arduino/libraries/Ethernet/src/utility/util.h:4:24: warning: result of '(256 << 8)' requires 18 bits to represent, but 'int' only has 16 bits [-Wshift-overflow=]
 #define htons(x) ( ((x)<< 8 & 0xFF00) | \
                     ~~~^~~~
/usr/share/arduino/libraries/Ethernet/src/Dns.cpp:183:21: note: in expansion of macro 'htons'
     twoByteBuffer = htons(QUERY_FLAG | OPCODE_STANDARD_QUERY | RECURSION_DESIRED_FLAG);
                     ^~~~~

You'll need to show you entire code; it seems you're trying to cram 18 bits into something that only holds 16 bits.

Thanks for replying. I'm not sure I was clear enough: these errors arise when I try and compile a script including hackair.h, it reports at first that it can't find these core libraries (SPI.h, ethernet.h); but I /can/ manually include these libraries in the script (they're not missing from the system).

Once I've done this the sketch looks like this:

/**
 * @file Serial Sensor example
 */

#include <SPI.h>
#include <Ethernet.h>
#include "hackair.h"

// Specify your sensor
hackAIR sensor(SENSOR_SDS011);

...

Now, the code successfully compiles, with errors, but doesn't work properly. But once I've done this, the original sketch (without the extra includes) compiles successfully.

If I need to be more verbose I can be - would that be helpful? I can also try and do some diagnostic work myself - but I'm new to this ecosystem and these languages so I thought I'd ask here first.

Cheers

I can see why now. By reading the auxiliary files, one can read it is calling for the SPI.h and Ethernet.h libraries.

What is your final project? Perhaps one could find a better library suited for you outcome.

JMeller:
I can see why now. By reading the auxiliary files, one can read it is calling for the SPI.h and Ethernet.h libraries.

Yes, that's part of the point. The other part is that those includes are not working consistently.

What is your final project? Perhaps one could find a better library suited for you outcome.

It's unlikely: this library does 90% of what I want to do (runs this specific air quality sensor and parses the output). The only thing I want to change is to get it talking to the linux chip on the Yun so I can post-process the data in python.

Not all libraries are compatible with all hardware - which appears to be the case in this instance. This library is looking for either an ethernet shield or a wifi shield; both of which the Yun is not using. Your only options are to cherry pic the relevant pieces of the library and incorporate them into your sketch OR purchase the hardware necessary to make the sketch work.

[EDIT]
I thought the Yun was only Wifi capable; it does have an ethernet jack.

Where are you libraries located on your hard drive?

That makes sense, and I'm happy to do that. But do you have any idea why the weird cycle I described above (include in the main sketch the libraries described as missing when the compile fails, compile, then delete the inclusions and compile again) seems to produce a working binary?

It has to do with the location of your libraries. Are they in the "C:\arduino-1.8.5\libraries" folder? You may want to toy with the placement; eventually, the sketch will be happy.

lumbar:
So it looks like something's blown in the environment, though I could easily be wrong about that. Any ideas?

(I'm running the arduino packaged for fedora 27.)

Arduino IDE 1.8.5 is significantly outdated and from your statement "the arduino packaged for fedora 27", I suspect you may be using an unofficial modified version of the IDE. Install the official release of Arduino IDE 1.8.7, downloaded from:
http://www.arduino.cc/en/Main/Software
then let me know if the problem still occurs.

JMeller:
You need this library to correct the error.

No. Arduino AVR Boards comes with its specialized version of the SPI library bundled:

The one you linked to is for the Teensy hardware package. If you install that library separately it's likely to cause a lot of problems later.

Thanks both. I scrapped the packaged version and installed 1.8.7 from the website. No more problems.

Should have thought of that before posting here. Forgive the absentmindedness.

I'm glad to hear it's working. That was definitely not the correct behavior. It hasn't been necessary to add #include directives to your sketch for dependencies of libraries since Arduino IDE 1.6.5.

Enjoy!
Per

Thanks for the info and correction, pert. I continue to use 1.8.5; when I cross spurious errors, I'll know it's time to upgrade.