how can i get the last entry data in the field to keep in variables using esp-01 and arduino mega | ThingSpeak API | 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
how can i get the last entry data in the field to keep in variables using esp-01 and arduino mega
No permission to create posts
October 7, 2019
12:03 pm
Avatar
all890
New Member
Members
Forum Posts: 1
Member Since:
October 7, 2019
sp_UserOfflineSmall Offline

hello everyone! i need to get the last entry data in the field of thingspeak to keep in variables and modify it using esp-01 and arduino mega

this is my code

#include

#define esp8266 Serial1

#define SSID "test" // put here the name of your wifi network
#define PASS "lolman123"
#define IP "184.106.153.149"

int counter = 120;
String valuetosend;

String GET = "GET /update?key=FYPW04TOYR3FX2QH&"; // put here your thingspeak key
String GET1 = "field1=";

String readGET1 = "GET https://api.thingspeak.com/channels/864152/fields/1.json?api_key=SH4GK117NS6EC9O2&results=2";

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
esp8266.begin(115200);
esp8266.println("AT");
delay(5000);
if(esp8266.find("OK")){
connectWiFi();
Serial.println("WIFI CONNECTED");
}
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
}

void loop() {
// put your main code here, to run repeatedly:
int readBtn = digitalRead(7);
int readSend = digitalRead(8);

if (readBtn == 0)
{
ReadLastEntryThingSpeak();
}

if (readSend == 0)
{
WriteThingSpeak();
}
}

boolean connectWiFi(){
esp8266.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
esp8266.println(cmd);
delay(5000);
if(esp8266.find("OK")){
Serial.println("OK");
return true;
}else{
Serial.println("KO");
return false;
}
}

void updateFunction(String valuetosend){
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
esp8266.println(cmd);
delay(2000);
if(esp8266.find("Error")){
Serial.print("Error1");
return;
}
cmd = GET + GET1;
cmd += valuetosend;
cmd += "
";
Serial.print(cmd);
esp8266.print("AT+CIPSEND=");
esp8266.println(cmd.length());
if(esp8266.find(">")){
esp8266.print(cmd);
}else{
esp8266.println("AT+CIPCLOSE");
}
}

void ReadLastEntryThingSpeak()
{
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
esp8266.println(cmd);
delay(2000);
if(esp8266.find("Error")){
Serial.print("Error1");
return;
}
cmd = readGET1;
cmd += "
";
Serial.print(cmd);
esp8266.print("AT+CIPSEND=");
esp8266.println(cmd.length());
if(esp8266.find(">")){
esp8266.print(cmd);
}else{
esp8266.println("AT+CIPCLOSE");
}
}

void WriteThingSpeak ()
{
String valuetosend = String(counter);
updateFunction(valuetosend);
}

October 8, 2019
4:20 pm
Avatar
cstapels
Moderator
Members


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

What do you see on the serial console? Can you describe what you think might be the problem and we can look specifically?

Have you tried using the ThingSpeak library? There is an example in the ThingSpeak Communication Library that will show you how to write with the AT command set on ESP-8266 using the library.
https://github.com/mathworks/thingspeak-arduino

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:

qkpham, SANGAMNATH B, swatiiot, kalyanigaware16@gmail.com, Romi, Drakan01

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457