Sample code for BLE ADC Continuous Data Send

ANy Sample code for BLE   ADC Continuous Data Send 

  • I have a working code that will use esp32 ble to transfer data as notification.Im not an expert in coding but the code will work sure.You need to install renesas gatt browser from playstore.Then run the code , scan for ble devices in renesas gatt browser,Then connect to the device ,After connecting you will see some characteristics, among them select notify characteristics, then turn on notification and stay on that window.Now press the ub0 button on the GR lychee board inorder to start recieving the analog values from A0 pin in every 1seconds.Code is posting below

  • int sent_flag=0;
    void ub0_interrupt()
    {
    sent_flag=1;
    }
    void setup()
    {
    Serial2.begin(115200);
    pinMode(PIN_ESP_IO0, OUTPUT);
    pinMode(PIN_ESP_EN, OUTPUT);
    digitalWrite(PIN_ESP_IO0, HIGH);
    digitalWrite(PIN_ESP_EN, LOW);
    delay(10);
    digitalWrite(PIN_ESP_EN, HIGH);
    pinMode(PIN_SW0, INPUT);
    attachInterrupt(4, ub0_interrupt, FALLING);
    delay(6000);
    Serial.begin(9600);
    while(Serial.available()>0)
    {
    Serial.write(Serial2.read());
    }
    setup_ble();
    Serial.println("bluetooth initialised");
    delay(1000);
    }
    void loop()
    {
    int analog_value=0;
    while(1)
    {
    analog_value=analogRead(A0);
    delay(1000);
    if(sent_flag)
    {
    sent_value(analog_value);
    }
    }
    }

    void sent_value(int value)
    {
    int l=len(value);
    Serial2.print("AT+BLEGATTSNTFY=0,1,6,");Serial2.print(l);
    send_with_response("",">",500);
    Serial2.println(value);

    }
    int len(int value)
    {
    if(value!=0)
    {
    int nDigits=floor(log10(abs(value)))+1;
    return nDigits;
    }
    return 0;
    }

    void setup_ble()
    {

    if(send_with_response("AT+BLEINIT=2","OK",1000))
    {
    Serial.println("bleinit");
    }
    if(send_with_response("AT+BLEGATTSSRVCRE","OK",1000))
    {
    Serial.println("gattcre");
    }
    if(send_with_response("AT+BLEGATTSSRVSTART","OK",1000))
    {
    Serial.println("gattstart");
    }
    if(send_with_response("AT+BLEADVSTART","OK",1000))
    {
    Serial.println("advstart");
    }
    }

    int send_with_response(char* data_to_send,char* expected_data,int wait_time)
    {
    char recieved_data[200];
    long cur,prev;
    int x=0;
    Serial2.println(data_to_send);
    cur=millis();
    prev=millis();
    while((cur-prev)<wait_time)
    {
    cur=millis();
    if(Serial2.available()>0)
    {
    recieved_data[x]=Serial2.read();
    Serial.print(recieved_data[x]);
    x++;
    }
    }
    recieved_data[x]='\0';
    Serial.print(recieved_data);
    if(strstr(recieved_data,expected_data)!=NULL)
    {
    return 1;
    }
    return 0;
    }

  • I tried and now it works ! Device gets recognised.
  • When i follow above example given by jihas, i get these in-correct answer from ESP32 module:

    AT<CR><LF>
    Ans: ÿÿs Jun 8 2016 .
    eÿs Jun ÿ8 20ÿ6ÿ0ÿÿÿÿ‘
    Éÿéÿbÿ[Ñ…ŠŠsJmš’½½.ÿÚ
    š01ÿ.Úš02+K;3L...Úš‚º2
    0.[ÿ;3ÿmÿÿ(ÿ69)ÿesõ0J.
    .šé *.•¹ªŠ.[ÿ;32mI (5
    41) es®ÿ.[0;32mI‚’.[0;
    Å0½Á<CR><LF>ready<CR><LF>

    AT+BLEINIT=2
    Ans: ets Jun 8
    2016 eÿÿ Juÿÿ 8 201ÿ
    ÿé0­}éÁÿ.‚‚ºÿÍÿ«ard
    ÿÿ´.½..."5)‚‚..% 0 0
    ;0 pa: ) .ÿ0;32mI (169
    ) esdr1700im=0.[ŠÂ.[ÿ;
    32mI (541) esÉõ.[0;32m
    Idrd..[z™*…<CR><LF>ready<CR><LF>

    AT+BLEGATTSSRVCRE
    Ans:ets Jun 8 2016 eÿÿ Jÿÿ ÿ8ÿ
    ÿÿ16 ÿ‹ÿcÿÁ0ÿ¹ÿ‚ÂÑ.‘•ÿ
    ÿÉÿHhÿ(Ñÿ.ÿ[ÿ‚Šÿÿÿÿ ÿ:
    ‚*P.MJ‚‚Ë.Éõm[.[0ÿ32m
    I ÿ169) esdr(1‚Á}Á0m.
    ‚Š..[ÿ;32mI (541ÿ es–õ
    .[0;32mI Ñ0.[0¥Ñ‰±<CR><LF>ready<CR><LF>


    AT+BLEGATTSSRVSTART
    Ans:ets Jun 8 2016 eÿÿ Jun ÿ
    8ÿ201ÿ ÿ‹ÿ±ÿbÿÿÿÿÿ‚ÿ×r«ÿÿ.ÿÿÿ%£+¬ë.ÿ"ÿJÿ‚‚.º
    ‚’..&¦LL.Wpama.[ÿ;32
    mI (169) es–õ1700im=0.
    [ŠÂ.ÿÿ;32mI (541) es–õ
    .[0;32mIdrd..[z™*…<CR><LF>ready<CR><LF>

    AT+BLEADVSTART
    Ans:ets Jun 8 2016 etÿ ÿÿÿ ÿ8 2016 0Âÿ.ÿÿÙÿ‚,
    l.[…1:®«£+2ÿot..’j‚‚ÿ.ÿm‚‚•’j..’jJ..š.
    [ÿ;ÿ2mIÿÿÿ68ÿ ÿs®‚S..šé *.«¹SS.[ÿ
    ÿ32mI (541) es‚Âÿÿ0;32
    mI’’.[0;‚‚Áå<CR><LF>ready<CR><LF>

    Please! help me to correct these errors.