Module Integration Guides
Linux
Pushing data to Bytebeam
6 min
this section talks about how to push the data from the device to the cloud data is pushed to the cloud in docid\ y7umxyghxnlxyfcevoggc to push data you need to write an application that connects to uplink over tcp and pushes data to it uplink then in turn pushes the data to the cloud over mqtt as an example, this guide assumes you are pushing temperature data to bytebeam create a stream on the cloud first, let us create a stream called temperature on the bytebeam cloud first, g o to https //cloud bytebeam io/ now, click on your username in the top right corner and navigate to the “ settings ” section next, select streams and click on " create stream " now, enter the stream name and column name in this example, we have chosen the data type of temperature as float64 click on the " submit " button the temperature stream should be visible under the " streams " section configuration on the device side you can now write to uplink from your application below you can see samples for some popular languages demo py import socket import json import time from gpiozero import cputemperature s = socket socket(socket af inet, socket sock stream) s connect(("localhost", 5050)) \# constructs a payload and sends it over tcp to uplink def send data(s, payload) send = json dumps(payload) + "\n" s sendall(bytes(send, encoding="utf 8")) \# send the temperature data def send temperature stream(s, sequence) cpu temperature = cputemperature() temperature timestamp = int(time time() 1000) payload = { "stream" "temperature", "sequence" sequence, "timestamp" timestamp, "temp" cpu temperature, } print(payload) send data(s, payload) print("starting uplink bridge app") sequence = 1 while true time sleep(5) send temperature stream(s, sequence) sequence += 1 demo js const net = require("net"); const util = require("util"); const exec = util promisify(require("child process") exec); // connect to the server const client = new net socket(); client connect(5050, "localhost", () => { console log("connected to server"); }); // constructs a payload and sends it over tcp to uplink function senddata(payload) { const send = json stringify(payload) + "\n"; client write(send); } async function getcputemperature() { try { const { stdout } = await exec("cat /sys/class/thermal/thermal zone0/temp"); const temperature = parsefloat(stdout) / 1000; return temperature; } catch (error) { console error("error ", error message); return null; } } async function sendtemperaturestream(sequence) { const cputemperature = await getcputemperature(); if (cputemperature !== null) { const timestamp = date now(); const payload = { stream "temperature", sequence sequence, timestamp timestamp, temp cputemperature, }; console log(payload); senddata(payload); } else { console log("failed to retrieve cpu temperature "); } } console log("starting uplink bridge app"); let sequence = 1; setinterval(() => { sendtemperaturestream(sequence++); }, 5000); demo rs use tokio net tcpstream; use tokio prelude ; use tokio time {self, duration}; use serde json json; use std fs; async fn send data(mut stream \&tcpstream, payload serde json value) { let send = format!("{}\n", payload to string()); stream write all(send as bytes()) await unwrap(); } fn read cpu temperature() > f32 { let contents = fs read to string("/sys/class/thermal/thermal zone0/temp") unwrap or else(| | "0" to string()); let temperature = contents trim() parse \<f32>() unwrap or(0 0); temperature / 1000 0 } async fn send temperature stream(mut stream \&tcpstream, sequence u64) { let cpu temperature = read cpu temperature(); let timestamp = systemtime now() duration since(systemtime unix epoch) unwrap() as millis(); let payload = json!({ "stream" "temperature", "sequence" sequence, "timestamp" timestamp, "temp" cpu temperature }); println!("{ ?}", payload); send data(\&mut stream, payload) await; } \#\[tokio main] async fn main() { let addr = "localhost 5050"; let mut stream = tcpstream connect(\&addr) await unwrap(); println!("starting uplink bridge app"); let mut sequence = 1; let mut interval = time interval(duration from secs(5)); loop { interval tick() await; send temperature stream(\&stream, sequence) await; sequence += 1; } } demo go package main import ( 	"bufio" 	"encoding/json" 	"fmt" 	"io/ioutil" 	"net" 	"strconv" 	"time" ) func readcputemperature() float64 { 	data, err = ioutil readfile("/sys/class/thermal/thermal zone0/temp") 	if err != nil { 	 fmt println("error reading temperature ", err) 	 return 0 0 	} 	tempstr = string(data) 	tempint, err = strconv atoi(tempstr\[ len(tempstr) 1]) 	if err != nil { 	 fmt println("error parsing temperature ", err) 	 return 0 0 	} 	return float64(tempint) / 1000 0 } func senddata(conn net conn, payload map\[string]interface{}) { 	data, = json marshal(payload) 	fmt println(string(data)) 	conn write(append(data, '\n')) } func main() { 	conn, err = net dial("tcp", "localhost 5050") 	if err != nil { 	 fmt println("error connecting ", err error()) 	 os exit(1) 	} 	defer conn close() 	fmt println("starting uplink bridge app") 	sequence = 1 	for { 	 time sleep(5 time second) 	 temp = readcputemperature() 	 t = time now() unixnano() / int64(time millisecond) 	 payload = map\[string]interface{}{ 	 "stream" "temperature", 	 "sequence" sequence, 	 "timestamp" t, 	 "temp" temp, 	 } 	 senddata(conn, payload) 	 sequence++ 	} } if you do not see your favorite language do not worry you can write to uplink in 2 easy steps create a tcp socket and connect to localhost and port 5050 periodically write data in the below json format to the port (note newline delimited) { "stream" "temperature", "sequence" 1, "timestamp" 1698806057153, "temp" 17 0, } in the above json you need to auto increment the sequence for each packet and the timestamp needs to be in milliseconds the temperature data should now be visible on the cloud head over to bytebeam cloud and go to device management > device tab now c lick on the device and select "show streams" select "temperature" stream the temperature data should be visible on the cloud now
