Tutorial: Perform a CURD action using CC3200 LaunchPad

Moderators: violet, jessie

Post Reply
longwang
Staff
Staff
Posts: 3
Joined: Tue Oct 14, 2014 9:53 am

Tutorial: Perform a CURD action using CC3200 LaunchPad

Post by longwang » Mon Nov 17, 2014 4:17 pm

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:

Code: Select all

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: Select all

/** 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 

aleezakhan268
Pre-kindergarten
Pre-kindergarten
Posts: 1
Joined: Mon Dec 29, 2014 6:23 pm

Re: Tutorial: Perform a CURD action using CC3200 LaunchPad

Post by aleezakhan268 » Mon Dec 29, 2014 6:27 pm

Gateway program will read its configuration files at startup. We create a yaml file in root directory. Modify the following items to meet your request.



___________________________________________
Our excellent online ccna training programs will lead you to success in the ccna exam questions We also offer latest ccna service provider and Rasmussen College with 100% success guarantee. Our Sterling College is rare in IT world.

sallusaima
Pre-kindergarten
Pre-kindergarten
Posts: 2
Joined: Thu Aug 27, 2015 10:01 pm

Re: Tutorial: Perform a CURD action using CC3200 LaunchPad

Post by sallusaima » Thu Aug 27, 2015 10:06 pm

Tutorial curd is very nice..

Post Reply