Jump to content









Photo

WizFi Shield with Arduino Mega


  • Please log in to reply
7 replies to this topic

#1 eric3231559

eric3231559

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts

Posted 22 January 2015 - 06:30 AM

Hi there, i would like to send a streams to carriots 
 

I refer to some of the manual for the wizfi Shield.

https://github.com/Wiznet/WizFiShield

 

when the moment i wanna pass the String for JSON to the streams it don't seems working

 

/******************************************************************
 WizFiShield Web Client Test Example
 
 This sketch connect to a website(http://www.google.com) using WizFi Shield
 
 Circuit:
 WizFiShield 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 18 Sep. 2012
 by James YS Kim  (jameskim@wiznet.co.kr, javakys@gmail.com)
 
 Modified 27 May. 2013
 by Jinbuhm Kim  (jbkim@wiznet.co.kr, jinbuhm.kim@gmail.com)
 
*****************************************************************/
 
// 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
 
 
byte APIKEY[] = "be3aa593d7316149b57a1d49f1813f8223542bdfb632e1f770e544d162287dd4"; // Replace with your Carriots apikey
const String DEVICE = "fypSensors@eric3231559.eric3231559";
byte d1[] = "POST /streams HTTP/1.1";
byte d2[] = "Host: api.carriots.com";
byte d3[] = "Accept: application/json";
byte d4[] = "User-Agent: Arduino-Carriots";
byte d5[] = "Content-Type: application/json";
byte d6[] = "carriots.apikey: ";
byte d7[] = "Content-Length: ";
byte d8[] = "Connection: close";
 
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()
{
  uint8_t retval;
  byte rcvdBuf[129];
 
  memset(rcvdBuf, 0, 129);
  
  if(Wifi_setup)
  {  
     myWizFi.RcvPacket();
     
     if(myClient.available()){       
       if(myClient.read(rcvdBuf))
       {
         Serial.print((char *)rcvdBuf);
       }
     }else{
         retval = myClient.connect();
         if(retval == 1)
         {
             String json = "{\"protocol\":\"v2\",\"device\":\""+DEVICE+"\",\"at\":1356390000,\"data\":{\"light\":\"on\"}}";
           Serial.println("Connected! ");
           //myClient.write((byte *)"GET /search?q=WizFi210 HTTP/1.0\r\n\r\n");
            myClient.write(d1);
            myClient.write(d2);
            myClient.write(d3);
            myClient.write(d4);
            myClient.write(d5);
            myClient.write(d6);
            
            myClient.write(APIKEY,80);
            myClient.write(d7);
            int thisLength = json.length();
            myClient.write(thisLength);
            myClient.write(d8);
            myClient.write((byte*)json);   <--- not allow me to cast. I did change the string to byte above but it                                                                    still seems failed.
 
         }else
           Serial.println("Connection Failed");
       }
  }
}
 
 
I know it is troublesome to you guys. But i really need this to work. This is my final year projects. I had only shallow knowledge on Arduino. but so far I need to get this work so that i can transfer the value from the sensors to the carriots. Helps are deeply appreciated.

 



#2 sdeancos

sdeancos

    Master of Puppets

  • Members
  • PipPipPip
  • 105 posts
  • LocationMadrid, Spain.

Posted 22 January 2015 - 09:58 AM

Hi

 

Maybe help this thread: http://forum.carriot...-modules/page-2

 

You could also give us more information about the type of error or where in the code stops working.

 

 

I hope it will be helpful.

 

Kind regards.



#3 eric3231559

eric3231559

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts

Posted 22 January 2015 - 01:20 PM

