Arduino Nano BLE 33 with DFPlayer not working - arduino

I'm pretty new with Arduino circuits and I'm trying to connect a DFPlayer Mini to Arduino Nano BLE 33 Sense. If I understood correctly the library "SoftwareSerial" doesn't work on the BLE Sense and I'm not able to find a way to make the DFPlayer work properly (I also tried to use HardwareSerial but for some reason it doesn't work). Do you know any way I can fix the connection or find a replacement library?
Here is my code
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
//mp3
SoftwareSerial mySoftwareSerial(12, 11); // RX, TX (meno 11)
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup(void) {
//mp3
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true);
}
myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
//----Set volume----
myDFPlayer.volume(3);//et volume value (0~30).
myDFPlayer.volumeUp(); //Volume Up
myDFPlayer.volumeDown(); //Volume Down
//----Set device we use SD as default----
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
//----Set different EQ----
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
//mp3
static unsigned long timer = millis();
if (millis() - timer > 3000) {
timer = millis();
myDFPlayer.next(); //Play next mp3 every 3 second.
}
if (myDFPlayer.available()) {
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
}
// wait for a short amount of time -- sometimes a short delay of 5ms will help
// technically we only need to execute this one time, since we aren't changing the colors but we will build on this structure
//----Mp3 play----
//Play the first mp3
}
void loop(void) {
myDFPlayer.play(1);
}
//Mp3
void printDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
The error I get by compiling is this
Arduino: 1.8.16 (Mac OS X), Board: "Arduino Nano 33 BLE"
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:362:2: error: #error This version of SoftwareSerial supports only 20, 16 and 8MHz processors
#error This version of SoftwareSerial supports only 20, 16 and 8MHz processors
^~~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp: In member function 'bool SoftwareSerial::listen()':
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:418:23: error: 'SREG' was not declared in this scope
uint8_t oldSREG = SREG;
^~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:418:23: note: suggested alternative: 'SING'
uint8_t oldSREG = SREG;
^~~~
SING
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:419:5: error: 'cli' was not declared in this scope
cli();
^~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp: In member function 'void SoftwareSerial::setTX(uint8_t)':
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:593:22: error: 'digitalPinToBitMask' was not declared in this scope
_transmitBitMask = digitalPinToBitMask(tx);
^~~~~~~~~~~~~~~~~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:593:22: note: suggested alternative: 'digitalPinToPinName'
_transmitBitMask = digitalPinToBitMask(tx);
^~~~~~~~~~~~~~~~~~~
digitalPinToPinName
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:595:27: error: 'portOutputRegister' was not declared in this scope
_transmitPortRegister = portOutputRegister(port);
^~~~~~~~~~~~~~~~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:595:27: note: suggested alternative: '_transmitPortRegister'
_transmitPortRegister = portOutputRegister(port);
^~~~~~~~~~~~~~~~~~
_transmitPortRegister
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp: In member function 'void SoftwareSerial::setRX(uint8_t)':
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:604:21: error: 'digitalPinToBitMask' was not declared in this scope
_receiveBitMask = digitalPinToBitMask(rx);
^~~~~~~~~~~~~~~~~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:604:21: note: suggested alternative: 'digitalPinToPinName'
_receiveBitMask = digitalPinToBitMask(rx);
^~~~~~~~~~~~~~~~~~~
digitalPinToPinName
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:606:26: error: 'portInputRegister' was not declared in this scope
_receivePortRegister = portInputRegister(port);
^~~~~~~~~~~~~~~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp: In member function 'void SoftwareSerial::begin(long int)':
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:617:31: error: 'table' was not declared in this scope
for (unsigned i=0; i<sizeof(table)/sizeof(table[0]); ++i)
^~~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:617:31: note: suggested alternative: 'tanl'
for (unsigned i=0; i<sizeof(table)/sizeof(table[0]); ++i)
^~~~~
tanl
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:633:9: error: 'digitalPinToPCICR' was not declared in this scope
if (digitalPinToPCICR(_receivePin))
^~~~~~~~~~~~~~~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:633:9: note: suggested alternative: 'digitalPinToPort'
if (digitalPinToPCICR(_receivePin))
^~~~~~~~~~~~~~~~~
digitalPinToPort
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:635:46: error: 'digitalPinToPCICRbit' was not declared in this scope
*digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin));
^~~~~~~~~~~~~~~~~~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:635:46: note: suggested alternative: 'digitalPinToPort'
*digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin));
^~~~~~~~~~~~~~~~~~~~
digitalPinToPort
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:635:42: error: '_BV' was not declared in this scope
*digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin));
^~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:635:42: note: suggested alternative: '_B'
*digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin));
^~~
_B
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:636:8: error: 'digitalPinToPCMSK' was not declared in this scope
*digitalPinToPCMSK(_receivePin) |= _BV(digitalPinToPCMSKbit(_receivePin));
^~~~~~~~~~~~~~~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:636:8: note: suggested alternative: 'digitalPinToPort'
*digitalPinToPCMSK(_receivePin) |= _BV(digitalPinToPCMSKbit(_receivePin));
^~~~~~~~~~~~~~~~~
digitalPinToPort
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:636:46: error: 'digitalPinToPCMSKbit' was not declared in this scope
*digitalPinToPCMSK(_receivePin) |= _BV(digitalPinToPCMSKbit(_receivePin));
^~~~~~~~~~~~~~~~~~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:636:46: note: suggested alternative: 'digitalPinToPort'
*digitalPinToPCMSK(_receivePin) |= _BV(digitalPinToPCMSKbit(_receivePin));
^~~~~~~~~~~~~~~~~~~~
digitalPinToPort
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp: In member function 'void SoftwareSerial::end()':
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:651:7: error: 'digitalPinToPCMSK' was not declared in this scope
if (digitalPinToPCMSK(_receivePin))
^~~~~~~~~~~~~~~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:651:7: note: suggested alternative: 'digitalPinToPort'
if (digitalPinToPCMSK(_receivePin))
^~~~~~~~~~~~~~~~~
digitalPinToPort
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:652:45: error: 'digitalPinToPCMSKbit' was not declared in this scope
*digitalPinToPCMSK(_receivePin) &= ~_BV(digitalPinToPCMSKbit(_receivePin));
^~~~~~~~~~~~~~~~~~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:652:45: note: suggested alternative: 'digitalPinToPort'
*digitalPinToPCMSK(_receivePin) &= ~_BV(digitalPinToPCMSKbit(_receivePin));
^~~~~~~~~~~~~~~~~~~~
digitalPinToPort
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:652:41: error: '_BV' was not declared in this scope
*digitalPinToPCMSK(_receivePin) &= ~_BV(digitalPinToPCMSKbit(_receivePin));
^~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:652:41: note: suggested alternative: '_B'
*digitalPinToPCMSK(_receivePin) &= ~_BV(digitalPinToPCMSKbit(_receivePin));
^~~
_B
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp: In member function 'virtual size_t SoftwareSerial::write(uint8_t)':
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:687:21: error: 'SREG' was not declared in this scope
uint8_t oldSREG = SREG;
^~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:687:21: note: suggested alternative: 'SING'
uint8_t oldSREG = SREG;
^~~~
SING
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:688:3: error: 'cli' was not declared in this scope
cli(); // turn off interrupts for a clean txmit
^~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:692:26: error: 'XMIT_START_ADJUSTMENT' was not declared in this scope
tunedDelay(_tx_delay + XMIT_START_ADJUSTMENT);
^~~~~~~~~~~~~~~~~~~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp: In member function 'virtual void SoftwareSerial::flush()':
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:735:21: error: 'SREG' was not declared in this scope
uint8_t oldSREG = SREG;
^~~~
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:735:21: note: suggested alternative: 'SING'
uint8_t oldSREG = SREG;
^~~~
SING
/Users/matteopaoli/Documents/Arduino/libraries/SoftwareSerial-master/SoftwareSerial.cpp:736:3: error: 'cli' was not declared in this scope
cli();
^~~
exit status 1
Error compiling for board Arduino Nano 33 BLE.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Thank you in advance!

