Hi Folks. I finally think I need some help with code.
I’m just a casual Arduino tinkerer, getting old, brain not working like it used to.
I am retired and so only hobbies with no commercial interest.
The code concerned is here, on the Dronebot Workshop so is easy available to see.
I wish to combine the 433Mhz Tx/Rx using the Radiohead library Demo2 where Bill uses a DHT22.
With the waterproof, Ultrasonic Transducer, the A….. that is compatible with the …….
My project is to combine both sketches in a manner that allows me to measure the height of a tank of water & Transmit it to a Receiver, some distance away.
The Communications video is at https://dronebotworkshop.com/433mhz-rf-modules-arduino/#Resources
The Ultrasonic video is at https://dronebotworkshop.com/waterproof-ultrasonic/
Bill uses the JSN-SR04T sensor in Mode1 and is equivalent to the A02YYUW dual element sensor I am using.
My hardware set up is on 2 breadboards as I need a Receiver 433Mhz as well.
Here is the Rub. And because the wiring uses all different Arduino pins, I can upload each sketch, separately and get them to work OK.
So, 433Mhz sketch ….. no problem by itself.
Upload the Ultrasonic sketch …. no problem by itself.
Try to merge both to get at minimum, just the Temperature and Humidity, =Various problems.
I have hacked the code, tried to “fault-find” and found some peculiar results but just can’t seem to hone on the root cause.
So, here I am. Cursing the hours I have spent to make a Tank Water level transmitter.
I’m hoping that a forum member could assist me.
I would prefer not to take this request any further and waste your time, if no-one feels up to the task.
I will work out, tomorrow how to update my hopelessly hacked code. It’s a mess.
The other problem is you need to take my word on what the Receiver is displaying on another PC, lest you have to build the hardware.
Regards, JimJtron.
@jimjtron Hi, can you post the error you are receiving?
Is it a compile error,, or a run error.
If a compile error, capture just the first few lines (in red) of the error, any following errors aren't important at this stage. I am attaching a picture of what I mean. Do NOT use the 'Copy error messages' button.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
I have an update. My edit window just expired after pasting code and photos.
The sketch compiles and uploads without errors.
It's the behaviour of the transmission / Reception that is a problem.
In order to get it to transmit anything at all I added this bit of code from the RadioHead examples.
// ************************************************
// ADD this piece from Radiohead examples for debugging purposes.
const char msg1 = "hello"; // removed * before msg1
rf_driver.send((uint8_t *)msg1, strlen(msg1));
rf_driver.waitPacketSent();
delay(200);
// ************************************************
I've been out shopping for hours and when I returned I found that the missing values had appeared.
So I will now reset the UNO and turn on TimeStamp in the Serial Monitor to see how long it takes to get values. Please refer attachment.
My ultimate aim to just send Ultrasonic distance messages to the receiver.
I'm just being cautious and using the DHT as a debugging tool, before I attempt to format the Strings etc for transmission.
The whole code (merged) is here but may require a bit of further explanation. Cheers.
[code]
/* Merging attempt. Brining in code from the Ultrasonic sketch into the 433 Mhz sketch 433 MHz RF Module Transmitter Demonstration 2 RF-Xmit-Demo-2.ino Demonstrates 433 MHz RF Transmitter Module with DHT-22 Sensor Use with Receiver Demonstration 2 DroneBot Workshop 2018 https://dronebotworkshop.com */ // ************************************************ // BRING IN PRE SETUP CODE NOW ..... // Include the Software Serial library #include <SoftwareSerial.h> // Define connections to sensor . int pinRX = 11; int pinTX = 10; // Array to store incoming serial data unsigned char data_buffer[4] = {0}; // Integer to store distance int distance = 0; // Variable to hold checksum unsigned char CS; // Object to represent software serial port SoftwareSerial mySerial(pinRX, pinTX); // ************************************************ // Include RadioHead Amplitude Shift Keying Library #include <RH_ASK.h> // Include dependant SPI Library #include <SPI.h> // Include DHT Libraries from Adafruit // Dependant upon Adafruit_Sensors Library #include "DHT.h"; //doesn't like "DHT_U.h" // Define Constants #define DHTPIN 7 // DHT-22 Output Pin connection #define DHTTYPE DHT22 // DHT Type is DHT 22 (AM2302) // Define Variables float hum; // Stores humidity value in percent float temp; // Stores temperature value in Celcius // Define output strings String str_humid; String str_temp; String str_out; // Create Amplitude Shift Keying Object RH_ASK rf_driver; // Initialize DHT sensor for normal 16mhz Arduino DHT dht(DHTPIN, DHTTYPE); void setup() { // ************************************************ // BRING IN THE SETUP HERE, FROM ULTRASONIC // Set up serial monitor Serial.begin(115200); // Set up software serial port mySerial.begin(9600); // ************************************************ // COMPILED OK before tampering with the VOID LOOP code. // Initialize ASK Object rf_driver.init(); //bills code if (!rf_driver.init()) //"!" is a NOT character. Serial.println("init failed"); // radiohead code. // Start DHT Sensor dht.begin(); } void loop() { // ************************************************ // // Run if data available // if (mySerial.available() > 0) { // I suspect problem with this. delay(4); // Check for packet header character 0xff if (mySerial.read() == 0xff) { // Insert header into array data_buffer[0] = 0xff; // Read remaining 3 characters of data and insert into array for (int i = 1; i < 4; i++) { data_buffer[i] = mySerial.read(); } //Compute checksum CS = data_buffer[0] + data_buffer[1] + data_buffer[2]; // If checksum is valid compose distance from data if (data_buffer[3] == CS) { distance = (data_buffer[1] << 8) + data_buffer[2]; // Print to serial monitor Serial.print("distance: "); Serial.print(distance); Serial.println(" mm"); } // delete closed curly here from original code // ************************************************ delay(2000); // Delay so DHT-22 sensor can stabalize hum = dht.readHumidity(); // Get Humidity value temp = dht.readTemperature(); // Get Temperature value // Convert Humidity to string str_humid = String(hum); // Convert Temperature to string str_temp = String(temp); // Combine Humidity and Temperature str_out = str_humid + "," + str_temp; // Compose output character static char *msg = str_out.c_str(); // ************************************************ // ADD this piece from Radiohead examples for debugging purposes. const char msg1 = "hello"; // removed * before msg1 rf_driver.send((uint8_t *)msg1, strlen(msg1)); rf_driver.waitPacketSent(); delay(200); // ************************************************ // then bills code. rf_driver.send((uint8_t *)msg, strlen(msg)); rf_driver.waitPacketSent(); // adding this in temporary JT Serial.print("Output Char: "); Serial.print(str_out); Serial.println(" combo"); delay(2000); // JT try to stop next ultra data. } // add this curly but now removed again. } // Get "Humid" & "Temp" text but no values to the Receiver ? Weird! // When I remove the Radio head example (Const Char etc) I get no receive data at all.
[/code]
@jimjtron Might be a classic 'buffer overflow' issue. The addition of msg1 provides a place for msg to overflow. Just a shot in the dark, but look at everything that affects the length of the string defined by msg in case something is a byte too long.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
Thanks Ron. I'll check but the original code without msg1 worked without issue.
So I have not tampered with that section. I'm still highly suspicious that the Ultrasonic's code method is "hogging" the time needed for the *msg to slowly transmit out.
Anyhow, an update.
This is the receivers output with the TimeStamp feature turned ON.
Started here:-
14:13:09.484 -> Humidity: - Temperature:
14:13:22.712 -> Humidity: - Temperature:
14:13:31.576 -> Humidity: - Temperature:
14:13:40.416 -> Humidity: - Temperature:
etc.
14:41:08.972 -> Humidity: - Temperature:
14:41:22.228 -> Humidity: - Temperature:
14:41:26.978 -> Humidity: 50.00 - Temperature: 23.40
14:41:31.045 -> Humidity: 50.00 - Temperature: 23.40
14:41:39.909 -> Humidity: 50.00 - Temperature: 23.40
14:41:53.155 -> Humidity: 50.00 - Temperature: 23.40
14:42:02.002 -> Humidity: 50.00 - Temperature: 23.40
14:42:10.865 -> Humidity: 50.00 - Temperature: 23.40
14:42:15.255 -> Humidity: 50.00 - Temperature: 23.40
14:42:24.118 -> Humidity: 50.00 - Temperature: 23.40
etc.
So the Delay is 14:41 minus 14:13hrs. = 28 minutes !!!
It would be interesting to know WHY ?????
Regards, JimJtron.
@jimjtron Ok, I am confused, if the original code worked, why did you change it which I gather broke it. Very confused.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@jimjtron Why is the timestamp not sequential?
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
// ************************************************
// ADD this piece from Radiohead examples for debugging purposes.
const char msg1 = "hello"; // removed * before msg1
rf_driver.send((uint8_t *)msg1, strlen(msg1));
rf_driver.waitPacketSent();
delay(200);
// ************************************************
// ADD this piece from Radiohead examples for debugging purposes. const char msg1 = "hello"; // removed * before msg1
Just an FYI: - Removing the asterisk is a complete mistake, and should not even compile.
Do you have your compiler warning turned up in your preferences?
By removing that, you're actually introducing bugs in your code.
Cheers
@frogandtoad I wonder what will happen if he removes 1 char from hello, that may change the memory addresses going from 5 to 4. I am still 100% convinced he has a buffer overflow issue that is stomping on some critical piece of memory. It's been decades since I used gcc, is there a compiler directive that will show the address map of the compiled code, and how do you pass compile time switches in the arduino IDE?
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
@frogandtoad I wonder what will happen if he removes 1 char from hello, that may change the memory addresses going from 5 to 4. I am still 100% convinced he has a buffer overflow issue that is stomping on some critical piece of memory. It's been decades since I used gcc, is there a compiler directive that will show the address map of the compiled code, and how do you pass compile time switches in the arduino IDE?
It's possible, but I did not review the whole code... the missing pointer was the first thing I spotted, which is 100% incorrect. There seems to be a whole mix of legacy C code with a sprinkle of C++ code here... a great recipe for problems 🙂
@frogandtoad IIRC, he did say he is very new so we may have a 'playing with sharp knives' issue here and the blood is obscuring the details.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
Thanks to all for your replies.
I think it’s best if I summarise what happened BEFORE I started hacking / adding code.
Code from the Drone Workshop’s 2 seperate examples (JSN-SR04T sensor in Mode1 )
and from the 433Mhz Tx/Rx using the Radiohead library Demo2 where Bill uses a DHT22.
Both these sketches compile without error and work fine on the same UNO if Uploaded and run separately.
No buffer overflow issues. No delay in transmission …. All good.
Movements in front of the Ultrasonic Sensor….. all responded quickly.
I proceeded to combine them without changing or adding any code.
The Ultrasonic code was gradually added to the original 433Mhz code.
In the void loop()section I added the Ultrasonic code before the Radio code.
So the only change I made then was to move the “{“ from the bottom of the Ultrasonic code
to the last line of the sketch.
No compile errors. No code additions, no alterations (except “{“) downloaded OK.
No response at the receiver.
After that, I started experimenting and then posted where I left off.
Which I apologise, must have been confusing.
I think next I will go back and change the “original merge” within the Void Loop() and place
the Radio code first, and the Ultrasonic code last, initially, without changing anything.
****** will get back to you sometime when I have done this. ******
Cheers for now. JimJtron 6/4/22.
Oh Dear! How utterly embarrassing. I found the problem. I made a copy of only the Radio 433Mhz code
that works with the DHT sensor. Slowly I added the declarations from the Ultrasonic sketch. (All OK)
Then from the Void setup sketch, each line of code uploaded so I could see the incoming messages still arrived on the other UNO. Until I added "mySerial.begin(9600);" Then it stopped sending. So what was it? Very simple. int pinRX = 11; int pinTX = 10; Wrong way around !!!!! Doh!!
Hours have been burned. Off to the PUB to drown my sorrows. Then back to finishing the project.
Regards, Village Idiot.
Update. Looks like I spoke too soon. With the pins around as original, the 433Mhz transmissions wont work but the Ultrasonic sensor does. Visa Versa. I just started to progress the new code and found that, after correcting the above, I had reliable transmissions to the other Uno. But turning on the Serial monitor at the Transmitter / Sensor Uno, I had no "Distance" measurements. So I changed to .... mySerial(11,10) and now I get distance measurements to the Serial Mon. but no longer get 433Mhz transmissions to the Receiver. Time to change the wiring ???