Iot Device always need the internet capabilities. So they can Get/Post some sensor data to/from a web server.
The simple step-by-step tutorial for IOT developers to getting up and running with Low-Energy WiFi device and rest api.
Making you connected things talk to your cloud in 30 minutes.
Here’s the deal: I’m going to show you how to get all of this stuff set up. I’ll be assuming that you’re a full-stack programmer who knows JavaScript/Node.JS/Express/Arduino well enough that I don’t have to explain those. If that’s you, then this should be a solid primer.
Let’s go.
Part1: Writing the Server side code:
var http = require('http');
var express = require('express');
var mongoose = require('mongoose');
var app = express();
app.use(express.logger());
app.use(express.bodyParser());
function getTimes(req, res) {
var now = new Date();
var hour = now.getUTCHours();
var minites = now.getUTCMinutes();
var strHour = hour.toString();
var strMinites = minites.toString();
return res.json({ hour: strHour, minites: strMinites });
}
app.get('/v1/times', getTimes);
In restful design, all of the resource can be described by URL.
here, we route URL /v1/times to method “getTimes()”
Every time CC3200 Get URL /v1/times, the method will be called.
and a json string will be returned.
Part2: Device side, CC3200 + Energia
Much like Arduino + WiFi shield. But more powerful.
[code]/** perform a Curd to web resource
@param serverIp IP address of the server to connect to
@param serverName Name of the server being connected to. If NULL, the
“Host” header line won’t be sent
@param urlPath Url to request
@param httpMethod CURD http method
@return response body in json string format
/
String PerformHttpCurd(int httpMethod, const char urlPath, const IPAddress &serverIp, const char* serverName, const int serverPort) {
String strJson = “”;
int err = 0;
if(httpMethod == GET) {
err = http.get(serverIp, serverName, serverPort , urlPath);
} else if(httpMethod == POST) {
err = http.post(serverIp, serverName, serverPort , urlPath);
} else if(httpMethod == PUT) {
err = http.put(serverIp, serverName, serverPort , urlPath);
} else if(httpMethod == DELETE) {
return “”; //delete is not supported now
} else {
return “”;
}
if(0 == err) {
Serial.println(F("StartedRequest OK"));
err = http.responseStatusCode();
if(err >= 0) {
Serial.print("Got status code: ");
Serial.println(err);
err = http.skipResponseHeaders();
if(err >= 0) {
int bodyLen = http.contentLength();
Serial.print("Body len:");
Serial.println(bodyLen);
//implement a timeout approach
uint32_t timeoutStart = millis();
while((http.connected() || http.available()) && ((millis() - timeoutStart) < kNetworkTimeout)) {
if(http.available()) {
char c = http.read();
strJson += c;
timeoutStart = millis(); //we read something, reset the timeout
} else {
delay(kNetworkDelay); //wait for any response
}
}
} else {
Serial.print(F("Fail to skip the response header: "));
Serial.println(err);
}
} else {
Serial.print(F("Get response failed: "));
Serial.println(err);
}
} else {
Serial.print(F("Connected fail: "));
Serial.println(err);
}
http.stop(); //release web service
return strJson; //return the http body in json string format
}
// #end region Call web service [/code]