I have to do a project for Uni using 2 arduino uno, 2 HC 05 bluetooth module and 2 sensors for each arduino. I have set a module to be the slave, and the master connects to the slave only.
When I am trying to send the data from slave to be read by master, it keeps reading 0.
I am using Arduino IDE, I have no errors and everything runs.
This is the code I have so far.
Also, I want to send data from 2 sensors from the slave to the master, but I do not know how to do that.
Slave code
#include <SoftwareSerial.h>
#define TILT 7 // tilt sensor pin
#define LDR 8 //light intensity sensor pin
#define rxPin 10
#define txPin 11
SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);
int sentBytes; ////// SLAVE CODE
byte data[2];
void setup() {
Serial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(TILT, INPUT);
pinMode(LDR, INPUT);
Serial.begin(9600);
nodeCommunication.begin(9600);
delay(2000);
}
void loop() {
//int LDRval = analogRead(LDR); // light intensity sensor (candela)
//int Tilt_Sensed = digitalRead(TILT);
data[0] = analogRead(LDR);
data[1] = digitalRead(TILT);
if(nodeCommunication.available()){
sentBytes = nodeCommunication.write(data[0]);
Serial.println("I reached this if");
}
Serial.println(analogRead(LDR));
Serial.println(sentBytes);
delay(2000);
}
Master Code
#include "dht.h"
#include <SoftwareSerial.h>
#define dht_apin A0 // Analog Pin sensor is connected to A0
dht DHT; // DHT.humidity // DHT.
#define rxPin 10
#define txPin 11
SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);
byte data = 1;
///// MASTER CODE
void setup(){
Serial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
nodeCommunication.begin(9600);
delay(2000);//Wait before accessing Sensor
}//end "setup()"
void loop(){
//Start of Program
DHT.read11(dht_apin); // reading the data from the sensor
if(nodeCommunication.available()){
data = nodeCommunication.read();
Serial.println("I reached this if");
}
Serial.print(data); Serial.println(" candela;");
Serial.print(DHT.temperature); Serial.println(" C");
Serial.print(DHT.humidity); Serial.println(" % humidity");
delay(2000);//Wait 2 seconds before accessing sensor again.
//Fastest should be once every two seconds.
}// end loop()
Please tell me what I am doing wrong. Thank you so much!
byte data = 1; - should be just byte data;
Have you checked the TX/RX connections? The TX from the master has to connect to the RX of the slave and visa versa. Als GND has to be connected to eachother.
After that, let the master (the transmitter) just Serial.write (in your case nodeCommunication.write() ) to the slave (the receiver).
So something like this:
Master:
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
byte data = 0x01; //Hex value for ''1''
SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);
void setup()
{
Serial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
nodeCommunication.begin(9600);
}
void loop()
{
nodeCommunication.write(data); //Sends byte data to slave
delay(1000);
}
Slave:
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
byte incoming
SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);
void setup()
{
Serial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
nodeCommunication.begin(9600);
}
void loop()
{
if(nodeCommunication.available > 0) //Only if data is available
{
byte incoming = nodeCommunication.read(); //read byte from master
Serial.println("Incoming = ");
Serial.println(incoming);
}
else
{
Serial.println("No data available....");
}
}
After your Serial communication is established, implementing the DHT value will be the next step.
Related
I am new to Arduino Programming.
This code works fine. It gives the correct output and functioning well. But I wanted to write the commands automatically without typing repeatedly.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(38400);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
}
void loop()
{
if (BTSerial.available()) {
Serial.write(BTSerial.read());
}
if(Serial.available()){
BTSerial.write(Serial.read());
}
}
So i tried doing the following change in the above code but i could not receive any response.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(38400);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
BTSerial.write("AT");
}
void loop()
{
if (BTSerial.available()) {
Serial.write(BTSerial.read());
}
}
I also tried BTSerial.print("AT"); But still no response.
AT commands are case sensitive and should end up with a terminator like an enter keystroke or a \r\n.
This should do:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(38400);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
BTSerial.write("AT\r\n");
}
void loop()
{
if (BTSerial.available()) {
Serial.write(BTSerial.read());
}
}
I'm trying to read voltage value using Analog pin A0 in arduino uno and transmit the read voltage to Nodemcu but not getting same voltage at NodeMcu as on Arduino side for Ex. for 5 volt at Arduino i get only 4 volt at Nodemcu.
i have made the delay of both the sketches equal even tried without any delay
also tried connecting the ground pin of both device
ARDUINO CODE
#include <SoftwareSerial.h>
SoftwareSerial s(5,6);
void setup() {
s.begin(9600);
Serial.begin(9600);
}
void loop() {
// read the input on analog pin 0:
int ADCdata = analogRead(A0);
float voltage = (ADCdata * 0.0048828125);
Serial.println(ADCdata);
Serial.println(voltage);
if(s.available()>0)
{
s.write(voltage);
}
delay(1000);
}
NODEMCU CODE
#include <SoftwareSerial.h>
SoftwareSerial s(D6,D5);
void setup() {
s.begin(9600);
Serial.begin(9600);
}
void loop() {
s.write("s");
if (s.available()>0)
{
data=s.read();
Serial.println(data);
}
delay(1000);
}
I would send the float data as a string:
s.println(value)
This will append a newline to mark the end of the string.
On the receiving side, read the line and convert to float.
float value = s.parseFloat();
I want to use bluetooth connection to control, how long something is on. So I read the Serial Input. If it is a number, I take the number and put it into a delay. After it is through, I write something and check again. If the Serial Read is not a number, it should turn off. in The problem is, the led keeps running. Do you see, what is my mistake?
#include <SoftwareSerial.h>
#define ledPin 13
#define rxPin 10
#define txPin 11
SoftwareSerial btSerial(rxPin, txPin);
int btData;
void setup() {
btSerial.begin(9600);
btSerial.println("bluetooth available");
pinMode(ledPin,OUTPUT);
serv.attach(3);
serv2.attach(5);
}
void loop() {
if (btSerial.available()){
btData = btSerial.read();
if(isDigit(btData)){
digitalWrite(ledPin,1);
btSerial.println("LED on Pin is on");
delay(btData*10);
}
else {
digitalWrite(ledPin,0);
btSerial.println("LED on Pin is off");
}
}
delay(100);
}
Try to use this:
#include <SoftwareSerial.h>
#define ledPin 13
#define rxPin 10
#define txPin 11
SoftwareSerial btSerial(rxPin, txPin);
int btData;
void setup() {
btSerial.begin(9600);
btSerial.println("bluetooth available");
pinMode(ledPin,OUTPUT);
serv.attach(3);
serv2.attach(5);
}
void loop() {
if (btSerial.available())
btData = btSerial.read();
if(isDigit(btData)){
digitalWrite(ledPin,1);
btSerial.println("LED on Pin is on");
delay(btData*10);
}
else {
digitalWrite(ledPin,0);
btSerial.println("LED on Pin is off");
}
delay(100);
}
And the reason is because the enabling and disabling was done only when there is serial data and if you put the if anywhere else it would be checked always.
I must send 3 values gave by 3 potentiometers ,connected by an Arduino Uno, and send them to another Arduino Uno whith a serial comunication. The received values must be distributed in 3 servo motors so that each knob able to control the servo motor movement.
the problem with this program is that the values received are not distributed correctly (for example the case that the value of the potentiometer 1 is to be read by the servo motor 3 or other cases). I ask if I could help to synchronize data received with the distribution of them to the servo motors. Thanks in advance.
sketch arduino with potentiometers:
#include <SoftwareSerial.h>
#define RX 2 //Pin tx
#define TX 3 //Pin rx
#define POTPIN A0
#define POTPIN2 A1
#define POTPIN3 A2
SoftwareSerial BTserial(RX, TX);
int lettura_pot;
int lettura_pot2;
int lettura_pot3;
byte val_servo;
byte val_servo2;
byte val_servo3;
void setup()
{
Serial.println("Inizializzazione seriale...");
Serial.begin(9600);
BTserial.begin(9600);
}
void loop()
{
BTserial.write(255); /* synch symbol */
lettura_pot = analogRead(POTPIN);
val_servo=map(lettura_pot,0,1023,0,180);
BTserial.write(val_servo);
Serial.println(val_servo);
lettura_pot2 = analogRead(POTPIN2);
val_servo2=map(lettura_pot2,0,1023,0,180);
BTserial.write(val_servo2);
Serial.println(val_servo2);
lettura_pot3 = analogRead(POTPIN3);
val_servo3=map(lettura_pot3,0,1023,0,180);
BTserial.write(val_servo3);
Serial.println(val_servo3);
}
sketch arduino with servo motors:
#include <SoftwareSerial.h>
#include<Servo.h>
SoftwareSerial BTserial(2, 3);
Servo myservo, myservo2, myservo3;
byte val_servo,val_servo2,val_servo3,a;
void setup() {
Serial.begin(9600);
BTserial.begin(9600);
myservo.attach(9);
myservo2.attach(10);
myservo3.attach(11);
}
void loop() {
if (BTserial.available() > 0) {
if (BTserial.available() == 255) { /* synch */
val_servo = BTserial.read();
val_servo2 = BTserial.read();
val_servo3 = BTserial.read();
}
Serial.print("SERVO1:");
Serial.println(val_servo);
Serial.print("SERVO2:");
Serial.println(val_servo2);
Serial.print("SERVO3:");
Serial.println(val_servo3);
myservo.write(val_servo);
myservo2.write(val_servo2);
myservo3.write(val_servo3);
BTserial.flush();
}
}
master code( no modificated):
#include <SoftwareSerial.h>
#define RX 2 //Pin tx
#define TX 3 //Pin rx
#define POTPIN A0
#define POTPIN2 A1
#define POTPIN3 A2
SoftwareSerial BTserial(RX, TX);
int lettura_pot;
int lettura_pot2;
int lettura_pot3;
byte val_servo;
byte val_servo2;
byte val_servo3;
void setup()
{
Serial.println("Inizializzazione seriale...");
Serial.begin(9600);
BTserial.begin(9600);
}
void loop()
{
BTserial.write(255);
lettura_pot = analogRead(POTPIN);
val_servo=map(lettura_pot,0,1023,0,180);
BTserial.write(val_servo);
Serial.println(val_servo);
lettura_pot2 = analogRead(POTPIN2);
val_servo2=map(lettura_pot2,0,1023,0,180);
BTserial.write(val_servo2);
Serial.println(val_servo2);
lettura_pot3 = analogRead(POTPIN3);
val_servo3=map(lettura_pot3,0,1023,0,180);
BTserial.write(val_servo3);
Serial.println(val_servo3);
}
slave code( modificated):
#include <SoftwareSerial.h>
#include<Servo.h>
SoftwareSerial BTserial(2, 3);
Servo myservo, myservo2, myservo3;
byte val_servo,val_servo2,val_servo3,a;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
BTserial.begin(9600);
myservo.attach(9);
myservo2.attach(10);
myservo3.attach(11);
}
void loop() {
// Read serial input:
if (BTserial.available() > 0) {
byte synch_symbol = BTserial.read();
if (synch_symbol == 255) {
while (BTserial.available() < 3) { }; /* wait for values */
val_servo = BTserial.read();
val_servo2 = BTserial.read();
val_servo3 = BTserial.read();
/* do something with values */
}
Serial.print("SERVO1:");
Serial.println(val_servo);
Serial.print("SERVO2:");
Serial.println(val_servo2);
Serial.print("SERVO3:");
Serial.println(val_servo3);
myservo.write(val_servo);
myservo2.write(val_servo2);
myservo3.write(val_servo3);
BTserial.flush();
}
}
Im using an arduino and an hc-06 to communicate with an android. I'm trying to send int values over, but the android says it's receiving empty strings. This is my code:
#include <SoftwareSerial.h>
#define rxPin 11 // define SoftwareSerial rx data pin
#define txPin 10 // define SoftwareSerial tx data pin
#define trigPin 5
#define echoPin 6
SoftwareSerial blueTooth(rxPin, txPin);
void setup() {
Serial.begin (9600);
blueTooth.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29;
Serial.println(distance);
blueTooth.print ((int)(Serial.read()));
delay(200);
}
I think what you want is
blueTooth.print(distance);
First suggestion, try switching the rx and tx pins. I know I've messed that up a bunch. If you didn't know, the rx from the hc-06 should be defined as the tx pin in software serial and visa versa. Michael Yu's comment is definitely preferred to what you originally had. The other thing I can suggest is posting the android code that receives and parses the incoming data.
My string sending function for arduino:
void sendStr(String str){
char b[2];
for(int i = 0; i < str.length(); i++){
String dataSend = str.substring(i,i+1);
dataSend.toCharArray(b,2);
bluetooth.print(b);
delay(1);
}
}