Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By cesar182
#69296 Greetings ... I am doing a small project which consists of sending a data of type long with the esp-01 to a database mysql, the data of type long I receive it through the serial port from an arduino nano ... Problem I have is that mysql does not record the value that I send, but another one different. If I send any number of 4 digits ... mysql only registers values ​​between 100-300. Attached the esp-01 code made in the arduino IDE
Code: Select all#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
Code: Select all#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
User avatar
By QuickFix
#69301 Is there a reason you're using an Arduino as well as an ESP-01?

It's perfectly possible to connect an HMC5883 directly to the ESP-01 and will help finding the problem much easier. :idea:
I haven't really looked into your code, but it wouldn't surprise me there's a conversion or interpretation error between the Adruino and the ESP.
User avatar
By cesar182
#69302
QuickFix wrote:Is there a reason you're using an Arduino as well as an ESP-01?

It's perfectly possible to connect an HMC5883 directly to the ESP-01 and will help finding the problem much easier. :idea:
I haven't really looked into your code, but it wouldn't surprise me there's a conversion or interpretation error between the Adruino and the ESP.

The truth did not know that it is possible to connect the hmc5883 directly to the esp-01 ... could you tell me how I can use the esp-01's i2c interface to connect the hmc5883 please?
User avatar
By QuickFix
#69303 This is how I connect I2C devices to an ESP-01:ImageThe resistors R1 and R3 on SDA and SCL are highly recommended: if you leave them out, chances are your board won't always start (because GPIO0 might get pulled low unintentionally. :idea: