foyezes
1
Hello. I'm using these 433mhz rf modules to communicate between two arduinos.
I succeeded in turning an LED on and off following these instructions https://mechatrofice.com/arduino/rf-remote-controller-using-arduino-and-ask-module.
Now I'm trying to send data as different packets or bytes, so that I can have multiple values sent via different packets.
I tried following a NRF24 code which sent data packets with the 433mhz. This is the code:
Rx
\
#include <SPI.h>
#include <RH_ASK.h>
RH_ASK radio(2000, 11, 12);
char receive[32];
struct Received_data {
byte ch1;
};
int ch1_value = 0;
Received_data received_data;
void setup() {
Serial.begin(9600);
received_data.ch1 = 127;
radio.init();
}
void receive_the_data()
{
while ( radio.available() ) {
radio.recv((uint8_t*)&received_data, sizeof(Received_data));
}
}
void loop() {
receive_the_data();
ch1_value = map(received_data.ch1, 0, 255, 1000, 2000);
Serial.println(ch1_value);
}
//
Tx
\
#include <SPI.h>
#include <RH_ASK.h>
RH_ASK radio(2000, 11, 12);
struct Data_to_be_sent {
byte ch1;
};
Data_to_be_sent sent_data;
void setup() {
radio.init();
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
sent_data.ch1 = 127;
}
void loop() {
sent_data.ch1 = map( analogRead(A0), 0, 1024, 0, 255);
radio.send((uint8_t*)&sent_data, sizeof(Data_to_be_sent));
}
//
I tried it didn't work. Any help is appreciated.
Here is code that I use with the 433MHZ radios to send 2 temperatures. Hopefully you can adapt these programs.
Send
// Include RadioHead Amplitude Shift Keying Library
// data to Uno pin 12
#include <RH_ASK.h>
// Include dependant SPI Library
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 4 on the Arduino
#define ONE_WIRE_BUS 4
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
OneWire oneWire(ONE_WIRE_BUS); // for DS18b20 sensors
DallasTemperature sensors(&oneWire);
typedef struct
{
float temp1;
float temp2;
} Temps;
Temps temps;
void setup()
{
// Initialize ASK Object
Serial.begin(9600);
Serial.println("starting transmitter");
rf_driver.init();
sensors.begin();
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 5000;
if (millis() - timer >= interval)
{
timer = millis();
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
temps.temp2 = 0;
temps.temp1 = sensors.getTempCByIndex(0);
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(temps.temp1);
rf_driver.send((uint8_t*)&temps, sizeof(temps));
rf_driver.waitPacketSent();
}
}
Receive
// Include RadioHead Amplitude Shift Keying Library
// data to Uno pin 11
#include <RH_ASK.h>
#include <SPI.h>
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h>
// Create Amplitude Shift Keying Object
RH_ASK out;
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
typedef struct
{
float temp1;
float temp2;
} Temps;
Temps temps;
void setup()
{
// Initialize ASK Object
out.init();
// Setup Serial Monitor
Serial.begin(9600);
Serial.println("starting receiver");
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.print("receiver starting!");
}
void loop()
{
uint8_t buflen = sizeof(temps);
// Check if received packet is correct size
if (out.recv((uint8_t*)&temps, &buflen) && buflen == sizeof(temps))
{
// Message received with valid checksum
Serial.println("Message Received: ");
Serial.print("temp 1 = ");
Serial.println(temps.temp1, 2);
Serial.print("temp 2 = ");
Serial.println(temps.temp2, 2);
Serial.println();
lcd.clear();
lcd.print("temp 1 = ");
lcd.print(temps.temp1, 1);
}
}
foyezes
3
Hello. Here's what I came up with:
Tx
#include <SPI.h>
#include <RH_ASK.h>
RH_ASK driver;
struct Data_to_be_sent {
byte ch1;
};
Data_to_be_sent sent_data;
void setup() {
driver.init();
sent_data.ch1 = 127;
}
void loop() {
uint8_t buflen = sizeof(sent_data);
sent_data.ch1 = map( analogRead(A0), 0, 1024, 0, 255);
driver.send((uint8_t*)&sent_data, sizeof(sent_data));
}
Rx
#include <SPI.h>
#include <RH_ASK.h>
RH_ASK driver;
char receive[32];
struct Received_data {
byte ch1;
};
int ch1_value = 0;
Received_data received_data;
void setup() {
Serial.begin(9600);
received_data.ch1 = 127;
driver.init();
}
void receive_the_data()
{
while ( driver.available() ) {
driver.recv((uint8_t*)&received_data, sizeof(Received_data));
}
}
void loop() {
uint8_t buflen = sizeof(received_data);
ch1_value = map(received_data.ch1, 0, 255, 1000, 2000);
Serial.println(ch1_value);
}
Still not working
foyezes
4
Just showing this value, which isn't even in the potentiometer range
I think your transmitter is missing a second coil. Some pictures I have seen show that coil. The schematic diagram shows it also.
foyezes
6
I tried led code, it works, my transmitter has two coils
system
Closed
7
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.