exit status 1 expected primary-expression before '.' token - c++

I'm trying to get the temperature and humidity every 60 seconds, but my code won't work. Whenever I compile it, there's this error "expected primary-expression before '.' token"
This line gets highlighted.
Serial.print(DHT.humidity,0);//prints humidity in serial
Here's my entire code:
#include <DHT_U.h>
#define DHT12PIN 7// sets pin 7 for DHT11 signal connection
void setup(){
Serial.begin(9600);//opens serial
}
void loop()
{
int chk = DHT.read12(DHT12PIN);//reads DHT12
Serial.print(DHT.temperature,0);//prints temp in serial
Serial.print(",");//pints comma in serial
Serial.print(DHT.humidity,0);//prints humidity in serial
Serial.println(); //carraiage return
delay(2000);//wait 2 seconsds
}

Wow there was a lot to unpack in this question.
I have used the DHT library by Adafruit a lot of times and thought that you just ended up using the class name instead of instantiating a variable and that's why you were facing the issue.
But then I realized that you are using the following function which is not a part of the Adafruit library:
int chk = dht.read12(DHT12PIN);//reads DHT12
So, I did a bit of digging on the internet and realized that someone has made a library for DHT with that specific function. (A lot of libraries to be specific)
Based on my analysis of this library, and the example code that was given, you have 3 issues.
You have included the wrong header file. The header file dht.h from RobTillaart's library should replace the header file DHT_U.h of Adafruit's library, in your code.
You have to instantiate the variable named DHT of the dht class. This can be done as follows, above your setup function
dht DHT;
Given that you had the code and the header files mixed up from TWO DIFFERENT LIBRARIES, I am guessing that you have installed the Adafruit library, in the place of RobTillaart's library. To fix this, you will have to remove the DHT-sensor-library-1.3.4 from your Documents/Arduino/libraries, create a new folder named DHTStable in it's place, and place all of the files listed here in the new folder.
Some words of experience-based wisdom, don't use RobTillaart's library, I can tell from a glance of it's folder structure that you will face more problems then you can solve if you use it. Instead use Adafruit's library, with their example. You will also have to install Adafruit's sensor library, but it is definitely worth it.

Related

how to resolve header order issue

I have the following problem:
My code relies on two external packages
EIGEN (Headers only)
IAU SOFA (static library + headers)
If I write the following code:
#include "sofa.h"
#include "Eigen/Dense"
I get the following error messages:
/some_path/sofam.h:85:18: error: expected unqualified-id before numeric constant #define DAYSEC ( 86400.0 ) ...
and many more lines of errors.
However, if I change the order of the includes it compiles:
#include "Eigen/Dense"
#include "sofa.h"
works fine.
What is causing the problem?
My main problem is that I do not know how this behavior is called so I cannot really google it effectively.
It is not an option to change any of the source code of EIGEN or IAU SOFA (since they are maintained by other groups and we regularly update them from their webpage)
It is also not an option to just fix it once and never touch it again. The order of our header files is automatically rearranged to be in alphabetic order which is messing up everything every time we commit any change and we regularly use an optimize imports algorithm which is also changing the includes automatically.

How to use time library in Arduino?

