Arduino With Ethernet shield Receive data from Asp.net Mvc Web API

Hi,

I have some problem about to use Arduino with Ethernet Shield to receive data from ASP.net MVC Web
API.
I create new ASP.net MVC Web API for response text "Hello", But Arduino has receive data not correct. (Ref. Attachment Image)

15-5-2558 12-21-24.png

Arduino Code

#include <ArduinoJson.h>
#include <SPI.h>
#include <Ethernet.h>

//Ethernet Setup
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte subnet[] = { 255, 255, 255, 0};
byte gateway[] = {192, 168, 1, 1};

IPAddress ip(192, 168, 1, 38);
IPAddress server(192, 168, 1, 34);
int serverPort = 5331;

EthernetClient client;

//Url data
String setting = "api/values";

//Limit for Enviroments
float temperatureLimit;
float humidityLimit;
float luxLimit;

void setup() {
  //Setup Console
  Serial.begin(9600);
  Serial.println("Welcome");

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");

    Ethernet.begin(mac, ip, gateway, subnet);
  }

  Serial.print("IP Address : ");
  Serial.println(Ethernet.localIP());
  Serial.print("Subnet : ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("Gateway : ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("DNS : ");
  Serial.println(Ethernet.dnsServerIP());

  delay(2000);
}

void loop() {

  SettingRequest();
}

void SettingRequest() {
  if (client.connect(server, serverPort)) {
    Serial.println("connecting to get setting");

    client.print("GET ");
    client.print(setting);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Content-Type: application/json; charset=utf-8");
    client.println("Connection: Close");
    client.println();
    delay(1000);

    char c;
    char* data;

    while (client.available()) {
      c = client.read();
      data = data + c;
    }

    delay(1000);

    Serial.print("Result from Server : ");
    Serial.println(data);
/*
    StaticJsonBuffer<200> jsonBuffer;
    JsonObject& root = jsonBuffer.parseObject(data);
    JsonObject& settingData = root["Data"];
    JsonObject& contentData = settingData["content"];

    delay(1000);

    temperatureLimit = contentData["Temperature"];
    humidityLimit = contentData["Humidity"];
    luxLimit = contentData["Lux"];

    Serial.println("Config from server");
    Serial.print("Temp : ");
    Serial.println(temperatureLimit);
    Serial.print("Hum : ");
    Serial.println(humidityLimit);
    Serial.print("Lux : ");
    Serial.println(luxLimit);
    */
  }
}

ASP.net MVC Web API Response Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;

namespace TEstAPI.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        [HttpGet]
        public HttpResponseMessage Get()
        {
            const string text = "Hello";

            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(text, Encoding.UTF8, "application/json")
            };
            
            return response;
        }
    }
}

I don't know how to resolve this problem, Please help me.

Thank you very much

15-5-2558 11-53-16.png

15-5-2558 15-01-19.png

    char* data;

    while (client.available()) {
      c = client.read();
      data = data + c;

Where does data point to? Do you have permission to write there? I seriously doubt it.

PaulS:

    char* data;

while (client.available()) {
      c = client.read();
      data = data + c;



Where does data point to? Do you have permission to write there? I seriously doubt it.

Permission for? This code for receive data from web API and print out to serial for debugging data correct or not.

    char c;
    char* data;

    while (client.available()) {
      c = client.read();
      data = data + c;
    }

    delay(1000);

    Serial.print("Result from Server : ");
    Serial.println(data);

This code for receive data from web API

Yes, it does. But where do you put it? You have a pointer that points nowhere. You offset the pointer by the ASCII value of the character that you read. That's silly.

Then, you print data from some random location is memory.

You need to make the pointer point to somewhere that you can write. You need to use the pointer to store the data. Then, you need to increment the pointer by ONE!

But, since you don't need a pointer at all, stop trying to use one.

Allocate an array, of fixed size - equal to the most data you expect to receive. Define an index variable. Store a character at the position indicated by the index. Increment the index. Store a NULL.

byte index = 0;
char data[80];

    while (client.available())
    {
      c = client.read();
      data[index] = c;
      index++;
      data[index] = '\0';
    }

When you are done using the data in the array, set index back to 0.

This true?

  byte index = 0;
  char data[80];

  while (client.available()) {
    char c = client.read();
    data[index] = c;
    index++;
    data[index] = '\0';
  }

  char result;
  for (byte  i = 0; i < data[80]; i++) {
    result = result + data[i];
  }

  Serial.print(result);
  char result;
  for (byte  i = 0; i < data[80]; i++) {
    result = result + data[i];
  }

Why are you ADDING characters? What is THAT supposed to do?

#include <SPI.h>
#include <Ethernet.h>

byte server[] = {192, 168, 1, 34};
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

String location = "hydropronic/setting/getsettingresult HTTP/1.0";

EthernetClient client;

char inString[256];
int stringPos = 0;
boolean startRead = false;

void setup() {
  Ethernet.begin(mac);
  Serial.begin(9600);
  Serial.println("Welcome");
}

void loop() {
  String pageValue = connectAndRead();
  Serial.println(pageValue);
  delay(5000);
}

String connectAndRead() {

  Serial.println("connecting...");

  if (client.connect(server, 80)) {
    Serial.println("Connected");
    client.print("GET ");
    client.println(location);
    client.println();

    return readPage();
    delay(5000);
  } else {
    return "Connection Failed";
  }
}

String readPage() {
  stringPos = 0;

  memset( &inString, 0, 256 );

  while (true) {
    if (client.available()) {
      char c = client.read();

      inString[stringPos] = c;
      stringPos ++;

      return inString;
    }
  }
}

This's Right?

ai1love6:
This's Right?

There's one way to find out.