Sorry if my post is not well formatted'm new in here.
/*
SimpleSend
This sketch transmits a short text message using the VirtualWire library
connect the Transmitter data pin to Arduino pin 12
*/
#include <VirtualWire.h>
String Mensagem = "eureca"; //I want to send this string
void setup(){
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
}
void loop(){
send(Mensagem); //Putting a string inside the function does not work,
//I want to send a String message inside that function like a parameter
delay(1000);
}
void send (char *message){
vw_send((uint8_t *)message, strlen(message));
vw_wait_tx(); // Wait until the whole message is gone
}
char *message means a string literal or a character pointer, but what you're passing into the function is a String.
You can fix it by using:
char Mensagem[]= "eureca";
in order to match the type. It's because Mensagem now is a static pointer of that character array.
Related
I am using Arduino UNO and a PN532 NFC module to receive P2P NDEF messages from an Android phone.
I am sending a plaintext string "Hello". When the transfer is successful, I get this on my Arduino:
image
How can I extract the string "Hello" (I think the pairs of numbers before it is the "Hello" in hexadecimal, same for the "text/plain" type indication, type length and payload length) from the NDEF message payload into a regular variable?
Here is my Arduino code:
// Receive a NDEF message from a Peer
// Requires SPI. Tested with Seeed Studio NFC Shield v2
#include "SPI.h"
#include "PN532_SPI.h"
#include "snep.h"
#include "NdefMessage.h"
PN532_SPI pn532spi(SPI, 10);
SNEP nfc(pn532spi);
uint8_t ndefBuf[128];
void setup() {
Serial.begin(9600);
Serial.println("NFC Peer to Peer Example - Receive Message");
}
void loop() {
Serial.println("Waiting for message from Peer");
//string payload = nfc.read();
int msgSize = nfc.read(ndefBuf, sizeof(ndefBuf));
NdefMessage msg = NdefMessage(ndefBuf, msgSize);
msg.print();
}
A NdefMessage is made up of multiple NdefRecord items and you msg has 1 record
so this should do it (not tested)
// Get the first record
NdefRecord record = msg.getRecord(0);
// Get the payload size
byte length = record.getPayloadLength();
// Create byte array big enough for payload
byte payload[length];
// Get payload to byte array
record.getPayload(payload);
// Convert byte Array to string
String string = String((char *)payload);
I connected my Bluetooth module to the mobile phone and made up a code to communicate between Arduino and mobile through Bluetooth (send messages from Bluetooth module to device and vice versa).
Now I want to make a response, which means that if I send from the mobile "hi" the arduino replies and says "Hello" or whatever.
I have tried tons of codes but none worked, so would anyone please help me?
#include <SoftwareSerial.h>
SoftwareSerial myserial (6,5);
void setup() {
myserial.begin(9600);
Serial.begin (9600);
}
void loop() {
if (myserial.available()) {
Serial.write(+ myserial.read());
}
if (Serial.available()) {
myserial.write(Serial.read());
}
}
Another code but making a loop without sending anything
#include <SoftwareSerial.h>
SoftwareSerial myserial(6,5); //Arduino: R:5,T:6; bluetooth: T:5, R:6;
void setup() {
myserial.begin(9600);
Serial.begin (9600);
}
void loop() {
if (myserial.available()) {
Serial.write(myserial.read());
}
if (Serial.available()) {
myserial.write(Serial.read());
}
for (int i = 0; i=2; i++) {
myserial.write("hello");
}
if (myserial.read() =="n") {
myserial.write("hello");
}
}
You need to build your serial string char by char using the SerialEvent() interrupt, then do a String comparison using the .equals method. Despite many people will tell you that String types are 'evil' (see this and this) it might be a good solution for making things clearer (and perhaps a bit easier if you don't want to mess with C char strcmp() functions and pointers. In the end, the String type is there for you and I see no reason for not using it in general projects.
Based on the SerialEvent() documentation [1], and the String reference [2], you could do something like:
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
//Then you compare the inputString with the word you want to detect using the .equals method from the String class
if(inputString.equals("Hi"){
Serial.println("Hello");
}
// clear the string for a new comparison:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
EDIT 1: Of course, you can replace the Serial object from this example code with your own myserial object from the BlueTooth communication.
On my NodeMCU V3 I get a string, convert it to two longs, one int and two bytes, save them in a union and send the whole union to an Arduino Mega, which is supposed to read it in an equal union.
When the NodeMCU prints the bytes to the serial monitor of my laptop it shows their exact value, but after sending them I just get two 0s. Still I think it has something to do with the conversion of the bytes but can't figure out what.
I tried:
Using atol instead of strtol for converting the string, but discarded that
after reading about the
disadvantages. atol() v/s. strtol()
It didn't work anyway.
I tried to convert the bytes with lowByte((int) int1).
Here is the relevant part of my code. I made up a string, instead of receiving one for test purposes.
#include <ESP8266WiFi.h>
union Serial { //Union to place the numbers in.
byte by[12];
struct {
long vsollneu;
long ssollneu;
int phisollneu;
byte priosollneu;
byte typsollneu;
} Befehlssammlung;
} Befehle;
long a1,pr1,ty1;
char str[]="2049714 -1927461000 17 80 4"; //String to be seperated
void convert(char*);
void setup(){
Serial.begin(9600);
}
void loop(){
convert(str);
//Serial.write(Befehle.by,12); //Send the Bytes of the Union to the Arduino MEGA
delay(5555);
}
void convert(char* str){ //Converting the String with strtol
char* ende;
Befehle.Befehlssammlung.vsollneu =strtol(str,&ende,10);
Befehle.Befehlssammlung.ssollneu =strtol(ende,&ende,10);
a1 = strtol(ende,&ende,10);
pr1= strtol(ende,&ende,10);
ty1= strtol(ende,NULL,10);
Befehle.Befehlssammlung.phisollneu=(int) a1; //Converting the long to an int
Befehle.Befehlssammlung.priosollneu=(byte) pr1; //Probably that's somehow wrong???
Befehle.Befehlssammlung.typsollneu=(byte) ty1;
// Serial.println(Befehle.Befehlssammlung.vsollneu);
// Serial.println(Befehle.Befehlssammlung.ssollneu);
// Serial.println(Befehle.Befehlssammlung.phisollneu);
// Serial.println(Befehle.Befehlssammlung.priosollneu);
// Serial.println(Befehle.Befehlssammlung.typsollneu);
}
Here is the receiving part of the Arduino Mega:
union IZweiCkontainer {
byte by[12];
struct {
long vsollneu;
long ssollneu ;
int phisollneu;
byte priosollneu;
byte typsollneu;
} Befehlssammlung;
} Befehle;
void setup(){
Serial.begin(115200); //Serial for debugging
Serial3.begin(9600); //Serial for conncetion the the Mcu
}
void loop(){
if(Serial3.available()>0){
while(Serial3.available()){
Serial3.readBytes(Befehle.by,12); //receiving the Bytes and writing them in the "same" union
}
}
Serial.println(Befehle.Befehlssammlung.vsollneu);
Serial.println(Befehle.Befehlssammlung.ssollneu);
Serial.println(Befehle.Befehlssammlung.phisollneu);
Serial.println(Befehle.Befehlssammlung.priosollneu);
Serial.println(Befehle.Befehlssammlung.typsollneu);
}
What bucks me is that everything is fine on the NodeMCU but after sending I get the following output from the Arduino Mega:
2049714
-1927461000
17
0
0
ints and longs on an Arduino and ESP8266 are not the same size.
Use int16_t and int32_t to make sure they are the same size across different architectures.
union {
byte by[12];
struct {
int32_t vsollneu;
int32_t ssollneu;
int16_t phisollneu;
byte priosollneu;
byte typsollneu;
} Befehlssammlung;
} Befehle;
I have the following example code of a Arduino Slave i2C. Taken from https://www.arduino.cc/en/Tutorial/MasterWriter
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
In the method void receiveEvent(int howMany), what exactly is the functionality of the parameter int howMany?
The function receiveEvent is provided as an event handler to Wire.onReceive().
So, from the documentation of Wire.onReceive
handler: the function to be called when the slave receives data; this should take a single int parameter (the number of bytes read from the master) and return nothing, e.g.: void myHandler(int numBytes)
It contains the amount of bytes of the received data.
Hi i am trying to communicate my 89c52 with sim548c module. I am sending AT commands and then making the microcontroller store all replies in an array and go through a search function to see if proper reply was sent so it can move on to next AT command. This requires two way serial transfer. i have to first send serially the AT command, then enable reception and store all replies from the module in an array. I am using this program but i cant get the microcontroller to accept the incoming data and store it in an array. it transfers successfully but doesnt receive. Can you kindly identify what is the problem?
int check=0;
int out=0;
unsigned char info[20]={"00000000000000000000"};
unsigned char *s;
unsigned char a[3],b[3];
void transmit_data(unsigned char str)
{
SBUF=str;
while(TI==0);
TI=0;
}
void send_serial(unsigned char *s)
{
delay(50);
while(*s!=0x0)
{
SBUF=*s;
while(TI==0)
{
}
TI=0;
s++;
}
}
void receive_data() interrupt 4
{
if(RI)
{
info[check++]=SBUF;
RI=0;
}
if(TI)
TI=0;
}
void search(unsigned char b[])
{
int l=0;
for(l;l<18;l++)
{
if(info[l]==b[0] && info[l+1]==b[1] && info[l+2]==b[2])
{
out=1;
break;
}
}
}
void compare(unsigned char *s, unsigned char a[]) //for CIPSEND
{
while(1)
{
out=0;
check=0;
delay(50);
send_serial("AT+CIPSEND\r");
delay(100);
send_serial(s);
transmit_data(0x0D);
transmit_data(0x0A);
transmit_data(0x1A);
IE=0x90;
delay(200);
IE=0x88;
search(a);
if (out==1)
break;
}
}
i have seen this a couple of times and th mistake is that your serial receive works with an interrupt and it is not a voidable function smply remove the VOID that is attached to INTERRUPT 4 so tha ur code becomes
receive_data() interrupt 4
{
if(RI)
{
info[check++]=SBUF;
RI=0;
}
if(TI)
TI=0;
}
I would suggest making a smaller (as simple as you can) program that does nothing more than receive (by interrupt?) data and blink a LED or echo it back or in some other way indicate that you can reliably receive. Use that to talk to a terminal emulator or another known working interface. Cut out all possible middlemen and unknowns.
Check also UART error registers and configuration to make sure your clock/parity/data settings match in both ends. Start with a slow rate first.
Make one piece at a time work reliably, then put them together.