Page 1 of 1

Controlling LED's with the Spark Core over the web.

Posted: Sun Jun 11, 2017 10:35 am
by Marcel
Hi,

My goal was to check if I could use smart BASIC to control my Spark Core IoT device. Here I show the smart BASIC code and the Spark Core c code that does the job. I've connected 2 leds to the Spark Core and will control them via the iPad and smart BASIC.

LED.JPG
LED.JPG (137.97 KiB) Viewed 2951 times

Below you see the Particle WEB IDE and the c code which is flashed to memory inside the Spark Core. The Spark is just trapped in a loop but when there is a matching API request for the leds the function ledControl() is run.

WEBIDE2.JPG
WEBIDE2.JPG (57.57 KiB) Viewed 2951 times

I created with help of the smart BASIC documentation the next BASIC code.

Code: Select all

COREID$ = "*******************"
ACCTKN$ = "********************"

led1 = 1
IF led1 = 1 THEN
	send_cmd$ = "params=l1,HIGH"
ELSE
	send_cmd$ = "params=l1,LOW"
ENDIF
URL$ = "https://api.*****/devices/" & COREID$ & "/led?access_token=" & ACCTKN$
h$(1) = "content-type:application/x-www-form-urlencoded; charset=UTF-8"
HTTP URL$ HEADER h$ POST send_cmd$
PRINT HTTP_RESPONSE$()
PRINT "HTTP HEADER: " & HTTP_HEADER$()
PRINT "HTTP STATUS: " & HTTP_STATUS()
PRINT "HTTP ERRORS: " & HTTP_ERROR$()
END
The result of this code is that the LED which is at the OFF state is switched ON. Below you see the HEADER and STATUS information after the request has been sent.

{"id":"************************","last_app":"","connected":true,"return_value":1}
HTTP HEADER: Connection: keep-alive
Date: Sun, 11 Jun 2017 08:19:34 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 81
Server: nginx
Access-Control-Allow-Origin: *
HTTP STATUS: 200
HTTP ERRORS:

I hope you wil get inpired to use smart BASIC in combination with the IoT devices like the Spark Core or Photon.

Namasté

Re: Controlling LED's with the Spark Core over the web.

Posted: Sun Jun 11, 2017 10:56 am
by Mr. Kibernetik
Very interesting!