Arduino receive data from software serial - json

Sorry for my weak English. I'm trying to receive the json data from Sim800 on my Arduino. To read the data on the serial port I used the following code:
while(serialSIM800.available()==0); //Wait until the data is received
String content = "";
while(serialSIM800.available()>0){ // When data is received
content = content + char(char (serialSIM800.read()));
}
Serial.print(content);
But incomplete data is received. As follows:
{"id":"1212","temp":"24","hum","4
For the better result I used the following code:
byte x;
char data[128];
void sim800Reply() {
x=0;
do{
while(serialSIM800.available()==0);
data[x]=serialSIM800.read();
Serial.print(data[x]);
x++;
} while(!(data[x-1]=='K'&&data[x-2]=='O'));
}
Data is completely received. As follows:
{"id":"1212","temp":"24","hum","45","date":"11.2018","status":"200"}
OK
But I think this code is not good and there are problems.for example If the serialSIM800 is not available like when sim800 are not connected, The following code causes crash while(serialSIM800.available()==0); Because this is always true OR If there is an error and OK Was not received, The following code causes crash while(!(data[x-1]=='K'&&data[x-2]=='O')); Because this is always true.The maximum data length is 120 bytes, What should I do to Receive the Json data from Arduino serial? Thank you all.

for start try:
if (serialSIM800.available()) {
String content = serialSIM800.readString();
Serial.print(content);
}
in setup() add serialSIM800.setTimeut(50);

Finally, I changed the code as follows:
String dump(bool printData) {
byte while_cunt = 0;
while (serialSIM800.available() == 0){
while_cunt++;
delay(15); // It can change for a proper delay
if(while_cunt >=250){ // If the data from serial was not received past 3.75 seconds
Serial.println("return");
return ""; // Exit from dump function
}
}
String content = "";
while (serialSIM800.available() > 0) {
content += serialSIM800.readString();
}
if (printData) {
Serial.println(content);
}
return content;
}
It can return and print serial data And it works fast Because every 15 milliseconds checks that the data has been received and if it has not been received for a certain period of time(In this case 3.75 seconds), it will be out of process and will not crash.
Examples for this function:
serialSIM800.println("AT");
dump(true);
// print Received response from sim800
serialSIM800.println("AT");
String str = dump(true);
// print Received response from sim800 and Saved in variable named 'str'
serialSIM800.println("AT");
String str = dump(false);
//Save response in variable named 'str' without print

Related

JSON decoding from stream in Kotlin

I have a server set up to send messages over a local host port. I am trying to decode the serialized json messages sent by the server and get this error.
Error decoding message: kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 55: Expected EOF after parsing, but had instead at path: $
JSON input: .....mber":13,"Timestamp":5769784} .....
The Racer State messages are formatted in JSON as follows: { “SensorId”: “value”, “RacerBibNumber” : “value”, “Timestamp” : “value” }, where the value’s are character string representations of the field values. I have also tried changing my RacerStatus Class to take String instead of Int but to a similar error. Am I missing something here? The symbol that is missing in the error was not able to be copied over so I know it's not UTF-8.
I have also added
val inputString = bytes.toString(Charsets.UTF_8)
println("Received input: $inputString")
This gets
Received input: {"SensorId":0,"RacerBibNumber":5254,"Timestamp":3000203}
with a bunch of extraneous symbols at the end.
data class RacerStatus(
var SensorId: Int,
var RacerBibNumber: Int,
var Timestamp: Int
) {
fun encode(): ByteArray {
return Json.encodeToString(serializer(), this).toByteArray()
}
companion object {
fun decode(bytes: ByteArray): RacerStatus {
print(bytes[0])
try {
val mstream = ByteArrayInputStream(bytes)
return Json.decodeFromStream<RacerStatus>(mstream)
} catch (e: SerializationException) {
println("Error decoding message: $e")
return RacerStatus(0, 0, 0)
}
// return Json.decodeFromString(serializer(), mstream.readBytes().toString())
}
}
}
So I found an answer to my question. I added a regex to include just the json components I know my json contains.
val str = bytes.toString(Charsets.UTF_8)
val re = Regex("[^A-Za-z0-9{}:,\"\"]")
return Json.decodeFromString<RacerStatus>(re.replace(str,""))
I thought that Charsets.UTF_8 would remove the misc characters but it did not. Is there a more intiuative solution? Also is there a regex that would cover all possible values in json?

Adaptive card with Luis intent

this approach was working for me nicely until last month. But now i am getting error msg that my bot code is havin an issue. Is there any changes to it recently. Here is my code>
protected override Task<string> GetLuisQueryTextAsync(IDialogContext context, IMessageActivity message)
{
if (message.Value != null)
{
dynamic value = message.Value;
// assuming your DataJson has a type property like :
// DataJson = "{ \"Type\": \"Curse\" }"
string submitType = value.x.ToString();
return Task.FromResult(submitType);
}
else
{
// no Adaptive Card value, let's call the base
return base.GetLuisQueryTextAsync(context, message);
}
}
Datajson> "x":"introduction"
When you debug into this method, which line does the error occur?
There are a number of places in the method that 'could' be potential problems.
Is 'message' null?
Are you sure message.Value is a properly formed json string?
Does the properly formatted json string actuall contain 'x'?
Let us know where in the method the error is occurring and what the exception is.

Groovy/Grails - Json web service, map received with extra properties

I'm busy working on a function that sends a map (and other irrelevant values) through a json web service but the problem is that on the receiving side it sees it as JSONObject (using the getClass() method) and coz of this it adds extra properties to the object thus causing the service to return an error. So on the sending side the map println's like this:
[21:{adultValue=1, kidValue=0}, 11:{adultValue=0, kidValue=4}]
But on the receiving side it println's like this:
[11:[metaPropertyValues:[[name:adultValue, modifiers:1, type:java.lang.Object], [name:kidValue, modifiers:1, type:java.lang.Object]], properties:[kidValue:4, adultValue:0]], 21:[metaPropertyValues:[[name:adultValue, modifiers:1, type:java.lang.Object], [name:kidValue, modifiers:1, type:java.lang.Object]], properties:[kidValue:0, adultValue:1]]]
So on the receiving side there is code that does the following (simplified for this question):
map.each { key, value ->
def adultValue = value.adultValue.toInteger()
}
But it obviously throws a NullPointer exception coz according to the receiving side there's no map.adultValue, there's only a map.properties.adultValue
So my question is, what can I do on the sending side so that the receiving side receives it the same way that it was sent? I'm not allowed to change the receiving side's code but I can look at the output.
For extra clarification, here's the code I use on the sending side to make the web service call:
def addAddons(map, anotherVar1, anotherVar2) {
println(map) // outputs the map as shown above
try {
def result
def http = new HTTPBuilder("the real url, not shown here")
http.request(Method.POST, groovyx.net.http.ContentType.JSON) { req ->
body = [anotherVar1: anotherVar1, anotherVar2: anotherVar2, map: map]
response.success = { resp, json ->
result = json
}
}
println("result: ${result}")
return result
} catch (all) {
all.printStackTrace()
return null
}
}
Please let me know if there's anything else I need to add
You may also consider to use a JSON converter, so you still can create a Map and create the string out of the map.
import groovy.json.JsonOutput
def json = JsonOutput.toJson(map)
Turns out I was looking at it all wrong. I didn't realise but I was supposed to send a Json object instead of anything else which is why a map didn't work, and neither would an Arraylist (I tried that too, not shown in my question). So I tried to convert it to json using groovy.json.JsonBuilder but I couldn't figure that out so I ended up manually converting my map to json with the following code:
def json = '{'
def counter = 0
map.each { key, value ->
if(counter == map.size()-1) {
json += "\"$key\":{\"adultValue\":${value.adultValue},\"kidValue\":${value.kidValue}}"
} else {
json += "\"$key\":{\"adultValue\":${value.adultValue},\"kidValue\":${value.kidValue}},"
}
counter++
}
json += '}'
And that solved my problem

How to Remove Half Received JSON String from an Array node js

I an using JSON and NODEJS
i am receiving data from TCP and i was receiving the data like this
[{"identification": {"id":3100,"version":1}},{"json1" : "THIS IS ONE json" },{"c]
or
[{"identification": {"id":3100,"version":1}},{"json1" : "THIS IS ONE json" },{"some n letters]
like this it is truncating while receiving TCP data i want to remove that half received data or is there any way to receive the full buffer
I want the Output to be
[{"identification": {"id":3100,"version":1}},{"json1" : "THIS IS ONE json" }]
A clumsy, but simple way would be to take the string/buffer and try to parse using JSON.parse(). Wrap it in a try/catch and only set the final value when it succeeds.
var badJson = '[{"identification": {"id":3100,"version":1}},{"json1" : "THIS IS ONE json" },{"c]';
try {
var result = JSON.parse(badJson);
}
catch(e) {
console.log('bad json, not parsing yet');
}

SocketIO cannot access JSON properties from ON

I don't know if I'm going crazy here but I'm doing something very basic that would usually be trivial, but I've been stuck on it for awhile now.
I'm receiving a message from a client like so:
socket.on('request_username', function(messageFromClient) {
if(messageFromClient == undefined) {
Logger.logError("messageFromClient was undefined in socket_api.js");
return;
}
// Parse the message into Json, might not be needed.
// Logs: "{\"username\":\"test\"}"
console.log(messageFromClient);
try {
messageFromClient = JSON.parse(messageFromClient);
//Logs: {"username":"test"}
console.log(messageFromClient);
} catch(err) {
console.log("Error parsing json from request_username. Mesage is %s", err);
return;
}
// All undefined.
console.log(messageFromClient['"username"']);
console.log(messageFromClient["username"]);
console.log(messageFromClient['username']);
// msg == undefined incase the parse failed.
if(messageFromClient == undefined || messageFromClient.username == undefined) {
Logger.logError("messageFromClient.username was undefined in socket_api.js");
return;
}
var usernameRequested = messageFromClient.username;
Now I'm getting this in the logs
"{\"username\":\"test\"}"
{"username":"test"}
Log Error: messageFromClient.username was undefined in socket_api.js
I've no idea what I'm doing wrong..
With socket.io, it automatically serializes Javascript data to/from JSON. You do not have to do that and if you attempt to, you can mess things up.
In socket.io, you can send like this:
var data = {username: "test"};
socket.emit('request_username', data);
Then, on the receiving side, you would have:
socket.on('request_username', function(data) {
console.log(data.username); // will show "test"
});
The serializing to/from JSON is done automatically by the socket.io library (one of its many useful features).
To debug your particular situation further, we will need to know exactly what the very first console.log(messageFromClient) shows right when the message first arrives before you've done anything to it?
You show a bunch of log info, but it's not entirely clear which debug line corresponds with which line of code. If that very first console.log shows:
"{\"username\":\"test\"}"
then your message is apparently still JSON which is probably because it was doubly JSON encoded which is an error on the sending side. This should be fixed on the sending side rather than trying to double parse it.
Also, when discussing this problem please be aware that JSON is a string format. A Javascript object is something you can directly access properties on in Javascript code. It appears you are sometimes calling them both JSON which is confusing for all. You convert a Javascript object to JSON with var jsonStr = JSON.stringify(obj) and you convert JSON to a Javascript object with var obj = JSON.parse(someJSON).
var obj = {username: test}; // Javascript object
var jsonStr = JSON.stringify(obj); // produces a JSON string
var obj2 = JSON.parse(jsonStr); // parses JSON string back to a Javascript object