#include <ESP8266WiFi.h>
const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* host = "10.0.0.9";
long valDetector=0,aux;
int resp;
String mensaje="",valor="";
String peticion1="GET /Proyecto/iniciar.php?start=1";
String peticion2="GET /Proyecto/D1.php?id=1";
boolean fin=false;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
httpRequest(peticion1);
while(resp=0){
delay(10000);
httpRequest(peticion1);
}
resp=0;
Serial.print(1);
}
void loop() {
if(Serial.available()>0){
httpRequest(peticion1);
switch(resp){
case 0:
Serial.print(resp);
break;
case 1:
valDetector=Serial.read();
sendData(peticion2,valDetector);
resp=0;
break;
case 2:
Serial.print(2);
resp=0;
break;
case 3:
Serial.print(3);
resp=0;
break;
}
aux=Serial.read();
}
}
void httpRequest(String peticion){
WiFiClient client;
const int httpPort = 80;
if (client.connect(host, httpPort)) {
client.print(peticion);
client.println("HTTP/1.1");
delay(100);
while(client.available()){
char c = client.read();
mensaje += c;
fin=true;
}
if(fin){
int longitud=mensaje.length();
int posicion=mensaje.indexOf("valor=");
for(int i=posicion+6;i<longitud;i++){
if(mensaje[i]==';')
{
i=longitud;
}else{
valor+=mensaje[i];
}
}
resp=valor.toInt();
fin=false;
}
mensaje="";
valor="";
}else{
client.stop();
resp=2;
if(WiFi.status() != WL_CONNECTED){
resp=3;
}
}
}
void sendData(String peticion,long dato){
WiFiClient client;
const int httpPort = 80;
if (client.connect(host, httpPort)) {
client.print(peticion);
client.print("&detector=");
client.print(dato);
client.println("HTTP/1.1");
}
else{
client.stop();
}
}
Este es el codigo del arduino nano
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
unsigned long iniTimer,endTimer,totTime;
int mx,my,mz,valMag,cont=0,magmin=5,magmax=40;
char start,aux;
void setup(){
Serial.begin(115200);
pinMode(3, OUTPUT);
digitalWrite(3,LOW);
if(!mag.begin()){
while(1);
}
digitalWrite(3,HIGH);
delay(500);
digitalWrite(3,LOW);
}
void loop(){
if(Serial.available()>0){
aux=Serial.read();
start=aux;
while(start=='1'){
digitalWrite(3,HIGH);
if(Serial.available()>0){
aux=Serial.read();
cont=0;
start='0';
digitalWrite(3,LOW);
}else{
sensors_event_t event;
mag.getEvent(&event);
mx=event.magnetic.x;
my=event.magnetic.y;
mz=event.magnetic.z;
valMag=sqrt(pow(mx,2)+pow(my,2)+pow(mz,2));
if(valMag<magmin||valMag>magmax){
iniTimer=millis();
while(valMag<magmin||valMag>magmax){
delay(250);
sensors_event_t event;
mag.getEvent(&event);
mx=event.magnetic.x;
my=event.magnetic.y;
mz=event.magnetic.z;
valMag=sqrt(pow(mx,2)+pow(my,2)+pow(mz,2));
}
endTimer=millis();
totTime=abs(endTimer-iniTimer);
Serial.write((long)totTime);
}
}
}
}
}
To save the data in mysql I am using a record of type int of 10 digits.
Someone who can help me with this little problem please