oh thanks for the info its really helpful. 
i saw that 

  1. Serial.println(F("Connected!"));
  2. client.fastrprintln(F("POST /streams HTTP/1.1"));
  3. client.fastrprintln(F("Host: api.carriots.com"));
  4. client.fastrprintln(F("Accept: application/json"));
  5. client.fastrprintln(F("User-Agent: Arduino-Carriots"));
  6. client.fastrprintln(F("Content-Type: application/json"));
  7. client.fastrprint(F("carriots.apikey: "));
  8. client.fastrprintln(API_KEY);
  9. client.fastrprint(F("Content-Length: "));
  10. client.println(stringLength);
  11. client.fastrprintln(F("Connection: close"));
  12. client.println();

    this is the part he is doing the http request

Can i need some help on how about mine ? 

https://github.com/W...ocument/English

 

in the manual i saw mine don't have the function of fastrprintln. 

instead of it come with 

 

for client 

void write(byte value)

void write(byte *buf)
void write(byte *buf, size_t size)

 

I get this code in my 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
AT+DNSLOOKUP=82,223,244,60,3,5
 
 
Rcvd Reply: [ERROR: INVALID INPUT]
Token: [ERROR
 

 

 

and this is my code.  I am so sorry for trouble you guys. Need help desperately

/******************************************************************
 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  "be3aa593d7316149b57a1d49f1813f8223542bdfb632e1f770e544d162287dd4" // Replace with your Carriots apikey
#define DEVICE  "fypSensors@eric3231559.eric3231559"
 
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()
{
  uint8_t retval;
  byte rcvdBuf[129];
 
  memset(rcvdBuf, 0, 129);
  
  if(Wifi_setup)
  {  
     myWizFi.RcvPacket();
     
     if(myClient.available()){       
       if(myClient.read(rcvdBuf))
       {
         Serial.print((char *)rcvdBuf);
       }
     }else{
         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");
       }
  }
}


#4 eric3231559

eric3231559

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts

Posted 26 January 2015 - 03:12 AM

Hi there, I found out that the way i wrote the client is wrong.

In WizFi2x0 Class, there are 2 type of definition.

 

WizFiClient::WizFiClient(const char* domain, uint16_t port)

WizFiClient::WizFiClient(uint8_t *ip, uint16_t port)

 

the way of using it is 

 

unsigned char  SIP[4]           = {192, 168, 1, 7};

myClient =  WizFiClient(SIP, ServerPort);

 

now i able to show connected in my serial port. but still carriots there won't able to get the data. 

 

I check through hurl, it shows internal error. I open a new post for that.  Hope i can get help asap



#5 eric3231559

eric3231559

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts

Posted 27 January 2015 - 03:15 AM

Further discover on my wizfi210.

 

I changed the client request to 

myClient =  WizFiClient("api.carriots.com", 80);

 

now it seems like getting proper reply

 

Rcvd Reply: IP:82.223.244.60
Token: IP
Token: 
Rcvd Reply: [OK]
Token: [OK]
[OK] receive
GetPeerIPAddr() retval: 82.223.244.60
0056
1050 8
2046 82
82
0050
1050 2
2051 22
3046 223
223
0050
1052 2
2052 24
3046 244
244
0054
1048 6
200 60
DNS Query Success
AT+NCTCP=82.223.244.60,80
 
but still the bad news is why i still can't receive data in my data streams?
 
I believe this is the vital part of http request 
may i know what's wrong with it ? I look through it for many times. still can't find an problem there.
 
String json = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"Air Temperature\":\"58\",\"Humidity\":\"59\"}}";
           byte thisLength = json.length();
           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\r\n");
 
            myClient.write((byte *)"Host: api.carriots.com\r\n");
            myClient.write((byte *)"Accept: application/json\r\n");
            myClient.write((byte *)"User-Agent: Arduino-Carriots\r\n");
            myClient.write((byte *)"Content-Type: application/json\r\n");
            myClient.write((byte *)"carriots.apikey: ");
            myClient.write((byte *)APIKEY);
            myClient.write((byte *)"\r\n");
            myClient.write((byte *)"Content-Length: ");
 
            myClient.write(thisLength);
            myClient.write((byte *)"\r\n");
            myClient.write((byte *)"Connection: close\r\n\r\n");
           
            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 *)"}}\r\n");


#6 eric3231559

eric3231559

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts

Posted 28 January 2015 - 04:19 AM

HI there, I discover the problem of that. I try to get the reply from the client site. It appears that the content length problem. 

 

It shows me 411 Length Required. Actually my http request already sent the content length, May i know what's wrong with the code?



#7 sdeancos

sdeancos

    Master of Puppets

  • Members
  • PipPipPip
  • 105 posts
  • LocationMadrid, Spain.

Posted 29 January 2015 - 12:44 PM

Hi,

 

Maybe your problem is in this line   myClient.write((byte *)"}}\r\n"); because the characters \r\n are not counted in byte thisLength = json.length();

 

 

I hope this help you.

 

 

BR.



#8 eric3231559

eric3231559

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts

Posted 30 January 2015 - 10:24 AM

Hi there,

 

I have been able to solve the problem i facing by edit the http request type

 

 

/******************************************************************
 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    "TM_BroadbandA23D"//"EricLeong3"        // SSID of your AP
#define Key     "61626331323334353637383930"  // Key or Passphrase
// Wi-Fi security option (NO_SECURITY, WEP_SECURITY, WPA_SECURITY, WPA2PSK_SECURITY)
#define Security        WEP_SECURITY
 
 
#define APIKEY  "be3aa593d7316149b57a1d49f1813f8223542bdfb632e1f770e544d162287dd4" // Replace with your Carriots apikey
#define DEVICE  "fypSensors@eric3231559.eric3231559"
 
WizFi2x0Class myWizFi;
WizFiClient myClient;
TimeoutClass ConnectInterval;
const int inPin = 0;
 
char charCelcius[32];
 
boolean Wifi_setup = false;
unsigned char SIP[4] = {82,223,244,60};
///////////////////////////////
// 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();
  delay(500);
  // Socket Creation with Server IP address and Server Port num 
  myClient =  WizFiClient(SIP, 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()
{
  uint8_t retval;
  byte rcvdBuf[129];
  memset(rcvdBuf, 0, 129);
  
 
  
 
  
  if(Wifi_setup)
  {  
     //myWizFi.RcvPacket();
     
     //if(myClient.available()){       
      // if(myClient.read(rcvdBuf))
      // {
         
       //  Serial.print((char *)rcvdBuf);
      // }
    // }else{
         retval = myClient.connect();
         if(retval == 1)
         {          
          
           Serial.println("Connected! ");
 
           
  //------temperature reading -----//
  int value = analogRead(inPin);
  Serial.print(value);
  Serial.print(">");
  float millivolts = (value/1024.0)*5000;
  float celcius = millivolts/10;
  Serial.print(celcius);
  Serial.println("Degree Celcius");           
   dtostrf(celcius,2,2,charCelcius);           
 
myClient.write((byte *)"POST /streams HTTP/1.1\r\n");
myClient.write((byte *)"Host: api.carriots.com\r\n");
myClient.write((byte *)"carriots.apikey: be3aa593d7316149b57a1d49f1813f8223542bdfb632e1f770e544d162287dd4\r\n");
myClient.write((byte *)"Content-Length: 100\r\n");
myClient.write((byte *)"\r\n");
myClient.write((byte *)"{\"protocol\":\"v2\",\"device\":\"fypSensors@eric3231559.eric3231559\",\"at\":\"now\",\"data\":{\"light\":");
myClient.write((byte *)charCelcius);
myClient.write((byte *)"}}\r\n\r\n");
 
 
myClient.write((byte *)"Accept: application/json\r\n");
delay(10000);
         }else
           Serial.println("Connection Failed");
       }
//}
}
 
 
 
I am not sure how actually the http request works, I get the solution by contact the engineer of the wizfi shield. Hope that the codes here can help others.
 
If you guys know how the why this http request works instead of before this let me know. tq in advance





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users