Reputation: 41
I am trying to write a program which is able to periodically upload data to a blockchain, for example the temperature in my office. Is it possible? What would the easiest approach be to achieve this?
Upvotes: 2
Views: 491
Reputation: 41232
There should not be a problem, you need to write your own chaincode which will be capable to receive and update temperature in ledger. Moreover to connect it with IoT device you will need to develop some mediating application which need to leverage one of available SDKs to interact with your chaincode.
Here you can find and chaincode API which you will need to implement. There is also a good tutorial on how to get started with Hyperledger Fabric network and how to Write Your First Application.
There is also a repository with a few sample applications to give you a taste of how to develop chaincode and interact with it from the application using NodeJS SDK.
Here is very primitive example of how your chaincode might look like:
type temperatureSmartContract struct {
}
func (contract *temperatureSmartContract) Init(stub shim.ChaincodeStubInterface) peer.Response {
fmt.Println("Initialize chaincode if needed")
return shim.Success(nil)
}
func (contract *temperatureSmartContract) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
funcName, params := stub.GetFunctionAndParameters()
if funcName == "addTemperature" {
// Store observation into ledger
stub.PutState("temperature", []byte(params[0]))
} else if funcName == "getTemperatures" {
iter, err := stub.GetHistoryForKey("temperature")
if err != nil {
shim.Error(fmt.Sprintf("%s", err))
}
var result []string
for iter.HasNext() {
mod, err := iter.Next()
if err != nil {
shim.Error(fmt.Sprintf("%s", err))
}
result = append(result, string(mod.Value))
}
return shim.Success([]byte(result))
}
return shim.Success(nil)
}
Of course this is very primitive way to capture periodic temperature updates into the ledger, while it could provide you a good start.
Upvotes: 3