Working with Raspberry Pi 3 + Windows 10 IoT Core + DHT22 - windows-10-iot-core

Wondering if there is any sample wiring and code for Raspberry Pi 3 + Windows 10 IoT Core + DHT22 ? Thanks!

if you go to the tutorials tab it will show you some sample code and wiring for lighting an LED.
I hope this answered your question.
CodeMaster5678

static class DHT22
{
private const int intGpioData = 18;
private static GpioPin gpioData;
public static void Start()
{
//Init GPIO And Capture Data Every 2000ms
GpioController gpioController = GpioController.GetDefault();
gpioData = gpioController.OpenPin(intGpioData);
gpioData.SetDriveMode(GpioPinDriveMode.InputPullUp);
new Timer(new TimerCallback((obj) => { GetData(); }), null, 2000, 2000);
}
private static void GetData()
{
byte[] data = new byte[40];
gpioData.SetDriveMode(GpioPinDriveMode.Output);
gpioData.Write(GpioPinValue.Low);
Task.Delay(1).Wait();
gpioData.SetDriveMode(GpioPinDriveMode.InputPullUp);
//Record Data
while (gpioData.Read() == GpioPinValue.High) ;
while (gpioData.Read() == GpioPinValue.Low) ;
while (gpioData.Read() == GpioPinValue.High) ;
byte low;
for (int i = 0; i < 40; i++)
{
low = 0;
data[i] = 0;
while (gpioData.Read() == GpioPinValue.Low && low <= byte.MaxValue)
low++;
while (gpioData.Read() == GpioPinValue.High && data[i] <= byte.MaxValue)
data[i]++;
}
//Analyze Data
byte humiH = 0;
byte humiL = 0;
byte tempH = 0;
byte tempL = 0;
byte sum = 0;
for (short i = 7; i >= 0; i--)
{
byte bit = data[7 - i] >= 11 ? (byte)1 : (byte)0;
humiH += (byte)(bit << i);
}
for (short i = 7; i >= 0; i--)
{
byte bit = data[15 - i] >= 11 ? (byte)1 : (byte)0;
humiL += (byte)(bit << i);
}
for (short i = 7; i >= 0; i--)
{
byte bit = data[23 - i] >= 11 ? (byte)1 : (byte)0;
tempH += (byte)(bit << i);
}
for (short i = 7; i >= 0; i--)
{
byte bit = data[31 - i] >= 11 ? (byte)1 : (byte)0;
tempL += (byte)(bit << i);
}
for (short i = 7; i >= 0; i--)
{
byte bit = data[39 - i] >= 11 ? (byte)1 : (byte)0;
sum += (byte)(bit << i);
}
//Verify Data
if ((byte)(humiH + humiL + tempH + tempL) == sum)
{
double humidity = (double)(humiH * 256 + humiL) / 10;
double temperature = (double)(tempH * 256 + tempL) / 10;
Debug.WriteLine(humidity + "% " + temperature + "°C");
}
}
}

If you are using C++, here is an example.
https://github.com/Microsoft/Windows-iotcore-samples/tree/develop/Samples/GpioOneWire
Also, if you want to stick to C#, this does work. I used this today with 2 dht11 sensors.
This is the github link:
https://github.com/porrey/Dht
Nuget package:
https://www.nuget.org/packages/Dht/
If you are comfortable with C++, then use that. If not I recommend the Nuget package, only because I verified that it works today. The dht11 sensors seem to have a lot of variance. I have a 3 degree different on two sensors.

Related

AS3 - Numbers won't display in dynamic text field

