I am using the attached script to transmit data from one Arduino Uno to another using 433 MHz transmit and receive modules, using radiohead library.
It is working to send a 'canned message' "Hello, my name is Scott!" just fine however, I want to take the value of a digitalRead (1 or 0) and send that to the receiver to be read and used in it's script. I would have thought this much easier than I've found it to be.
I'm new to serial communication and am not sure what's being sent; I know, highs and lows but is it ASCII or what?
// ask_transmitter.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to transmit messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) transmitter with an TX-C1 module
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
int id_1 = 2;
int id_2 = 3;
int id_3 = 4;
int id_0 = 5;
int buzzer_id_0 = 0;
int buzzer_id_1 = 0;
int buzzer_id_2 = 0;
int buzzer_id_3 = 0;
int pin6 = 6;
RH_ASK driver(2000, 11, 12, 5); // ESP8266 or ESP32: do not use pin 11
void setup()
{
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
pinMode(pin6,INPUT);
pinMode(id_1, INPUT);
pinMode(id_2, INPUT);
pinMode(id_3, INPUT);
pinMode(id_0, INPUT);
}
void loop()
{
buzzer_id_0 = digitalRead(id_0);
buzzer_id_1 = digitalRead(id_1);
buzzer_id_2 = digitalRead(id_2);
buzzer_id_3 = digitalRead(id_3);
if(digitalRead(pin6) == LOW){
const char *msg = "Hello, my name is Scott!";
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
delay(200);
}
}
uint8_t values[4]; // just a global variable here
// later
// in the loop fill the array
values[0] = buzzer_id_0; // and do the same for 1, 2 and 3
// then send the whole thing
driver.send(values, 4);
On the other side, catch those in an array, use them however.
You could use an array directly instead of the four variables whose name differ only by an appended digit - a sure sign that array variables might be useful.
Either send an 'A' for digitalRead()==HIGH or an 'a' for digitalRead()==LOW.
Up to you.
You can design and programme your own character-driven software protocol.
I tried putting the 4 digital read values in a char array, get weird box like chars on receiving end. So, in a nutshell, how can I get the 4 digital read values into the msg variable as text (I assume the "Hello, my name is Scott!" is simply text and whatever I change that text to sends and receives perfectly).
Let us not that int y = digitalRead(DPin); returns HIGH or LOW of type int; where, HIGH has been defined as 1 (0x01) or 0 (0x00). You can transmit this value to the receiver as binary as it is or in ASCII format like 0x31 (0x30 + HIGH = 0x30 + 0x01 = 0x31) for HIGH and 0x30 (0x30 + LOW = 0x30 + 0x00) for LOW. For example:
int y = digitalRead(4);
if(y == HIGH)
{
SUART.print(y + 0x30); // or Serial.print('1'); sending ASCII for 1 = HIGH
}
else
{
SUART.print(y + 0x30); //or Serial.print('0'); sending ASCII for 0 = LOW
}