Mining on Teensy 4.1 (and some improvements for Arduino)
Hi! I've been working on mining with the Teensy 4.1 for a couple of days now. At the beginning it was a little bit hard because I don't have a direct way to know what's going on inside the Teensy since its serial port is being used by the python code. I even thought connecting another board to other serial port of the Teensy and send messages from there to the other board but didn't have too much time and didn't want to invest too much effort on this test to be honest. At the end I make it to work fine! So in order to make it work I have to change some things on the Arduino code and on the Python code too. Remember that the changes I made were only for the Teensy, I think some of them can be improvements for all the boards and other are just for the Teensy. Here is what I change:
Arduino code
- Include TeensyID library: This is something similar to what the uniqueID library do, it gets a unique ID for the microcontroller, you can even choose if you want to use the MAC address or the Serial ID. This is just for the Teensy.
- Line 53: replace strlen for .length(). In my experience I have find that dealing with pointers is not a trivial job and one have to be very careful with the functions related to memory allocation and sizes. The function strlen not always returns the size of the string if the string is referred as a pointer, this can change between compilers and platforms. Instead of doing this you can call newblockhash.length() at least you know that the String class of Arduino takes care of the sizes. This is for all boards.
- Line 55: Do the memory allocations just once. I moved the malloc function outside the ducos1a function, on the setup section to be specific and make the newblockhash1 pointer as global. I did the same thing with the ESP32. This is for all boards.
- Change the serial baudrate to 1000000: The Teensy 4.1 is quite powerful and can handle higher serial speeds. Your PC, laptop or Raspberry Pi can also handle this speed, I have tested it on a laptop with Windows and on a Raspberry Pi 4. You need to change the speed on the python code too for this to work out. This is for the Teensy or any other powerful board that can handle that speed (I had some problems using the ESP32 at this speed, I think 500000 is better for the ESP32).
- Line 63: sizeof(hash_bytes) should return just the size of the pointer, this will depend on the MCU, I think for the Teensy is four bytes. At the end this line of code is just comparing the first bytes of the hash and not everything, so if you leave it like this it should improve the speed but some jobs would be rejected because it's not comparing the whole hash. If you want to compare all the hash you should use "SHA1_HASH_LEN", I'm not sure where this comes from but I think this has the actual size of the hash. Please let me know if I'm wrong on this, right now is working but maybe I'm comparing too many bytes. This is for all the boards.
- Clear the serial receive buffer: This should be done after reading the job from the serial interface and before sending the result to the computer. I have found that if the board takes too much time to solve the job the computer will send the job again, this will stay in the buffer until the board finish the calculation and will read the same job again. This will be taken as a rejected job because the computer will be expecting the next job. To do this a Serial.flush is not enough, you have to read all the bytes inside the buffer with something like this:
while(Serial.available()) Serial.read();Then the buffer will be empty. This is for all the boards.
Python code
- Change the serial baudrate and timeout: In lines 260 and 261, this is to match with the baudrate of the Teensy and to wait a longer time for the board to respond. I'm not sure if it's OK for the Arduino UNO to wait that much, I'm sure that you chose this time just for the Arduino UNO or Mega or DUE, but for the Teensy is too low, since the Teensy can have a very high hashrate and the server assigns a high difficulty that results on a higher response time from the board.
- Use sendall instead of send for the sockets: I don't know the differences of this two at detail, but what I know is that if you use send the TCP messages could be segmented in more than one message, with sendall this is not the case or at least it performs better. I had a hard time to find this, I have looked for example codes on the internet and almost in every example for clients they use sendall, for servers they use send, don't ask me why haha I just find out that it is better like this.
- Improve the handling of the response from the server: As I said above, I had a hard time finding this. It seems that the server not always sends the feedback as fast as we want and sometimes we lose that message. The problem is that when the code lose the feedback it will appear inside the next job message mixed and this can cause rejected jobs too. For this I don't have an final solution, I have a temporary solution that works most of the times but sometimes it can be tricky, for example when the computer lose the connection this solution will get stuck on an infinite loop. Here is the code I'm using, replace this with line 973:
feedback = "" while (len(feedback)<3): sleep(0.05) feedback = feedback + socConn.recv(48).decode() debugOutput("Receiving feedback. Data received: "+feedback) feedback = feedback.rstrip("\n")
I have to say again that this is a temporary solution and it will cause problems for sure sometimes. I recommend to improve it, the most important thing is that the code needs to wait for the server response and it should get out if the conection is lost, that's all. Oh and I'm doing this everytime the socket do a recv (for the job and for the server version too).
- Commenting lines 900 to 902: This is an exception if the hashrate is too high lol I don't know why the code do this, I'm sure there is a reason, but for the Teensy this should be removed.
Well I have to say that with all the changes the Teensy is working just great, not one bad job from the server. On the other hand, since the server sees a high hashrate (between 70KH/s and 98KH/s) it increase the difficulty and the board takes too much time to solve all the jobs. They are all good but it's just too much time, sometimes 30 seconds or even 40 seconds. I'm not sure how can this be tuned because if I leave it like that then is just not proffitable to mine. So I added some delaymicroseconds on the Arduino code to lower the hashrate. Here are some results from the changes I made, and the modified hashrate:

Feel free to use and change my code, I'm posting it here with the Issue. As I said before there are some things that could improve all the boards and some that are just for the Teensy. Sorry for the long post, I like to think that it is always better to be specific if you are posting issues about the code of someone else, take it as a sign of respect, and I hope this can help at least someone. Thanks!
Damn, nicely explained, @JoyBed will be interested too in hearing these tips
I've added the Teensy code to the unofficial miners folder. Closing for now.