why i get the same value that i send the last to thingspeak
#include <SoftwareSerial.h>
#include <string.h>
#include <stdlib.h>
#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 = 100;
int resetpoint = 100;
String keep;
String valuetosend;
const int resetPin = 3;
const int resetPoint = 3;
String GET = "GET /update?key=FYPW04TOYR3FX2QH&"; // put here your thingspeak key
String GET1 = "field1=";
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
String readGET1 = "GET https://api.thingspeak.com/channels/864152/fields/1/last";
int ex_int = 10;
int ex_int_keep;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
esp8266.begin(115200);
Serial.println("Resetting...");
esp8266.println("AT");
delay(2000);
if(esp8266.find("OK")){
connectWiFi();
Serial.println("WIFI CONNECTED");
}
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(resetPoint, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int readBtn = digitalRead(7);
int readSend = digitalRead(8);
int readResetPoint = digitalRead(resetPoint);
if (readBtn == 0)
{
ReadLastEntryThingSpeak();
}
if (readSend == 0)
{
WriteThingSpeak();
}
}
boolean connectWiFi(){
esp8266.println("AT+CWMODE=3");
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 += "\r\n";
Serial.print(cmd);
esp8266.print("AT+CIPSEND=");
esp8266.println(cmd.length());
if(esp8266.find(">")){
esp8266.print(cmd);
}else{
esp8266.println("AT+CIPCLOSE");
}
delay(2000);
ResetEverything ();
}
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 += "\r\n";
Serial.print(cmd);
esp8266.print("AT+CIPSEND=");
esp8266.println(cmd.length());
if(esp8266.find(">")){
esp8266.print(cmd);
}else{
esp8266.println("AT+CIPCLOSE");
}
delay(5000);
if (esp8266.find("+IPD,"))
{
recvWithStartEndMarkers();
Serial.print("The last value of field 1 is ");
Serial.println(receivedChars);
ex_int_keep = (receivedChars[0] - '0') * 100; //Convert char to int
ex_int_keep += (receivedChars[1] - '0') * 10;
ex_int_keep += receivedChars[2] - '0';
Serial.print("The value of ex_int_keep is ");
Serial.println(ex_int_keep);
}
}
void WriteThingSpeak ()
{
int sum = counter + ex_int_keep;
String valuetosend = String(sum);
updateFunction(valuetosend);
}
void ResetEverything ()
{
esp8266.println("AT+RST");
delay(2000);
ex_int_keep = 0;
Serial.println("Ready to use!");
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = ':';
char endMarker = 'C';
char rc;
while (esp8266.available() > 0 && newData == false) {
rc = esp8266.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
when i press read button to get the last data to keep in ex_int_keep and then i press send button to modify it by plus with 100 and sent it back to thingspeak
the problem is here. after i press send button, i press read button to get the last data, but i got the same value that i get from the begin
From Serial Monitor
Resetting...
OK
WIFI CONNECTED
GET https://api.thingspeak.com/channels/864152/fields/1/last
The last value of field 1 is 400
The value of ex_int_keep is 400
GET /update?key=FYPW04TOYR3FX2QH&field1=500 //the value that i send
Ready to use!
GET https://api.thingspeak.com/channels/864152/fields/1/last
GET https://api.thingspeak.com/channels/864152/fields/1/last
The last value of field 1 is 400 //the actual result is 500
The value of ex_int_keep is 400 //the actual result is 500
to make the result correct, i must reset arduino. but it has a better way to do that without resetting?