0

I'm working on a project aimed on sending alerts via SMS. I signed up for a SMS service that works via HTTP requests (REST standard). I need, then, to send data as JSON, so that their server processes the data and sends the SMS message to a list of recipients.

I am working with an ESP8266 board. I have tried some code, but I couldn't do the JSON part properly, given I have little knowledge on HTTP requests and HTTP libraries for Arduino. I managed to test things using python code (with which I am much more familiar).

In short, I have this piece of code in python that works quite perfectly:

payload = {'sender': '558499999999','content': 'Just a message', 'groupName': 'Recipients_list'}
request = requests.post('https://cheap.sms.sending.guys/sendcontactmessage',
                         data=json.dumps(payload),
                         headers={'content-type': 'application/json', 'auth-key': 'My_authentication_key'})

I tried to write something that does the same thing on a ESP8266, but it is not working and I can't figure the correct way to do it. The code I tried was the following:

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "my_ssid";
const char* password = "my_password";

void setup() {
  Serial.begin(115200);

  //TESTING JSON CREATION
  Serial.println("Starting JSON");
  StaticJsonBuffer<69> jsonBuffer;
  char json[] = "{\"sender\":\"558499999999\",\"content\":\"Just a message\",\"groupName\":\"Recipients_list\"}";
  JsonObject& root = jsonBuffer.parseObject(json);
  if(!root.success()) {
    Serial.println("parseObject() failed");
  } else {
    Serial.println("JSON OK");
  }


  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print("Connecting... ");
  }


  //TESTING POST
  //headers={'content-type': 'application/json', 'auth-key': 'My_authentication_key'}
  String sms_service_URL = "https://cheap.sms.sending.guys/sendcontactmessage";


  Serial.println("TESTING POST");
  //Declare an object of class HTTPClient
  HTTPClient http;

  if (WiFi.status() == WL_CONNECTED) {
    //Specify request destination
    http.begin(sms_service_URL);
    http.addHeader("Content-Type", "application/json");
    http.addHeader("auth-key", "My_authentication_key");

    String data;
    root.printTo(data);
    //Send the request
    int httpCode = http.POST(data);

    //Check the returning code
    if (httpCode > 0) { 
      //Get the request response payload
      String payload = http.getString();
      //Print the response payload
      Serial.println(payload);
    }
    //Close connection
    http.end();
    Serial.println(httpCode);
  }
}

void loop() {


}

I am aware that the arduino code is pretty lame. I was just in a trial-failure process, lol.

I'd appreciate any suggestions

  • 1
    What is the problem? What output do you get? Also, why do you first create a JSON string, then parse that string into an JSON object tree, and then convert it back into a string (which should be identical to the first string)? You can just skip those last two steps. – Gerben Oct 10 at 13:47
  • I'm not sure if I have understood your requirements right, but generally JSON is just text and so the actual transmission should ve trivial. It's up to the devices at either end how to handle this data – Sim Son Oct 10 at 17:56

Your Answer

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

Browse other questions tagged or ask your own question.