Go Down

Topic: Arduino UNO Wifi - http post request (JSON) (Read 1 time) previous topic - next topic

Juraj

#15
Dec 05, 2017, 03:44 pm Last Edit: Dec 05, 2017, 03:46 pm by Juraj
not the right wifilink. use 1.1.0 from here. how did you end up with 1.0.1? have I someware in docs a wrong link?

suneelng

Works fine now, thanks :)

Not sure, i think i had a copy of older version before and i did not make it point to verify with the new link, my bad.


Regarding the post request, say we have the api similar to this here.

It takes below parameters and also returns the same.
{
  "ID": 0,
  "UserName": "string",
  "Password": "string"
}

Thanks

Juraj

post request

Code: [Select]

  if (client.connect(server, 80)) {
    client.println("POST /api/Users HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
    client.println(data);
  }


Juraj

to build json string you can use ArduinoJson library (is in Library Manager with examples)

or build the string with sprintf

Code: [Select]

char data[50];
sprintf(data, "{\"i\":%i,\"t\":%lu,\"v1\":%d,\"v2\":%d,\"c\":%u}", ix, events[ix].timestamp, events[ix].value1, events[ix].value2, events[ix].count);



(it is a snippet from my project)

suneelng

Thanks it works.

Have a question,

code snippet:

  char data[100]; 
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["ID"] = 10;
  root["UserName"] = "user1";
  root["Password"] = "123456";
  sprintf(data, "{\"ID\":10,\"UserName\":\"user1\",\"Password\":\"123456\"}");


      Serial.println("Connected to server");
      client.println("POST /api/Users HTTP/1.1");
      client.println("content-type: application/json");
      client.print("content-length: ");
      client.println(strlen(data));
      client.print("Host: ");
      client.println(server);
      client.println("Connection: close");
      client.println();
//      root.prettyPrintTo(client);
//      client.println();
      client.println(data);


1) The code works fine if i send the json object store in data.
2) But it does not work when i use the root variable. I use the below code lines to post data,
      root.prettyPrintTo(client);
      client.println();
3) how can i extract only the data part from the response and discard other headers section.


Thanks



Juraj

2) But it does not work when i use the root variable. I use the below code lines to post data,
      root.prettyPrintTo(client);
      client.println();
use print not prettyprint

3) how can i extract only the data part from the response and discard other headers section.
search for empty line in response. after that are the data.






suneelng


suneelng

@Juraj,

Is there a websocket example available for serial1 library?

I need to post data to server every 0.5 seconds. What would you recommend for the same. POST request might not be the best solution.

Thanks
Suneel

Juraj

try ArduinoHttpClient library over WiFi Link's WiFiClient. I did not use it yet, but it should work. Someone in some thread used it with WiFiEsp or WiFi Link.


suneelng

Thanks,


I did the below code and i see that the code freezes at line "wsClient.begin()".


***************************************************************************
#include <SoftwareSerial.h>
#include <Wire.h>
#include <UnoWiFiDevEdSerial1.h>
#include "WiFiLink.h"
#include "ArduinoJson.h"
#include <ArduinoHttpClient.h>


WiFiClient client;
char server[] = "fakerestapi.azurewebsites.net";
char wsServer[] = "echo.websocket.org";  // server address
int port = 80;
WebSocketClient wsClient = WebSocketClient(client, wsServer, port);


void setup() {
  /* Socket Connect */
  Serial.println("starting WebSocket client");
  wsClient.begin();
  Serial.println("WebSocket client connected");
}


void loop() {

  /* websocket code */
  if(wsClient.connected()){
  wsClient.beginMessage(TYPE_TEXT);
  wsClient.print("Heart Rate Standard               :");
  wsClient.println("hello");
  wsClient.endMessage();
  }
  else{
    Serial.println("Ws not connected");
  }
  // check if a message is available to be received
  int messageSize = wsClient.parseMessage();

  if (messageSize > 0) {
    Serial.println("Received a message:");
    Serial.println(wsClient.readString());
  }
delay(5000);
}

****************************************************************************

Thanks



Juraj

and where is the usual WiFi Link initialization?

suneelng

thats done as below,


void setup() {


  Serial1.begin(115200);
  Serial1.resetESP();
  delay(3000); //wait while WiFiLink firmware connects to WiFi with Web Panel settings

  WiFi.init(&Serial1);
  while (WiFi.status() != WL_CONNECTED) {
    delay(10);
  }

  Serial.println("You're connected to the network");
  printWifiStatus();
  Serial.println();


  /* Socket Connect */
  Serial.println("starting WebSocket client");
  wsClient.begin();
  Serial.println("WebSocket client connected");
}

Juraj

#27
Dec 15, 2017, 11:01 am Last Edit: Dec 15, 2017, 11:27 am by Juraj
please post the complete sketch in code tags

suneelng

Below is the code

Code: [Select]

#include <SoftwareSerial.h>
#include <Wire.h>
#include <UnoWiFiDevEdSerial1.h>
#include "WiFiLink.h"
#include "ArduinoJson.h"
#include <ArduinoHttpClient.h>


/* Client and server details */
WiFiClient client;
char wsServer[] = "echo.websocket.org";  // server address
int port = 80;
WebSocketClient wsClient = WebSocketClient(client, wsServer, port);


static void postData(void);

void setup() {
 
  /* Initialise serial comm ports */
  Serial.begin(9600);    // serial / USB port

 
  /* Wifi module */
  Serial1.begin(115200);
  Serial1.resetESP();
  delay(3000); //wait while WiFiLink firmware connects to WiFi with Web Panel settings

  WiFi.init(&Serial1);
  while (WiFi.status() != WL_CONNECTED) {
    delay(10);
  }

  Serial.println("You're connected to the network");
  printWifiStatus();
  Serial.println();

  /* Socket Connect */
  Serial.println("starting WebSocket client");
  wsClient.begin();
  Serial.println("WebSocket client connected");
}


void loop() {

  postData(void);
  delay(5000);

}

static void postData(void)
{
  /* websocket code */
  if(wsClient.connected()){
    wsClient.beginMessage(TYPE_TEXT);
    wsClient.print("Hello");
    wsClient.endMessage();
  }
  else{
    Serial.println("Ws not connected");
  }

  // check if a message is available to be received
  int messageSize = wsClient.parseMessage();

  if (messageSize > 0) {
    Serial.println("Received a message:");
    Serial.println(wsClient.readString());
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}




Juraj

I cant find ArduinoHtppClient example on which your sketch is based. Modify the SimpleWebSocket example for WiFi Link library with Serial1

Go Up