Arduino: Write.Server -> int to byte array - arduino

I am trying to send an integer from my Arduino Mega to my Android app. I am trying to split the int into two bytes which my Android app will then recieve in a buffer with a size of 16384 (two bytes). I've just started with arduino so I'm a bit lost!
So far I have this:
int val = analogRead(A0); // as states int value from 0 - 1023
byte high = highByte(val);
byte low = lowByte(val);
byte byteArray[2] = {high, low};
server.write(16384, byteArray);
The error I get is:
ProArd.ino: In function 'void loop()':
ProArd:88: error: invalid conversion from 'int' to 'const uint8_t*'
ProArd:88: error: initializing argument 1 of 'virtual size_t WiFiServer::write(const uint8_t*, size_t)'
ProArd:88: error: invalid conversion from 'byte*' to 'size_t'
ProArd:88: error: initializing argument 2 of 'virtual size_t WiFiServer::write(const uint8_t*, size_t)'

You are passing in the array itself as a pointer into the size parameter of the write function, which only takes a positive int.
Try using this to get the length of the array: sizeof(arr);

Related

What does the &char and &dword mean in the ReadFile

char szBuf;
DWORD dwIncommingReadSize;
(ReadFile(m_hSerialComm, &szBuf, 10, &dwIncommingReadSize, NULL)
What does the &szBuf and &dwIncommingReadSize mean when adding & in char and DWORD
[out] lpBuffer
A pointer to the buffer that receives the data read from a file or device.
[out, optional] lpNumberOfBytesRead
A pointer to the variable that receives the number of bytes read.
lpBufer is $szBuf definition.
lpNumberOfBytesRead is &dwIncommingReadSize definition.

Convert String to uint8_t for Arduino C++

I'm trying to send a user input String type over Bluetooth with function
BluetoothSerial SerialBT;
SerialBT.write()
But the function won't accept String, but instead it accepts uint8_t.
I tried some conversions like
char buf[packet.length()];
memcpy(buf, packet.c_str(), packet.length());
SerialBT.write(buf, packet.length());
But it shows an error
invalid conversion from 'char*' to 'const uint8_t* {aka const unsigned char*}' [-fpermissive]
Could please someone show me how to convert it right?
Thanks

First project! Arduino uno r3 says "no matching function for call to 'SevSeg::SevSeg()'"

I was following this tutorial, and everything is wired correctly as far as I can tell, but my 7seg display is only showing 8888.
I tried uploading the code to the controller but I get back "no matching function for call to 'SevSeg::SevSeg()'"
I'm not sure what I'm doing wrong. Any help would be greatly appreciated. Thank you!
full error message:
DaysCounterTest:4:8: error: no matching function for call to 'SevSeg::SevSeg()'
SevSeg sevseg; //Instantiate a seven segment controller object
^~~~~~
In file included from C:\Users\Cel\Documents\Arduino\DaysCounterTest\DaysCounterTest.ino:2:0:
C:\Users\Cel\Documents\Arduino\libraries\Firmware/SevSeg.h:49:3: note: candidate: SevSeg::SevSeg(byte*, byte*, byte, byte, bool, bool, bool)
SevSeg( byte segmentPinsIn[], byte digitPinsIn[], byte numDigitsIn = 4, byte hardwareConfig = COMMON_ANODE,
^~~~~~
C:\Users\Cel\Documents\Arduino\libraries\Firmware/SevSeg.h:49:3: note: candidate expects 7 arguments, 0 provided
C:\Users\Cel\Documents\Arduino\libraries\Firmware/SevSeg.h:46:7: note: candidate: constexpr SevSeg::SevSeg(const SevSeg&)
class SevSeg
^~~~~~
C:\Users\Cel\Documents\Arduino\libraries\Firmware/SevSeg.h:46:7: note: candidate expects 1 argument, 0 provided
C:\Users\Cel\Documents\Arduino\libraries\Firmware/SevSeg.h:46:7: note: candidate: constexpr SevSeg::SevSeg(SevSeg&&)
C:\Users\Cel\Documents\Arduino\libraries\Firmware/SevSeg.h:46:7: note: candidate expects 1 argument, 0 provided
C:\Users\Cel\Documents\Arduino\DaysCounterTest\DaysCounterTest.ino: In function 'void setup()':
DaysCounterTest:87:50: error: no matching function for call to 'SevSeg::begin(byte&, byte&, byte [4], byte [8], bool&, bool&, bool&, bool&)'
updateWithDelays, leadingZeros, disableDecPoint);
^
In file included from C:\Users\Cel\Documents\Arduino\DaysCounterTest\DaysCounterTest.ino:2:0:
C:\Users\Cel\Documents\Arduino\libraries\Firmware/SevSeg.h:53:8: note: candidate: void SevSeg::begin(byte*, byte*, byte, byte, bool, bool, bool)
void begin( byte segmentPinsIn[], byte digitPinsIn[], byte numDigitsIn = 4, byte hardwareConfig = COMMON_ANODE,
^~~~~
C:\Users\Cel\Documents\Arduino\libraries\Firmware/SevSeg.h:53:8: note: candidate expects 7 arguments, 8 provided
exit status 1
no matching function for call to 'SevSeg::SevSeg()'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I noticed my links were not posted in my original post.
I found that the libraries I needed were not importing correctly, more specifically the sevseg one. I needed to import another one with it. Now the 7 seg display is showing up correctly

Sending floats w/NRF8001

Using Blend Micro (basically Arduino Uno + Nordic nRF8001) to gather and send sensor data via BLE. Can't figure out how to send floats.
Here's a snip of code:
if ((lib_aci_is_pipe_available(&aci_state, PIPE_AIR_QUALITY_SENSOR_TEMPERATURE_MEASUREMENT_TX)) && (lib_aci_is_pipe_available(&aci_state, PIPE_AIR_QUALITY_SENSOR_RELATIVE_HUMIDITY_TX)) && (lib_aci_is_pipe_available(&aci_state, PIPE_AIR_QUALITY_SENSOR_CARBON_MONOXIDE_LEVEL_TX)))
{
// Read DHT & MQ7 sensors
h = dht.readHumidity(); // Read humidity from DHT
t = dht.readTemperature(); // Read temperature as Celsius from DHT
f = dht.readTemperature(true); // Read temperature as Fahrenheit from DHT
int VoutAn = analogRead(MQ7PIN); //Read Vout in analog from MQ7
// Compute heat index
hi = dht.computeHeatIndex(f, h);
// Compute CO in PPM
float Vout = (5.0/1023)*VoutAn;
co = 100.468*(pow(((5/Vout)-1),-1.43));
lib_aci_send_data(PIPE_AIR_QUALITY_SENSOR_TEMPERATURE_MEASUREMENT_TX, &f, sizeof(f));
lib_aci_send_data(PIPE_AIR_QUALITY_SENSOR_RELATIVE_HUMIDITY_TX, &h, sizeof(h));
lib_aci_send_data(PIPE_AIR_QUALITY_SENSOR_CARBON_MONOXIDE_LEVEL_TX, &co, sizeof(co));
}
And here's the compile error:
Arduino: 1.0.6 (Mac OS X), Board: "Blend Micro 3.3V/16MHz (overclock)"
BLE-DHT-MQ7-Test.ino: In function 'void loop()':
BLE-DHT-MQ7-Test.ino:287: error: cannot convert 'float*' to 'uint8_t*'
for argument '2' to 'bool lib_aci_send_data(uint8_t, uint8_t*,
uint8_t)'
BLE-DHT-MQ7-Test.ino:288: error: cannot convert 'float*' to 'uint8_t*'
for argument '2' to 'bool lib_aci_send_data(uint8_t, uint8_t*,
uint8_t)'
BLE-DHT-MQ7-Test.ino:289: error: cannot convert 'float*' to 'uint8_t*'
for argument '2' to 'bool lib_aci_send_data(uint8_t, uint8_t*,
uint8_t)'
My understanding of the aci_send_data function is that argument 2 is a uint8_t variable that points to the data in memory to be sent, and that this data can be in any form. But it only seems to work if the data is in a uint8_t variable - e.g. this compiles if I convert my variables to uint8_t - but I need floats...
So my questions:
Am I understanding the aci_send_data function correctly?
If so, what am I doing wrong?
If not, how can I send floats?
Thanks!
I am not sure about the parameters needed by lib_aci_send_data function however, according to this code it seems that the first parameter accepts the TX pin, the second one is the reference of the first byte (in a byte array) to be interpreted as a number, and the third is the length or number of bytes to consider.
The data type float is a 4-byte variable. If we have an array of bytes representing the float number, we can pass the reference of the first byte in the second parameter of the function, and pass 4 as the length (or number of bytes to consider) in the third parameter.
To convert float to byte array, see this thread. In your case, it would look like this:
typedef union _data {
float f;
char s[4];//since f takes 4 bytes
} myData;
myData q;
void setup(){
Serial.begin(9600);
}
void loop(){
q.f = 1.234;
//you can access the char array by using q.s
//q.s[0] q.s[1] ...
//take note that q.s is a reference to the first element
//so we can pass that immediately to the 2nd parameter
}
Now you can use the function in this way: lib_aci_send_data(PIPE_AIR_QUALITY_SENSOR_RELATIVE_HUMIDITY_TX, q.s, 4);
I cannot test this as of now since I'm on a different machine, please let me know if this was successful, I'm willing to add edits if necessary.
Thanks, it works! Seems a little cumbersome, but here's the code for getting all 3 variables I need to send as floats:
if ((lib_aci_is_pipe_available(&aci_state, PIPE_AIR_QUALITY_SENSOR_TEMPERATURE_MEASUREMENT_TX)) && (lib_aci_is_pipe_available(&aci_state, PIPE_AIR_QUALITY_SENSOR_RELATIVE_HUMIDITY_TX)) && (lib_aci_is_pipe_available(&aci_state, PIPE_AIR_QUALITY_SENSOR_CARBON_MONOXIDE_LEVEL_TX)))
{
typedef union _dataF
{
float f;
uint8_t fInt[4];
} myDataF;
myDataF F;
typedef union _dataH
{
float h;
uint8_t hInt[4];
} myDataH;
myDataH H;
typedef union _dataCO
{
float co;
uint8_t coInt[4];
} myDataCO;
myDataCO CO;
// Read DHT & MQ7 sensors
h = dht.readHumidity(); // Read humidity from DHT
t = dht.readTemperature(); // Read temperature as Celsius from DHT
f = dht.readTemperature(true); // Read temperature as Fahrenheit from DHT
int VoutAn = analogRead(MQ7PIN); //Read Vout in analog from MQ7
// Compute heat index
hi = dht.computeHeatIndex(f, h);
// Compute CO in PPM
float Vout = (5.0/1023)*VoutAn;
co = 100.468*(pow(((5/Vout)-1),-1.43));
F.f = f;
H.h = h;
CO.co = co;
lib_aci_send_data(PIPE_AIR_QUALITY_SENSOR_TEMPERATURE_MEASUREMENT_TX, F.fInt, 4);
lib_aci_send_data(PIPE_AIR_QUALITY_SENSOR_RELATIVE_HUMIDITY_TX, H.hInt, 4);
lib_aci_send_data(PIPE_AIR_QUALITY_SENSOR_CARBON_MONOXIDE_LEVEL_TX, CO.coInt, 4);
}
I put the sensor data into wider scope floats as I draw on them elsewhere in the program.

Invalid conversion from char to 'uint8_t'

I'm using a shift out statement to drive a few 7 seg displays (in the end)
but I'm running into a problem.
I have used #include <avr/pgmspace.h> libary as to save space for the processing.
At the end of it I need to shiftOut a binary number to be fed into a reg then to a BCD then to my display:
strcpy_P(buffer, (char*)pgm_read_word(&(Times[big])));
shiftOut(dataPin, clockPin, MSBFIRST, buffer);
in the buffer place will go the selected value (e.g. B00100011 should display 23),
my code gives me
Invalid conversion from char to 'uint8_t'
with the shiftOut line highlighted
Any ideas?
The problem here is that shiftOut expects a byte (uint8_t) as its 4th argument (value). The passed value is a char* (presumably declared as a prog_char array).
To fix this, the declaration of the value will need to use prog_uchar, like this:
prog_uchar values[] PROGMEM = { (prog_uchar) B00100011, ... };
...
int valueIndex = ...; // Index of value in the values array
shiftOut(dataPin, clockPin, MSBFIRST, pgm_read_byte(&(values[valueIndex])));

Resources