Go Down

Topic: connection arduino to react js through local aREST  (Read 249 times) previous topic - next topic

rajesh1129

Mar 28, 2019, 06:47 am Last Edit: Mar 28, 2019, 12:05 pm by rajesh1129
how can i connect my arduino to react js through a Rest link.Before that I need to create the react application connected with PostgreSQL that SQL data  should collected to my arduino with ESP8266

steve_mcdonald

There is a library that may be of some interest - https://arest.io/arest-arduino-2-0-1

Since you have n't given much detail I can't say if it will help you or not.

rajesh1129

i need to connect to local db and local server not cloud aREST.
how can i communicate with esp to local server.
example:
over view of the project:
1.i need to create the express js(server) application connected with local PostgreSQL.
2.By the arduino and ESP i need to take the data from the local db through the server link.
3.By that data i need to calculate it and sent to the server page again.
I think that now you can understand what i am thinking.

rajesh1129

How to tracking live location using wifi`s or esp`s by using RSSI.
 i need to take the connected devices data from db and make calculation and send to local express server by that server we can make the design and track the live location of the person.
my hole project is in local server.

i haired that aREST mean that local server URL.

and another query, that how can i code in arduino to connect the local server by using server url .

steve_mcdonald

#4
Mar 28, 2019, 08:14 pm Last Edit: Mar 28, 2019, 08:19 pm by steve_mcdonald
Here's how i connect the arduino to my server, update a database and disconnect:
// Connect to the server, open PHP datalogger page, send data, disconnect.
void writeInputsPhp(String outPort, byte value) {
  if (client.connect("192.168.1.100", 7634)) {
    client.print( "GET /datalogger/inputlogger.php?data1=");
    client.print(outPort);
    client.print( "&&data2=");
    client.println(value);
    client.println( "Connection: close" );
    client.println();
    client.println();
    client.stop();
  }
}

At the server there is a php script that writes the data to the database

Also, not sure what your point about cloud aREST is about.  The aREST library is an Arduino library that allows a simple API to operate between an Arduino and a client

rajesh1129

#5
Mar 29, 2019, 03:52 am Last Edit: Mar 29, 2019, 05:30 am by rajesh1129
Thank you for replay.if my arduino want to connect to local server ,i want to use aREST library for that?
which library i want to use to connect to local server.
and
how to do in arduino that after calculated i need to publish in to server.

At the server there is a php script that writes the data to the database

i am using the java script is it ok.

steve_mcdonald

A server allows clients to connect so your arduino acts as a client and the server responds to the arduino.  That's why in the snippet above the arduino makes a client connection to the server to send its data to the server.  You only need the Ethernet library for that.  And you start a client instance like so:
EthernetClient client;                  // Start the client with DHCP

The client then makes the above get call to the server url.  Can you post your code if you would like further help?

rajesh1129

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>

const char* msg;

#define ssid "ssid"
#define password "pass"
#define mqtt_server "broker_ip"

#define client_name "NodeMCU 8CH"

const char* intopic = "home/esp";
const char* outtopic = "home/msg";

const char* outtopic1 = "home/msg1";

const char* outtopic2 = "home/device_number";


WiFiClient espClient;
PubSubClient mqttclient(espClient);

int status = WL_IDLE_STATUS;
//WL_IDLE_STATUS: it is a temporary status assigned when WiFi.begin()
//is called and remains active until the number of attempts expires
//(resulting in WL_CONNECT_FAILED) or a connection is established (resulting in WL_CONNECTED);

void setup(void)
{
  // Start Serial
  Serial.begin(115200);//bits per secound
  Serial.println("Attempting to connect to WPA network...");
  Serial.print("SSID: ");

  // Connect to WiFi
  status = WiFi.begin(ssid, password);
  // WiFi.mode(WIFI_STA);
  if (status != WL_CONNECTED) //WL_CONNECTED: assigned when connected to a WiFi network;
  {
    delay(500);
    Serial.print(".");
  }
//  else {
//    Serial.println("Connected to wifi");
//    Serial.println("\nStarting connection...");
//    Serial.println("IP address: ");
//    Serial.println(WiFi.localIP());
//  }
  mqttclient.setServer(mqtt_server, 1883);
  mqttclient.setCallback(callback);
}

// handle message arrived
void callback(char* topic, byte* payload, unsigned int length) {
  if (strcmp(topic, intopic) == 0) {
    Serial.print("Message arrived [");
    Serial.print(intopic);
    Serial.print("] ");
    for (int i = 0; i < length; i++) {
      Serial.print((char)payload);
    }
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  if (!mqttclient.connected()) {
    reconnect();
  }
  mqttclient.loop();

  delay(100);

  float n = WiFi.scanNetworks();
  Serial.printf("%d network(s) found\n", n);  //Scan start ... 5 network(s) found

  if (n == 0) {
    Serial.println("no wifi`s are connected");
  }
  else {
    int a[20];
    float array1[20];
    float dis[20];
    int k = 0;
    int j = 0;
    for (float i = 0; i < n; i++)
    {
      Serial.printf("%d: %d, Ch:%d (%ddBm)\n", i + 1, WiFi.SSID(i).c_str(), WiFi.channel(i), WiFi.RSSI(i));  //1: Tech_D005107, Ch:6 (-72dBm)
    }

    for (int i = 0; i < n; i++) {
      a[j] = WiFi.RSSI(i);
      j++;
    }

    for (j = 0; j < n; j++) {
      int d = j;
      while ( d > 0 && array1[d - 1] < array1[d]) {
        //swapping
        int  t      = array1[d];
        array1[d]   = array1[d - 1];
        array1[d - 1] = t;
        d--;
      }
    }

    //top 3
    Serial.println("Sorted order:\n");
    for (j = 0; j < 2; j++) {
      Serial.println(array1[j]);//top three rssi values

      //distance calculation......
      float exp = (27.55 - (20 * float(log10(2400))) + float(abs(array1[j]))) / 20.0;
      dis[k] = float(pow(10.0, exp));
      k++;
    }
    for (k = 0; k < 2; k++) {
      Serial.println(dis[k]);
    }

    //we need to send rssi,ssid,tagid at a time...!
    //device-number publish
    const char* Device_number = WiFi.SSID().c_str();
    Serial.println(Device_number);
    mqttclient.publish(outtopic2, Device_number);

    for (float i = 0; i < 2; i++)
    {
      Serial.printf("%d: %s, Ch:%d (%ddBm)\n", i + 1, WiFi.SSID(i).c_str(), WiFi.channel(i), WiFi.RSSI(i));//1: Tech_D005107, Ch:6 (-72dBm)
      //WiFi.SSID(i).c_str()we can use the standard strings.
      Serial.printf("sending messages:");
      msg = WiFi.SSID(i).c_str();
      Serial.printf("sending message");
      Serial.println(msg);
      mqttclient.publish(outtopic, msg);
      //n networks ssid

      float dis[k];
      char msg1[3];
      String str;
      str = String(dis[k]);
      str.toCharArray(msg1, 3);
      mqttclient.publish(outtopic1, msg1);
      //n network distance
    }
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!mqttclient.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (mqttclient.connect(client_name)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      //mqttclient.publish(msgTopic, "MQTT is connected");
      // ... and resubscribe
      mqttclient.subscribe(intopic);
    }
    else {
      Serial.print("failed, rc=");
      Serial.print(mqttclient.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}




i need to post ssid,distance and user esp name to server
it will generate the equation and give the position of the user

is the above code will run successfully?

Go Up