iotarduino-unosim900gsmcomm

How to pass the 'function value' in api address of thingSpeak.. I am using Arduino and sim900 GSM


I want to upload rain sensor data to thingspeak using HTTP POST method, But while using "Api key= data to be sent .." The sensor data is not being uploaded to server.. and also as it is in quotes so it is not taking as function may be.... How to solve this problem??

Serial.print("api_key=QI8G7PVTC2BVIREC&field1=TellWater()\\r\\n"); 

Solution

  • You need to capture the return value of the TellWater() in a variable.

    Here I am assuming TellWater() return float. You need to convert it to string.

    float water_value = TellWater();
    String water_value_str;
    water_value_str = String(f);
    

    After you convert the sensor data to string, you need to perform string concatenation to prepare final output string.

    String output_string = "api_key=QI8G7PVTC2BVIREC&field1=" + water_value_str + "\r\n";
    Serial.print(output_string);
    

    You should not escape back slash \ in \\r\\n. This will cause compiler to interpret the statement as character \ (0x5c) and character r (0x72) instead as \r (0x0D) (carriage-return). Similarly \\n will be interpreted as character \ (0x5c) and character n (0x6e) instead as \n (0x0A) (line-feed) causing your GSM module to wait for data as it has not received line-ending characters (\r\n).