I am writing a program that my brother needs for a project he is working on. It "solves" a password and then shows the number of loops the program goes through before figuring out the solution. I've gotten everything working but the program won't show the numbers in the dynamic text field. It only shows the letters.
Here's the code:
import flash.events.MouseEvent;
button1.addEventListener(MouseEvent.CLICK, solvePassword);
function solvePassword(e:MouseEvent):void
{
var pass:Number = Number(inputText.text);
var attempts:Number = 0;
var i:String = pass.toString();
var passLength:Number = i.length;
var n:Number = 0;
while(n != pass)
{
if(passLength == 1)
{
n += 1;
attempts += 1;
}
if(passLength == 2)
{
n += 1;
if(n < 10) { n = 10 }
attempts += 1;
}
if(passLength == 3)
{
n+=1;
if(n < 100) { n = 100 }
attempts += 1;
}
if(passLength == 4)
{
n+=1;
if(n< 1000) { n = 1000 }
attempts += 1;
}
if(passLength == 5)
{
n+=1;
if(n < 10000) { n = 10000 }
attempts += 1;
}
if(passLength == 6)
{
n+=1;
if(n < 100000) { n = 100000 }
attempts += 1;
}
if(passLength == 7)
{
n+=1;
if(n < 1000000) { n = 1000000 }
attempts += 1;
}
}
i = "Attempts: " + String(attempts);
trace(i);
aBox.text = i;
Any info would help, thank you!

Who to talk to about non recursive Ackermann function?

I have written a non recursive solution to the Ackermann function, it seems to work perfectly and work faster than the common recursive solution. So I am confused as to why it is a non primitive recursive function if it can be solved iteratively? Could anyone tell me if I have misunderstood something about what primitive recursive functions are or who should I talk to about this to get an answer?
Below is the Java code:
import java.util.Scanner;
import java.util.ArrayList;
public class ackermann {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter m:");
int m = in.nextInt();
System.out.println("Enter n:");
int n = in.nextInt();
ack(m, n);
}
public static void ack(int inM, int inN){
if(inM < 0 || inN < 0) return;
ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>();
for(int m = 0; m <= inM; m++){
arr.add(new ArrayList<Integer>());
}
Boolean done = false;
while(done == false){
for(int m = 0; m <= inM; m++){
int n = arr.get(m).size();
int a = 0;
if(m == 0) a = n + 1;
else if(n == 0){
if(arr.get(m - 1).size() <= 1) break;
a = arr.get(m - 1).get(1);
} else {
int k = arr.get(m).get(n - 1);
if(arr.get(m - 1).size() <= k) break;
a = arr.get(m - 1).get(k);
}
arr.get(m).add(a);
if(m == inM && n == inN){
System.out.println("Ack(" + inM + ", " + inN + ") = " + a);
done = true;
break;
}
}
}
}
}
Primitive recursive functions can be implemented using only assignment, +, and definite loops. By this I mean loops of the form:
for(int i = 0; i < n; i++) { ... }
Where n is a variable that isn't changed in the loop body. To get Ackermann's function, which majorizes all primitive recursive functions, one needs to add either a goto command or indefinite loops like your while loop.

FFTW - computing the IFFT without first computing an FFT

This may seem like a simple question but I've been trying to find the answer on the FFTW page and I am unable to.
I created the FFTW plans for forward and backward transforms and I fed some data into the fftw_complex *fft structure directly (instead of computing an FFT from input data first). Then I compute an IFFT on this and the result is not correct. Am I doing this right?
EDIT: So what I did is the following:
int ht=2, wd=2;
fftw_complex *inp = fftw_alloc_complex(ht * wd);
fftw_complex *fft = fftw_alloc_complex(ht * wd);
fftw_complex *ifft = fftw_alloc_complex(ht * wd);
fftw_plan plan_f = fftw_plan_dft_1d(wd *ht, inp, fft, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_plan plan_b = fftw_plan_dft_1d(wd * ht, fft, ifft, FFTW_BACKWARD, FFTW_ESTIMATE );
for(int i =0 ; i < 2; i++)
{
for(int j = 0; j<2; j++)
{
inp[wd*i + j][0] = 1.0;
inp[wd*i + j][1] = 0.0;
}
}
// fftw_execute(plan_f);
for(int i =0 ; i < 2; i++)
{
for(int j = 0; j<2; j++)
{
fft[wd*i + j][1] = 0.0;
if(i == j == 0)
fft[wd*i+j][0] = 4.0;
else
fft[wd*i+j][0] = 0.0;
std::cout << fft[wd*i+j][0] << " and " << fft[wd*i+j][1] << std::endl;
}
}
fftw_execute(plan_b);
for(int i =0 ; i < 2; i++)
{
for(int j = 0; j<2; j++)
std::cout << ifft[wd*i+j][0]/(double)(wd*ht) << " and " << ifft[wd*i+j][1]/(double)(wd*ht) << std::endl;
}
This is the full code. The ifft should return [1 1 1 1] for the real part. It doesn't.
I did the stupidest thing - in the if condition I posted:
i == j == 0
instead of i ==0 && j == 0
when I fixed that, it works. Thank you all so much for helping me out.

Converting from Platform::String^ to std::string corrupts the string?

I am building a windows phone app and i added a windows phone runtime component with this function in order to decode a string which i provide. The same function seems to be working correctly in a console application if i provide an std::string and return the same type. I think my problem is that i am converting from one type to another in the wrong way, but i can't see what i am doing wrong!
String^ Base64Encoding::base64_decode(String^ someString) {
std::wstring ws1(someString->Data());
std::string encoded_string(ws1.begin(), ws1.end());
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i == 4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret += char_array_3[i];
i = 0;
}
}
if (i) {
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
}
std::wstring widestr = std::wstring(ret.begin(), ret.end());
const wchar_t* wcstring2 = widestr.c_str();
return ref new String(wcstring2);
}
So what am i doing wrong here?
Normally, the output of a Base64 decode isn't a string, it's a byte array. Base64 can encode arbitrary binary data, and depending on the string's encoding, not all byte arrays are valid strings.
To do a Base64 decode, use Convert::FromBase64String. If you want to interpret that byte array as a string, I suggest you use one of the Encoding classes, such as Encoding.Unicode, to do that conversion.