I am trying to upload this simple program to my Arduino that gets to clock time:
#include <Time.h>
time_t nowTime;
void setup() {
nowTime = now();
}
However, its failing to compile:
exit status 1
'now' was not declared in this scope
Why is now() not declared in this scope? The Time.h file was included. So why wouldn't now() be declared? How can I get around this?
The compilation fails because the Time.h file that the compiler finds has nothing to do with time libraries such as that by Paul Stoffregen (https://github.com/PaulStoffregen/Time).
I tried your Sketch, compiled for an Arduino Uno, and saw the same error you see: that Time.h resolves (the file exists somewhere), yet now() is not defined by that Time.h
After searching my Windows PC for a while, I finally found what I think is the file that #include includes on my installation: C:\Users\Brad\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\firmwares\wifishield\wifiHD\src\time.h or perhaps C:\Users\Brad\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5\avr\include\time.h
Neither of those files defines the now() function.
If you want to use Paul Stoffregen's Time library, download and install it from https://github.com/PaulStoffregen/Time. If instead you wish to use Michael Margolis' Time library, you can find and install it in the Arduino IDE, under Tools / Manage Libraries... and entering "Time" (without quotes) in the search term.
As others have pointed out, the Arduino environment doesn't always know the current date and time. The functions mills() and micros(), return the number of milliseconds or microseconds, respectively, since the Arduino booted. For just looking at the passage of time, most people use millis() or micros() instead of a more complex library.

Keyboard not found even though I'm including Keyboard.h

I want to make a simple program to put on my Arduino/Genuino (Sunfounder) Uno board that when plugged in, it invokes the Keyboard.print() function. When I compile my program to do that, it says that I'm not including Keyboard.h even though I actually am at the beginning of my program.
My code:
#include <Keyboard.h>
void setup() {
Keyboard.begin();
Keyboard.print("Hello, world!");
Keyboard.end();
}
void loop() {
}
When I compile the code, I get this error:
KeyboardMessage:4:3: error: 'Keyboard' not found. Does your sketch include the line '#include <Keyboard.h>'?
I checked my libraries file in the Arduino sketch editor file and Keyboard.h is there.
Any help is appreciated.
Arduino/Genuino Uno does not support Keyboard. You can see it in hardware/ardunino/avr/libraries/HID/HID.h. This file is included from Keyboard.h and contains #if defined(USBCON). But USBCON is not defined for Arduino/Genuino Uno. You need to use another board for Keyboard.h
Following controllers support it:
ATmega32U6
ATmega8U2
ATmega16U2
ATmega16U4
ATmega32U2
ATmega32U4
and some ATxxUSBxx
Arduino/Genuino Uno uses ATmega32U8.
You know what you don't require the 32u4 now, I got a solution to use keyboard and mouse through any Arduino.
All you have to do is something like this:
Serial.println("pressA")
On the Arduino
Head to a python script and do this:
import serial
import pyautogui
Arduino_Serial = serial.Serial('COM5', 9600)
while 1:
incoming_data = str(Arduino_Serial.readline())
print(incoming_data)
if 'pressA' in incoming_data:
pyautogui.press('a')
incoming_data = ""

MFRC522 PICC responded with NAK (Porting MFRC522 arduino library[C++] to [C])

First some introducing.
I am trying to make the MFRC522 library for Arduino work on an ATmega328 programmed in C(I am using a 'normal' controller first, to make it work on a raspberry pi in a later state).
I copied the .h and .cpp from the library to my own project and renamed the .c to .cpp. After removing the classes in the .h file, it was time for the .c file. I replaced all the 'byte' statements to 'uint8_t', replaced the 'Serial.print' with printf and did the changes for GPIO and SPI.
The problem.
After some small mistakes I finally got data from a keycard. However it looked like to work, I get an error with reading line 58 from the card. The error is:
MIFARE_READ() failed: A MIFARE PICC responded with NAK.
I added a print statement to the SPI write and read and found out the following difference(on the left the [C] version and on the right the Arduino version): (because of my reputation, the picture can be found in the BitBucket I mentioned at the code part)
Code
The code is pretty long, but i made it available on BitBucket
I hope someone can point me where to look(some [C++] >> [C] different interpretations), because I don't know anymore where to look.
Sander
You need to run the PCD_Authenticate function before reads and writes. There are a few pre programmed keys in the linked github library that will authenticate the cards. I was getting this when trying to write to the card because I was using KEY_B and not KEY_A. You can see this Authenticate used in the samples provided on that GitHub page. It should looks something like this.
status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
From what I can tell the NAK simply means that the wrong key was used or maybe no key.

LoadLibrary fails when including a specific file during DLL build

I'm getting really strange behavior in one of the DLLs of my C++ app. It works and loads fine until I include a single file using #include in the main file of the DLL. I then get this error message:
Loading components from D:/Targets/bin/MatrixWorkset.dll
Could not load "D:/Targets/bin/MatrixWorkset.dll": Cannot load library MatrixWorkset: Invalid access to memory location.
Now I've searched and searched through the code and google and I can't figure out what is going on. Up till now everything was in a single DLL and I've decided to split it into two smaller ones. The file that causes the problems is part of the other second library (which loads fine).
Any ideas would really be appreciated.
Thanks,
Jaco
The likely cause is a global with class type. The constructor is run from DllMain(), and DllMain() in turn runs before LoadLibrary() returns. There are quite a few restrictions on what you can do until DllMain() has returned.
Is it possible that header includes a #pragma comment(lib,"somelibrary.lib") statement somewhere? If so it's automatically trying to import a library.
To troubleshoot this I'd start by looking at the binary with depends (http://www.dependencywalker.com/), to see if there are any DLL dependencies you don't expect. If you do find something and you are in Visual Studio, you should turn on "Show Progress" AKA /VERBOSE on the linker.
Since you are getting the Invalid Access to memory location, it's possible there's something in the DLLMAIN or some static initializer that is crashing. Can you simplify the MatrixWorkset.dll (assuming you wrote it)?
The error you describe sounds like a run-time error. Is this error displayed automatically by windows or is it one that your program emits?
I say attach a debugger to your application and trace where this error is coming from. Is Windows failing to load a dependency? Is your library somehow failing on load-up?
If you want to rule in/out this header file you're including, try pre-compiling your main source file both with and without this #include and diff the two results.
I'm still not getting it going. Let me answer some of the questions asked:
1) Windows is not failing to load a dependency, I think since Dependency Walker shows everything is ok.
2) I've attached a debugger which basically prints the following when it tries to load MatrixWorkset.dll:
10:04:19.234
stdout:&"warning: Loading components from D:/ScinericSoftware/VisualWorkspace/trunk/Targets/bin/MatrixWorkset.dll\n"
10:04:19.234
stdout:&"\n"
status:Stopped: "signal-received"
status:Stopped.
10:04:19.890
stdout:30*stopped,reason="signal-received",signal-name="SIGSEGV",signal-meaning="Segmentation fault",thread-id="1",frame={addr="0x7c919994",func="towlower",args=[],from="C:\\WINDOWS\\system32\\ntdll.dll"}
input:31info shared
input:32-stack-list-arguments 2 0 0
input:33-stack-list-locals 2
input:34-stack-list-frames
input:35-thread-list-ids
input:36-data-list-register-values x
10:04:19.890
3) MSalters: I'm not sure what you mean with a "global with class type". The file that is giving the problems have been included in a different DLL in which it worked fine and the DLL loaded successfully.
This is the top of the MatrixVariable.h file:
#include "QtSF/Variable.h" // Located in depending DLL (the DLL in which this file always lived.
#include "Matrix.h" // File located in this DLL
#include "QList" // These are all files from the Qt Framework
#include "QModelIndex"
#include "QItemSelection"
#include "QObject"
using namespace Zenautics;
using namespace std;
class MatrixVariable : public Variable
{
Q_OBJECT
Q_PROPERTY(int RowCount READ rowCount WRITE setRowCount)
Q_PROPERTY(int ColumnCount READ columnCount WRITE setColumnCount)
Q_PROPERTY(int UndoPoints READ undoPoints WRITE setUndoPoints)
public:
//! Default constructor.
MatrixVariable(const QString& name, int rows, int cols, double fill_real = 0, double fill_complex = 0, bool isReal = true);
etc. etc. etc.
A possible solution is to put the MatrixVariable file back in the original DLL but that defeats the whole idea of splitting the DLL into smaller parts which is not really a option.
I get that error from GetLastError() when I fail to load a DLL from a command line EXE recently. It used to work, then I added some MFC code to the DLL. Now all bets are off.
I just had this exact same problem. A dll that had been working just fine, suddenly stopped working. I was taking an access violation in the CRT stuff that initializes static objects. Doing a rebuild all did not fix the problem. But when I manually commented out all the statics, the linker complained about a corrupt file. Link again: Worked. Now I can LoadLibrary. Then, one by one, I added the statics back in. Each time, I recompiled and tested a LoadLibrary. Each time it worked fine. Eventually, all my statics were back, and things working normally.
If I had to guess, some intermediate file used by the linker was corrupted (I see the ilk files constantly getting corrupted by link.exe). If you can, maybe wipe out all your files and do a clean build? But I'm guessing you've already figured things out since this is 6 months old ...