-->
Page 1 of 2

Separate values from a string

PostPosted: Mon Jul 02, 2018 5:58 am
by exp
I use a nodemcu with a tft-display to show "weatherdata" (humidity= 2 digits, airpressure= 4 digits and temperature=3 digits) - I get those three values from my Arduino webserver as one String which contains all three values! What I need to do is to separate the three values and assign each of them to a different variable? This would give me the possibility to locate them in different spots of the display! The whole thing would be more attractive and better readable!!
There are lots of string functions in Arduino but my problem is that I don´t know which of them I should use. And - since I never did this before I honestly don´t know how this is to be done!!
I would really appreciate your help - heres the sketch of my nodemcu:

Code: Select all#include <ESP8266WiFi.h>
   #include <ESP8266WiFiMulti.h>
   #include <ESP8266HTTPClient.h>
   #define Serial Serial
   #include "Adafruit_GFX.h"
   #include "Adafruit_ILI9340.h"
   #include "SPI.h"

   ESP8266WiFiMulti WiFiMulti;

   //Pinout of display

   #define TFT_CS D0   // GPIO 16
   #define TFT_RST 10  // D12 GPIO10
   #define TFT_DC D1   // GPIO 5
   #define    TFT_MOSI D7 // GPIO 13
   #define TFT_CLK D5  // GPIO 14
   #define TFT_MISO D6 // GPIO 12

   Adafruit_ILI9340 tft = Adafruit_ILI9340(TFT_CS, TFT_DC, TFT_RST);


   #define ROTATION_NO 0
   #define ROTATION_90 1
   #define ROTATION_180 2
   #define ROTATION_270 3


   String weatherdata;



   void setup() {

     Serial.begin(115200);
     // Serial.setDebugOutput(true);

     Serial.println();
     Serial.println();
     Serial.println();

     for (uint8_t t = 4; t > 0; t--) {
       Serial.printf("[SETUP] WAIT %d...\n", t);
       Serial.flush();
       delay(1000);
     }


     WiFi.mode(WIFI_STA);

     WiFiMulti.addAP("", "");


     tft.begin();

     tft.setRotation( ROTATION_270);
     tft_printframe();
     tft_printcaption();


   }
   //Void setup() end

   void tft_printcaption(void)
   {
     tft.setTextColor(ILI9340_WHITE);
     //tft.setCursor( 10,55);
     //tft.setTextSize(2);
     //tft.print("");
     tft.setTextSize(1);
     //tft.print("O");
     tft.setTextSize(3);
     //tft.print("C");


     tft.setTextSize(2);
     tft.setCursor( 50, 15);
     tft.print("             HPascal");
     tft.setTextSize(2);
     tft.setCursor( 10, 210);
     tft.print("                Percent");

   }

   void tft_printframe( )
   {
     tft.fillScreen(ILI9340_BLACK);
     tft.drawRoundRect(1, 0, tft.width() - 1, 40, 10, ILI9340_BLUE);
     tft.drawRoundRect(1, 50, tft.width() - 1, 135, 10, ILI9340_GREEN);
     tft.drawRoundRect(0, 200, tft.width() - 1, 40, 10, ILI9340_RED);
   }

   void tft_show_weatherdata()
   {

     tft.setTextSize(4);
     tft.setTextColor(ILI9340_YELLOW, ILI9340_BLACK );
     tft.setCursor(0, 50);
     tft.print(weatherdata);

   }


   void loop(void) {

     // wait for WiFi connection
     if ((WiFiMulti.run() == WL_CONNECTED)) {

       HTTPClient http;

       Serial.print("[HTTP] begin...\n");

       // configure traged server and url
       http.begin("http://192.168.1.15/?mode=text");

       Serial.print("[HTTP] GET...\n");
       // start connection and send HTTP header
       int httpCode = http.GET();

       // httpCode will be negative on error
       if (httpCode > 0) {
         // HTTP header has been send and Server response header has been handled
         Serial.printf("[HTTP] GET... code: %d\n", httpCode);
         Serial.println();
         // file found at server
         if (httpCode == HTTP_CODE_OK) {
           weatherdata = http.getString(); // That´s the variable that i want to split!!
           Serial.println(weatherdata);
           tft_show_weatherdata();
            }
       }

       else {
             Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
              }


       http.end();
     }
                 }


Re: Separate values from a string

PostPosted: Mon Jul 02, 2018 5:05 pm
by McChubby007
sscanf a C string (char array).

I'd just like to add that this is a general programming question, not esp8266, but I have nonetheless answered it for you.

Re: Separate values from a string

PostPosted: Mon Jul 02, 2018 5:25 pm
by exp
McChubby007 wrote:sscanf a C string (char array).

I'd just like to add that this is a general programming question, not esp8266, but I have nonetheless answered it for you.


Thanks for your answer - I thought that newbies should post here - my bad!!!

l8r...

Re: Separate values from a string

PostPosted: Tue Jul 03, 2018 2:57 am
by picstart
Is sscanf available for the esp8266 Arduino? If so does it have its own library or a special include?
I tried it but it never compiles since the function wasn't found.