Cuda demoting double to float error despite no doubles in code

I'm writing a kernel using PyCUDA. My GPU device only supports compute capability 1.1 (arch sm_11) and so I can only use floats in my code. I've taken great effort to ensure I'm doing everything with floats, but despite that, there is a particular line in my code that keeps causing a compiler error.
The chunk of code is:
// Gradient magnitude, so 1 <= x <= width, 1 <= y <= height.
if( j > 0 && j < im_width && i > 0 && i < im_height){
gradient_mag[idx(i,j)] = float(sqrt(x_gradient[idx(i,j)]*x_gradient[idx(i,j)] + y_gradient[idx(i,j)]*y_gradient[idx(i,j)]));
}
Here, idx() is a __device__ helper function that returns a linear index based on pixel indices i and j, and it only works with integers. I use it throughout and it doesn't give errors anywhere else, so I strongly suspect it's not idx(). The sqrt() call is just from the standard C math functions which support floats. All of the arrays involved, x_gradient , y_gradient, and gradient_mag are all float* and they are part of the input to my function (i.e. declared in Python, then converted to device variables, etc.).
I've tried removing the extra cast to float in my code above, with no luck. I've also tried doing something completely stupid like this:
// Gradient magnitude, so 1 <= x <= width, 1 <= y <= height.
if( j > 0 && j < im_width && i > 0 && i < im_height){
gradient_mag[idx(i,j)] = 3.0f; // also tried float(3.0) here
}
All of these variations give the same error:
pycuda.driver.CompileError: nvcc said it demoted types in source code it compiled--this is likely not what you want.
[command: nvcc --cubin -arch sm_11 -I/usr/local/lib/python2.7/dist-packages/pycuda-2011.1.2-py2.7-linux-x86_64.egg/pycuda/../include/pycuda kernel.cu]
[stderr:
ptxas /tmp/tmpxft_00004329_00000000-2_kernel.ptx, line 128; warning : Double is not supported. Demoting to float
]
Any ideas? I've debugged many errors in my code and was hoping to get it working tonight, but this has proved to be a bug that I cannot understand.
Added -- Here is a truncated version of the kernel that produces the same error above on my machine.
every_pixel_hog_kernel_source = \
"""
#include <math.h>
#include <stdio.h>
__device__ int idx(int ii, int jj){
return gridDim.x*blockDim.x*ii+jj;
}
__device__ int bin_number(float angle_val, int total_angles, int num_bins){
float angle1;
float min_dist;
float this_dist;
int bin_indx;
angle1 = 0.0;
min_dist = abs(angle_val - angle1);
bin_indx = 0;
for(int kk=1; kk < num_bins; kk++){
angle1 = angle1 + float(total_angles)/float(num_bins);
this_dist = abs(angle_val - angle1);
if(this_dist < min_dist){
min_dist = this_dist;
bin_indx = kk;
}
}
return bin_indx;
}
__device__ int hist_number(int ii, int jj){
int hist_num = 0;
if(jj >= 0 && jj < 11){
if(ii >= 0 && ii < 11){
hist_num = 0;
}
else if(ii >= 11 && ii < 22){
hist_num = 3;
}
else if(ii >= 22 && ii < 33){
hist_num = 6;
}
}
else if(jj >= 11 && jj < 22){
if(ii >= 0 && ii < 11){
hist_num = 1;
}
else if(ii >= 11 && ii < 22){
hist_num = 4;
}
else if(ii >= 22 && ii < 33){
hist_num = 7;
}
}
else if(jj >= 22 && jj < 33){
if(ii >= 0 && ii < 11){
hist_num = 2;
}
else if(ii >= 11 && ii < 22){
hist_num = 5;
}
else if(ii >= 22 && ii < 33){
hist_num = 8;
}
}
return hist_num;
}
__global__ void every_pixel_hog_kernel(float* input_image, int im_width, int im_height, float* gaussian_array, float* x_gradient, float* y_gradient, float* gradient_mag, float* angles, float* output_array)
{
/////
// Setup the thread indices and linear offset.
/////
int i = blockDim.y * blockIdx.y + threadIdx.y;
int j = blockDim.x * blockIdx.x + threadIdx.x;
int ang_limit = 180;
int ang_bins = 9;
float pi_val = 3.141592653589f; //91
/////
// Compute a Gaussian smoothing of the current pixel and save it into a new image array
// Use sync threads to make sure everyone does the Gaussian smoothing before moving on.
/////
if( j > 1 && i > 1 && j < im_width-2 && i < im_height-2 ){
// Hard-coded unit standard deviation 5-by-5 Gaussian smoothing filter.
gaussian_array[idx(i,j)] = float(1.0/273.0) *(
input_image[idx(i-2,j-2)] + float(4.0)*input_image[idx(i-2,j-1)] + float(7.0)*input_image[idx(i-2,j)] + float(4.0)*input_image[idx(i-2,j+1)] + input_image[idx(i-2,j+2)] +
float(4.0)*input_image[idx(i-1,j-2)] + float(16.0)*input_image[idx(i-1,j-1)] + float(26.0)*input_image[idx(i-1,j)] + float(16.0)*input_image[idx(i-1,j+1)] + float(4.0)*input_image[idx(i-1,j+2)] +
float(7.0)*input_image[idx(i,j-2)] + float(26.0)*input_image[idx(i,j-1)] + float(41.0)*input_image[idx(i,j)] + float(26.0)*input_image[idx(i,j+1)] + float(7.0)*input_image[idx(i,j+2)] +
float(4.0)*input_image[idx(i+1,j-2)] + float(16.0)*input_image[idx(i+1,j-1)] + float(26.0)*input_image[idx(i+1,j)] + float(16.0)*input_image[idx(i+1,j+1)] + float(4.0)*input_image[idx(i+1,j+2)] +
input_image[idx(i+2,j-2)] + float(4.0)*input_image[idx(i+2,j-1)] + float(7.0)*input_image[idx(i+2,j)] + float(4.0)*input_image[idx(i+2,j+1)] + input_image[idx(i+2,j+2)]);
}
__syncthreads();
/////
// Compute the simple x and y gradients of the image and store these into new images
// again using syncthreads before moving on.
/////
// X-gradient, ensure x is between 1 and width-1
if( j > 0 && j < im_width){
x_gradient[idx(i,j)] = float(input_image[idx(i,j)] - input_image[idx(i,j-1)]);
}
else if(j == 0){
x_gradient[idx(i,j)] = float(0.0);
}
// Y-gradient, ensure y is between 1 and height-1
if( i > 0 && i < im_height){
y_gradient[idx(i,j)] = float(input_image[idx(i,j)] - input_image[idx(i-1,j)]);
}
else if(i == 0){
y_gradient[idx(i,j)] = float(0.0);
}
__syncthreads();
// Gradient magnitude, so 1 <= x <= width, 1 <= y <= height.
if( j < im_width && i < im_height){
gradient_mag[idx(i,j)] = float(sqrt(x_gradient[idx(i,j)]*x_gradient[idx(i,j)] + y_gradient[idx(i,j)]*y_gradient[idx(i,j)]));
}
__syncthreads();
/////
// Compute the orientation angles
/////
if( j < im_width && i < im_height){
if(ang_limit == 360){
angles[idx(i,j)] = float((atan2(y_gradient[idx(i,j)],x_gradient[idx(i,j)])+pi_val)*float(180.0)/pi_val);
}
else{
angles[idx(i,j)] = float((atan( y_gradient[idx(i,j)]/x_gradient[idx(i,j)] )+(pi_val/float(2.0)))*float(180.0)/pi_val);
}
}
__syncthreads();
// Compute the HoG using the above arrays. Do so in a 3x3 grid, with 9 angle bins for each grid.
// forming an 81-vector and then write this 81 vector as a row in the large output array.
int top_bound, bot_bound, left_bound, right_bound, offset;
int window = 32;
if(i-window/2 > 0){
top_bound = i-window/2;
bot_bound = top_bound + window;
}
else{
top_bound = 0;
bot_bound = top_bound + window;
}
if(j-window/2 > 0){
left_bound = j-window/2;
right_bound = left_bound + window;
}
else{
left_bound = 0;
right_bound = left_bound + window;
}
if(bot_bound - im_height > 0){
offset = bot_bound - im_height;
top_bound = top_bound - offset;
bot_bound = bot_bound - offset;
}
if(right_bound - im_width > 0){
offset = right_bound - im_width;
right_bound = right_bound - offset;
left_bound = left_bound - offset;
}
int counter_i = 0;
int counter_j = 0;
int bin_indx, hist_indx, glob_col_indx, glob_row_indx;
int row_width = 81;
for(int pix_i = top_bound; pix_i < bot_bound; pix_i++){
for(int pix_j = left_bound; pix_j < right_bound; pix_j++){
bin_indx = bin_number(angles[idx(pix_i,pix_j)], ang_limit, ang_bins);
hist_indx = hist_number(counter_i,counter_j);
glob_col_indx = ang_bins*hist_indx + bin_indx;
glob_row_indx = idx(i,j);
output_array[glob_row_indx*row_width + glob_col_indx] = float(output_array[glob_row_indx*row_width + glob_col_indx] + float(gradient_mag[idx(pix_i,pix_j)]));
counter_j = counter_j + 1;
}
counter_i = counter_i + 1;
counter_j = 0;
}
}
"""
Here's an unmistakable case of using doubles:
gaussian_array[idx(i,j)] = float(1.0/273.0) *
See the double literals being divided?
But really, use float literals instead of double literals cast to floats - the casts are ugly, and I suggest they will hide bugs like this.
-------Edit 1/Dec---------
Firstly, thanks #CygnusX1, constant folding would prevent that calculation - I didn't even think of it.
I've tried to reproduce the environment of the error: I installed the CUDA SDK 3.2 (That #EMS has mentioned they seem to use in the lab), compiling the truncated kernel version above, and indeed nvopencc did optimize the above calculation away (thanks #CygnusX1), and indeed it didn't use doubles anywhere in the generated PTX code. Further, ptxas didn't give the error received by #EMS. From that, I thought the problem is outside of the every_pixel_hog_kernel_source code itself, perhaps in PyCUDA. However, using PyCUDA 2011.1.2 and compiling with that still does not produce a warning like in #EMS's question. I can get the error in the question, however it is by introducing a double calculation, such as removing the cast from gaussian_array[idx(i,j)] = float(1.0/273.0) *
To get to the same python case, does the following produce your error:
import pycuda.driver as cuda
from pycuda.compiler import compile
x=compile("""put your truncated kernel code here""",options=[],arch="sm_11",keep=True)
It doesn't produce an error in my circumstance, so there is a possibility I simply can't replicate your result.
However, I can give some advice. When using compile (or SourceModule), if you use keep=True, python will print out the folder where the ptx file is being generated just before showing the error message.
Then, if you can examine the ptx file generated in that folder and looking where .f64 appears it should give some idea of what is being treated as a double - however, deciphering what code that is in your original kernel is difficult - having the simplest example that produces your error will help you.
Your problem is here:
angle1 = 0.0;
0.0 is a double precision constant. 0.0f is a single precision constant.
(a comment, not an answer, but it is too big to put it as a comment)
Could you provide the PTX code around the line where the error occurs?
I tried compiling a simple kernel using the code you provided:
__constant__ int im_width;
__constant__ int im_height;
__device__ int idx(int i,int j) {
return i+j*im_width;
}
__global__ void kernel(float* gradient_mag, float* x_gradient, float* y_gradient) {
int i = threadIdx.x;
int j = threadIdx.y;
// Gradient magnitude, so 1 <= x <= width, 1 <= y <= height.
if( j > 0 && j < im_width && i > 0 && i < im_height){
gradient_mag[idx(i,j)] = float(sqrt(x_gradient[idx(i,j)]*x_gradient[idx(i,j)] + y_gradient[idx(i,j)]*y_gradient[idx(i,j)]));
}
}
using:
nvcc.exe -m32 -maxrregcount=32 -gencode=arch=compute_11,code=\"sm_11,compute_11\" --compile -o "Debug\main.cu.obj" main.cu
got no errors.
Using the CUDA 4.1 beta compiler
Update
I tried compiling your new code (I am working within CUDA/C++, not PyCUDA, but this shouldn't matter). Didn't catch the error either! Used CUDA 4.1 and CUDA 4.0.
What is your version of CUDA installation?
C:\>nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2011 NVIDIA Corporation
Built on Wed_Oct_19_23:13:02_PDT_2011
Cuda compilation tools, release 4.1, V0.2.1221