Wifi Shield to Thingspeak - Connection to ThingSpeak Failed (1)

Hi all,

I am using an Arduino Uno, Arduino Wifi Shield(now retired). I am trying to send data from a temperature sensor to thing speak. But i can’t seen to connect to thingspeak.

I have tried many other programs, but i still can’t solve this issue. Please help me!
code is attached.
i thought it’s something to do with the IP address.

#include <SPI.h>
#include <WiFiClient.h>
#include <Math.h>
#include <WiFi.h>
#include <ThingSpeak.h>

//handphone hotspot ID:jerrylua
//handphone hotspot pass:11111111
// Local Network Settings
byte mac[] = { 0x78, 0xC4, 0x0E, 0x03, 0xA8, 0x69}; // Mobile Hotspot MAC Address
char ssid[] = "Cumaran";  //  your network SSID (name)
char pass[] = "Abc123456";  // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status
//IPAddress server(192,168,43,200); // Mobile Hotspot IP address


// ThingSpeak Settings
char thingSpeakAddress[] = "https://thingspeak.com/channels/66512"; 
byte server[] = { 184, 106, 153, 149 };// ThingSpeak MAC address
String writeAPIKey = "UJ4IU5MBYMA49QGT"; // API Write Key
String channelID = "66512"; // ThingSpeak Channel
const
 int updateThingSpeakInterval = 10 * 1000;  // Time interval in 
milliseconds to update ThingSpeak (number of seconds * 1000 = interval)

// Variable Setup
long lastConnectionTime = 0; 
boolean lastConnected = false;
int failedCounter = 0;
int resetCounter; // Declare resetCounter 
int tempPin = 0; // temperature sensor input to A0
float tempC; // Declare Temperature  

// Initialize Arduino WiFi Client library
WiFiClient client; 

void setup() {
  //Initialize serial and wait for port to open:
  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);
  }
  String fv = WiFi.firmwareVersion();
  if ( fv != "1.0.0" )
    Serial.println("Please upgrade the firmware");
    else
    Serial.println("WiFi Shield firmware updated!");

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  
  // you're connected now, so print out the data:
  Serial.print("You're connected to the network");
  printCurrentNet();
  printWifiData();
}

void loop() 
{ 
  // Print Update Response to Serial Monitor
  if (client.available()&& status == WL_CONNECTED)
  {
    char c = client.read();
    Serial.print(c);
  }
  
  // Update ThingSpeak server
  (!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval));
  
    updateThingSpeak("field1="+(byte)tempC); // reading of the signal to send to thinkspeak field 1 aka the graph display
  
  // check the network connection once every 10 seconds:
  delay(10000);
  printCurrentNet();

  int reading= analogRead(tempPin); // reading input from A0 Pin
  float voltage= (reading*5.0)/1024.0; // conversion of signal to voltage/bit
  float tempC= (voltage-0.5)*100; // conversion to Degree Celsuis
  Serial.print((byte)tempC); Serial.println(" Degree C"); //display in serial monitor
  delay(1000);
  client.flush();
   client.stop();
  
  // 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.
//  ThingSpeak.writeField(channelID, 1, tempC, writeAPIKey);
//  delay(20000); // ThingSpeak will only accept updates every 15 seconds.
}
/*------------------------------------------------
Sends sensor data to Thingspeak
Inputs: String, data to be entered for each field
Returns: 
------------------------------------------------*/
void updateThingSpeak(String tsData) //
{
  if (client.connect(thingSpeakAddress,80 ))
  { 
      String postStr = "UJ4IU5MBYMA49QGT";
           postStr +="&field1=";
           postStr += String((byte)tempC);
           postStr += "\r\n\r\n";        
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: UJ4IU5MBYMA49QGT\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();

          lastConnectionTime = millis(); 
    }
  }
  else
  {
    failedCounter++;
    
    Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");   
    Serial.println();
    
    lastConnectionTime = millis(); 
  }
}
void printWifiData() {
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  Serial.println(ip);

  // print your MAC address:
  byte mac[6];
  WiFi.macAddress(mac);
  Serial.print("MAC address: ");
  Serial.print(mac[5], HEX);
  Serial.print(":");
  Serial.print(mac[4], HEX);
  Serial.print(":");
  Serial.print(mac[3], HEX);
  Serial.print(":");
  Serial.print(mac[2], HEX);
  Serial.print(":");
  Serial.print(mac[1], HEX);
  Serial.print(":");
  Serial.println(mac[0], HEX);

}

void printCurrentNet() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the MAC address of the router you're attached to:
  byte bssid[6];
  WiFi.BSSID(bssid);
  Serial.print("BSSID: ");
  Serial.print(bssid[5], HEX);
  Serial.print(":");
  Serial.print(bssid[4], HEX);
  Serial.print(":");
  Serial.print(bssid[3], HEX);
  Serial.print(":");
  Serial.print(bssid[2], HEX);
  Serial.print(":");
  Serial.print(bssid[1], HEX);
  Serial.print(":");
  Serial.println(bssid[0], HEX);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);

  // print the encryption type:
  byte encryption = WiFi.encryptionType();
  Serial.print("Encryption Type:");
  Serial.println(encryption, HEX);
  Serial.println();
}

Connect_to_hotspot_191115_1800.ino (6.36 KB)

The arduino does not support secure https:// connections.

Hi Shannon,

Thanks for helping me out!.

I just have a quick question. If Arduino Wifi does not support http connection, then does that mean that i would not be able to connect to almost all interfaces online?

You can connect to http (port 80) web servers. You cannot connect to https secure SSL servers (port 443).

If Arduino Wifi does not support http connection,

You need to understand that there is a difference between “http” and “https”.

Do you have the latest firmware for the Wi-fi shield. I found I had to update it. Can you get the basic cheerlights example running?