#define tempPin 32
#define luxPin 34
struct sensor {
int deviceId;
int measurementType;
float value;
};
TaskHandle_t printHandle = NULL;
TaskHandle_t readHandle = NULL;
QueueHandle_t queue;
void readSensors(void*) {
while (1) {
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
int analogValue = analogRead(tempPin);
float celsius = 1 / (log(1 / (4095. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
sensor tempSensor = {
tempPin,
1,
celsius
};
xQueueSend(queue, &tempSensor, (TickType_t)0);
// These constants should match the photoresistor's "gamma" and "rl10" attributes
const float GAMMA = 0.7;
const float RL10 = 50;
// Convert the analog value into lux value:
analogValue = analogRead(luxPin);
float voltage = analogValue / 4095. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
sensor luxSensor = {
luxPin,
2,
lux
};
xQueueSend(queue, &luxSensor, (TickType_t)0);
vTaskDelay(2000 / portTICK_RATE_MS);
}
}
void printSensors(void*) {
sensor sensorRecv;
while (1) {
if ( xQueueReceive(queue, &(sensorRecv), (TickType_t)5))
{
printf("================================\n");
printf(" Received data from queue\n");
printf("Sensor id: %d\n", sensorRecv.deviceId);
printf("Sensor type: %d\n", sensorRecv.measurementType);
printf("Sensor value: %.2f\n", sensorRecv.value);
printf("================================\n\n");
vTaskDelay(500 / portTICK_RATE_MS);
}
}
}
void setup() {
queue = xQueueCreate( 10, sizeof( struct sensor ) );
xTaskCreate(readSensors, "Read Sensor", 4096, NULL, 1, &readHandle);
xTaskCreatePinnedToCore(printSensors, "Print Sensor", 4096, NULL, 1, &printHandle, 1);
}
void loop() {
delay(1000);
}