I'm getting an error when trying to run some code on my raspberry pi. Here is the source code:
/*
SimpleReceive
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 11
*/
#include "VirtualWire.h"
#include "wiringSerial.h"
#include <wiringPi.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
const char message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
unsigned char messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
int fd;
wiringPiSetup();
fd = serialOpen("/dev/ttyAMA0", 9600);
void send (char *message)
{
vw_send((uint8_t *)message, strlen(message));
vw_wait_tx(); // Wait until the whole message is gone
}
void setup()
{
serialOpen("/dev/ttyAMA0", 9600);
serialPrintf(fd, "Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
}
void loop()
{
send("Hello there");
//delay(1000);
if (vw_get_message(message, &messageLength)) // Non-blocking
{
serialPrintf(fd, "Received: ");
for (int i = 0; i < messageLength; i++)
{
serialPrintf(fd, message[i]);
}
serialPuts(fd, "test");
}
}
I'm getting this error:
receiver.c:37:6: error: initializer element is not constant
fd = serialOpen("/dev/ttyAMA0", 9600);
Any ideas what they mean?
Thanks!
messageLengthis declared; ideally you should move it intoloop(),if that is the only place it is used. If not it gets slightly more complicated. – goldilocks Jan 25 '20 at 12:38It fails the precompiler, by attempting to use a function as initialiser. It never actually gets to the c compiler because of this error, but if it did would produce a string of error messages.
It is attempting to call an Arduino library.
– Milliways Jan 25 '20 at 22:05main()doesn't make it invalid (though the globalwiringPi()call probably does). If the OP expects that to run as a standalone program we are very far from home -- but I read "some code" "getting an error", and the code provided does show the cause of the problem, which is a perfectly sane and valid way of asking a question. I doubt that's the OP's only problem, but it's the one he's digging into here. – goldilocks Jan 26 '20 at 11:49loop()based Arduino sketch and run it as a normal C or C++ program, you need to sit down and read some basic tutorials on those languages outside of an Arduino context. – goldilocks Jan 26 '20 at 12:02