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   [ 6 posts ]
Author Message
 Post subject: transmission bewteen gateway , wifi and web (opengarden)
PostPosted: Sun Jan 29, 2017 9:58 am 

Joined: Fri Jan 13, 2017 5:23 am
Posts: 9
Hello,

On the gateway, I have the light, the air temperature, the air humidity and the soil humidity sensors and I want to send information to the web (code hereafter)

It works when I plug the gateway with usb but it does not work when I plug the power supply.

Also, I do not understand how the serial port is used both to communicate with wifi and with serial monitor.

Thank you.

Code:
/* 
 *  Wifi Module
 * 
 *  envoie toutes les 10 minutes les info de la gateway vers le web via le wiko
 */

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

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

const char server[] = "xxx";
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[150];

String web_data;

char buffer[20];

unsigned long time;

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

  time= -10000000;



}

void loop()
{

Serial.print('.');

  if ( millis()-time > 1*60000 ){ //Only enter 1 time each 10 minutes


    wificonfig();

    time=millis();
    OpenGarden.initSensors();
    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;

 
    GET[0]=0;
    web_data.toCharArray(GET, 100);


    Serial.println("Data=>"+web_data);

    sendGET();
  }

  if (time>millis()) {
    time=millis();
  }
  delay(10000); // 90 sec

}



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);
 
  snprintf(aux_str, sizeof(aux_str), "join %s\r", wifi_ssid);
  answer = sendCommand(aux_str,"Associated",5000);


  if (answer == 1){

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

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



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);

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

  snprintf(aux_str, sizeof(aux_str), "set c r GET$/%s\r", GET);
  sendCommand(aux_str,"AOK",2000);
 
  // Calls open to launch the configured connection.
  sendCommand("open\r","*CLOS*",2000);

  findResponse(20000);

}


void findResponse(unsigned int timeout){
 
  boolean go_On = true;
  uint16_t counter = 0;
  unsigned long start;

  start=millis();
 
  while ((go_On == 0) && ((millis() - start) < timeout)) { 

    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;
}



Top
 Profile  
 
 Post subject: Re: transmission bewteen gateway , wifi and web (opengarden)
PostPosted: Mon Jan 30, 2017 9:11 am 

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

could you upload to any photo (using a public free hosting web) of your hardware setup, and your power supply? What voltage and current specifications do your power supply have? Are you using Arduino Uno?

Arduino Uno only has one UART and it can be used for comunicating with other UART modules (as WiFi module), for debugging with serial monitor, and besides this to upload the codes in the Arduino Uno microcontroller.

When you are using the UART to communicate with other module, you must be careful with your debug messages, since the module will receive them too.

Regards


Top
 Profile  
 
 Post subject: Re: transmission bewteen gateway , wifi and web (opengarden)
PostPosted: Mon Jan 30, 2017 10:56 am 

Joined: Fri Jan 13, 2017 5:23 am
Posts: 9
Thank you Dev,

This is a url for the hardware: http://bryoflor.com/IMG/jpg/arduino.jpg

arduino board is uno

I debug using usb and I try to make it work with the power supply 12V 2000 mA ordered with the outdoor kit, I do not use both at the same time (can I for debugging ?)


Top
 Profile  
 
 Post subject: Re: transmission bewteen gateway , wifi and web (opengarden)
PostPosted: Tue Jan 31, 2017 8:31 am 

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

Is a very strange problem, since there should be no difference between powering Arduino with USB and power supply. Do you have a multimeter to check 5V in arduino and 3V3 in Wifi module (pin 1) in both cases?

No, you shouldn't use UART por debug messages when you communicate with WiFi module because the module will try to parse these messages and can cause errors.

Regards


Top
 Profile  
 
 Post subject: Re: transmission bewteen gateway , wifi and web (opengarden)
PostPosted: Tue Jan 31, 2017 1:54 pm 

Joined: Fri Jan 13, 2017 5:23 am
Posts: 9
This code works (clean the serial buffer before starting a new connection and sending information to the web):
Code:
/* 
 *  Wifi Module
 * 
 *  envoie toutes les 10 minutes les info de la gateway vers le web via le wifi
 */

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

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

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


char Rep;


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

String web_data;

char buffer[20];

unsigned long time;

void setup()
{
  //Write here you correct baud rate
  Serial.begin(9600);
  time= -10000000;
}

void loop()
{

Serial.print('.');

  if ( millis()-time > 10*60000 ){ //Only enter 1 time each 10 minutes


    while( Serial.available() > 0) { // Read the buffer (answer from GET)
      Rep=Serial.read();     
      Serial.print(Rep);
    }

    wificonfig();

    time=millis();
    OpenGarden.initSensors();
    OpenGarden.sensorPowerON();  //Turns on the sensor power supply
    delay(1000); // 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;

 
    GET[0]=0;
    web_data.toCharArray(GET, 100);


    Serial.println("Data=>"+web_data);

    sendGET();
   
   
  }

  if (time>millis()) {
    time=millis();
  }
  delay(90000); // 90 sec

}



void wificonfig() {

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

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

  // 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

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


  if (answer == 1){

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

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



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

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

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

  snprintf(aux_str, sizeof(aux_str), "set c r GET$/%s\r", GET);
  sendCommand(aux_str,"AOK",2000);
 
  // Calls open to launch the configured connection.
  sendCommand("open\r","*CLOS*",2000);


}


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 answer with time out
  while((answer == 0) && ((millis() - previous) < timeout));   

  return answer;
}



Top
 Profile  
 
 Post subject: Re: transmission bewteen gateway , wifi and web (opengarden)
PostPosted: Wed Feb 01, 2017 8:45 am 

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

thank you for your contribution. Do you still have the problem with the power supply?

Regards


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 6 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.