1

I would like to do an HTTP POST to Carriots. I saw a few tutorials.

Both of them are using ethernet instead of WiFi. I realize the ways of using Arduino WiFi shield and WizFi Shield are a bit different. What do I need to do to run a basic server using the WizFi Shield?

Code:

/******************************************************************
 WizFiShield Web Client Test Example
*****************************************************************/

// WizFiShield communicates using SPI, so include the SPI library:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SPI.h>
#include <WizFi2x0.h>
#include <WizFiClient.h>
#include <TimerOne.h>

#define SSID    "EricLeong3"        // SSID of your AP
#define Key     "ericChoong91"  // Key or Passphrase
// Wi-Fi security option (NO_SECURITY, WEP_SECURITY, WPA_SECURITY, WPA2PSK_SECURITY)
#define Security        WPA2PSK_SECURITY
#define APIKEY  "xxxxxxxxx" // Replace with your Carriots apikey
#define DEVICE  "[email protected]"

WizFi2x0Class myWizFi;
WizFiClient myClient;
TimeoutClass ConnectInterval;
boolean Wifi_setup = false;

///////////////////////////////
// 1msec Timer
void Timer1_ISR() {
  myWizFi.ReplyCheckTimer.CheckIsTimeout();
}
//
//////////////////////////////

void setup() {
  byte retval;
  Serial.begin(9600);
  Serial.println("\r\nSerial Init");
  // initalize WizFi2x0 module:
  myWizFi.begin();
  ConnectInterval.init();
  // Socket Creation with Server IP address and Server Port num 
  myClient =  WizFiClient("82,223,244,60", 80);
  // Timer1 Initialize
  Timer1.initialize(1000); // 1msec
  Timer1.attachInterrupt(Timer1_ISR);
  myWizFi.SendSync();
  myWizFi.ReplyCheckTimer.TimerStart(3000);
  Serial.println("Send Sync data");
  while(1) {
    if(myWizFi.CheckSyncReply()) {
      myWizFi.ReplyCheckTimer.TimerStop();
      Serial.println("Rcvd Sync data");
      break;
    }
    if(myWizFi.ReplyCheckTimer.GetIsTimeout()) {
      Serial.println("Rcving Sync Timeout!!");
      // Nothing to do forever;
      for(;;)
      ;
    }
  }
  ////////////////////////////////////////////////////////////////////////////
  // AP association  
  while(1) {
    retval = myWizFi.associate(SSID, Key, Security, true);
    if(retval == 1) {
      Serial.println("AP association Success");
      Wifi_setup = true;
      break;
    } else {
      Serial.println("AP association Failed");
    }
  }
}

void loop() {
  Serial.println("apleleee ");
  uint8_t retval;
  retval = myClient.connect();
  if(retval == 1) {
    String json = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"Air Temperature\":\"58\",\"Humidity\":\"59\"}}";
    Serial.println("Connected! ");
    //myClient.write((byte *)"GET /search?q=WizFi210 HTTP/1.0\r\n\r\n");
    myClient.write((byte *)"POST /streams HTTP/1.1");
    myClient.write((byte *)"Host: api.carriots.com");
    myClient.write((byte *)"Accept: application/json");
    myClient.write((byte *)"User-Agent: Arduino-Carriots");
    myClient.write((byte *)"Content-Type: application/json");
    myClient.write((byte *)"carriots.apikey: ");
    myClient.write((byte *)APIKEY);
    myClient.write((byte *)"Content-Length: ");
    byte thisLength = json.length();
    myClient.write(thisLength);
    myClient.write((byte *)"Connection: close");
    myClient.write((byte *)"");
    myClient.write((byte *)"{\"protocol\":\"v2\",\"device\":\"");
    myClient.write((byte *)DEVICE);
    myClient.write((byte *)"\",\"at\":\"now\",\"data\":{\"Air Temperature\":");
    myClient.write((byte *)"58");
    myClient.write((byte *)",\"Humidity\":");
    myClient.write((byte *)"59");
    myClient.write((byte *)"}}");
  } else
    Serial.println("Connection Failed");
  }

The response I get on the serial monitor

Serial Init
Send Sync data
Rcvd Sync data
AT

Rcvd Reply: [ERROR]
[ERROR]
Timeout  with ERROR reply
AP association Failed
AT

Rcvd Reply: õAT
õAT
-1

Rcvd Reply: [ERROR: INVALID INPUT]
[ERROR
INVALID
INPUT]
-1

Rcvd Reply: AT
AT
-1

Rcvd Reply: [OK]
[OK]
ATE0

Rcvd Reply: ATE0
ATE0
-1

Rcvd Reply: [OK]
[OK]
AT+XDUM=0

Rcvd Reply: [OK]
[OK]
AT+BDATA=1

Rcvd Reply: [OK]
[OK]
AT+WD

Rcvd Reply: [OK]
[OK]
AT+WM=0

Rcvd Reply: [OK]
[OK]
AT+WPAPSK=EricLeong3,ericChoong91

Rcvd Reply: Computing PSK from SSID and PassPhrase...
Computing
PSK
from
SSID
and
PassPhrase...
-1

Rcvd Reply: [OK]
[OK]
AT+NDHCP=1

Rcvd Reply: [OK]
[OK]
AT+WA=EricLeong3

Rcvd Reply:     IP              SubNet         Gateway   
    IP              SubNet         Gateway   
IP
SubNet
Gateway

Rcvd Reply:  192.168.1.42: 255.255.255.0: 192.168.1.1
 192.168.1.42: 255.255.255.0: 192.168.1.1
192.168.1.42
192.168.1.42
255.255.255.0
255.255.255.0
192.168.1.1
192.168.1.1

