Serials Communication with Arduino Uno?
-
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).
-
Here's a short video that shows the project that I did. https://www.youtube.com/watch?v=WQDmI404N5k
-
@sindarius I think I kind of understood most of the code, only little bits of syntax I didn't understand. I turn on debugging and the end result was that it either paused completely after 500ms (first time it polled the duet) or ran mostly correctly but hotend temps and fan always showed the original values in the library of 10, 285 etc.
I guess this means there was no data coming from the Duet. Maybe wrong port number. I used M575 P1 B57600 S0 and connector to the RX and TX on the Panel due 4 pin connector next to the 4 pin probe connector.
-
@mintytrebor This is cool, I guess its easy when you know how. I've never used this before though and not sure how I would sync it up with the other data from the arduino.
-
@sindarius Oh i did get the thing with pins 8 and 9 after realising what the library was for and that the designated TX and RX pins are basically the same serial i'm using to connect to the laptop.
For connection, do you connect duet TX to pin 8, and duet RX to pin 9? -
@mintytrebor I've just installed and had a little look at this. I quite like it and might be more my speed but don't understand it really yet. It looks like it'll be capable of polling the arduino and duet at the same time and just merging them into a single file on the pc which could be useful. Maybe also a powerful option for when i'm adding more features later down the line.
Are you able to share details on what you showed here?
-
@adam-v3d you mentioned you are using a duet 2 wifi but I didn’t see what firmware version you are running on the duet
-
@adam-v3d Sure.
There is an attempt at a wiki for the nodes used to connect to the duet here :
https://github.com/MintyTrebor/NodeDSF/wiki/AboutI also have a nodeDSF channel over on TeamGloomy's discord
https://discord.gg/bXcYqGvtThe quick POC I threw together earlier, just pulls the T0 temp from the Duet object model ~5 times a second, adds a time stamp, and writes a line to a csv file. That's it. There is an import/export function in nodered, so I can share the POC with you if its useful.
I have not used nodered to talk to arduino, there is a nodered faq that covers it here:
https://nodered.org/docs/faq/interacting-with-arduino. But if all you need is serial connectivity, then something like this (https://flows.nodered.org/node/node-red-node-serialport) will probably work..
-
@adam-v3d you might like to take a look at the CN Pendant project at https://github.com/Duet3D/CNC-Pendant-Firmware. This communicates to the Duet through the serial port.
If you will he using the USB interface to the Arduino as well, then I suggest you use an Arduino Leonardo, Arduino Pro Micro, or Arduino Mega. All of these give you a hardware serial port that is separate from the USB port. Also, the Leonardo and the Pro Micro accept 3.3V input signals, so although you need a simple resistive divider to reduce the Tx output of the Arduino from 5V to 3.3V, you don't need to level shift the output from the Duet up to 5V - although I suggest you use a 1K series resistor.
-
@adam-v3d
Attached is a example flow you can import which creates 2 connections to 2 different control boards, 1 via serial com port (over usb), the second over the network using the NodeDSF nodes.For the example:
Serial Connection Board 1: Send the M409 command via serial to get the chamber temp from the object model, and request a new update every 200ms, writing the values to a csv file.
Network Connection Board 2: Connect to board, and get updated bed temp every 200ms and write result to different csv file.You can then combine this data in whatever tool is required (excel etc).
If you want to push it further you can map the data to a chart in real-time in node-red using dashboard nodes eg
Flow in node-red
-
@mintytrebor thankyou!
-
@adam-v3d Gave the node red thing a pretty good shot today, spent some time learning how it works and managed to get it working and sending back data. In the current state I have it, on a 200ms request, it take anywhere from 227ms to over 1600ms to get a response when idle and also seems to be crashing the duet quite often too which would make the test void so going to abandon that idea. Currently I can get reading every 25.5ms +-0.5ms with the arduino and would like to keep that kind of rate and precision where possible.
I got some new level shifters that seem to work a bit better so i'm going to give the direct serial connection another go and also try with the arduino mega if It doesn't go well with the uno.
-
@adam-v3d said in Serials Communication with Arduino Uno?:
on a 200ms request, it take anywhere from 227ms to over 1600ms to get a response when idle and also seems to be crashing the duet quite often too
Out of interest is that using the serial connection node?