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