Libelium is offering new technical support channels, to contact with de Technical Support team to solve any issue our doubt please contact with your sales agent or visit our contact site



Post a new topicPost a reply Page 1 of 1   [ 2 posts ]
Author Message
 Post subject: Connection between wifi, gateway and node in open garden
PostPosted: Fri Jan 20, 2017 9:45 am 

Joined: Fri Jan 13, 2017 5:23 am
Posts: 9
I tried to transmit sensors information from node to web via gateway and wifi.

On the node, there is a DHT22 Probe for Open Garden and a Soil Moisture Probe for Open Garden.

This code does not work:
Code:
/* 
 *  Wifi Module + node
 * 
 */

#include <OpenGarden.h>
#include <Wire.h>
Payload node1Packet;

//Enter here your data
const char wifi_ssid[] = "xxxxx";
const char wifi_password[] = "xxxxx";

const char server[] = "xxxxx.com";
const char server_port[] = "80";
const String  Get0 = "og/set_sensors.php?data=";
char GET[100];


int8_t wifi_ok;
char response[300];
char response2[100];
int cont;
char aux_str[150];

String web_data;

char buffer[20];

void setup()
{
  //Write here you correct baud rate
  Serial.begin(9600);
  OpenGarden.initRF();
  wificonfig();

}

void loop()
{

  //Receive data from node
  OpenGarden.receiveFromNode();
  delay(1000);
  //Extract the last data received from nodes in a packet
  node1Packet = OpenGarden.getNodeData(node1);
  delay(1000);


  OpenGarden.printNode(node1Packet);  //Serial Print data



  int soilMoisture1 = node1Packet.moisture;
  float airTemperature1 = node1Packet.temperature;
  float airHumidity1 = node1Packet.humidity;
  int luminosity1 = node1Packet.light;
  int battery1 = node1Packet.supplyV;

  if (battery1>0) {

    dtostrf(airTemperature1,2,1,buffer);
    String airTemperature1_wf = String (buffer);
    dtostrf(airHumidity1,2,1,buffer);
    String airHumidity1_wf = String (buffer);

    //web_data=Get0+"0:0:" + soilMoisture +";0:2:" + airHumidity0_wf+";0:3:" +  airTemperature0_wf +";0:4:"+luminosity0;


    web_data=Get0+"1:0:" + soilMoisture1 +";1:2:" + airHumidity1_wf+";1:3:" +  airTemperature1_wf;
    // +";1:4:"+luminosity1+";1:5:"+battery1;


// Data us lost here
//---------------------

    Serial.print('webdata:');
    Serial.println(web_data); [/b]
    web_data.toCharArray(GET, sizeof(web_data));


    delay(1000);

  Serial.print("GET:");
    Serial.println(GET);

//------------------------

    if (wifi_ok==1) {
      sendGET();
    } else {
      wificonfig();
    }
  }
  delay(30000);
}



void wificonfig() {

  sendCommand("exit\r","EXIT",2000);
  delay(2000);

  enterConfig(2000);
  sendCommand("leave\r","DeAuth",2000);
  delay(1000);
  // Sets DHCP and TCP protocol
  sendCommand("set ip dhcp 1\r","AOK",2000);
  delay(1000);   
  sendCommand("set ip protocol 18\r","AOK",2000);
  delay(1000); 

  // Configures the way to join the network AP, sets the encryption of the
  // network and joins it
  sendCommand("set wlan join 1\r","AOK",2000); //The auto-join feature is disabled
  delay(1000);

  snprintf(aux_str, sizeof(aux_str), "set wlan phrase %s\r", wifi_password);
  sendCommand(aux_str,"AOK",2000);
  delay(1000);   
  snprintf(aux_str, sizeof(aux_str), "join %s\r", wifi_ssid);
  wifi_ok = sendCommand(aux_str,"Associated",10000);

}



void sendGET() {

  enterConfig(2000);
  sendCommand("set i h 0\r","AOK",2000);
  delay(1000);
  snprintf(aux_str, sizeof(aux_str), "set d n %s\r", server);
  sendCommand(aux_str,"AOK",2000);

  delay(1000);
  //Configures HTTP connection
  snprintf(aux_str, sizeof(aux_str), "set i r %s\r", server_port);
  sendCommand(aux_str,"AOK",2000);
  delay(1000); 

  sendCommand("set o f 1\r","AOK",2000);
  delay(1000);

  snprintf(aux_str, sizeof(aux_str), "set c r GET$/%s\r", GET);
  sendCommand(aux_str,"AOK",10000);
  delay(1000);

  // Calls open to launch the configured connection.
  sendCommand("open\r","*CLOS*",10000);
  delay(1000);

  findResponse();


}


void findResponse(){
  boolean go_On = true;
  uint16_t counter = 0;

  while (go_On){

    if (response[counter] == '\r'){

      if (response[counter+1] == '\n'){

        if (response[counter+2] == '\r'){

          if (response[counter+3] == '\n'){

            go_On = false;
          }

        }   

      }      

    }
    counter++;
  }
  counter = counter + 3;

  for (int i=0;response[i+counter]!='*'; i++){
    response2[i] = response[i+counter];
    delay(100);


  }

  snprintf(aux_str, sizeof(aux_str), "GET response:\"%s\"", response2);
  Serial.println(aux_str);


}


int8_t sendCommand(const char* Command, const char* expected_answer, unsigned int timeout){

  uint8_t x=0,  answer=0;
  unsigned long previous;

  memset(response, 0, 300);    // Initialize the string

  delay(100);

  while( Serial.available() > 0) Serial.read();    // Clean the input buffer

  Serial.println(Command);    // Send Command

    x = 0;
  previous = millis();

  // this loop waits for the answer
  do{
    if(Serial.available() != 0){   
      // if there are data in the UART input buffer, reads it and checks for the asnwer
      response[x] = Serial.read();
      x++;
      // check if the desired answer  is in the response of the module
      if (strstr(response, expected_answer) != NULL)   
      {
        answer = 1;
      }
    }
  }
  // Waits for the asnwer with time out
  while((answer == 0) && ((millis() - previous) < timeout));   

  return answer;
}


int8_t enterConfig(unsigned int timeout){

  uint8_t x=0,  answer=0;
  unsigned long previous;

  memset(response, 0, 300);    // Initialize the string

  delay(100);

  while( Serial.available() > 0) Serial.read();    // Clean the input buffer

  Serial.print("$$$");    // Send Command


  x = 0;
  previous = millis();

  // this loop waits for the answer
  do{
    if(Serial.available() != 0){   
      // if there are data in the UART input buffer, reads it and checks for the asnwer
      response[x] = Serial.read();
      x++;
      // check if the desired answer  is in the response of the module
      if (strstr(response, "CMD") != NULL)   
      {
        answer = 1;
      }
    }
  }
  // Waits for the asnwer with time out
  while((answer == 0) && ((millis() - previous) < timeout));   

  return answer;
}




The code comes from the "open garden/wifi/setSensors Example
mixed with the Wifi Module for Arduino and Raspberry Pi "Roving RN-XVee" Tutorial code.

It seems there are conflicts in using the serial port between the usb cable the wifi. I cannot buikd the string/char array (in bold above)

Wifi only works (I do not read the sensors on the node), sensors only works (I do not use the wifi)

Also, I have tested the DHT22 Probe for Open Garden and Soil Moisture Probe on the gateway and if works end to end:

Code:
/* 
 *  Wifi Module
 * 
 */

#include <OpenGarden.h>
#include <Wire.h>

//Enter here your data
const char wifi_ssid[] = "xxxxxxx";
const char wifi_password[] = "xxxxxxxx";

const char server[] = "xxxxxxx.com";
const char server_port[] = "80";
const String  Get0 = "og/set_sensors.php?data=";
char GET[100];


int8_t answer;
char response[300];
char response2[100];
int cont;
char aux_str[100];

String web_data;

char buffer[20];

void setup()
{
  //Write here you correct baud rate
  Serial.begin(9600);

  OpenGarden.initSensors();    //Initialize sensors power
  wificonfig();
 
}

void loop()
{
 

  OpenGarden.sensorPowerON();  //Turns on the sensor power supply
  delay(2000); // Time for initializing the sensor
  float airTemperature0 = OpenGarden.readAirTemperature();
  Serial.println(airTemperature0);
  float airHumidity0 = OpenGarden.readAirHumidity();
  Serial.println(airHumidity0);
  int luminosity0 = OpenGarden.readLuminosity();  //Read the sensor 
 Serial.println(luminosity0);

  int soilMoisture = OpenGarden.readSoilMoisture(); //Read the sensor
  Serial.println(soilMoisture);
 
 
 
  OpenGarden.sensorPowerOFF();  //Turns off the sensor power supply

 
    dtostrf(airTemperature0,2,1,buffer);
    String airTemperature0_wf = String (buffer);
    dtostrf(airHumidity0,2,1,buffer);
    String airHumidity0_wf = String (buffer);
 
 
  web_data=Get0+"0:0:" + soilMoisture +";0:2:" + airHumidity0_wf+";0:3:" +  airTemperature0_wf +";0:4:"+luminosity0;
 

  web_data.toCharArray(GET, 50);
 
 
  Serial.println("Data=>"+web_data);


  sendGET();
  delay(15000);
}



