Receive data through the I2C on the Spark core and send it to the cloud

Dear @peekay123
Yes, that is correct. The core can tell which sensor is on or off by displaying the data and see if it is all zeros (SA0000E) or these are some active sensor(SA0001E). For the sleep issue, I’ll leave it for now, and go forward to be able to flash a correct code on the spark too if I can see the correct data or not.
Thanks

Dear @Jack
For the info that I want, I need to be as fast as possible. May be I need to see the data each second because the data is continuously streaming from the PIC18. I aso need the info to my future historic log.
Thanks.

Dear @ScruffR
Thanks for your kindly explanation. I’l to do that when I’m publishing the core. I corrected the Capital letter as you told me since the C is case sensitive.
Appreciating your help.
Ahmed

Does your code compile? I noticed that you had INT RXPIN = RX; and str incomingBytes1 = 0; which each have issues. INT should be int (going back to case issues) and str probably should be char. I encourage you to keep iterating on it and post any additional questions :smile:

In that case, forget going to sleep mode. If you are inside a house, then I assume you are powering from the house power, rather than a battery, so It is not that important to try to save battery power anyway.

When the Core shuts down the Wi-Fi chipset (CC3000), it may take "several" seconds to get it connected back to the cloud once the Core wakes up.

Do you have the 4 Cores yet, or one ? Do you have code running on the core yet?

Dear @Jack
Right now, I have two cores, and I tried to send the data from the Pic18 to the Core. I can see the data on the Dashboard, but it is a byte for each line. I mean the data looks like this:
E
0
2
0
0
A
S

Firstly, Can I display these data as a line by line such as:
SA0020E
Secondly, how can I create a server Node.Js on the cloud to collect the data when it is come from more than one core?
I’ll share the code whenever I have access to my laptop.
Thanks.

Here is the code that I’m using to receive the data from the PIC18 and send it to the cloud Dashboard.

int RXPIN = RX;
int LED = D7;
int  incomingBytes1=0;
char incomingBytes2=0;
void setup()
{
  pinMode(RXPIN,INPUT);
  pinMode(LED,OUTPUT);
  Serial1.begin(19200);
}
void loop()
{
  if (Serial1.available())
  {
    incomingBytes1 = Serial1.read();
    incomingBytes2=(char) incomingBytes1; 
    delay(2000);
    Spark.publish("Carpet1",& incomingBytes2);
    digitalWrite(LED,HIGH);
    delay(1000);
    digitalWrite(LED,LOW);
  }
}

 to format a code block propperly and keep indentations, wrap it between 

code block

Despite being short, your code would be easier to read and it’s good practice to indent your code propperly, please.

Thanks @ScruffR
Yes this is easier for me also. Please do you have idea how can I do these things:
Firstly, Can I display these data as a line by line such as:
SA0020E instead of displaying it as:
E
0
2
0
0
A
S

Secondly, how can I create a server Node.Js on the cloud to collect the data when it is come from more than one core?
Have a great night.

You are publishing each incoming byte separately, that's why it is displayed one letter per line and not whole value. Accumulate the data until you find trailing "E", then publish them all at once.

You can use something like String value = Serial1.readStringUntil("E") or something like that (you’ll have to dig into the Stream API), though, I think that particular command might ignore/skip the E (i.e. value would be SA0020 without the E), I don’t remember.

Thank you so much @lami
I think you are telling me the same thing that is posted in the next comment.
Have a great night.

Hello @tjp
I’ll try this method to see if it works to publish a whole line instead of a byte. Thanks for your suggestion. I’ll post the result if it works or not.
Best.

Good Morning @tjp
I tried to use this:

String value = Serial1.readStringUntil("E");

but it didn't compile. Here is the error that I got:

In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from publish.cpp:2:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
publish.cpp: In function 'void loop()':
publish.cpp:15:47: error: invalid conversion from 'const char*' to 'char' [-fpermissive]

