B4R Question Convert to string

Discussion in 'B4R Questions' started by AndroidMadhu, Jun 10, 2019.

  1. AndroidMadhu

    AndroidMadhu Member Licensed User

    I am sending data from arduino to Esp as below
    Code:
    Private Sub Timer1_Tick
        
    'Dim FixedValue As String=80
        Dim humid(),temp() As Byte
        humid=
    "33.39"
        temp=
    "77.99"
        Astrem.Write(ser.ConvertArrayToBytes(
    Array(humid,temp)))
    Log("Send to Esp8266 Successfully!!")
    End Sub
    At Esp Side Receiving the data:
    Code:
    Private Sub Arduino_Received_Data(buffer() As Byte)
    ser.ConvertBytesToArray(buffer,BE)
    Log("Humid from Ardiono :",BE(0))  <---- I am able to see the value recd from Arduino
    Log("Temp from Arduino :",BE(1))   <---- I am able to see the value recd from Arduino
    But now I want to convert BE(0) and BE(1) to String.
    How Do I do that?
    Code:
    dim humiditytosend,temperaturetosend As byte
    dim bc as byteconverter
    <
    Next I am lost....>
     
  2. Cableguy

    Cableguy Expert Licensed User

    BC.bytestostring(....)
     
  3. AndroidMadhu

    AndroidMadhu Member Licensed User

    bytetostring no option is present .... :(.
    I have used "bc.StringFromBytes", which is working as expected... But with one issue...
    when I am declaring the variable humidtosend as global variable and trying to assign the value to humidtosend , then I am getting error as
    Code:
    "Cannot modify Global (non-primitive) Variable
    This is not true for float or int.
    But when I am declare it locally within the sub it is working fine.
    My confusion is if I cannot declare the variable Globally then how other sub can access the value (specifically Inline C/C++)
     
  4. bdunkleysmith

    bdunkleysmith Active Member Licensed User

    inakigarm and Erel like this.
  5. Erel

    Erel Administrator Staff Member Licensed User

    bdunkleysmith likes this.
  6. AndroidMadhu

    AndroidMadhu Member Licensed User

    This has already been I have incorporated...
    Code:
    Private ser As B4RSerializator
        
    Private BE(20As Object
        
    Dim humiditytosend(),temperaturetosend() As Byte
        
    Private bc As ByteConverter
    Private Sub Arduino_Received_Data(buffer() As Byte)
    ser.ConvertBytesToArray(buffer,BE)
    Log("Humid from Ardiono :",BE(0))
    Log("Temp from Arduino :",BE(1))
    'bc.ArrayCopy(BE(0),humiditytosend)
    GlobalStore.Put(0,BE(0))   'For Humid
    Log("Humid is:",GlobalStore.Slot0)
    But how to copy the GlobalStore.Slot0 to a variable?
     
  7. AndroidMadhu

    AndroidMadhu Member Licensed User

    How to copy the GlobalStore.Slot0 value to a Global variable[It should be datatype String]?

    Please advice.....
     
  8. bdunkleysmith

    bdunkleysmith Active Member Licensed User

    In the absence of any other information I would say:

    Code:
    Dim s As String = bc.StringFromBytes(GlobalStore.Slot0)
    however in all my uses of GlobalStore I've never had to copy it to a local variable.

    But AndroidMadhu based on my experience of receiving GREAT assistance from this community in resolving problems, you will get the best assistance if you provide a complete "picture" of what you are trying to achieve by including all of your current code. I have followed the various posts regarding your current project with interest and even tried to help, but I'm now unsure what parts of the code you have published are still being used or what parts have been discarded to take on board suggestions of other users. Often answers will depend very much on the context and so to get help we must provide that context.

    Also as someone who began using an Arduino (UNO) connected to an ESP8266 to do a data collection and send it via WiFI, the solution may be simpler by using just the ESP8266 alone (ESP8266 WiFi shield on UNO). Just food for thought . . .
     
    Last edited: Jun 11, 2019
  9. bdunkleysmith

    bdunkleysmith Active Member Licensed User

    First I would ask why do you need to "copy the GlobalStore.Slot0 value to a Global variable"?
     
  10. AndroidMadhu

    AndroidMadhu Member Licensed User

    I need to copy the GlobalStore.Slot0 value to global variable as because, it is required to feed the value o the variables in inline C/C++ code.
    Below is the inline C code for ESP to send the value [Both temp and Humid to webpage]. I need to put the value received from Arduino to Esp8266 at inline C variables [temperature and humidity ]

    Code:
    #if C
    #include <ESP8266WiFi.h>
    #include <WiFiClient.h>
    #include <ESP8266WebServer.h>
    #include <ESP8266HTTPClient.h>
    void setup(B4R::Object* o){
    const char *host = "http://mypage.com";
    }
    void read (B4R::Object* o) {
        HTTPClient http;    //Declare object of class HTTPClient
        String getData, Link ;

      //GET Data
      getData = "?temperature=" + temperature + "&humidity=" + humidity ;
      Link = "http://mypage.com/addTemperature.php" + getData;
      http.begin(Link);     //Specify request destination
     
      int httpCode = http.GET();            //Send the request
      String payload = http.getString();    //Get the response payload

      Serial.println(httpCode);   //Print HTTP return code
      Serial.println(payload);    //Print request response payload

      http.end();  //Close connection
    }
    #End if
     
  11. bdunkleysmith

    bdunkleysmith Active Member Licensed User

    I do not understand C and so avoid such inline code whenever I can. I assumed taking the advice of Erel and others in this post How to Send Data to webpage using NodeMCU and using the HttpJob module would avoid the need to use that inline code.

    I'm getting out of my depth now and so hope someone else can help. All the best with your project . . .
     
  12. tigrot

    tigrot Well-Known Member Licensed User

    No need to use inline C, use bdunkleysmith's example and live happy.
     
  13. AndroidMadhu

    AndroidMadhu Member Licensed User

    It is just from my interest.. to know in depth...
    Is is possible to copy/assign "GlobalStore.Slot0" data to a variable in inline C/C++?
    Like from the previous post
    Code:
    #if C
    String temperature, humidity;  <--- Can assign any value from GlobalStore
    #End if
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Arduino String is not the same as B4R String.
     
  15. AndroidMadhu

    AndroidMadhu Member Licensed User

    So there is any way or method to put the value to Arduino String? Could you please put some hint on this please if any?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice