Unsolved Serials Communication with Arduino Uno?
-
So as part of a testing rig I need to be able to send some data from the Duet 2 Wifi to an arduino uno. Things like nozzle temperature for example.
I spent a few hours last weekend trying to get any form of serial communication between these two devices and I didn't make much progress at all. Some broken or illegible messages here and there, but nothing consistent.
I have a level shifter for connecting them, have connected grounds together, and have TX to RX and RX to TX so i think the hardware wiring is ok, i'm just really struggling to see how I can setup some two way communication to request data from the duet and have it respond.
I am trying to connect via the panel duet pins and added this line to config:
;Serial port via PanelDue Pins M575 P1 B57600 S2
For the arduino:
int incomingByte = 0; // for incoming serial data void setup() { Serial.begin(57600); // opens serial port, sets data rate to 57600 bps } void loop() { // send data only when you receive data: while (Serial.available() > 0) { // read the incoming byte: incomingByte = Serial.read(); // say what you got: //Serial.print("Arudino received: "); //Serial.print(incomingByte, DEC); Serial.print((char)incomingByte); } //Serial.println("M117 ""This is Arduino"" \r\n"); //Serial.print("M117 ""This is Arduino"" \r\n"); //Serial.println("M117 ""This is Arduino"" \r"); //Serial.println("M118 P0 \"This is Arduino\" \r\n"); delay(5000); Serial.print("M115 \r\n"); delay(5000); }
You can see where I have commented out things as I've tried different attempts to get a response. I suspect i'm missing some fundamental steps here somewhere.
Any help would be much appreciated.
Thanks.
EDIT: I should add, the goal is to write this data to serial so it can be captured on my laptop along with other data and time signature that is happening on the Arduino so all the data can be aligned in time.
EDIT2: Is it a problem to try and communication via two serial types at once? If the Arduino is communicating via serial to the Duet, would that prevent a USB connection to a PC to log data?
-
@adam-v3d I don’t know if this will help but I played around with a project some time back that had an arduino communicating with a duet to set some neopixel rings based on things like the heater temp, print progress etc.
You can take a look here
https://github.com/Sindarius/Sindarius_Duet_Neopixels -
@adam-v3d you could comvert the duet 2 wifi to sbc mode. And then use a pi for it. Or use something like the node red plugin?
-
Would it help you to log paneldue status to USB serial? Using
M111 P4 S1
and thenM408 S3
or the likes?(Sorry, I mess up M111 all the time, should be right now)
One last edit: Logging documented here https://docs.duet3d.com/en/User_manual/Troubleshooting/Logging
-
@sindarius I think that will help. There is a lot of code there. It looks like main.cpp is the main bit that I need to look at. How often were you able to grab data from the Duet and how long does response take, any idea?
-
@pcr already got a good bunch of stuff working on the arduino which i'd rather keep. I'll keep that in mind though If I can't get it working, thanks.
-
@oliof That only raises the question, how do I then connect the the USB port to the arudino?
-
@sindarius I've read through all your code and I think I can modify that to do exactly what I need. One question, did you need anything in the duet configuration to get this to work, or does it automatically respond on serial?
-
This might be irrelevant depending on what you need, but you can use for example an ESP32 to call the Duet board via WiFi (if your Duet board supports WiFi ofc) and request data via a REST GET call.
Non functional snippet just as an example
void getDuetStatus() { if((wifiMulti.run() == WL_CONNECTED)) { http.begin(HOSTNAME, 80, REQUEST_STATUS_1_PATH); //HTTP int httpCode2 = http.GET(); if(httpCode2 >= 0) { // file found at server if(httpCode2 == HTTP_CODE_OK) { StaticJsonDocument<2048> doc; DeserializationError error = deserializeJson(doc, http.getStream()); if (error) { Serial.print("deserializeJson() failed: "); Serial.println(error.c_str()); return; } const char* status = doc["status"]; // "I" JsonObject coords = doc["coords"]; JsonArray coords_axesHomed = coords["axesHomed"]; int coords_axesHomed_0 = coords_axesHomed[0]; // 1 int coords_axesHomed_1 = coords_axesHomed[1]; // 1 int coords_axesHomed_2 = coords_axesHomed[2]; // 1 ...
I don't know if it's recommended but it's possible.
Of course you don't need the ESP32 at all if you can execute the calls directly from the PC
-
@adam-v3d I was more thinking of using your PC that you already log data to and leaving out the Arduino.
-
This is another Neopixel project: https://github.com/mule1972/NeoPixelBLVmgn
I wrote the readme for the project a while ago, and I think it outlines the hardware part of the serial communication with the Duet well.
You do need to enable the serial port on the Duet. See the "Changes to your Duet Configuration" section of the readme.(Edit: I see you've already figured that out! It is a great read though.) -
I've been trying with the first neopixel code to get some response from the duet but it doesn't seem to send anything back at all. I've confirmed the TX is still working as i connected up a panel due and it worked just fine.
This is clearly well out of my skill level but i think ardunio to duet is still the best way to do this.
-
Whats confusing me at the moment is that in duet.h there is this:
private: void ProcessBuffer(); int bufferIdx; byte* buffer; DynamicJsonDocument doc; #ifdef SOFTWARESERIAL AltSoftSerial duetSerial; //Software serial on pins 8/9 on Arduino Uno. #endif };
Which is about the json called doc which should hold all the data from the duet.
At the bottom of this:
void DuetData::ProcessBuffer() { //Process the json data Status = DUET_STATUS::UNKNOWN; //Scan for invalid characters for (int i = 0; i < bufferIdx - 1; i++) { if (buffer[i] < 32 || buffer[i] > 126) { #ifdef DEBUG Serial.println("Throwing out buffer due to bad characters"); #endif return; } } DeserializationError error = deserializeJson(doc, buffer); if (error.code() != error.Ok) { Status = DUET_STATUS::ERROR; Serial.println("Error"); Serial.print(error.c_str()); Serial.println(error.f_str()); return; //There was an error deserializing the json } const char *status = doc["status"]; switch (status[0]) { case 'I': Status = DUET_STATUS::IDLE; break; case 'P': Status = DUET_STATUS::PRINTING; break; case 'M': Status = DUET_STATUS::SIMULATING; break; case 'B': Status = DUET_STATUS::BUSY; break; default: Status = DUET_STATUS::ERROR; break; } Progress = doc["fraction_printed"]; //Hotend Temp1 hotendTemp = doc["heaters"][1]; fanSpeed = doc["fanPercent"][0];
The hotendTemp and fanspeed look like they are getting 'extracted' from doc and assigned to variables. But when i print those, they are still just zero, or whatever they were initially assigned.
-
@adam-v3d I'm on a bit deep water here as I haven't played much at all with the Arduino Uno but I remember that when I used the Uno as a USB to serial bridge to flash an ESP32 I had to connect RX(0) to RX and TX(1) to TX. Not as one should expect. Are the Uno pin mislabeled or maybe only my board (Clone) ?
-
@gixxerfast Im using a genuine Arduino. I have two elegoo clones too, but they are all labelled the same.
I have discovered from doing this that the RX and TX dedicated pins (0 and 1) seem to be the same as what is used to communicate with laptop via USB so the connection to the duet is using AltSoftSerial which is a library that adds software serial connection using pins 8 and 9 on the arduino.
-
@adam-v3d Switching my connection on pins 8 and 9 has changed things. This might be whats going on. More errors now, but different ones at least...
-
@adam-v3d Sorry you have to open the comms in your config.g after you have wired the arduino to a serial capable IO using M575. I lost some of my notes when the computer I wrote that on crashed. The code you probably want to look at is where the comms is handled in /src/DuetData.cpp that handles talking back and forth with the duet.
this video shows it showing the print % progress on a simulated g-code file on a Duet 3 Mini5+
https://www.youtube.com/watch?v=OjPsP5I6l70 -
@adam-v3d In that code I use AltSoftSerial package which on an arduino uno forces the use of pins 8/9 on the arduino uno. If you use a different board you need to look up the required pins. There is a package bblanchon/ArduinoJson@^6.17.2 that takes the json data sent from the Duet and turns it into a document that you can look up the ObjectModel values.
-
I found an old SD card which had the config
;Duet Mini5+
M575 P2 B57600 S0 -
@adam-v3d If this helps as a backup/alternative ---
Because you are already planning to capture the data on a laptop, you can log data easily with node-red & nodeDSF:
For example - The screen shot below show a csv which logged T0 temp with a time stamp (millisecond) as it cooled down..
It took less than 5 mins to put together and gives you access to nearly the entire object model in almost real-time over the network (it uses the same connection methods as DWC).