^
In file included from ../inc/spark_wiring.h:34:0,
from ../inc/application.h:29,
from publish.cpp:2:
../inc/spark_wiring_stream.h:98:10: error: initializing argument 1 of 'String Stream::readStringUntil(char)' [-fpermissive]
String readStringUntil(char terminator);
^
publish.cpp:17:36: error: no matching function for call to 'SparkClass::publish(const char [8], String*)'
{
^
publish.cpp:17:36: note: candidates are:
In file included from ../inc/spark_wiring.h:33:0,
from ../inc/application.h:29,
from publish.cpp:2:
../inc/spark_utilities.h:110:14: note: static void SparkClass::publish(const char*)
static void publish(const char eventName);
^
../inc/spark_utilities.h:110:14: note: candidate expects 1 argument, 2 provided
../inc/spark_utilities.h:111:14: note: static void SparkClass::publish(const char
, const char*)
static void publish(const char eventName, const char eventData);
^
../inc/spark_utilities.h:111:14: note: no known conversion for argument 2 from 'String
' to 'const char
'
../inc/spark_utilities.h:112:14: note: static void SparkClass::publish(const char*, const char*, int)
static void publish(const char eventName, const char eventData, int ttl);
^
../inc/spark_utilities.h:112:14: note: candidate expects 3 arguments, 2 provided
../inc/spark_utilities.h:113:14: note: static void SparkClass::publish(const char
, const char
, int, Spark_Event_TypeDef)
static void publish(const char *eventName, const char eventData, int ttl, Spark_Event_TypeDef eventType);
^
../inc/spark_utilities.h:113:14: note: candidate expects 4 arguments, 2 provided
../inc/spark_utilities.h:114:14: note: static void SparkClass::publish(String)
static void publish(String eventName);
^
../inc/spark_utilities.h:114:14: note: candidate expects 1 argument, 2 provided
../inc/spark_utilities.h:115:14: note: static void SparkClass::publish(String, String)
static void publish(String eventName, String eventData);
^
../inc/spark_utilities.h:115:14: note: no known conversion for argument 2 from 'String
' to 'String'
../inc/spark_utilities.h:116:14: note: static void SparkClass::publish(String, String, int)
static void publish(String eventName, String eventData, int ttl);
^
../inc/spark_utilities.h:116:14: note: candidate expects 3 arguments, 2 provided
../inc/spark_utilities.h:117:14: note: static void SparkClass::publish(String, String, int, Spark_Event_TypeDef)
static void publish(String eventName, String eventData, int ttl, Spark_Event_TypeDef eventType);
^
../inc/spark_utilities.h:117:14: note: candidate expects 4 arguments, 2 provided
make: *** [publish.o] Error 1

Here is the full code:

int RXPIN = RX;
int LED = D7;

void setup()
{
pinMode(RXPIN,INPUT);
pinMode(LED,OUTPUT);
Serial1.begin(19200);
}

void loop()
{
if (Serial1.available())
{
String value = Serial1.readStringUntil("E");
delay(2000);
Spark.publish("Carpet1",& value);
digitalWrite(LED,HIGH);
delay(1000);
digitalWrite(LED,LOW);
}
}


Please Any suggestion? Do I need to define something like this at the beginning:

String value=0;

Thanks.

I’ve reformatted your code twice now, please read the comment I added to your previous post and obide to it

 to format a code block propperly and keep indentations, wrap it between 

code block


This error line

publish.cpp:15:47: error: invalid conversion from 'const char*' to 'char' [-fpermissive]

tells you that you are using some wrong datatype at that very line.

Looking at the Stream implementation, as @tjp suggested, the call to readStringUntil() should rather look like this

Serial1.readStringUntil('E');  // it needs to be char rather than a const char*

No, you don’t need to do String value=0;, but if it doesn’t work try

  String value;
  value = "" +  Serial1.readStringUntil('E');  // overcome possible quirk with String class

Thank you so much @ScruffR
It compiled successfully. I’ll try to flash it on the spark, and see what happen.
Do you mean that I should put like this before and after the code?


My code

I’m really sorry that I couldn’t understand you from the first time. I’ve already put my code between the Blockquote.
Thanks.

It’s not the lines that make the difference, but the three grave-accent symbols


whicht have to be put on the first column of a new line, before and after your code

---

Another issue with your code is the ampersand `&` in this line

Spark.publish(“Carpet1”,& value);
// should rather be
Spark.publish(“Carpet1”, value);
// or
Spark.publish(“Carpet1”, value.c_str());

I'm a bit surprised, that it compiled without error.

Thanks @ScruffR
For the code, I've already delete the (&) and put just (value) in the publish function. I flashed the code on the spark, and as @tjp said it shows me the code without (E). Can I make concatenating a char (which is a return ('\n')) into a string, so I can read the whole data from (S to E) and change the:
``

String value = Serial1.readStringUntil(' E ');
``

``

String value = Serial1.readStringUntil(' '\n' ');
``

Sorry for the stupid question, but how can I insert these three grave-accent?
Thanks in advance.

If they are separated by a \n then the following will work:

String value = Serial1.readStringUntil('\n');

Be very careful where you put spaces, ' E ' is not the same as 'E' and will not compile. That statement does not appear to be 100% accurate. But do be careful. Code is not as forgiving as the human brain, case, whitespace and syntax tend to be very important depending on the language.

Thanks for that @tjp
I see what you mean. In fact, I don’t have a (\n) in my code, but I think I should change the code in the PIC18 to make the transmitting data has a (\n) at the end. In this case, it will be easier for me to detect the \n in the receiving data, and make the condition read until the '\n. Is this what you meant?