Cooking Hacks Forum
https://cooking-hacks.com/forum/

ArduinoUno+MySignals development platform+esp8266+mysqldb
https://cooking-hacks.com/forum/viewtopic.php?f=30&t=13055
Page 1 of 1

Author:  lux [ Thu May 10, 2018 7:46 pm ]
Post subject:  ArduinoUno+MySignals development platform+esp8266+mysqldb

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

Author:  libelium-dev [ Fri May 11, 2018 11:39 am ]
Post subject:  Re: ArduinoUno+MySignals development platform+esp8266+mysqld

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

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/