The code is awfully long, but here goes:
#include "ThingSpeak.h"
#include <SPI.h>
#include <WiFi101.h>
char ssid[] = "TripMate-4530"; // your network SSID (name)
char pass[] = "********"; // your network password
WiFiClient client;
#include <dht.h>
#define dht_apin A2 // Analog Pin sensor is connected to
dht DHT;
unsigned long myChannelNumber = 341384; //Put your channel number in here
const char * myWriteAPIKey = "*****************"; //Put your API key in here
uint8_t temperature, humidity;
void setup() {
Serial.begin(57600); // Make sure that the number in the bottom right of the Serial Monitor matches this
WiFi.setPins(8,7,4,2); // Setup the WiFi on the Feather boards
/* Start the WiFi connection */
Serial.println("Starting...");
Serial.println("Connecting to WiFi");
int conn = WiFi.begin(ssid, pass);
if( conn == WL_CONNECTED ) { Serial.println("OK!");}
else if( conn == WL_IDLE_STATUS ) {Serial.println("Idle");}
else {Serial.println("Unknown response");}
/* Now connect to ThingSpeak */
ThingSpeak.begin(client);
Serial.println("Started");
}
void loop() {
/* First of all, get the data and calculate the voltage */
// read the input on analog pin 0:
int sensorValue0 = analogRead(A0);
//The voltage can be between 0 and 3.3v, and gives readings from 0 to 1023
float Vout0 = sensorValue0 * (3.3 / 1024.0);
float RLDR0 = (1.0 * (3.3 - Vout0))/Vout0;
float Lux0 = (500.0/RLDR0);
//Check that a channel number has been set
if( myChannelNumber < 0 ) { Serial.println("Warning! No channel number set!"); }
/*
* Finally, try to write to ThingSpeak
* There are up to 8 fields in a channel, allowing you to store up to 8 different
* pieces of information. Here, we write to field 1.
*/
Serial.print("Writing Lux0" );
Serial.print(Lux0);
Serial.print(" to field 4 of channel " );
Serial.println(myChannelNumber);
int resp0 = ThingSpeak.writeField(myChannelNumber, 4, Lux0, myWriteAPIKey);
Serial.print("Response: ");
Serial.println(resp0);
delay(15000); // ThingSpeak will only accept updates every 15 seconds.
/* First of all, get the data and calculate the voltage */
// read the input on analog pin 1:
int sensorValue1 = analogRead(A1);
//The voltage can be between 0 and 3.3v, and gives readings from 0 to 1023
float Vout1 = sensorValue1 * (3.3 / 1024.0);
float RLDR1 = (1.0 * (3.3 - Vout1))/Vout1;
float Lux1 = (500.0/RLDR1);
//Check that a channel number has been set
if( myChannelNumber < 0 ) { Serial.println("Warning! No channel number set!"); }
/*
* Finally, try to write to ThingSpeak
* There are up to 8 fields in a channel, allowing you to store up to 8 different
* pieces of information. Here, we write to field 1.
*/
Serial.print("Writing Lux1" );
Serial.print(Lux1);
Serial.print(" to field 5 of channel " );
Serial.println(myChannelNumber);
int resp1 = ThingSpeak.writeField(myChannelNumber, 5, Lux1, myWriteAPIKey);
Serial.print("Response: ");
Serial.println(resp1);
delay(15000); // ThingSpeak will only accept updates every 15 seconds.
//Read temperature and humidity values from DHT sensor:
static boolean data_state = false;
float temperature;
temperature = dht.readTemperature();
float humidity;
humidity = dht.readHumidity();
Serial.print("Temperature Value is :");
Serial.print(temperature);
Serial.println("C");
Serial.print("Humidity Value is :");
Serial.print(humidity);
Serial.println("%");
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
if( data_state )
{
ThingSpeak.writeField(myChannelNumber, 5, temperature, myWriteAPIKey);
data_state = false;
}
else
{
ThingSpeak.writeField(myChannelNumber, 6, humidity, myWriteAPIKey);
data_state = true;
}
delay(15000); // ThingSpeak will only accept updates every 15 seconds.
}