Please I am trying to use an arduino Due and the arduino wifi shield to send random generated numbers on the thingspeak server. I have already created a channel on the thingspeak server but it is not working.Please can anyone be of help? Below is the arduino code.
#include <SPI.h>
#include <WiFi.h>
// Local Network Settings
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "Redmi"; // your network SSID (name)
char pass[] = "08$boateng$01"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String APIKey = "2116N63A3ZTG791L"; // enter your channel's Write API Key
const int updateThingSpeakInterval = 20 * 1000; // 20 second interval at which to update ThingSpeak
// Variable Setup
long lastConnectionTime = 1;
boolean lastConnected = false;
long randNumber;
// Initialize Arduino Ethernet Client
WiFiClient client;
void setup() {
// Start Serial for debugging on the Serial Monitor
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();
}
int i;
int valSensor;
void loop(){
//for(i=1;i<1000;i++){
randNumber=random(100000); //generate a random number
//Serial.println(randNumber);}
// read values from pins and store as strings
//String light = String(analogRead(A0), DEC); // read light value
// find temp value
//float voltage = analogRead(A1) * (3.3 / 1024); // convert 0-1023 range to 3.3V range
//int tempVal = (voltage - 0.5) * 100; // convert voltage to temperature in *C
//String temp = String(tempVal);
// Print Update Response to Serial Monitor
valSensor = random(100000); // random value, change with sensor value if using sensor
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("field 1=" + valSensor );
Serial.println(valSensor);
//Serial.println(temp);
}
lastConnected = client.connected();
}
void updateThingSpeak(String tsData) {
if (client.connect(thingSpeakAddress, 80)) {
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();
}
}
}
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");
}