Data is not getting uploaded to the thingspeak server. | General | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
Data is not getting uploaded to the thingspeak server.
Avatar
shivam00408
New Member
Forum Posts: 1
sp_UserOfflineSmall Offline
1
October 10, 2017 - 4:58 pm
sp_Permalink sp_Print

Any help would be much appreciated.

Thanks in Advance

For the following code:

#include <ESP8266WiFi.h>

const char* ssid     = "iPhone";
const char* password = "iamafreeloader";

const char* host = "api.thingspeak.com";
char thingSpeakAddress[] = "api.thingspeak.com";

String writeAPIKey = "7T1NBNS3JH4XOHJV";
const int updateThingSpeakInterval = 3 * 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 pulseCount = 0;
int count = 0;
int pulseTime=0;
int prevMillis=0;
int treshold=50;
unsigned long EndTime;
//int time;
boolean pulse;
float totalkWh;
int curWatts;

float myvalue = 1234;
// Initialize Arduino Ethernet Client
WiFiClient client;

void setup()
{
  // Start Serial for debugging on the Serial Monitor
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(13, OUTPUT);
  //int time = millis();
  EndTime = millis() +60000;

  // Start Ethernet on Arduino
  startEthernet();
}

void loop()
{
  // Read value from Analog Input Pin 0
  //String analogValue0 = String(500);//String(myvalue++, DEC);
 
  int sensor = analogRead(A0);
  Serial.println(sensor);
  delay(100);

//Serial.println(pulseCount);
String analogValue0 = String(sensor);
//Serial.println(totalkWh);
delay(1000);
  // Print Update Response to Serial Monitor
  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="+analogValue0);
  }

  // Check if Arduino Ethernet needs to be restarted
  if (failedCounter > 3 ) {startEthernet();}

  lastConnected = client.connected();
}

void updateThingSpeak(String tsData)
{
  Serial.println(tsData);
  if (client.connect(thingSpeakAddress, 80))
  {        
    client.print("POST /update HTTP/1.1
");
    client.print("Host: api.thingspeak.com
");
    client.print("Connection: close
");
    client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"
");
    client.print("Content-Type: application/x-www-form-urlencoded
");
    client.print("Content-Length: ");
    client.print(tsData.length());
    client.print("

");

    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();
  }
}

void startEthernet()
{
   failedCounter = 0;

  client.stop();

  Serial.println("Connecting Arduino to network...");
  Serial.println(); 

  delay(1000);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  delay(1000);
}

Avatar
cstapels

Gold
Forum Posts: 96
sp_UserOfflineSmall Offline
2
October 11, 2017 - 1:54 pm
sp_Permalink sp_Print

Have you made sure your channel is set up correctly?  Use your address bar and make sure you can update your channel first 

" https://api.thingspeak.com/update?api_key=WRITEAPIKEY&field1=123"

Make sure you replace it with your Write API Key.  Does your channel update this way?  What is the response from the browser?  See update a channel field for more info.  If that works, we can look into the code.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
21 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

donj51

trilee

Bfletche

stuartWAI

shivmkatiyar

AidanThing

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee