Jump to content
43oh

Transmit data from sensors to server over Wifi


Recommended Posts

Hi, 
I'm trying to read in data from my sensor and then send it to my server over Wifi. I am not very sure about the best format to send numerical data. the CC3200 is the client that periodically sends values to the server. The server is being built using Ruby on Rails with HTTP and TCP.
There also seems to be some ambiguity about the usage of the GET method. Is the PUT method a better choice? if so, what would be the correct format to send the data??

The code I'm working with:
 

#ifndef __CC3200R1M1RGC__
// Do not include SPI for CC3200 LaunchPad
#include <SPI.h>
#endif
#include <WiFi.h>
 
const int pingPin = 7;
 
// your network name also called SSID
char ssid[] = "energia";
// your network password
char password[] = "launchpad";
 
// Initialize the Wifi client library
WiFiClient client;
 
// server address:
char server[] = "energia.nu";
//IPAddress server(50,62,217,1);
 
unsigned long lastConnectionTime = 0;            // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
  long duration, cm;
 
 
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
    
 
  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  // print the network name (SSID);
  Serial.println(ssid); 
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    // print dots while we wait to connect
    Serial.print(".");
    delay(300);
  }
  
  Serial.println("\nYou're connected to the network");
  Serial.println("Waiting for an ip address");
  
  while (WiFi.localIP() == INADDR_NONE) {
    // print dots while we wait for an ip addresss
    Serial.print(".");
    delay(300);
  }
 
  Serial.println("\nIP Address obtained");
  // We are connected and have an IP address.
  // Print the WiFi status.
  printWifiStatus();
 
 
  Serial.begin(9600);
}
 
void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  while (client.available()) {
    char c = client.read();
    Serial.write
Link to post
Share on other sites

I'm using a CC3200 to push data to data.sparkfun.com - using GET works just fine for me.  I honestly haven't tried post, and whether or not it will work obviously depends a bit on the technology you have on the back-end.  data.sparkfun.com is fine accepting GET, but if I was using WebAPI (.NET) - I'd have to set my methods to accept GET or switch to POST.

client.println("GET /input/" + PhantStreams[2] + "?private_key=" + PhantPrivateKeys[2] + dataParams + "&temp2=0 HTTP/1.1");
client.println("Host: data.sparkfun.com");
client.println("User-Agent: Energia/1.1");
client.println("Connection: close");
client.println();

It looks like you've just posted the beginning wifi sample for Energia (host name is still energia.nu for instance) - is there a specific problem you're running into?  

Link to post
Share on other sites

Hi, I'm using heroku to host my server which is coded using ruby on rails. the server returns an error message. code 500 and the logs state that the template isn't found.

 

Yeah, I just used the example as a base to build my own application as i'm just starting off with the cc3200. 
The current code is:

#ifndef __CC3200R1M1RGC__
// Do not include SPI for CC3200 LaunchPad
#include <SPI.h>
#endif
#include <WiFi.h>
 
 
const int pingPin = 1;
// your network name also called SSID
char ssid[] = "samiya";
// your network password
char password[] = "samiya95";
 
// Initialize the Wifi client library
WiFiClient client;
 
// server address:
char server[] = "autosewage.herokuapp.com";
//IPAddress server(50,62,217,1);
 
unsigned long lastConnectionTime = 0;            // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
long cm=50;
 
 
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
 
  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  // print the network name (SSID);
  Serial.println(ssid); 
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    // print dots while we wait to connect
    Serial.print(".");
    delay(300);
  }
  
  Serial.println("\nYou're connected to the network");
  Serial.println("Waiting for an ip address");
  
  while (WiFi.localIP() == INADDR_NONE) {
    // print dots while we wait for an ip addresss
    Serial.print(".");
    delay(300);
  }
 
  Serial.println("\nIP Address obtained");
  // We are connected and have an IP address.
  // Print the WiFi status.
  printWifiStatus();
}
 
void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  while (client.available()) {
    char c = client.read();
    Serial.write©;
  }
 
  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }
 
}
 
// this method makes a HTTP connection to the server:
void httpRequest() {
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();
 
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.println("PUT /treatment_plants HTTP/1.1");
    client.println("Host: autosewage.herokuapp.com");
    //client.println("User-Agent: Energia/1.1");
    client.print("Content-Length: ");
 
    // calculate the length of the sensor reading in bytes:
    // 8 bytes for "sensor1," + number of digits of the data:
    int thisLength = 4;
    client.println(thisLength);
 
    // last pieces of the HTTP PUT request:
    client.println("Accept :text/plain");
    client.println("Content-Type: text/csv");
    client.println("Connection: close");
    client.println();
    
     client.print(pingPin);
    client.print(",");
    client.println(cm);
  
  
  
    // note the time that the connection was made:
    lastConnectionTime = millis();
  }
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}
 
 
void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
 
  // print your WiFi IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
 
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}
Link to post
Share on other sites

I haven't yet included the ping function of the sensors yet. once the database accepts these dummy values i can incorporate the function into the code as well. as of now, the cc3200 connects periodically to the server, but as i mentioned, the server returns only an error message. i am trying to update the value of a particular variable in the database.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...