void wificonfig() {
 
  Serial.println(F("Setting Wifi parameters"));
  sendCommand("exit\r","EXIT",2000);
  delay(2000);
 
  enterConfig(2000);
  sendCommand("leave\r","DeAuth",2000);
  delay(1000);
  // Sets DHCP and TCP protocol
  sendCommand("set ip dhcp 1\r","AOK",2000);
  delay(1000);   
  sendCommand("set ip protocol 18\r","AOK",2000);
  delay(1000); 

  // Configures the way to join the network AP, sets the encryption of the
  // network and joins it
  sendCommand("set wlan join 1\r","AOK",2000); //The auto-join feature is disabled
  delay(1000);

  snprintf(aux_str, sizeof(aux_str), "set wlan phrase %s\r", wifi_password);
  sendCommand(aux_str,"AOK",2000);
  delay(1000);   
  snprintf(aux_str, sizeof(aux_str), "join %s\r", wifi_ssid);
  answer = sendCommand(aux_str,"Associated",10000);


  if (answer == 1){

    snprintf(aux_str, sizeof(aux_str), "Connected to \"%s\"", wifi_ssid);
    Serial.println(aux_str);
    delay(5000);
  }

  else {
    snprintf(aux_str, sizeof(aux_str), "Error connecting to: \"%s\"", wifi_ssid);
    Serial.println(aux_str);
    delay(1000);
  } 
}



void sendGET() {
 
  enterConfig(2000);
  sendCommand("set i h 0\r","AOK",2000);
  delay(1000);
  snprintf(aux_str, sizeof(aux_str), "set d n %s\r", server);
  sendCommand(aux_str,"AOK",2000);

  delay(1000);
  //Configures HTTP connection
  snprintf(aux_str, sizeof(aux_str), "set i r %s\r", server_port);
  sendCommand(aux_str,"AOK",2000);
  delay(1000); 

  sendCommand("set o f 1\r","AOK",2000);
  delay(1000);

  snprintf(aux_str, sizeof(aux_str), "set c r GET$/%s\r", GET);
  sendCommand(aux_str,"AOK",10000);
  delay(1000);

  // Calls open to launch the configured connection.
  sendCommand("open\r","*CLOS*",10000);
  delay(1000);

  findResponse();

}


void findResponse(){
  boolean go_On = true;
  uint16_t counter = 0;

  while (go_On){

    if (response[counter] == '\r'){

      if (response[counter+1] == '\n'){

        if (response[counter+2] == '\r'){

          if (response[counter+3] == '\n'){

            go_On = false;
          }

        }   

      }      

    }
    counter++;
  }
  counter = counter + 3;

  for (int i=0;response[i+counter]!='*'; i++){
    response2[i] = response[i+counter];
    delay(100);


  }

  snprintf(aux_str, sizeof(aux_str), "GET response:\"%s\"", response2);
  Serial.println(aux_str);


}


int8_t sendCommand(const char* Command, const char* expected_answer, unsigned int timeout){

  uint8_t x=0,  answer=0;
  unsigned long previous;

  memset(response, 0, 300);    // Initialize the string

  delay(100);

  while( Serial.available() > 0) Serial.read();    // Clean the input buffer

  Serial.println(Command);    // Send Command

    x = 0;
  previous = millis();

  // this loop waits for the answer
  do{
    if(Serial.available() != 0){   
      // if there are data in the UART input buffer, reads it and checks for the asnwer
      response[x] = Serial.read();
      x++;
      // check if the desired answer  is in the response of the module
      if (strstr(response, expected_answer) != NULL)   
      {
        answer = 1;
      }
    }
  }
  // Waits for the asnwer with time out
  while((answer == 0) && ((millis() - previous) < timeout));   

  return answer;
}


int8_t enterConfig(unsigned int timeout){

  uint8_t x=0,  answer=0;
  unsigned long previous;

  memset(response, 0, 300);    // Initialize the string

  delay(100);

  while( Serial.available() > 0) Serial.read();    // Clean the input buffer

  Serial.print("$$$");    // Send Command


  x = 0;
  previous = millis();

  // this loop waits for the answer
  do{
    if(Serial.available() != 0){   
      // if there are data in the UART input buffer, reads it and checks for the asnwer
      response[x] = Serial.read();
      x++;
      // check if the desired answer  is in the response of the module
      if (strstr(response, "CMD") != NULL)   
      {
        answer = 1;
      }
    }
  }
  // Waits for the asnwer with time out
  while((answer == 0) && ((millis() - previous) < timeout));   

  return answer;
}
       



Thank you for your help


Top
 Profile  
 
 Post subject: Re: Connection between wifi, gateway and node in open garden
PostPosted: Mon Jan 23, 2017 8:44 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hello bryoflor,

please change your line "Serial.print('webdata:');" for "Serial.print("webdata:");" and try again.

Regards


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 2 posts ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Libelium theme based on 610nm Style by Daniel St. Jules of http://www.gamexe.net


© Libelium Comunicaciones Distribuidas S.L. | Terms of use.