I am currently trying to see the result of a query to a thingspeak field. This works great when I just use a empty sketch and type the commands manually, but using the following sketch I cannot see the value of +IDP,0,1:1 This means, at field 1, the value 1 is stored. If 0 was stored, it would be +IDP,0,1:0 How it is possible to "ask" if "+IDP,0,1:0" exists at the serial message from the ESP8266? Serial.find does not work....
I tried with an array to collect all data and go through it checking for the ":"... also did not work
Here my sketch:
#include<stdlib.h>
#include <SoftwareSerial.h>
SoftwareSerial monitor(2,3); //rx tx
#define SSID "WLAN-0E0265"
#define PASS "2738564673200634"
int ledPin = 13;
String c;
void setup() {
Serial.begin(9600);
monitor.begin(9600);
monitor.println("AT");
delay(1000);
monitor.println("AT+CIPMUX=1");
delay(1000);
monitor.println("AT+CWJAP=\"WLAN-0E0265\",\"2738564673200634\"");
delay(5000);
if(monitor.find("OK"))
Serial.println("JAP worked");
monitor.println("AT+CIPSTART=0,\"TCP\",\"api.thingspeak.com\",80");
delay(2000);
if(monitor.find("OK"))
Serial.println("CIPSTART worked");
monitor.println("AT+CIPSEND=0,110");
delay(2000);
if(monitor.find(">"))
Serial.println("CIPSEND worked");
monitor.print("GET https://api.thingspeak.com/channels/54209/fields/1/last \r\n");
delay(10000);
monitor.println("GET https://api.thingspeak.com/channels/54209/fields/1/last");
delay(2000);
String readString;
ledPin = 13;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//SEARCH FOR THE :0 or :1 <-- Here is the problem, rest works fine
if(Serial.find(":0")){
//turn pin off
}
if(Serial.find(":1")){
//turn pin on
}
}
void loop(){
}
if(monitor.available()){
while(monitor.available()){
s = s + monitor.readString(); // monitor.read() will lead to the same result ....
}
}
The if statement is pointless. The while statement will iterate exactly the same number of times (including 0) with or without it.
Congratulations on finally figuring out that you can read data faster than it can arrive. Just like you can read faster than I can type one-handed.
Now, the next step is to figure out what to do about that. This post is full of clues. The spaces between words delimit one kind of packet. The periods delimit another kind of packet.
Try this to log the characters sent in each direction. I suspect the fact that you aren't reading result codes from 'monitor' means they are sitting in the input buffer and will be read after some future command.
#include<stdlib.h>
#include <SoftwareSerial.h>
SoftwareSerial monitor(2, 3); //rx tx
#define SSID "WLAN-0E0265"
#define PASS "2738564673200634"
const int ledPin = 13;
String command(const char *toSend, unsigned long milliseconds) {
String result;
Serial.print("Sending: ");
Serial.println(toSend);
monitor.println(toSend);
unsigned long startTime = millis();
Serial.print("Received: ");
while (millis() - startTime < milliseconds) {
if (monitor.available()) {
char c = monitor.read();
Serial.write(c);
result += c; // append to the result string
}
Serial.println();
}
return result;
}
void setup() {
String result;
Serial.begin(9600);
monitor.begin(9600);
command("AT", 1000);
command("AT+CIPMUX=1", 1000);
result = command("AT+CWJAP=\"WLAN-0E0265\",\"2738564673200634\"", 5000);
if (result.indexOf("OK") != -1)
Serial.println("JAP worked");
result = command("AT+CIPSTART=0,\"TCP\",\"api.thingspeak.com\",80", 2000);
if (result.indexOf("OK") != -1)
Serial.println("CIPSTART worked");
result = command("AT+CIPSEND=0,110", 2000);
if (result.indexOf(">") != -1)
Serial.println("CIPSEND worked");
result = command("GET https://api.thingspeak.com/channels/54209/fields/1/last", 10000);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//SEARCH FOR THE :0 or :1 <-- Here is the problem, rest works fine
if (result.indexOf(":0") != -1) {
//turn pin off
digitalWrite(ledPin, LOW);
} else if (result.indexOf(":1") != -1) {
//turn pin on
digitalWrite(ledPin, HIGH);
}
}
void loop() {
}
With a 64 byte buffer, there will NEVER be more than 100 characters to read. You MUST make the thingspeak query include an end of packet marker, or recognize the one it is now sending. You MUST read and store data as it arrives, as fast as it arrives, until the end of packet marker arrives. When the end of packet marker arrives, THEN, and only then, do you parse and use the data that you got.