Related

Library trying to call wire.requestFrom results in error on esp32

I am trying to run two I2C busses on a Nano32.
#include <Wire.h>
#include "MS5837.h"
// Setup sensor 1
#define I2C_SDA_1_PIN 21
#define I2C_SCL_1_PIN 22
TwoWire I2C_1 = TwoWire(0);
MS5837 sensor_1;
// Setup sensor 2
#define I2C_SDA_2_PIN 17
#define I2C_SCL_2_PIN 16
TwoWire I2C_2 = TwoWire(1);
MS5837 sensor_2;
void setup() {
Serial.begin(115200);
Serial.println("Starting");
I2C_1.begin(I2C_SDA_1_PIN, I2C_SCL_1_PIN);
I2C_2.begin(I2C_SDA_2_PIN, I2C_SCL_2_PIN);
while (!sensor_1.init(I2C_1)) {
Serial.println("Init sensor 1 failed!");
delay(5000);
}
while (!sensor_2.init(I2C_2)) {
Serial.println("Init sensor 2 failed!");
delay(5000);
}
}
void loop() {
}
The MS5837 library comes from https://github.com/bluerobotics/BlueRobotics_MS5837_Library and is not the one that can be installed via the Arduino IDE. The reason for that is that that version does not allow the setting of the TwoWire port to use.
The issue I am facing is that I get the following error message when compiling:
In file included from c:\Users\tammo\OneDrive\Documents\Arduino\libraries\BlueRobotics_MS5837_Library\MS5837.h:41,
from c:\Users\tammo\OneDrive\Documents\Arduino\libraries\BlueRobotics_MS5837_Library\MS5837.cpp:1:
C:\Users\tammo\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.6\libraries\Wire\src/Wire.h: In member function 'bool MS5837::init(TwoWire&)':
C:\Users\tammo\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.6\libraries\Wire\src/Wire.h:127:13: note: candidate 1: 'uint8_t TwoWire::requestFrom(int, int)'
uint8_t requestFrom(int address, int size);
^~~~~~~~~~~
C:\Users\tammo\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.6\libraries\Wire\src/Wire.h:125:13: note: candidate 2: 'uint8_t TwoWire::requestFrom(uint8_t, uint8_t)'
uint8_t requestFrom(uint8_t address, uint8_t size);
^~~~~~~~~~~
C:\Users\tammo\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.6\libraries\Wire\src/Wire.h: In member function 'void MS5837::read()':
C:\Users\tammo\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.6\libraries\Wire\src/Wire.h:127:13: note: candidate 1: 'uint8_t TwoWire::requestFrom(int, int)'
uint8_t requestFrom(int address, int size);
^~~~~~~~~~~
C:\Users\tammo\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.6\libraries\Wire\src/Wire.h:125:13: note: candidate 2: 'uint8_t TwoWire::requestFrom(uint8_t, uint8_t)'
uint8_t requestFrom(uint8_t address, uint8_t size);
^~~~~~~~~~~
C:\Users\tammo\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.6\libraries\Wire\src/Wire.h:127:13: note: candidate 1: 'uint8_t TwoWire::requestFrom(int, int)'
uint8_t requestFrom(int address, int size);
^~~~~~~~~~~
C:\Users\tammo\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.6\libraries\Wire\src/Wire.h:125:13: note: candidate 2: 'uint8_t TwoWire::requestFrom(uint8_t, uint8_t)'
uint8_t requestFrom(uint8_t address, uint8_t size);
^~~~~~~~~~~
Sketch uses 273661 bytes (20%) of program storage space. Maximum is 1310720 bytes.
Global variables use 22672 bytes (6%) of dynamic memory, leaving 305008 bytes for local variables. Maximum is 327680 bytes.
I am at a loss as to how to fix this and would appreciate pointers.

