Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@MechanicalCoder
Last active June 6, 2020 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MechanicalCoder/4176f925652788cb3532b7c61ba8ec2f to your computer and use it in GitHub Desktop.
Save MechanicalCoder/4176f925652788cb3532b7c61ba8ec2f to your computer and use it in GitHub Desktop.

What I learnt about Particle (Its awesome!)

Particle Cloud API

So we wanted to send our sensor data over wifi to a server to be published on web through spark photon. Looking into the Internet, we came across this spark.publish() function.

First we need to understand what Particle cloud API is.

Actually any API supports three basic operations - GET, POST & PUT. Here we request the URL using device ID and access token. You can easily get the access token by clicking the 'setting' button in Particle Build editor.

On searching for what's the quickest way to stream data from Photon I found these code snippets -

Example code to read udp packets in nodejs.

var dgram = require('dgram');
var udpServer = dgram.createSocket('udp4');

function processMonitorMsg(message){
	// Your logic here to process the udp message
} 

updServer.on('message',function(message, remote){
	processMonitorMsg(message);
});

udpServer.bing(9000);

Later on the developer who has written the above code also posted an small example that sends a udp packet to 10.0.0.3 -

UDP Udp;

unsigned char TxMsg[12] = {1, 254, 1, 254, 1, 254, 43, 212, 71, 184, 3, 252 };

void setup(){
	Udp.begin(9000);
}

void loop(){
	if(WiFi.ready()){
		Udp.beginPacket(IPAddress(10,0,0,3), 9000);
		//manipulate TxMsg array here
		//or put together your own array here
		Udp.write(TxMsg, 12);
		Udp.endPacket();
	}
}

Bonus -

I came across this awesome thing while I was searching for any method to view my Github profile traffic. It is just like Google analytics and it does give you lot of information about visitors, your repository performance and code commits in a very interactive way. Have a look at https://github.com/MechanicalCoder/Nvision2016/graphs/traffic. Just put the user name and his/her repository you're interested in.

Further Story - Day 2

Ok, so after lot of head-scratching moments, we were finally able to transfer data to a particular URL from the spark photon.

While looking for the alternative HTTP request testing websites, I came across requestbin. It is a general purpose utility website for testing your HTTP request (if you're making any) and display the data being sent on the page. It takes some times to get the data and display, so you've to continuously refresh the page after the lights on the spark photon started signalling the transfer event.

There is something called SSE event stream. Its is also defined in HTML5 API. It is basically server-side event stream.

@MechanicalCoder
Copy link
Author

Introduction

I used Particle Cloud API for sending data and retrieving it by HTTP request for displaying the parsed JSON data on the web interface. This is a snippet of my learning (whatever I could intermittently note) while figuring out lot of stuffs I didn't know about but was essential to learn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment