Calculating the Power spectral density
Well, first of all you should note that you are taking batches of 512 samples. It basically means that the first FFT will perform a transform of the first 512 samples, slightly more than a quarter of a period, assuming that signal is periodic. (The windowing tries to compensate for this to some extent.) However, the information from that signal is not really what brings you any information about the spectrum.

While I haven't read the code, I suggest that you try one of these things (maybe both):


Increase the signal frequency
Increase the window length


Ideally, there should be at least one full period of the sinusoidal in the window if you want to interpret the result in a straightforward way.