Arduino Serial.read doesn't work - arduino

My Arduino Mega (with CH340 but all the drivers needed installed) has stopped reading from the serial port. Doesn´t matter what I write it keeps saving number 50. I have tried with simple examples codes as this one:
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
And I get the same response.
The arduino has been working fine untill today. Serial.print() works also fine, it's just Serial.read() Anyone knows what the problem could be?? I haven't found much help on the internet, seems I am the first one dealing with this issue.

Okay, so I found what my problem was. Don't know if it was Serial.read or Serial println / Serial.write, some of these functions were converting from integers to chars, so numbers appeared to be only between 49 and 58. Using Serial.print instead it works perfectly. Thanks for the answers by the way.

Related

Arduino - Garbled serial output even at 9600 baud

I am doing a project that requires the Arduino to read data from a potentiometer. I was trying to get the potentiometer working so I used this code, which should read the potentiometer and then output it to the serial monitor.
int val = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
val = analogRead(A1);
Serial.println("Head " + val);
}
I used the code from https://www.arduino.cc/en/tutorial/potentiometer and changed the code from blinking an LED to outputting the value to Serial where I Am monitoring it using the Arduino IDE's Serial monitor tool and all I get is this
⸮⸮>W07⸮⸮7M⸮⸮⸮[+⸮⸮⸮⸮⸮⸮z⸮R⸮⸮⸮a~⸮⸮yo6|⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮߉⸮[⸮}⸮⸮⸮⸮⸮7⸮%
G2⸮{⸮⸮⸮⸮⸮ݪ}⸮⸮⸮>⸮⸮{⸮K⸮ݻ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮?⸮⸮⸮:w⸮⸮⸮⸮6⸮⸮⸮⸮O⸮⸮⸮⸮?w⸮⸮07⸮⸮7M⸮⸮⸮[+⸮⸮⸮}<⸮z⸮R⸮⸮⸮a~⸮⸮yo6|⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮߉⸮[⸮}⸮⸮⸮⸮⸮7⸮%
G2⸮{⸮⸮⸮⸮⸮ݪ}⸮⸮⸮>⸮⸮{⸮K⸮ݻ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮?⸮⸮⸮:w⸮⸮⸮⸮6⸮⸮⸮⸮O⸮⸮⸮⸮?w⸮⸮07⸮⸮7M⸮⸮⸮[+⸮⸮⸮}<⸮⸮m⸮M⸮⸮jo⸮⸮⸮⸮⸮.⸮⸮⸮⸮d⸮⸮o⸮⸮⸮⸮⸮.|⸮}⸮⸮⸮啺7|g⸮⸮⸮⸮~⸮⸮⸮:⸮N⸮?⸮{⸮n.o⸮⸮e6⸮?l⸮⸮h⸮⸮⸮⸮ϻ⸮⸮⸮⸮ԟy⸮⸮⸮o7s7⸮?u⸮⸮⸮?⸮⸮⸮⸮߰⸮⸮⸮⸮⸮~߹h⸮⸮⸮ӿw⸮⸮g⸮⸮⸮T⸮z⸮⸮|⸮t⸮⸮⸮^⸮-⸮o⸮/=⸮ݩ⸮i⸮H⸮⸮ם⸮E⸮⸮ݛ⸮筷?⸮⸮⸮⸮⸮G}⸮⸮U⸮6⸮⸮ۮ⸮2׳⸮⸮⸮⸮G}⸮⸮⸮⸮⸮⸮⸮=⸮j⸮k⸮⸮>⸮2X⸮⸮J⸮⸮⸮{⸮⸮⸮⸮⸮n⸮⸮⸮⸮i{⸮>⸮}⸮⸮{⸮⸮⸮⸮⸮⸮j⸮⸮⸮;⸮⸮un⸮⸮⸮⸮⸮⸮⸮_7⸮⸮⸮⸮y⸮/⸮/⸮⸮⸮u⸮yvN⸮F⸮շy⸮⸮⸮g⸮⸮n⸮o⸮⸮⸮⸮l⸮7⸮nw⸮⸮/⸮⸮⸮⸮⸮⸮⸮⸮{,⸮⸮⸮|⸮⸮⸮⸮⸮eſ
⸮⸮⸮⸮⸮j⸮⸮}⸮⸮⸮z⸮]٫ϛ⸮⸮⸮⸮⸮⸮⸮ݶ⸮⸮ܟ⸮⸮⸮=c⸮o⸮g⸮l^⸮⸮ƭߛ⸮?⸮[9w⸮\[⸮⸮y⸮⸮⸮⸮⸮3⸮yѷ⸮⸮⸮g⸮}⸮⸮⸮⸮⸮⸮v⸮⸮⸮]>_⸮⸮⸮ױs⸮'~⸮i;W⸮⸮⸮⸮1ֽܢ⸮⸮⸮'⸮⸮t⸮u⸮⸮]⸮_ڙͿo⸮y⸮W⸮ٗ⸮K⸮⸮_⸮⸮⸮⸮:⸮|M⸮⸮⸮⸮G⸮⸮⸮⸮⸮N⸮⸮⸮⸮⸮⸮⸮<⸮⸮⸮w^⸮⸮⸮⸮⸮⸮e⸮⸮⸮"⸮⸮Xg⸮N⸮n⸮⸮⸮⸮⸮߷⸮⸮⸮⸮⸮⸮⸮⸮c⸮⸮~⸮⸮⸮⸮⸮⸮.⸮>⸮⸮⸮⸮⸮⸮⸮n⸮⸮?7?_⸮⸮⸮'>⸮⸮k|ۉ.⸮췅⸮T⸮ϝ⸮=⸮⸮⸮⸮⸮⸮׿}⸮⸮⸮Y⸮⸮Z⸮⸮⸮⸮⸮C⸮⸮9⸮v⸮⸮~>⸮⸮⸮Z⸮z?⸮⸮⸮⸮⸮⸮^⸮Ӿ⸮Md⸮⸮⸮⸮⸮MZK⸮j⸮⸮M⸮׳⸮⸮⸮⸮⸮⸮⸮]/Wꣷ⸮?⸮;o⸮u⸮⸮9⸮⸮⸮Y˧⸮{⸮W⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮ݮYn⸮ο⸮⸮⸮⸮⸮;⸮⸮⸮7{}⸮v⸮⸮⸮⸮⸮⸮⸮v⸮⸮7⸮⸮⸮}O+Y+⸮^⸮⸮⸮⸮⸮⸮q⸮⸮z^\⸮⸮~⸮⸮⸮⸮⸮~~⸮⸮⸮⸮⸮⸮o⸮⸮⸮⸮⸮t⸮⸮⸮⸮⸮⸮⸮L⸮⸮⸮⸮⸮⸮⸮+=⸮⸮⸮C⸮⸮Ƭ⸮⸮⸮⸮⸮⸮⸮W⸮⸮⸮/⸮⸮⸮5⸮~{{⸮|.⸮;P⸮⸮p⸮⸮⸮⸮⸮⸮⸮oZ⸮⸮⸮⸮⸮⸮⸮⸮ߝ⸮⸮⸮⸮⸮⸮⸮⸮g⸮⸮⸮⸮⸮W⸮Wj⸮oY⸮;⸮⸮/?⸮⸮~⸮6⸮⸮m⸮⸮~⸮⸮⸮k⸮_O⸮߃⸮b⸮m⸮⸮kӾ?⸮l⸮⸮⸮⸮⸮⸮⸮⸮X׬>{a⸮⸮[k#⸮/⸮~ݍ⸮⸮⸮⸮_⸮⸮O_⸮9⸮⸮⸮⸮t⸮⸮g⸮⸮⸮⸮*⸮1⸮⸮⸮⸮⸮wߝ;⸮z⸮⸮⸮⸮⸮?⸮2⸮⸮⸮⸮O⸮c⸮⸮䝿⸮⸮⸮_Ŭ⸮⸮׹զޫ:⸮⸮旚⸮⸮r⸮⸮oo⸮⸮7⸮⸮⸮O,⸮G~⸮⸮.5⸮⸮⸮,⸮w⸮o⸮⸮⸮A⸮⸮⸮⸮⸮⸮uo⸮⸮⸮?o븨⸮⸮yp⸮~W⸮o⸮⸮⸮iH2⸮⸮8⸮⸮w⸮{⸮⸮C⸮F;⸮⸮!⸮?⸮⸮;⸮⸮⸮⸮⸮⸮⸮m٫⸮?o⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮ϻ⸮o.⸮⸮⸮⸮c⸮⸮⸮⸮⸮ߴ⸮q"S{⸮c⸮ٻ⸮⸮⸮H⸮~⸮⸮[⸮⸮~⸮⸮⸮}⸮w4⸮⸮⸮⸮⸮⸮⸮⸮%⸮⸮⸮⸮⸮t쟕v⸮⸮>⸮⸮⸮⸮⸮⸮oY⸮⸮⸮⸮⸮⸮~c⸮⸮a⸮⸮⸮^q⸮⸮⸮⸮⸮׍⸮⸮⸮ޟәw_⸮⸮O⸮⸮e⸮⸮⸮V⸮⸮⸮⸮⸮o⸮⸮uy⸮8⸮⸮gޟ⸮_⸮⸮w⸮⸮⸮⸮.
⸮⸮⸮⸮⸮⸮⸮r⸮R⸮?ݍ⸮⸮⸮͖⸮⸮⸮⸮:;_⸮⸮{⸮^⸮m⸮⸮E_*⸮⸮⸮[⸮⸮\wU⸮g.}⸮ۻ/?⸮⸮}j⸮⸮⸮w⸮⸮⸮⸮⸮⸮⸮⸮⸮·⸮y{>⸮⸮#}⸮⸮⸮⸮U⸮⸮⸮⸮⸮|o⸮Q⸮\⸮⸮'⸮ ⸮⸮⸮⸮iZo_⸮⸮⸮⸮⸮8⸮⸮⸮⸮⸮=⸮⸮}zݽ⸮Q⸮w⸮⸮⸮^⸮⸮t⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮|'⸮⸮T⸮⸮⸮⸮ߩ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮ ?⸮3⸮⸮⸮⸮s}⸮⸮⸮⸮⸮߽⸮v⸮⸮⸮⸮⸮"⸮⸮>⸮⸮N?⸮ד⸮⸮_W⸮o⸮⎼⸮w⸮G|;⸮V⸮⸮⸮⸮⸮{Jg⸮⸮⸮~⸮⸮⸮⸮⸮W⸮3⸮⸮?⸮݌⸮s⸮ew⸮{⸮⸮!⸮⸮g⸮⸮⸮⸮⸮⸮⸮K⸮⸮_⸮⸮⸮⸮V7⸮j[mFԿA⸮q⸮⸮⸮lc⸮⸮⸮.⸮⸮N_ۿ⸮⸮⸮⸮fs}⸮{⸮⸮]⸮Ѷݧ⸮⸮⸮훽⸮⸮n⸮7⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮u⸮⸮⸮=⸮⸮r⸮;⸮~⸮⸮⸮⸮=⸮⸮{uߥܽ⸮⸮⸮⸮9⸮⸮⸮.ӷ⸮⸮⸮޹⸮7⸮⸮S⸮;⸮⸮⸮⸮⸮vy⸮⸮e⸮⸮⸮⸮⸮⸮s⸮n⸮⸮_<⸮⸮5Q⸮⸮⸮⸮=⸮{{⸮⸮N⸮⸮⸮sߛ⸮⸮⸮⸮⸮v⸮⸮⸮N⸮⸮#z⸮m⸮⸮⸮Q⸮⸮⸮Wq⸮⸮g⸮⸮O⸮⸮~⸮⸮⸮⸮m⸮⸮⸮⸮>W⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮p⸮⸮⸮⸮꿿⸮⸮⸮⸮^⸮⸮⸮⸮⸮E[:}⸮⸮⸮o_⸮⸮⸮⸮~⸮⸮?⸮⸮⸮⸮6⸮⸮⸮⸮⸮V⸮z⸮⸮?⸮5⸮~O⸮⸮=⸮⸮⸮⸮f⸮⸮⸮7⸮⸮⸮⸮Z]O⸮⸮ֻ⸮⸮⸮s⸮g[g⸮⸮⸮⸮?sN⸮x⸮⸮⸮⸮⸮0>⸮⸮⸮⸮⸮⸮⸮⸮_⸮⸮⸮e
v?⸮⸮j⸮o⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮߅⸮[⸮⸮t⸮3⸮⸮y⸮⸮8~⸮⸮J-4⸮v⸮?⸮⸮ֺ[⸮⸮⸮⸮N%{⸮⸮⸮⸮]⸮wxY/⸮vw⸮⸮⸮.⸮⸮e]⸮⸮{⸮⸮⸮w⸮⸮⸮⸮⸮e⸮9⸮n⸮>⸮Y!⸮⸮⸮s]⸮⸮⸮7t%⸮⸮ǿ⸮⸮⸮⸮⸮_⸮ӷ⸮⸮⸮⸮MC7⸮⸮N⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮u⸮u⸮χu⸮⸮⸮uT}⸮⸮⸮w⸮⸮⸮⸮⸮ߗ⸮o⸮⸮⸮LG⸮^^M⸮⸮n_⸮⸮t~q⸮⸮⸮~U~~`⸮l⸮-߷⸮l⸮⸮K⸮G⸮⸮⸮⸮⸮U⸮⸮⸮⸮⸮⸮ҟ='⸮⸮⸮}Ç
⸮7uO⸮⸮⸮⸮o⸮⸮⸮U_⸮⸮⸮⸮⸮;⸮⸮⸮߼_⸮⸮⸮⸮މ⸮⸮⸮y⸮{⸮W>m_⸮s⸮⸮⸮⸮-ߕȺ⸮'oN_U⸮⸮Ϳ⸮/}t⸮mo~⸮⸮sz⸮⸮⸮⸮⸮⸮~⸮_⸮⸮⸮⸮ù޿⸮&⸮⸮⸮?⸮s⸮y⸮⸮⸮7⸮⸮⸮⸮⸮⸮}⸮⸮⸮⸮⸮w⸮⸮⸮_⸮⸮⸮⸮⸮⸮⸮z⸮⸮⸮⸮L⸮⸮⸮⸮⸮⸮{⸮⸮go⸮z⸮⸮⸮⸮⸮⸮o⸮⸮⸮~⸮i⸮⸮7⸮⸮~⸮⸮M⸮⸮⸮⸮⸮⸮⸮⸮⸮v⸮⸮y⸮⸮⸮⸮޿⸮⸮⸮⸮2ߏ⸮⸮⸮⸮W⸮x⸮⸮⸮^}⸮U⸮⸮⸮J⸮w⸮⸮⸮⸮kya⸮⸮⸮N}?f⸮e⸮{⸮⸮⸮⸮k⸮⸮⸮2⸮>⸮~⸮⸮⸮⸮⸮⸮⸮⸮⸮i⸮⸮⸮⸮⸮⸮ζ⸮⸮⸮k⸮⸮w⸮⸮⸮⸮⸮⸮⸮Y⸮⸮wi⸮梲ܻ|?w⸮⸮ߥ⸮W⸮⸮⸮⸮eOӞ⸮⸮⸮z⸮⸮⸮o⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮7⸮⸮-J⸮⸮⸮⸮⸮⸮⸮+ڏ⸮⸮߿⸮j⸮}⸮9⸮?⸮⸮⸮[X_⸮⸮⸮⸮⸮⸮⸮.⸮⸮⸮⸮⸮n⸮;⸮ߝP⸮⸮⸮p⸮H⸮F[~⸮⸮⸮⸮⸮⸮⸮zL⸮⸮⸮⸮⸮⸮ݗ⸮⸮w|^⸮⸮⸮]⸮_⸮⸮⸮k⸮⸮⸮⸮U⸮o4⸮⸮mn⸮⸮>⸮⸮⸮[⸮3⸮/⸮w⸮淯⸮⸮⸮}wD⸮⸮f쿋⸮⸮z⸮⸮H⸮cP⸮YÜ5⸮y⸮?~⸮⸮⸮⸮⸮⸮⸮⸮⸮M}⸮⸮wiP#⸮⸮}⸮L⸮⸮⸮ynx|^⸮wn⸮=⸮泩⸮⸮⸮C⸮⸮⸮⸮⸮~ݳ⸮׍D⸮[⸮⸮⸮,⸮kGȯ⸮⸮⸮Q⸮⸮u⸮⸮⸮O|⸮⸮⸮⸮⸮_|?⸮⸮⸮⸮~ʖ9⸮⸮⸮⸮⸮tNW⸮⸮⸮⸮⸮⸮⸮⸮v⸮⸮e⸮⸮s⸮⸮W⸮⸮⸮⸮~⸮~⸮⸮e⸮⸮⸮⸮⸮⸮⸮q%⸮⸮⸮⸮U⸮=⸮⸮⸮?⸮⸮'⸮⸮`⸮⸮⸮⸮⸮⸮⸮⸮~⸮⸮⸮ɋ⸮⸮{⸮⸮⸮C⸮⸮'⸮Mn⸮ٷ>⸮}⸮⸮⸮⸮⸮⸮⸮ߜ⸮⸮⸮]ڞ⸮ ߶_8⸮⸮⸮⸮{.⸮⸮⸮⸮S\+⸮vSǏ⸮⸮⸮⸮⸮^⸮⸮\⸮_⸮⸮⸮˫m⸮^⸮⸮=o⸮ůw_yݨ⸮⸮⸮⸮뿜9?_⸮⸮⸮⸮⸮٧⸮⸮⸮⸮⸮ǜ⸮ήo⸮⸮f>⸮~5⸮⸮~⸮v⸮v߿?Z⸮%
⸮{⸮|⸮⸮⸮⸮~ou⸮⸮⸮⸮T⸮No⸮⸮⸮⸮⸮⸮ֹi&;⸮⸮⸮⸮⸮⸮ޛ⸮⸮⸮⸮⸮;⸮⸮⸮⸮⸮⸮⸮?⸮⸮^⸮w;⸮⸮⸮⸮ÿ⸮⸮⸮7⸮⸮⸮⸮⸮ׯ}⸮⸮8⸮⸮⸮F⸮⸮⸮⸮⸮⸮⸮⸮Z⸮⸮??⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮n⸮⸮⸮⸮ _⸮⸮⸮⸮⸮⸮s{⸮⸮⸮⸮U⸮⸮wmt[o⸮7f⸮⸮⸮⸮⸮⸮R⸮⸮⸮L⸮|>⸮M0⸮⸮v⸮%v֟}⸮⸮⸮j7⸮⸮⸮⸮⸮⸮⸮e{n⸮⸮⸮⸮{⸮⸮⸮⸮⸮⸮N}}⸮⸮CsLB⸮⸮⸮j⸮⸮x輣⸮⸮⸮⸮⸮⸮#_⸮y⸮⸮⸮⸮⸮⸮P⸮⸮⸮Z⸮(6⸮_⸮[K<⸮ם⸮_⸮{⸮⸮싛⸮߼5:?k⸮⸮߿⸮~⸮ɫ⸮⸮⸮~⸮⸮⸮K⸮⸮⸮⸮⸮⸮⸮⸮⸮x񭳶⸮⸮9⸮ɖ⸮⸮⸮]⸮⸮⸮|[⸮⸮?⸮⸮>eOs⸮⸮⸮⸮Z⸮⸮⸮⸮⸮⸮A?3⸮⸮⸮⸮⸮~⸮⸮[⸮⸮⸮⸮8<⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮}69?⸮⸮⸮⸮K⸮⸮⸮Sb⸮eJ⸮⸮oO⸮⸮''⸮⸮⸮뷳⸮O⸮⸮⸮⸮G_⸮⸮⸮.⸮⸮{⸮⸮
It works fine when using the original code.
Please Note:
First make sure that the baud rate used in the sketch is same as the baud rate used in the Serial Monitor
Use a delay of 10 milliseconds for stability: delay(10);
The val variable is int, so better to use Serial.printline with String Constructor as:
Serial.println("Head " + String(val);
The baud rate in your code and the baud rate chosen in the Serial Monitor window are different. make sure they match and the output should then be legible.

RCSwitch library seemingly not working for 433 Hz receiver on ESP32 NodeMCU

I'm pretty new to Arduino and especially ESP32. But - before I receive the tip "use an Arduino" - I decided to go for the ESP32 because of the size and the capability to connect it to the WLAN.
However, I am trying to build some control box for my terrarium which should - in the first design - steer various lamps and the rain pump via remote controlled outlets. For this I got an ESP32 NodeMCU, a RTC time module (which seems to work quite fine) and a 433 Hz receiver/sender set.
I followed several tutorials regarding the wiring and uploaded the example files to the ESP32. No matter which pin I connect the Receiver to (I need to connect the receiver first in order to read out the signals of the 433 Hz control which came with the outlets) I won't receive any signals on the receiver.
I embedded the library RCSwitch and I tried to configure my switch as follows (here with PIN 13 as example - I tried several other pins as well):
mySwitch.enableReceive(13)
As I read in some other blog, there might be the need to convert the pin number to its interrupt address, so I tried the following:
mySwitch.enableReceive(digitalPinToInterrupt(13))
The result is always the same: dead silence on the serial monitor (except the boot messages, etc.).
Am I using the wrong library or what am I doing wrong here?
I read that there should be a library called RFSwitch, but the only version I found only features the 433 Hz sender, not the receiver.
I would be really grateful for any hint concerning this issue - I'm pretty stuck here for many hours now...
I know this is pretty old and maybe you've resolved the issue by now but maybe it will help others. I had the same issue and what helped me was to set the pinMode:
pinMode(GPIO_NUM_35, INPUT);
mySwitch.enableReceive(digitalPinToInterrupt(GPIO_NUM_35));
Have been successful with RCSwitch today on ESP32 Dev Board and a 433MHZ receiver and sender. Here is what I have been stumbling on my journey.
Connecting the receiver (requires 5V)
You can use the ESP32-VIN for 5V if the Micro-USB is used to supply power
You may connect the Receiver-DATA to any ESP-32-Input-PIN BUT you might damage your ESP32 since it only allows ~3.3V
I tried first with some "makeshift" level shifting through resistors but I guess it lowers speed too much => A proper level-shifter (5V => 3.3V) might work out well
When referencing the PIN "xx" I have been just using the PIN-Number "Dxx" written on the ESP32-Dev-Board
You may connect an antenna of ~17.3cm to improve range
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
#define RXD2 27
void setup() {
Serial.begin(115200);
Serial.print("Ready to receive.");
mySwitch.enableReceive(RXD2);
}
void loop() {
if (mySwitch.available()) {
Serial.print("Received ");
Serial.print( mySwitch.getReceivedValue() );
Serial.print(" / ");
Serial.print( mySwitch.getReceivedBitlength() );
Serial.print("bit ");
Serial.print("Protocol: ");
Serial.print( mySwitch.getReceivedProtocol() );
Serial.print(" / ");
Serial.println( mySwitch.getReceivedDelay() );
mySwitch.resetAvailable();
}
}
In your RC and Outlet can be configured by DIP-Switches you might not need to connect the receiver overall - you can directly insert the DIP-Switches levels in the RCSwitch-Library
Connecting the sender (is fine with just 3.3V)
You can use the ESP32-3.3 to supply power
You may want to double check the PIN-Labels - I got confused because the DATA-Label was off and first interpreted as GND | DATA | VCC instead of GND | VCC | DATA
You may connect an antenna of ~17.3cm to improve range
#include <Arduino.h>
#include <WiFi.h>
#include <RCSwitch.h>
#define TXD2 25
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(115200);
// Transmitter is connected to Arduino Pin #10
mySwitch.enableTransmit(TXD2);
// Optional set protocol (default is 1, will work for most outlets)
// mySwitch.setProtocol(2);
// Optional set pulse length.
mySwitch.setPulseLength(311);
// Optional set number of transmission repetitions.
// mySwitch.setRepeatTransmit(15);
}
void loop() {
/* See Example: TypeA_WithDIPSwitches */
mySwitch.switchOn("01010", "10000");
Serial.println("Switch On");
delay(10000);
mySwitch.switchOff("01010", "10000");
Serial.println("Switch Off");
delay(10000);
}
I have not yet used sender or receiver while WiFi being active. Though I have been reading about issues while WiFi is active and receiving / sending via 433Mhz.
The sender must have 5 V supply to go far, and it has not output pin which can damage the ESP32, and the receiver. Instead, must be connected to 3.3 V because it has an output which goes to ESP2 (3.3 V supply) and the output of the receiver must not be more than 3.3 V, so as not to damage the GPIO input of ESP32.
ESP32
The data sender(input) goes to: GPIO 5: pinMode(5, OUTPUT)
The data receiver (output), goes to GPIO 4: pinMode(4, INPUT)
Sender supply: 5 V
Receiver supply: 3.3 V (not to damage ESP32 GPIO 4)

ESP8266 shutdown after compile

I have a big problem with esp8266-1.
Im updating code (any code, even "hello wordl") to esp8266. It was working for some time (sending temp reads from dallas temp through MQTT to RPI3) and now every upload to esp8266 has effect in interruption in any wireless comunication port: mwireless mouse has big problems, wifi is jammed.
What i did wrong?
Here some simple code:
#define pirPin 2 // Input for HC-SR501
int pirValue;
void setup() {
pinMode(pirPin, INPUT);
}
void loop() {
Serial.print(" Checking...");
pirValue = digitalRead(pirPin);
Serial.println(pirValue);
delay(1000);
}
Conection is good, becuse i madenter image description heree some sort of programming station for it with adapter, so i only pull it in to program.
Arduino setup
try this: add a capacitor in between VCC and GND. Pullup all the GPIO pins. Check the rest pin have floating values? if so pull up the reset pin too!

Two xbee beacons drowning each other out?

I'm trying to create a pair of arduino/xbee beacons that will transmit some information (ultimately GPS coordinates) to each other at all times. The problem is that when they're both on, neither one seems to receive. I'm guessing it's because both might be transmitting at the same time and drowning the others message out. However, I'm not sure of a better approach since neither one is a slave or master. Here's my setup:
Hardware:
A pair of XBee-Pro 900 XSC S3B xbees.
A pair of teensy (arduino compatible with multiple UARTs)
XBee Setup
VID: 542D
DT: 8153
Serial connected to Serial 3 on Teensy
Code
long lastTxTime = 0
void setup() {
Serial.begin(9600);
Serial3.begin(9600);
}
void loop() {
long now = millis();
// Send ever 0.5 seconds
if (now - lastTxTime > 500) {
Serial.println("SEND!");
Serial3.println("WOOT!");
lastTxTime = now;
}
delay(100);
// Print anything received
while(Serial3.available()) {
char c = Serial3.read();
Serial.print(c);
}
}
When I connect both Teensy's to separate terminals, both of them output "SEND" and rarely receive a "WOOT". I put one of the XBees straight on a USB board to a terminal and it output most of the "WOOT" messages from the other.
What can I do to make this work?
What you need to look into is flow control. In the wireless standard, they use RTS (Request to Send) & CTS (Clear to Send). The RTS/CTS pins on Xbee might be what you need,

Arduino: serial.read within ISR

I am writing a small test program that attempts to perform a serial.write() followed by a serial.read() within an ISR. The code will eventually be used to prompt an external GSM shield to send an SMS on a regular basis.
ISR(TIMER2_OVF_vect) {
Serial.println("AT+CMGS=\"0123456789\""); // Tell Sim900 To prepare sms to number 01...
while(Serial.read()!='>'); // Wait for Sim900 to respond
Serial.print("A text message"); // the SMS body
Serial.write(0x1A); //Confirm send instruction
Serial.write(0x0D);
Serial.write(0x0A);
}
}
What I have found after a lot of testing is that Serial.read() within an ISR is not capable of reading a live serial prompt, instead it will only read any input that was buffered before the ISR was triggered.
Is there any way around this?
The only solution I have found is to place this code instead within the main loop(). But I want to send the SMS using a timer interrupt.
Thank you
You need to place the code in the loop() but using an IF:
float toBeSent = interval;
loop() {
if (millis() > toBeSent) {
Send();
toBeSent = milli() + interval;
}
}
interval is your sending interval in milliseconds.
I had a similar problem a while ago which I managed to resolve by using the Arduino SoftwareSerial library instead of the hardware based Serial.read.
There are some overheads associated with using SoftwareSerial, and you can only read one port at a time, so I leave it up to those with a better understanding of the Arduino platform to tell you if this is a good idea, but one of the benefits of this library is that you can use it within an ISR.
To use the SoftwareSerial library, add the following two lines of code at the top of your sketch remembering to replqce the rx_pin and tx_pin with the corresponding pin values you want to use:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(rx_pin, tx_pin);
Then replace the key word Serial throughout your sketch with mySerial (or whatever name you have chosen to give your SoftwareSerial instance).
An important thing to keep in mind when using SoftwareSerial is that you can only use certain pins on the Arduino so read the documentation first.
If you wanted to live dangerously you could enable interrupts inside the ISR and use a flag to prevent reentry.
int flag=0;
ISR(TIMER2_OVF_vect) {
flag = 1
if (flag){return;}
sei();
Serial.println("AT+CMGS=\"0123456789\""); // Tell Sim900 To prepare sms to number 01...
while(Serial.read()!='>'); // Wait for Sim900 to respond
Serial.print("A text message"); // the SMS body
Serial.write(0x1A); //Confirm send instruction
Serial.write(0x0D);
Serial.write(0x0A);
}
flag = 0;
}

Resources