Rcvd Reply: [OK]
[OK]
AP association Success
apleleee 
AT+DNSLOOKUP=82,223,244,60,3,5

Rcvd Reply: [ERROR: INVALID INPUT]
Token: [ERROR

May I know what's wrong with the code? Why does it show a token error instead of success? What I am trying to do is posting an HTTP POST request to Carriots, which is a place to do my m2m things. I am still wondering which part I am missing. I referred to few sources such as

http://forum.carriots.com/index.php/topic/61-wireless-gardening-with-arduino-cc3000-wifi-modules/page-2

https://github.com/Wiznet/WizFiShield

3
  • 1
    Welcome to Arduino SE. I'm sorry we can't provide general tutorials here. Is there a specific issue you're struggling with? Jan 24, 2015 at 14:08
  • @PeterR.Bloomfield I had edit the question.hopefully i can get some help here Jan 25, 2015 at 17:26
  • What he meant was asking for a tutorial somewhere is generally not accepted since we strive to be the information, not a collection of links. I edited your question to narrow it down a bit... Jan 28, 2015 at 0:10

1 Answer 1

1

The library provides an example for a server.

Here's what it looks like:

/******************************************************************
 WizFiShield Web Server Test Example

 A simple web server that shows "Hello World" string 

 Circuit:
 WizFi2x0 connected to Arduino via SPI

 RST: pin 2  // Output
 DRDY: pin 3  // Input
 CSB: pin 4  // output
 MOSI: pin 11  // output
 MISO: pin 12  // input
 SCK: pin 13  // out

 Created 27 Sep. 2012
 by James YS Kim  ([email protected], [email protected])

 Modified 27 May. 2013
 by Jinbuhm Kim  ([email protected], [email protected])
*****************************************************************/

// WizFi210 communicates using SPI, so include the SPI library:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SPI.h>
#include <WizFi2x0.h>
#include <WizFiClient.h>
#include <WizFiServer.h>
#include <TimerOne.h>

#define SSID    ""        // SSID of your AP
#define Key     ""  // Key or Passphrase
// Wi-Fi security option (NO_SECURITY, WEP_SECURITY, WPA_SECURITY, WPA2PSK_SECURITY)
//#define Security        WPA_SECURITY

#define MAX_SOCK_NUM    4

unsigned int SrcPort = 80;

WizFi2x0Class myWizFi;
WizFiClient myClient[MAX_SOCK_NUM];//(SIP, ServerPort);
WizFiServer myServer(SrcPort);

boolean Wifi_setup = false;

///////////////////////////////
// 1msec Timer
void Timer1_ISR()
{
  myWizFi.ReplyCheckTimer.CheckIsTimeout();
}
//
//////////////////////////////
void setup() {
  byte retval, i;

  Serial.begin(9600);
  Serial.println("\r\nSerial Init");

  for(i=0; i<MAX_SOCK_NUM; i++)
    myClient[i] =  WizFiClient();

  myWizFi.begin();

  // Timer1 Initialize
  Timer1.initialize(1000); // 1msec
  Timer1.attachInterrupt(Timer1_ISR);

  myWizFi.SendSync();
  myWizFi.ReplyCheckTimer.TimerStart(3000);

  Serial.println("Send Sync data");

  while(1)
  {
    if(myWizFi.CheckSyncReply())
    {
      myWizFi.ReplyCheckTimer.TimerStop();
      Serial.println("Rcvd Sync data");
      break;
    }
    if(myWizFi.ReplyCheckTimer.GetIsTimeout())
    {
      Serial.println("Rcving Sync Timeout!!");
      return;
    }
  }

  ////////////////////////////////////////////////////////////////////////////
  // AP association  
  while(1)
  {
    byte tmpstr[32];

    retval = myWizFi.associate(SSID, Key, Security, true);

    if(retval == 1){
      myWizFi.GetSrcIPAddr(tmpstr);
      Serial.println("WizFi2xo AP Associated");
      Serial.print("MY IPAddress: ");
      Serial.println((char *)tmpstr);
      Wifi_setup = true;
      break;
    }else{
      Serial.println("AP association Failed");
    }
  }

  if(myServer.begin())
    Serial.println("Server Listen OK");
  else
    Serial.println("Server Listen Failed");
}

void loop()
{
  uint8_t retval, i;
  byte rcvdBuf[129];

  memset(rcvdBuf, 0, 129);

  if(Wifi_setup)
  {  
    myWizFi.RcvPacket();
    for(i=0; i<MAX_SOCK_NUM; i++)
    {
      if(myClient[i].available()){    
         retval = myClient[i].read(rcvdBuf);   
         if(retval > 0)
         {
           Serial.print("CID[");
           Serial.print((char)myClient[i].GetCID());
           Serial.print("]");
           Serial.println((char *)rcvdBuf);

           if((rcvdBuf[retval - 1] == 0x0A) && (rcvdBuf[retval - 2] == 0x0D) && (rcvdBuf[retval - 3] == 0x0A) && (rcvdBuf[retval - 4] == 0x0D))
           {
             Serial.print("Receiving Completed");
             myClient[i].write((byte *)"HTTP/1.1 200 OK\r\n");
             myClient[i].write((byte *)"Content-Type: text/html\r\n");
             myClient[i].write((byte *)"\r\n");
             myClient[i].write((byte *)"Hello World !\r\n");

             delay(100);
             myClient[i].disconnect();
           }
         }
       }
    }
  }
}

Using the WizFiServer class, it seems to assign a WizFiClient instance to each HTTP socket connection. It can then call clientName.write( /* Data to be sent to client */ ); and clientName.read(); to get values.

Once it is done with the client, it can free up resources by calling clientName.disconnect();.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.