0

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....

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(){

}
1

The problem is that you can't use serial's find function to look for one thing or another. It is a greedy function and it will consume everything coming in to the serial port until it finds what it's looking for or until it times out. That includes the :1 if you are using find to look for the :0 at the time.

Instead you either need to receive the whole string into memory and examine the string to see what it contains, or you need to manually process the data as it comes in character by character to decide what there is.

| improve this answer | |
  • Hi thaks for the reply, you mean probably something like this: if(monitor.available()){ while(monitor.available()){ s = s + monitor.readString(); // monitor.read() will lead to the same result .... } } Serial.println(s); this also does not show up the whole code like I see when I manually type it into the Serial monitor.. I think the Serial.available and serial.find has some timeout or so... – Merki Sep 5 '15 at 19:06
  • @Merki, if your if statement has no else part, just leave it off. That is, for a condition c, the construct while(c){...} has the same effect as if(c){ while(c){...} } (unless race conditions apply). – James Waldby - jwpat7 Sep 5 '15 at 20:33
  • You should read this to understand how to read the whole string into memory: hacking.majenko.co.uk/reading-serial-on-the-arduino - it's not as easy as you are thinking it is. – Majenko Sep 5 '15 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.