I am troubling about how to upload the data onto the Thingspeak channel. I am using Uno and wifi shield and updating the temperature and humidity using dht11. I had followed the tutorial and then change the code. But it cannot connect to Thingspeak. Hope that someone can point out the probelm, thanks for helping
coding:
#include <dht.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
#include <SPI.h>
#define dht_dpin 2
char ssid[] = "xxxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
int status = WL_IDLE_STATUS;
dht DHT;
// Local Network Settings
byte mac[] = {0xD0, 0x50, 0x99, 0x5C, 0x39, 0xB0}; // Must be unique on local network
#define IP "184.106.153.149"
char thingSpeakAddress[] = "api.thingspeak.com";
String APIKey = "xxxxxxxxxxxxxxx"; // enter your channel's Write API Key
const int updateThingSpeakInterval = 20 * 1000; // 20 second interval at which to update ThingSpeak
WiFiServer server(80);
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
// Initialize Arduino Ethernet Client
WiFiClient client;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the status:
printWifiStatus();
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void loop() {
int humidity = int (DHT.humidity);
int temp = int (DHT.temperature);
String temper = String(temp);
String humid = String(humidity);
DHT.read11(dht_dpin);
Serial.print("Humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
delay(1000);
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected) {
Serial.println("...disconnected");
Serial.println();
client.stop();
}
// Update ThingSpeak
if (!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval)) {
updateThingSpeak("field1=" + humid + "&field2=" + temper);
Serial.println(humid);
Serial.println(temper);
}
lastConnected = client.connected();
}
void updateThingSpeak(String tsData)
{
if(client.connect(thingSpeakAddress,80))
{
Serial.println("Posting...");
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+APIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
}