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: ArduinoUno+MySignals development platform+esp8266+mysqldb
PostPosted: Thu May 10, 2018 7:46 pm 

Joined: Mon Apr 16, 2018 2:46 pm
Posts: 2
Hello, i'm having problems with my code, can anyone give me a hand?
HW used : ArduinoUno, MySygnals development patform v2.0 with esp8266 module.
I'm trying to connect to mysql database and i succeeded but depends on how i make the http Post request:
- this is the example that works:
char* post = "POST /ardu.php HTTP/1.1\r\nHost: 192.168.0.2\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 20\r\n\r\ng=100&b=10&p=30&t=25\r\n";

The point is that i want to put my sensor values in the post request like this and it's not working(it's like there's nothing to send):
String data="gsr="+String(gsr)+"&bodyPos="+String(bp)+"&bpm="+String(bpm);
String postString="POST /ardu.php HTTP/1.1\r\nHost: 192.168.0.2\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: "+String(data.length())+"\r\n\r\n"+data+"\r\n";
char* post = new char[postString.length()+1];
strcpy(post,postString.c_str());

here's the code :
#include <MySignals.h>
#include "Wire.h"
#include "SPI.h"

unsigned long currentMillis;

const unsigned long period = 10000;


void setup() {
Serial.begin(115200);

MySignals.begin();

MySignals.initBodyPosition();

//Enable WiFi ESP8266 Power -> bit1:1
bitSet(MySignals.expanderState, EXP_ESP8266_POWER);
MySignals.expanderWrite(MySignals.expanderState);

MySignals.initSensorUART();

MySignals.enableSensorUART(WIFI_ESP8266);
delay(1000);

// Controllo se il modulo e funzionante
int8_t answer = sendATcommand("AT", "OK", 6000);
if (answer == 0) {

MySignals.println("Error");
// Aspetto la risposta
while (answer == 0) {

// Mando comando AT fino ricevo esito positivo
answer = sendATcommand("AT", "OK", 6000);
}
} else if (answer == 1) {

MySignals.println("Wifi ModuleOK !");
//******Reset del modulo ******
if (sendATcommand("AT+RST", "OK", 2000)) {
MySignals.println("restart module ok");
} else {
MySignals.println("Errore restarting module");
}

//******Connect to home network ******
if (sendATcommand("AT+CWJAP=\"*****************\",\"*************\"", "OK", 20000)) {
MySignals.println("Connected!");

} else {
MySignals.println("Connection failled");

}

//******Set Module as Client ******
if (sendATcommand("AT+CWMODE=1", "OK", 2000)) {
MySignals.println("Client Mode OK");
} else {
MySignals.println("Cliente Mode - ERROR");
}

}
delay(1000);
//****************************Set up OK *************************
}

void loop(){
int conductance = MySignals.getGSR(CONDUCTANCE); ///****************************************DATA to use 1 *********** FLOAT
delay(1000);

int position = MySignals.getBodyPosition(); /// ****************************************Data to use 2 *********** INT
delay(1000);

MySignals.getAcceleration();
char x_acc_string[10];
dtostrf (MySignals.x_data, 2, 2, x_acc_string);
float alt = atof (x_acc_string);
delay(1000);

int heartbeat = getBpm(); //*************************************************************DATA to use 3***********
delay (1000);
httpBase(conductance,position,heartbeat);
delay(10000);
}

void httpBase(int gsr,int bp,int bpm) {

String data="gsr="+String(gsr)+"&bodyPos="+String(bp)+"&bpm="+String(bpm);
String postString="POST /ardu.php HTTP/1.1\r\nHost: 192.168.0.2\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: "+String(data.length())+"\r\n\r\n"+data+"\r\n";
char* post = new char[postString.length()+1];
strcpy(post,postString.c_str());

String inf = "AT+CIPSEND="+String(strlen(post));
char *info = new char[inf.length() + 1];
strcpy(info, inf.c_str());

delay(100);
sendATcommand("AT+CIPMUX=0", "OK", 1000);
sendATcommand("AT+CIPSTART=\"TCP\",\"192.168.0.2\",80", "OK", 2000);

sendATcommand(info, "OK", 1000);
sendATcommand(post, "OK", 1000);
sendATcommand("AT+CIPCLOSE", "OK", 1000);

delay(100);
}



int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout) {

uint8_t x = 0, answer = 0;
char response[500];
unsigned long previous;
memset(response, '\0', sizeof(response)); // Initialize the string
delay(100);
while ( Serial.available() > 0) Serial.read(); // Clean the input buffer
delay(1000);
Serial.println(ATcommand); // Scrivi il commando AT
x = 0;
previous = millis();
// In Questo Loop aspettiamo la risposta
do {
if (Serial.available() != 0) {
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer1) != NULL) {
answer = 1;
MySignals.println(response);
}
}
// TimeOut per la risposta
}
while ((answer == 0) && ((millis() - previous) < timeout));
return answer;
}


int getBpm() {
//MySignals.println("Conatore bpm iniziato..");
currentMillis = millis();
int flag = 0; int contatore = 0;
while (currentMillis + period >= millis()) {
uint16_t ecg = (uint16_t)MySignals.getECG(DATA);
ecg = map(ecg, 0, 1023, 1023, 0);
if (ecg >= 700 && flag == 0) {
flag = 1;
} else if (flag == 1 && ecg <= 700) {
contatore += 1;
flag = 0;
}
delay(10); // oppure 6
}
//MySignals.println("Bpm acquisito");
return contatore * 6;
}


Top
 Profile  
 
 Post subject: Re: ArduinoUno+MySignals development platform+esp8266+mysqld
PostPosted: Fri May 11, 2018 11:39 am 

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

You should use snprintf or sprintf to format a string. Here you can find a example
http://www.libelium.com/development/was ... d-strings/

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.