I keep getting a compilation error on Arduino IDE using MFRC522 RFID saying that it needs a data type

I have been using the standard code for Arduino to get this RFID to work
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();
}
void loop()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "BD 31 15 2B") //change here the UID of the card/cards that you want to give access
{
Serial.println("Authorized access");
Serial.println();
delay(3000);
}
else {
Serial.println(" Access denied");
delay(3000);
}
}
And I keep getting these errors
RFID:6: error: 'MFRC522' does not name a type
RFID.ino: In function 'void setup()':
RFID:12: error: 'mfrc522' was not declared in this scope
RFID.ino: In function 'void loop()':
RFID:20: error: 'mfrc522' was not declared in this scope
RFID:25: error: 'mfrc522' was not declared in this scope
RFID:33: error: 'mfrc522' was not declared in this scope
Any reasons why this is happening? I'm using Arduino 1.0.5 because my Uno drivers won't load with a newer version. If there's a post about this already I couldn't find it, so a link to that would also be appreciated

Why is the delay instruction causing a compiler error?

Compiler throw an error when I added delay(timer) in the code, timer is a const integer defined at the beginning of the code.
When I comment out the delay line, the compiler completes successfully.
4th line from the bottom
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library
#include <SPI.h>
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
const int BLUE = 3; // the number of the pushbutton pin
const int GREEN = 4; // the number of the LED pin
const int RED = 5; // the number of the LED pin
const int thisPin = 4;
const int timer = 100;
// the setup function runs once when you press reset or power the board
void setup() {
// Initialize ASK Object
rf_driver.init();
Serial.begin(9600);
// initialize digital pin LED_BUILTIN as an output.
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
int mesg = 0;
// Set buffer to size of expected message
uint8_t buf[8];
uint8_t buflen = sizeof(buf);
if (rf_driver.recv(buf, &buflen)) {
// Message received with valid checksum
Serial.print("Message Received: ");
Serial.println((char*)buf);
mesg = 1;
}
if (mesg == 1) {
mesg = 0;
digitalWrite(thisPin, HIGH);
//delay(timer);
digitalWrite(thisPin, LOW);
}
}
Below is the error message:
C:\Users\Swee-Chuan Khoo\Documents\Arduino\libraries\RadioHead\RH_ASK.cpp: In member function 'setModeIdle.constprop':
C:\Users\Swee-Chuan Khoo\Documents\Arduino\libraries\RadioHead\RH_ASK.cpp:421:1: internal compiler error: Segmentation fault
}
^
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
lto-wrapper.exe: fatal error: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc returned 1 exit status
compilation terminated.
c:/program files/windowsapps/arduinollc.arduinoide_1.8.21.0_x86__mdqgnx93n4wtt/hardware/tools/avr/bin/../lib/gcc/avr/5.4.0/../../../../avr/bin/ld.exe: error: lto-wrapper failed
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Nano.
Downgrade AVR core to 1.6.21 as suggested by Juraj. All working properly now.
Now i can continue with the project.
Thank you everyone for the help, datafiddler, Juraj and M.R.

