Update ThingSpeak channel and getting talkBack commands in the same program using arduino Mega2560 and Arduino 101 WiFi shield | 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
Update ThingSpeak channel and getting talkBack commands in the same program using arduino Mega2560 and Arduino 101 WiFi shield
No permission to create posts
October 17, 2018
1:39 pm
Avatar
motipr1401
New Member
Members
Forum Posts: 2
Member Since:
October 17, 2018
sp_UserOfflineSmall Offline

Hello,

i'm using ThingSpeak with arduino Mega2560 and Arduino 101 WiFi shield.
i tried to write a code that update my ThingSpeak channel as well as getting a talkBack commands from the application.
the code contains to seperate functions, regarding update the ThingSpeak channel and checking for talkBack commands.
it seems that something in the checkTalkBack function interfere the operation of the updateThingSpeak function and
the channel is not updated at all as well as not receiving the command right.

Please note that i'm working with WiFi shield and not ETHERNET shield.
the code is attached to the following link:
https://www.dropbox.com/s/9u4tfuxl8lox0nq/sketch_motti2920918experiment.ino?dl=0

Can anyone review my code as well as advise what the possible problem can be?
Thank you in advance.
Motti

October 22, 2018
2:26 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 861
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

Can you describe what you are seeing and what you think is happening? You could consider pasting small snippets of your code that you think might be the issue. Add serial monitor outputs to check the results and then show the output here if you are still stuck.

October 29, 2018
6:20 pm
Avatar
JasonW

MathWorks
Members
Forum Posts: 54
Member Since:
October 21, 2016
sp_UserOfflineSmall Offline

motipr1401,

I was looking into why your code didn't work, then I got pulled onto something else. I'm digging into it again.

October 31, 2018
1:17 pm
Avatar
JasonW

MathWorks
Members
Forum Posts: 54
Member Since:
October 21, 2016
sp_UserOfflineSmall Offline

Try this:

#include
#include
#include "secrets.h"

char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password

WiFiClient client;

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
const char * myTalkbackKey = SECRET_TALKBACK_APIKEY;

// some values to send to ThingSpeak
int number1 = 0;
int number2 = random(0,100);
int number3 = random(0,100);
int number4 = random(0,100);

void setup() {
Serial.begin(115200);
}

void loop() {

// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass);
Serial.print(".");
delay(5000);
}
Serial.println("
Connected.");
}

// Create the message body for the POST out of the values
String postMessage = String("field1=") + String(number1) +
String("&field2=") + String(number2) +
String("&field3=") + String(number3) +
String("&field4=") + String(number4) +
String("&apikey=") + String(myWriteAPIKey) +
String("&talkback_key=") + String(myTalkbackKey);

// make a String for any commands that might be in the queue
String newCommand = String();

// make the POST to ThingSpeak
int x = httpPOST(postMessage, newCommand);
client.stop();

// Check the result
if(x == 200){
Serial.println("Channel update successful, checking queue...");

// check for a command returned from Talkback
if(newCommand.length() != 0){
Serial.print(" do this: ");
Serial.println(newCommand);
}
else{
Serial.println(" do nothing.");
}

}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}

// change the values to prove things are happening
number1++;
if(number1 > 99){
number1 = 0;
}
number2 = random(0,100);
number3 = random(0,100);
number4 = random(0,100);

delay(20000); // Wait 20 seconds to update the channel again
}

// general function to POST to ThingSpeak
int httpPOST(String postMessage, String &response){

bool connectSuccess = false;
connectSuccess = client.connect("api.thingspeak.com",80);

if(!connectSuccess){
return -301;
}

postMessage += "&headers=false";

String Headers = String("POST /update HTTP/1.1
") +
String("Host: api.thingspeak.com
") +
String("Content-Type: application/x-www-form-urlencoded
") +
String("Connection: close
") +
String("Content-Length: ") + String(postMessage.length()) +
String("

");

client.print(Headers);
client.print(postMessage);

long startWaitForResponseAt = millis();
while(client.available() == 0 && millis() - startWaitForResponseAt < 5000){
delay(100);
}

if(client.available() == 0){
return -304; // Didn't get server response in time
}

if(!client.find(const_cast("HTTP/1.1"))){
return -303; // Couldn't parse response (didn't find HTTP/1.1)
}

int status = client.parseInt();
if(status != 200){
return status;
}

if(!client.find(const_cast("

"))){
return -303;
}

String tempString = String(client.readString());
response = tempString;

return status;

}

Here are the contents of the secrets.h

// Use this file to store all of the private credentials
// and connection details

#define SECRET_SSID "MySSID" // replace MySSID with your WiFi network name
#define SECRET_PASS "MyPassword" // replace MyPassword with your WiFi password

#define SECRET_CH_ID 0000000 // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "XYZ" // replace XYZ with your channel write API Key

#define SECRET_TALKBACK_APIKEY "ABC" // replace ABC with your talkback key

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
9 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:

khanhNguyen_CT, susyburr, quijano__, meosuckhoe, briskpanda, Dejw0089

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457