Go Down

Topic: ESP8266 to Arduino firmware (Read 567 times) previous topic - next topic

Deva_Rishi

Of course if you just want proof of concept,  a small adjustment to the nano sketch
Code: [Select]
while (ESPserial.available()){
      char c =ESPserial.read();
      IncommingString+= c;
      if (!ESPserial.available()) delayMicroseconds(1200); // this waits enough time for at least 1 more character
      StringReady = true;     
    }
and for the ESP sketch
Code: [Select]
String payload = "";
  payload = "{";
  payload += "\"state\":";
  while(Serial.available()) {
     char c=Serial.read();  // of course this could also be just 1 line but now we can test
     payload += c;            // the value of 'c' if we want to
     if (!Serial.available()) delayMicroseconds(1200);
   }
   payload += "}";
would suffice (code without compile test, may have typos)
To 'Correct' you have to be Correct. (and not be condescending..)

DavidRK

have a look at Serial Input basics there are several good methods available. Actually usually they use the Arduino to command the ESP-firmware. definitely a way, but not The wayYou are on the right track !
few things :
Code: [Select]
String IncommingString = "";
    boolean StringReady = false;
    while (ESPserial.available()){
      IncommingString = ESPserial.readString();
      StringReady = true;      
    }

First of all, if you use 'Strings' on an Arduino people on the forum will likely blame any mishaps in your programs execution on it, so i would advice to switch over to using char * as a habit.
Second, in this snippet you are likely not to read a complete input (definitely since you are using a low BAUD-rate)  anyway have a look at Robin2's examples and consider once you've gone beyond the debugging stage o use the nano's hwSerial instead for faster communication.
About the ESP-sketch:
Code: [Select]
randomSeed(micros());
although you are not using the random at all, an easier way to get a proper randomSeed
Code: [Select]
randomSeed(analogRead(A0));
that it is not connected to a physical pin doesn't mean it is not there, and it is remarkable how consistent connection times can be.
Code: [Select]
while (Serial.available()) {
    sendToClient();
  }

 the same issue with the Serial comm. here, you are sending incomplete messages.
Code: [Select]
char attributes[100];
  boolean published = false;
  payload.toCharArray( attributes, 100 );

and if you are going to convert from String to char *,  check the length of the String, add 1 for the '\0' terminator, and declare that as size for the array and as size for the copy. With the ESP it usually is fine to use Strings due to it's much bigger memory size (and different/better String memory managing) and i would actually even recommend it's use over char * at times since there has been a report on strtok() not functioning properly on an ESP.


Awesome, have been amending a couple things the last few days, especially string lol, they came back to bite.


On my journey of try to also find an OTA for the ESP, I stumbled across this https://github.com/jeelabs/esp-link]esp-link Pretty much sorts all the functions I required, and as long as you have the 1Mb ESP-01 you can also do OTA too.

Deva_Rishi

Quote
On my journey of try to also find an OTA for the ESP
I use ArduinoOTA.h and it works like a charm. It wasn't very complex to create a system with multiple users with different permissions.
To 'Correct' you have to be Correct. (and not be condescending..)

Go Up