arduino and esp8266 wifi module compiling issue

I want to monitor the condition of the 2HP IM to using arduino uno and sensors and want to push the data to cloud using esp8266 in thingspeak. But I keep getting the error ""Softwere serial esp8266" redeclared as different kind of symbol " when compiled. please fix this problem
this is my coding:
#include "SoftwareSerial.h"
#define RX 10
#define TX 11
String AP = "wifi name"; // CHANGE ME
String PASS = "wifi password"; // CHANGE ME
String API = "api key"; // CHANGE ME
String HOST = "api.thingspeak.com";
String PORT = "80";
String field = "field1";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
SoftwareSerial esp8266(RX,TX,false,256);
void setup() {
Serial.begin(9600);
esp8266.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void loop() {
valSensor = getSensorData();
String getData = "GET /update?api_key="+ API +"&"+ field +"="+String(valSensor);
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266.println(getData);delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}
int getSensorData(){
return random(1000); // Replace with
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
this is the error I am getting:
Arduino: 1.8.9 (Windows 8), Board: "Generic ESP8266 Module, 80 MHz, Flash, Disabled, ck, 26 MHz, 40MHz, DOUT (compatible), 512K (no SPIFFS), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"
C:\Users\Admin\Desktop\n12\n12.ino: In function 'void setup()':
n12:19:10: error: expected primary-expression before '.' token
esp8266.begin(115200);
^
C:\Users\Admin\Desktop\n12\n12.ino: In function 'void loop()':
n12:30:9: error: expected primary-expression before '.' token
esp8266.println(getData);delay(1500);countTrueCommand++;
^
C:\Users\Admin\Desktop\n12\n12.ino: In function 'void sendCommand(String, int, char*)':
n12:43:12: error: expected primary-expression before '.' token
esp8266.println(command);//at+cipsend
^
n12:44:15: error: expected primary-expression before '.' token
if(esp8266.find(readReplay))//ok
^
exit status 1
expected primary-expression before '.' token
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Variadic functions not working on arduino

I'm trying to write my own basic libraries to program the Arduino in pure C++. I've tried using a variadic function to implement something similar to Linux's ioctl() to control the SPI module but it just won't work and I have no idea why. I don't pin 13 (Arduino SCK) light up as expected during SPI transactions indicating that SPI is not operating. All the other functions in my library work properly.
The following is my SPI library:
/*
spi.h: SPI driver for Atmega328p
*/
#ifndef _SPI_H
#define _SPI_H
#include <avr/io.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>
#include <inttypes.h>
// SPI bus pin mapping ///////////////////////////////////
#define PORT_SPI PORTB // Port register containing SPI pins
#define DDR_SPI DDRB // Data direction register containing SPI pins
#define DDR_SCK DDB5 // Data direction bit of SPI SCK pin
#define DDR_MISO DDB4 // Data direction bit of SPI MISO pin
#define DDR_MOSI DDB3 // Data direction bit of SPI MOSI pin
#define DDR_HWCS DDB2 // Data direction bit of SPI hardware chip select pin
#define PIN_SCK PB5 // Port register bit of SPI SCK pin
#define PIN_MISO PB4 // Port register bit of SPI MISO pin
#define PIN_MOSI PB3 // Port register bit of SPI MOSI pin
#define PIN_HWCS PB2 // Port register bit of SPI hardware chip select pin
// SPI ioctl commands ////////////////////////////////////
#define SPIIOCCONF 0 // Configure SPI command
#define SPIIOCDECONF 1 // Deconfigure SPI command
#define SPIIOCTRANSMIT 2 // SPI byte exchange command
// Clock frequency settings //////////////////////////////
#define SCK_DIV2 2 // Divide source pulse by 2
#define SCK_DIV4 4 // Divide source pulse by 4
#define SCK_DIV8 8 // Divide source pulse by 8
#define SCK_DIV16 16 // Divide source pulse by 16
#define SCK_DIV32 32 // Divide source pulse by 32
#define SCK_DIV64 64 // Divide source pulse by 64
#define SCK_DIV128 128 // Divide source pulse by 128
// SPI modes /////////////////////////////////////////////
#define SPI_MODE0 0
#define SPI_MODE1 1
#define SPI_MODE2 2
#define SPI_MODE3 3
// SPI transaction data orders ///////////////////////////
#define LSBFIRST 0
#define MSBFIRST 1
// The SPI module ////////////////////////////////////////
class spiModule {
private:
bool configured; // Indicates whether SPI is operating with a valid configuration
uint8_t ddrOld, portOld; // Value of DDR_SPI and PORT_SPI just before an SPI configuration was called for
// ( These variables are used to restore the state of the
// SPI pins when SPI is deconfigured )
/* ioctls used to operate the SPI module */
void spiiocconf(int, int, int); // Configure SPI with a valid clock frequency, data order and SPI mode
void spiiocdeconf(void); // Deconfigure SPI and restore SPI pins to their original states
void spiioctransmit(uint8_t, uint8_t *); // Exchange a byte of data over SPI
/* ioctl handlers */
/* These routines check the validity of the arguments and call the ioctls (above) only if all arguments make sense */
/* I've tested these functions by making them public and found that they work perfectly */
int conf(int, int, int); // call spiiocconf() if arguments are valid and SPI is configured
int deconf(void); // call spiiocdeconf() if all arguments are valid and SPI is configured
int transmit(uint8_t, uint8_t *); // call spiioctransmit() if all arguments are valid and SPI is configured
public:
spiModule(void); // Initialize this class
int ioctl(int action, ... ); // Core ioctl handler (supposed to work like the Linux ioctl() system call). But this one just won't work.
};
spiModule spi; // Object of class spiModule for using SPI
// Constructor ///////////////////////////////////////////
spiModule::spiModule(void) {
configured = false;
}
// Private routines //////////////////////////////////////
/* Ioctls */
void spiModule::spiiocconf(int clkDiv, int dataOrder, int mode) {
// Store the values of DDR_SPI and PORT_SPI so they may be recovered when SPI is deconfigured
ddrOld = DDR_SPI;
portOld = PORT_SPI;
// Configure SCK, MOSI and HWCS as output pins and MISO as an input pin
DDR_SPI |= (_BV(DDR_HWCS) | _BV(DDR_SCK) | _BV(DDR_MOSI));
DDR_SPI &= ~_BV(DDR_MISO);
// Power up the SPI module
PRR &= ~_BV(PRSPI);
// Enable SPI and configure it as master
SPCR = 0x00;
SPCR |= (_BV(SPE) | _BV(MSTR));
// Set data order
switch(dataOrder)
{
case LSBFIRST:
SPCR |= _BV(DORD);
break;
case MSBFIRST:
SPCR &= ~_BV(DORD);
break;
}
// Set SPI mode
switch(mode)
{
case SPI_MODE0:
SPCR &= ~(_BV(CPOL) | _BV(CPHA));
break;
case SPI_MODE1:
SPCR |= _BV(CPHA);
SPCR &= ~_BV(CPOL);
break;
case SPI_MODE2:
SPCR &= ~_BV(CPHA);
SPCR |= _BV(CPOL);
break;
case SPI_MODE3:
SPCR |= (_BV(CPOL) | _BV(CPHA));
break;
}
// Set SPI clock frequency
switch(clkDiv)
{
case SCK_DIV2:
SPCR &= ~(_BV(SPR0) | _BV(SPR1));
SPSR |= _BV(SPI2X);
break;
case SCK_DIV4:
SPCR &= ~(_BV(SPR0) | _BV(SPR1));
SPSR &= ~_BV(SPI2X);
break;
case SCK_DIV8:
SPCR |= _BV(SPR0);
SPCR &= ~_BV(SPR1);
SPSR |= _BV(SPI2X);
break;
case SCK_DIV16:
SPCR |= _BV(SPR0);
SPCR &= ~_BV(SPR1);
SPSR &= ~_BV(SPI2X);
break;
case SCK_DIV32:
SPCR &= ~_BV(SPR0);
SPCR |= _BV(SPR1);
SPSR |= _BV(SPI2X);
break;
case SCK_DIV64:
SPCR |= _BV(SPR0);
SPCR |= _BV(SPR1);
SPSR |= _BV(SPI2X);
break;
case SCK_DIV128:
SPCR |= _BV(SPR0);
SPCR |= _BV(SPR1);
SPSR &= ~_BV(SPI2X);
break;
}
// SPI is now configured
configured = true;
return;
}
void spiModule::spiiocdeconf(void) {
// Clear SPI configuration, power down the SPI module and restore the values of DDR_SPI and PORT_SPI
SPCR = 0x00;
PRR |= _BV(PRSPI);
DDR_SPI = ddrOld;
PORT_SPI = portOld;
// SPI is no longer configured
configured = false;
return;
}
void spiModule::spiioctransmit(uint8_t txbyte, uint8_t * rxbyte) {
// Write TX byte to data register
SPDR = txbyte;
while(!(SPSR & _BV(SPIF)))
{
/* wait for data transmission to complete */
}
SPSR &= ~_BV(SPIF);
// Return RX byte by storing it at the specified location
if(rxbyte != NULL)
{
*rxbyte = SPDR;
}
return;
}
/* Ioctl handlers (verify that all arguments are appropriate and only then proceed with the ioctl) */
int spiModule::conf(int clkDiv, int dataOrder, int mode) {
// Return with error of SPI is not configured
if(!configured)
{
return -1;
}
// Verify validity of clkDiv (clock pulse division factor)
switch(clkDiv)
{
case SCK_DIV2:
break;
case SCK_DIV4:
break;
case SCK_DIV8:
break;
case SCK_DIV16:
break;
case SCK_DIV32:
break;
case SCK_DIV64:
break;
case SCK_DIV128:
break;
default:
return -1;
}
// Verify validity of dataOrder (order of byte transfer)
switch(dataOrder)
{
case LSBFIRST:
break;
case MSBFIRST:
break;
default:
return -1;
}
// Check validity of mode (SPI mode)
switch(mode)
{
case SPI_MODE0:
break;
case SPI_MODE1:
break;
case SPI_MODE2:
break;
case SPI_MODE3:
break;
default:
return -1;
}
// If all goes well, execute the ioctl
spiiocconf(clkDiv, dataOrder, mode);
return 0;
}
int spiModule::deconf(void) {
// If SPI is configured, deconfigure it
if(!configured)
{
return -1;
}
spiiocdeconf();
return 0;
}
int spiModule::transmit(uint8_t tx, uint8_t * rx) {
// If SPI is configured, make a byte exchange
if(!configured)
{
return -1;
}
spiioctransmit(tx, rx);
return 0;
}
// Public routines ///////////////////////////////////////
int spiModule::ioctl(int action, ... ) {
// This routine checks the value of action and executes the respective ioctl
// It returns with error if the value of action is not valid
va_list ap;
int clkDiv, dataOrder, mode;
uint8_t txbyte;
uint8_t * rxbyte;
int retVal;
switch(action)
{
case SPIIOCCONF:
va_start(ap, action);
clkDiv = va_arg(ap, int);
dataOrder = va_arg(ap, int);
mode = va_arg(ap, int);
va_end(ap);
retVal = conf(clkDiv, dataOrder, mode);
return retVal;
case SPIIOCDECONF:
retVal = deconf();
return retVal;
case SPIIOCTRANSMIT:
va_start(ap, action);
txbyte = va_arg(ap, uint8_t);
rxbyte = va_arg(ap, uint8_t*);
va_end(ap);
retVal = transmit(txbyte, rxbyte);
return retVal;
default:
return -1;
}
}
#endif
I'm compiling and uploading my code to the Arduino using the following commands (spiTest.cpp is a code that I used to test this library)
COMPILER=~/Softwares/arduino-1.6.8/hardware/tools/avr/bin/avr-g++
HEXGENERATOR=~/Softwares/arduino-1.6.8/hardware/tools/avr/bin/avr-objcopy
UPLOADER=~/Softwares/arduino-1.6.8/hardware/tools/avr/bin/avrdude
AVRDUDE_CFG=~/Softwares/arduino-1.6.8/hardware/tools/avr/etc/avrdude.conf
$COMPILER -c -g -w -D F_CPU=16000000UL -mmcu=atmega328p -std=gnu++11 -o spiTest.o spiTest.cpp
$COMPILER -mmcu=atmega328p spiTest.o -o spiTest
$HEXGENERATOR -O ihex -R .eeprom spiTest spiTest.hex
$UPLOADER -C $AVRDUDE_CFG -v -p atmega328p -c arduino -P /dev/ttyACM0 -b 115200 -D -U flash:w:spiTest.hex:i
I've used variadic functions to implement ioctl() before and it worked when I used the Arduino IDE to compile and upload my program. I don't understand what's preventing variadic functions from working properly in this code.
You are in the C++, you can do it in better. For example something like:
class SPIIOCONF_t {} SPIIOCONF;
class SPIIOCDECONF_t {} SPIIOCDECONF;
class SPIIOCTRANSMIT_t {} SPIIOCTRANSMIT;
int ioctl(SPIIOCONF_t, int clkDiv, int dataOrder, int mode) {
return conf(clkDiv, dataOrder, mode);
}
int ioctl(SPIIOCDECONF_t) {
return deconf();
}
int ioctl(SPIIOCTRANSMIT_t, uint8_t txbyte, uint8_t rxbyte) {
return transmit(txbyte, rxbyte);
}
void setup() {
Serial.begin(115200);
Serial.println(ioctl(SPIIOCONF, 10, 1, 1));
Serial.println(ioctl(SPIIOCDECONF));
uint8_t rx;
Serial.println(ioctl(SPIIOCTRANSMIT, 0xFF, &rx));
}
or similarly without defining dummy class instance, but you have to create one in the call:
class SPIIOCONF {};
class SPIIOCDECONF {};
class SPIIOCTRANSMIT {};
int ioctl(SPIIOCONF, int clkDiv, int dataOrder, int mode) {
return conf(clkDiv, dataOrder, mode);
}
int ioctl(SPIIOCDECONF) {
return deconf();
}
int ioctl(SPIIOCTRANSMIT, uint8_t txbyte, uint8_t rxbyte) {
return transmit(txbyte, rxbyte);
}
void setup() {
ioctl(SPIIOCONF{}, 10, 1, 1);
uint8_t rx;
Serial.println(ioctl(SPIIOCTRANSMIT{}, 0xFF, &rx));
ioctl(SPIIOCDECONF{});
}

Resources