I'm trying to get an ESP8266 to work with Arduino by using PlatformIO. But, I get errors when importing SoftwareSerial.h
Tried: Arduino IDE, PlatformIO, Change baudrate, change port
How can I get SoftwareSerial to work with the ESP8266?
#include <SoftwareSerial.h>
SoftwareSerial BTserial(3, 1); // RX | TX
char Bluetooth_Name = ' ';
void setup()
{
// Arduino IDE serial monitor
Serial.begin(9600);
// HC-05 default serial speed for AT mode is 38400
BTserial.begin(38400);
// Wait for hardware to initialize
delay(1000);
// Print debug string
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTserial.available())
{
reading = BTserial.read();
Serial.println(reading);
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
{
reading = Serial.read();
BTserial.write(reading);
}
}
I want to use the SoftwareSerial without errors.
Error code:
Compiling .pio\build\huzzah\lib0be\EspSoftwareSerial_ID168\SoftwareSerial.cpp.o
In file included from C:\Users\Bart\.platformio\lib\EspSoftwareSerial_ID168\src/SoftwareSerial.h:27:0,
from C:\Users\Bart\.platformio\lib\EspSoftwareSerial_ID168\src\SoftwareSerial.cpp:23:
C:\Users\Bart\.platformio\lib\EspSoftwareSerial_ID168\src/circular_queue/circular_queue.h:144:10: error: expected ';' at end of member declaration
bool IRAM_ATTR push(T&& val);
^
Actually the standard software serial library didn't work for me with my NodeMCU v1.0... And in the rare cases when it worked, it was very limited. Maybe check out this library:
ESP 8266/32 Software Serial Library
in platformio.ini you can add the following line to choose a specific version which compiles with the latest released 8266 platform
lib_deps_external =
plerup/espsoftwareserial#5.0.3 ; this version compiles with a standard 8266 platform
Using this way the code is directly fetched from github.
Related
I am currently trying to send my sensor data from my Arduino to an android app made on android studio using an HC05 module for Arduino.
I tried to configure the HC05 as every tutorial on the internet says, but i meet some problems.
I am using the arduino code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int PotPin = A7;
int Vdata = 15;
void setup() {
Serial.begin(9600);
pinMode(9,OUTPUT); digitalWrite(9,HIGH);
Serial.println("Enter AT commands:");
mySerial.begin(38400);
}
void loop()
{
Vdata = analogRead(PotPin);
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}
When I type "AT" in the Serial monitor, it returns me "OK" (that is normal).
But when I try to see the name/the address/the password of the module, it returns me "Error:(0)". The strangest thing is that the command " AT+NAME="NameWanted" " or even " "AT+PWD="4321" " works since it correctly changes the name of the module.
I looked on the internet but I didn't see someone with the same problem as mine, I hope someone will lead me to the solution!
Thanks
I found out what's the problem. My mcu's bauderate was bigger (115200), then my hc05's bauderate (38400). With this bauderate, my MCU sent the messages faster, then hc05 could read it. So, I decreased the bauderate of my MCU and now it's working
The Problem:
Hi, First of all, i am no expert at C++.
I seem can't find any of the guide(or documentation) of modbusMaster especially for this particular library. Which explains all the class method uses.
I use this library because it supports stream class. not like official library or others. Therefore, i can use softwareSerial to add more serial for rs485.
I am using Arduino pro mini, that's why i need software serial. And this Max485 module:
I am very sure everything is connected properly, thus my aim is to read coil from a Chinese brand ultrasonic level meter which the guide says:
Here's the program what i am trying to do:
#include <ModbusMaster.h>
#include <SoftwareSerial.h>
/*!
We're using a MAX485-compatible RS485 Transceiver.
Rx/Tx is hooked up to the hardware serial port at 'Serial'.
The Data Enable and Receiver Enable pins are hooked up as follows:
*/
#define MAX485_DE 9
#define MAX485_RE_NEG 8
//for software serial
#define RO_RX 7
#define DI_TX 6
// instantiate ModbusMaster object
ModbusMaster node;
// RX, TX on softser
SoftwareSerial mySerial(RO_RX, DI_TX);
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
void setup()
{
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
// for serial monitoring on PC
Serial.begin(115200);
// Modbus communication runs at 9600 baud
mySerial.begin(9600);
// Modbus slave ID 1 and pass the software serial
node.begin(1, mySerial);
// Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
Serial.println("init done...");
}
void loop()
{
// now, i'm kinda lost here..
delay(100);
Serial.println("loop end.");
}
I am confused with most of the class method without a proper explanation.
modbusObj.readWriteMultipleRegisters();
modbusObj.writeSingleRegister();
modbusObj.setTransmitBuffer();
modbusObj.readCoils();
modbusObj.writeMultipleRegisters();
modbusObj.readHoldingRegisters();
// etc..
They had no documentation. Well, Some method names are self explanatory but not about the arguments.I am trying just to read the holding register.
TL;DR:
I want to read the level meter's distance data, but the library doesn't provide a proper documentation.
Nb:
nothing wrong with the ultrasonic device, tested in PC to read holding register with some modbus tool and hooked up to rs485 to usb and working!
this question intended in stack overflow rather than stack-electrical-engineering because I am mainly asking about the code.
I've been discussing it with someone in a local facebook forum that said he's successfully utilizing the lib alongside soft-serial and pro-mini.
I am sending data from Arduino UNO R3 to nodeMcu esp8266. In that case sometime data are send properly but at sometime data are are not send by arduino or not get by nodemcu esp8266.Also tx light not blinks after i upload the code to the Arduiono.
enter code here
Code Uploaded to Arduino:
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
SoftwareSerial s(10,11);
SoftwareSerial h(10,11);
int i=0;
void setup() {
// put your setup code here, to run once:
s.begin(9600);
Serial.begin(9600);
}
int f1=0;
int f2=0;
String st="sy";
void loop() {
f1=f1+1;
f2=f2+2;
// put your main code here, to run repeatedly:
StaticJsonBuffer <1000> bf;
StaticJsonBuffer <1000> rec;
JsonObject& root=bf.createObject();
JsonObject& receives=rec.parseObject(h);
if (receives==JsonObject::invalid())
{
Serial.println("no data from nodemcu");
root["data3"]="no data from nide";
}
else
{
root["data3"] = receives["data3"];
//st = (const char*)receive["data3"];
}
root["data1"]=f1;
root["data2"]=f2;
//root["data3"]=st;
if(s.available()>0)
{
root.printTo(s);
Serial.println("send");
}
else
{
Serial.println("NOt Available");}
//i=i+1;
//s.write(i);
delay(1000);
}
In your code thera are two reasons I can spot on the first look:
1 Never use delay in server client applications. Delay stops proccessing and inhibts communication. For more details how to avoid look at the blinkwithoutdelay built in Example in the Arduino IDE.
2. You use ArduinoJSON: This lib is absolute overkill for 99% of the applications it is used for. Don't get me wrong - the lib offers fantastic features, but if not needed this can be regarded as bloat-ware.
I wrote my own simple JSON coding/decoding (~250 lines of code) for a performant application using an Arduino as "Signal generator" and the ESP8266/ESP32 as a webserver. Communication between Arduino and ESPis time critical and so far it works over a year without problems,
When I try to use rosserial to communicate with an Arduino to control the MX-64 motor, every time I use rostopic pub to send a message, it will give me this error:
"Mismatched protocol version in packet: lost sync or rosserial_python is from different ros release than the rosserial client "
If I remove the Dynamixel.move(X_SERVO_ID,rollint); function in the code or don't send any information though ROS, there will be no error. I tried all the rosserial examples, all of them are working just fine without any error.
The weird thing is even if it gives me an error, the program is still working. Every time I send a message, the DYNAMIXEL motor will start moving.
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <ros.h>
#include <std_msgs/Int8.h>
ros::NodeHandle nh;
#include <Dynamixel_Serial.h> // Library needed to control Dynamixal servo
#include <sensor_msgs/Imu.h>
#define X_SERVO_ID 0x01 // ID which we will be set in Dynamixel too
int rollint = 0;
void motor_cb(const std_msgs::Int8& cmd_msg) {
rollint = ((1.00*(90-cmd_msg.data))/360)*4095;
Dynamixel.move(X_SERVO_ID,rollint);
}
ros::Subscriber<std_msgs::Int8> sub("MX_Motor", motor_cb);
void setup() {
delay(100); // Give time for Dynamixel to start on power-up
nh.initNode();
nh.subscribe(sub);
}
void loop(){
nh.spinOnce();
delay(1);
}
It looks like your Dynamixel servo is hooked up to the same serial port that you use to communicate with the rosserial server. In many Arduino boards like the Uno the hardware serial port (where your Dynamixel is hooked up) is connected to a USB serial converter (which connects ROS via USB).
You need to connect your Dynamixel to a different hardware (or software) serial on your Arduino and initialize it with Dynamixel.begin(Stream&) (Dynamixel_Serial.h#L162), where Stream& is that hardware / software serial.
have really been suffering with my simulation! Tried connecting my arduino mega 2560 to COMPIM (used as GSM Module) in my circuit and also loaded sample code but damn! In vain! I wanted to view the data sent in virtual terminal but am getting nothing! and its also complaining of excessive CPU usage.
And thereafter, the messages are displayed as shown below.
Someone please help me out. Am I making a mistake oh? Infact the arduino code for displaying in the simulation is also giving me hard time. The one am using is as shown below.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello, world?");
}
void loop() // run over and over
{
if (mySerial.available())
mySerial.println("Hello, world?");
if (Serial.available())
mySerial.println("Hello, world?");
}
Will b every grateful for any help offered.