I have two Arduino Unos, two lcds and Xbee s2.
I connected two xbee s2 module to each arduino uno.
I searched this implementation hard and tried it.
but, it didn't work. I appreciate your help.
Configuration: AP=2
Coordinator API and Router API
Sender: Router
#include <XBee.h>
#include <SoftwareSerial.h
#include <LiquidCrystal.h>
#define LED 13
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
SoftwareSerial xSerial(3,4);
XBee xbee=XBee();
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
void setup(){
//Serial.begin(9600);
xSerial.begin(9600);
xbee.setSerial(xSerial);
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, Gang!");
//Serial.println("xbee start");
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
delay(3000);
}
void loop(){
ZBTxRequest zbTx;
uint8_t payload[]={'H', 'E', 'Y', '\0'};
XBeeAddress64 address=XBeeAddress64(0x13a200, 0x40b450f4);
zbTx=ZBTxRequest(address, payload, sizeof(payload));
xbee.send(zbTx);
if (xbee.readPacket(500)) {
if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
xbee.getResponse().getZBTxStatusResponse(txStatus);
// get the delivery status, the fifth byte
if (txStatus.getDeliveryStatus() == SUCCESS) {
lcd.setCursor(0,1);
lcd.print("Success");
} else {
// the remote XBee did not receive our packet. is it powered on?
}
}
} else if (xbee.getResponse().isError()) {
//nss.print("Error reading packet. Error code: ");
//nss.println(xbee.getResponse().getErrorCode());
} else {
// local XBee did not provide a timely TX Status Response -- should not happen
}
delay(1000);
}
Receiver: Coordinator
#include <SoftwareSerial.h>
#include <XBee.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
SoftwareSerial xSerial(3,4);
XBee xbee=XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
ZBRxResponse rx = ZBRxResponse();
void setup(){
xSerial.begin(9600);
xbee.setSerial(xSerial);
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, tang!");
delay(3000);
}
void loop(){
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
lcd.setCursor(0, 1);
lcd.print( "MSG");
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
// got a zb rx packet
// now fill our zb rx class
xbee.getResponse().getZBRxResponse(rx);
lcd.setCursor(0, 1);
lcd.print(rx.getData(1));
if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
// the sender got an ACK
lcd.setCursor(0,1);
lcd.print( "ACK");
} else {
lcd.setCursor(0,1);
lcd.print("ERROR");
}
} else {
// not something we were expecting
//flashLed(errorLed, 1, 25);
lcd.print("7 print");
}
} else if (xbee.getResponse().isError()) {
lcd.print("error");
}
delay(3000);
}
Ah, I connect xbee s2 to xbee pro shield on arduino. There are no dline/uart switch on xbee shield. so, I use softwareserial library...But it didn't work.
I have done the communication between two XBee with Arduino, you can take a look at my coding and try to change your coding a little bit.
Sender =
#include <SoftwareSerial.h>
#define Dout 2
#define Din 3
#define LED 9
SoftwareSerial XBeeSerial (Dout, Din);
void setup() {
XBeeSerial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
delay(500);
XBeeSerial.write("+");
analogWrite(LED,13);
delay(3000);
XBeeSerial.write("-");
analogWrite(LED,0);
delay(500);
}
Receiver =
#include<SoftwareSerial.h>
#define LED 9
#define Dout 2
#define Din 3
SoftwareSerial XBeeSerial( Dout, Din);
char XBee_message;
void setup() {
Serial.begin(9600);
XBeeSerial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
while(XBeeSerial.available()) {
XBee_message = XBeeSerial.read();
Serial.print(XBee_message);
switch (XBee_message){
case '+':
analogWrite(LED, 13);
break;
case '-':
analogWrite(LED, 0);
break;
default:
analogWrite(LED, 0);
}
}
}
Related
I can't seem to create a serial communication between esp866 nodemcuv3 and arduino mega 2560. The code seems to be working with only text messages. However, when the lcd, motor and temperature sensor is added the code doesn't seem to be working. Code is given below.
WORKING CODE:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //rx,tx
void setup(){
mySerial.begin(115200);
Serial.begin(115200);
}
void loop (){
if(mySerial.available() >= 2) {
int data = mySerial.read();
data = (data << 8) + mySerial.read();
if (data == 0){
Serial.println("BREWING: Light COFFEE");
Serial.println(data);
mySerial.write("reset");
} else if (data == 1) {
Serial.println("BREWING: Normal COFFEE");
Serial.println(data);
mySerial.write("reset");
}else if (data == 2) {
Serial.println("BREWING: Strong COFFEE");
Serial.println(data);
mySerial.write("reset");
}
}
}
CODE THAT DOESN'T WORK:
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#define ENA 5 // Enable Pin for Motor A
#define IN1 7 // Input Pin 1 for Motor A
#define IN2 6 // Input Pin 2 for Motor A
SoftwareSerial mySerial(10, 11); //rx,tx
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup(){
mySerial.begin(115200);
Serial.begin(115200);
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
lcd.begin(20, 21);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
sensors.begin();
}
void loop(){
if(mySerial.available() >= 2){
int data = mySerial.read();
data = (data << 8) + mySerial.read();
if (data == 0){
Serial.println("BREWING: Light COFFEE");
Serial.println(data);
// Read temperature value from the DS18B20
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
// print the temperature on the LCD
lcd.setCursor(0, 1);
lcd.print(temp);
// Control the L298N motor
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
delay(1000);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 128);
mySerial.write("reset");
} else if (data == 1) {
Serial.println("BREWING: Normal COFFEE");
Serial.println(data);
// Read temperature value from the DS18B20
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
// print the temperature on the LCD
lcd.setCursor(0, 1);
lcd.print(temp);
mySerial.write("reset");
} else if (data == 2) {
Serial.println("BREWING: Strong COFFEE");
Serial.println(data);
// Read temperature value from the DS18B20
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
// print the temperature on the LCD
lcd.setCursor(0, 1);
lcd.print(temp);
mySerial.write("reset");
}
}
}
I want to be able to completely run the conditions since we want to run a motor using an app which is connected to thingspeak through nodemcu v3 which also sends values (0,1,2) to arduino. However, as I stated earlier the code doesn't work with the added codes for I2C lcd, motor and temp sensor.
Thank you for everyone who will be willing to help.
Sofware serial doesn't really work well at those rates. Turn it down to 9600 or use hardware serial which the mega has a few of.
https://forum.arduino.cc/t/does-softwareserial-really-work-at-115200-baud/327450
Need your help again: I'm doing this time Master - Slave Using SPI communication, there is no error in the code when I simulate the code but the LED won't turn on.
The supposed outcome that should happen is that when I push the push button on master board the LED on the slave board will turn on.
Master code:
// Master Board
#include <SPI.h>
#define button1 4
#define SS 10
int buttonvalue;
int x;
void setup(void) {
Serial.begin(115200); //set baud rate to 115200 for usart
digitalWrite(SS, HIGH); // disable Slave Select
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV8); //divide the clock by 8
}
void loop(void) {
digitalWrite(SS, LOW);
buttonvalue = digitalRead(button1);
if (buttonvalue == HIGH) {
x = 1;
} else {
x = 0;
}
digitalWrite(SS, HIGH);
delay(1000);
}
Slave code:
// Slave Board
#include <SPI.h>
#define led1 2
volatile byte Slavereceived;
volatile boolean received;
int x;
void setup(void) {
Serial.begin(115200);
pinMode(2, OUTPUT);
pinMode(MISO,OUTPUT);
SPCR |= _BV(SPE);
received = false;
SPI.attachInterrupt();
}
ISR (SPI_STC_vect) {
Slavereceived = SPDR;
received = true;
}
void loop() {
if (received) {
if (Slavereceived == 1) {
digitalWrite(led1, HIGH);
} else {
digitalWrite(led1, LOW);
}
delay(1000);
}
}
I too was stuck in the same situation, there is no support for the SPI library in tinkercad, you can include it without errors, and even use it, but any useful command will let the code stuck at that command
Sorry, but there no much you can do
this link if for a tinkercad forum, where one of the people said SPI library amoung two others are not supported
Add SPI.transfer(x); below the if else to your master code.
The master code will look somewhat like this:
// Master Board
#include <SPI.h>
#define button1 4
#define SS 10
int buttonvalue;
int x;
void setup(void) {
Serial.begin(115200); //set baud rate to 115200 for usart
digitalWrite(SS, HIGH); // disable Slave Select
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV8); //divide the clock by 8
}
void loop(void) {
digitalWrite(SS, LOW);
buttonvalue = digitalRead(button1);
if (buttonvalue == HIGH) {
x = 1;
} else {
x = 0;
}
SPI.transfer(x);
digitalWrite(SS, HIGH);
delay(1000);
}
The code im working on, is suppose to show temperature, humidity and able to take and show heart rate on the lcd. After data is shown, it will send data to "ThingSpeak". After sending, there will be a http code error -401 which is ok as it can only send data very 15 sec. But after awhile, it will change it error http code -301... and then it will hang. Another issue is when i try to use the temperature sensor with the heart rate sensor, the lcd will hang and it will not work till i reset.
#include "ThingSpeak.h"
#include "SPI.h"
#include "DHT.h"
#include <Ethernet.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(10, 9, 5, 4, 3, 2); //numbers of interface pins
#define redLED 8
int sensorPin = A8;
float tempC;
#define DHTPIN 6
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float h;
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
// Variables
const int PulseWire = A9; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int blinkPin = 22; // The on-board Arduino LED, close to PIN 13.
int Threshold = 550; // Determine which Signal to "count as a beat" and which to ignore.
PulseSensorPlayground pulseSensor; // Creates an instance of the PulseSensorPlayground object called "pulseSensor"
byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0x40, 0x4F};
unsigned long myChannelNumber = ;
const char * myWriteAPIKey = "";
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(172, 17, 171, 199);
IPAddress myDns(172, 17, 171, 254);
float get_temperature(int pin)
{
float temperature = analogRead(pin); // Calculate the temperature based on the reading and send that value back
float voltage = temperature * 5.0;
voltage = voltage / 1024.0;
return ((voltage - 0.5) * 100);
}
EthernetClient client;
void setup()
{
lcd.begin(16, 2);
pinMode(redLED, OUTPUT);
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(blinkPin); //auto-magically blink Arduino's LED with heartbeat.
pulseSensor.setThreshold(Threshold);
pulseSensor.begin();
dht.begin();
Ethernet.init(10); // Most Arduino Ethernet hardware
Serial.begin(9600); //Initialize serial
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware)
{
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true)
{
delay(10); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF)
{
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
}
else
{
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop()
{
h = dht.readHumidity();
{
tempC = get_temperature(sensorPin);
}
if (tempC < 31)
{
lcd.setCursor(0, 0);
lcd.print(tempC);
lcd.print(" "); //print the temp
lcd.print((char)223); // to get ° symbol
lcd.print("C");
lcd.print(" ");
lcd.print(h);
lcd.print("%");
delay(750);
}
else if (tempC > 31)
{
lcd.setCursor(0, 0);
lcd.print(tempC);
lcd.print(" "); //print the temp
lcd.print((char)223); // to get ° symbol
lcd.print("C");
lcd.print(" ");
lcd.print(h);
lcd.print("%");
delay(750);
}
int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int".
// "myBPM" hold this BPM value now.
if (pulseSensor.sawStartOfBeat())
{
lcd.setCursor(0,1);
lcd.print("BPM:"); // Print phrase "BPM: "
lcd.println(myBPM); // Print the value inside of myBPM.
lcd.print(" ");
delay(100);
}
// Write to ThingSpeak channel.
ThingSpeak.setField(1, tempC);
ThingSpeak.setField(2, h);
ThingSpeak.setField(3, myBPM);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (x == 200)
{
Serial.println("Channel update successful.");
}
else
{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
}
I have a temperature sensor set up working with an LCD and a stick to adjust the brightness. I now want the temperature sensor to send a text whenever it reaches a certain temperature. Can somebody please help.
The GSM unit i have is the SIM800L
below is what I have so far :
#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int sensor=A1; // Assigning Analog Pin A1 to variable 'sensor'
float tempc; //variable to store temperature in degree Celsius
float tempf; //variable to store temperature in Fahrenheit
float vout; //temporary variable to hold sensor reading
void setup()
{
pinMode(sensor,INPUT); // Configuring pin A1 as INPUT Pin
Serial.begin(9600);
lcd.begin(16,2);
delay(500);
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
}
you could use the library Fona to send SMS with a Sim800L
to send a message you use the command -> fona.sendSMS(sendto, message)
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_RX, FONA_TX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
while (!Serial);
Serial.begin(115200);
Serial.println(F("FONA basic test"));
Serial.println(F("Initializing....(May take 3 seconds)"));
fonaSS.begin(9600);
if (!fona.begin(fonaSS)) {
Serial.println(F("Couldn't find FONA"));
while (1);
}
Serial.println(F("FONA is OK"));
char sendto[21], message[141];
:
:
//initialize sendto and message
:
:
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}
to adapt the program to your case: i have put some lines of codes from setup to loop (easy to understant sendto and message definitions in loop now)
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_RX, FONA_TX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
fonaSS.begin(9600);
// you initialisation code
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
if (tempc > 30.0) {
SendSms();
}
}
void SendSms() {
char sendto[] = "+19999999999"; //put the desired destination phone number for sms here
char message[141];
sprintf(message, "Alert TEMP is %.2f", tempc);// limit to 140
//sends the message via SMS
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}
another way to send sms
you could test the hayes command: for example
void sendsms(){
Serial.println("Sending text message...");
fonaSS.print("AT+CMGF=1\r"); // SMS MODE
delay(100);
// phone number
fonaSS.print("AT+CMGS=\"+33676171212\"\r"); //indicate your phone number
delay(100);
// message here
fonaSS.print("Message test \r");
// CTR+Z in mode ASCII, to indicate the end of message
fonaSS.print(char(26));
delay(100);
fonaSS.println();
Serial.println("Text send");
}
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
// you initialisation code
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
if (tempc > 30.0) {
SendSms();
}
}
void SendSms() {
char sendto[] = "+19999999999"; //put the desired destination phone number for sms here
char message[141];
sprintf(message, "Alert TEMP is %.2f", tempc);// limit to 140
//sends the message via SMS
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}
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.