Sketch sends data only once and stops | Arduino | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





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

sp_Feed sp_TopicIcon
Sketch sends data only once and stops
No permission to create posts
December 1, 2014
8:32 am
Avatar
MrMarv
New Member
Members
Forum Posts: 1
Member Since:
December 1, 2014
sp_UserOfflineSmall Offline

Hi everybody,

I am currently working with an Intel Edison and I connecting it to ThingSpeak was successful.
Sadly it just sends data once. After that the sketch stops working. I hope somebody can help me solving these issues.

Sketch:

#include
#include

// Local Network Settings
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
char ssid[] = "***"; // your network SSID (name)
char pass[] = "***"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "****";
const int updateThingSpeakInterval = 16 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)

// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;

//sensors
const int pinTemp = A0; // pin of temperature sensor
float temperature; //temperature gets stored in here
int B=3975; // B value of the thermistor
float resistance;

// Initialize Arduino Ethernet Client
WiFiClient client;

void setup()
{
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);

// Start Ethernet on Arduino
startWiFi();
}

void loop()
{
// Read value from Analog Input Pin 0
int val = analogRead(pinTemp); // get analog temp value
resistance = (float)(1023-val)*10000/val; // get resistance
temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15; // calc temperature
char buffer[10];
String tem = dtostrf(temperature, 5, 1, buffer);
String analogPin0 = (tem);

// 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="+analogPin0);
}

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

lastConnected = client.connected();
}

void updateThingSpeak(String 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 startWiFi()
{

client.stop();

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

delay(1000);

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.1.0" )
Serial.println("Please upgrade the firmware");

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

/*
// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else
{
Serial.println("Arduino connected to network using DHCP");
Serial.println();
}
*/

delay(1000);
}

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

char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
char fmt[20];
sprintf(fmt, "%%%d.%df", width, prec);
sprintf(sout, fmt, val);
return sout;
}

______________________________________________________________
Serial monitor:

Connecting Arduino to network...

Attempting to connect to WPA SSID: ***
You're connected to the networkSSID: ***
BSSID: 8D:3E:F5:11:1A:2
signal strength (RSSI):-19
Encryption Type:4

IP Address: 192.168. 43.207
192.168. 43.207
MAC address: C5:D8:A8:87:4B:78
Connecting to ThingSpeak...

HTTP/1.1 200 OK
Server: nginx/1.7.5
Date: Mon, 01 Dec 2014 13:21:03 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Status: 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: "c9f0f895fb98ab9159f51fd0297e236d"
Cache-Control: max-age=0, private, must-revalidate
Set-Cookie: request_method=POST; path=/
X-Request-Id: ffd320e0-f6bc-4013-8e33-bfbe8ec64caf

1
8
0

_______________________________________________________________
Thanks for your help!

July 1, 2015
6:37 am
Avatar
prabhjeet
New Member
Members
Forum Posts: 3
Member Since:
July 1, 2015
sp_UserOfflineSmall Offline

hey,i have similar problem that thingspeak update data only once.
If you done with it,please give me suggestion about it

October 11, 2015
5:04 pm
Avatar
BSB

Silver
Members
Forum Posts: 19
Member Since:
October 10, 2015
sp_UserOfflineSmall Offline

Similar here only getting maybe two sends to thingspeak and then nothing.
Hitting the arduino reset starts it again. Been doing that most of the day while playing around looking for the issue.
Will post code if requested.

UPDATE...
Turns out my issue was related to delays and timers in my sketch and too many print statements for debug to the serial monitor.

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
20 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

Team_mafia, icXu, emorphistechno, VTVMART, manimozhi, adamhebrew

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457