How to write in a database with wireless communication and with photon

Hello,

To validate my year at school I need to make a automatic watering and write the data of all the sensors in a database with “particle photon”

And I have a problem I don’t know how how to write in a database…

Currently I have a lightsensor and a computer with WAMP Server and I can access to the database with an other computer on the same wireless connection.

I have already succeeded to show the data of the lightsensor in the particle dashboard, but how I can write this data on my own data base with wireless communication ?

Best regards.

1 Like

Should be simple enough if you know some php/c, uses httpclient lib, you can also use a webhook instead if you prefer.

String path = String::format("/incoming.php?v=1&id=%s&temp=%0.2f&hum=%0.2f&dp=%0.2f&hi=%0.2f&A0=%u&A1=%u&A2=%u&A3=%u&D0=%u&D1=%u&D2=%u&D3=%u&RSSI=%i", id, temp, hum, dp, hi, analogSensors[0], analogSensors[1], analogSensors[2], analogSensors[3], digitalSensors[0], digitalSensors[1], digitalSensors[2], digitalSensors[3], WiFi.RSSI());

    request.hostname = "myserver.com";
    request.port = 80;
    request.path = path;
    http.get(request, response, headers);

PHP

foreach ( $_GET as $key => $value )
{
    if ($key == "v" || $key == "id")
        continue;

//insert into DB ($key, $val)

}

Just a thought - a spreadsheet can be considered a database of sorts. With particle.publish, IFTTT, and Google drive it’s pretty trivial to log lots of data for visualization and interpretation.

Please disregard if you’re required to use a formal database.

Best of luck!

I need to write directly in the database :confused:

My teacher say I don’t need a librairies to write in the database, I’m a little bit confused

you can see my code (write in build.particle.io)

int luminosite = A0; //Nome le pin A0 luminosite
int vLum = 0; //Initialise la valeur de vLum à 0
int Dw = A5; //Nome le pin A5 Dw
    
void setup() {
    
    pinMode(Dw, OUTPUT); //Initialise le pin Dw (A5) en sortie
    digitalWrite(Dw, HIGH); //Initialise le pin Dw au niveau haut
    Serial.begin(9600); //Initialise la connexion série à 9600 bauds
}

void loop() {
    
 vLum = analogRead(luminosite); //Stock dans vLum la valeur analogique lu sur le pin luminosite
 Serial.println(vLum); //Ecris sur le port série la valeur de vLum
 Spark.publish("Luminosité", String(vLum)); //Publie les valeurs de vLum sur la dashboard (https://dashboard.particle.io)
 delay(5000); //Délai d'attente de 5 seconde entre chaque relever et 1 seconde entre chaque affichage
}

You are going to need something in between the data coming off of the Photon and the database. There are a couple options that you have here. (You can’t really just write code on the Photon that interacts directly with the database.)

  1. Have the particle photon communicate directly with the server by sending data to the server that is then stored in the database. (In my opinion, the most modular and therefore the best option.)
    For this reference the http code MORA’s post above. Also, you will need a basic understanding of POST, GET, PUT, etc. in PHP:
    Here are links for that:
    https://www.youtube.com/watch?v=CEl3bhCBjtI

To elaborate further on this option, you need to be able to access your WAMP server from the web.
So, you need to open the ports on your firewall on port 80 if running locally, or you could also push your WAMP stuff to another service that hosts your application publicly. Once you have that set, you should be able to go to your website such as www.mysite.com (or perhaps just an ip address if there is no domain). If you do not want to do this portion, you can probably find the local ip address of the computer running the server and use that instead!

Then on the code behind mysite.com you need to code a page such as photon.php that can handle a request from the photon.

So when you POST data to photon.php at www.mysite.php/photon.php there will be php code to take the data coming off of the sensor and then store it in the database via a mysql insertion through php.

This is a lot of different abstract concepts in one place, so if you have any questions just reach out.

  1. Have code on the server that reaches out to the Photon by retrieving the data you have published using the particle.
    Something like this:
    phpSpark - interacting with the Spark Cloud (spark.io) using PHP

If you use the webhook system, then you dont need a library correct.
https://docs.particle.io/guide/tools-and-features/webhooks/

In this system you make a regular publish and the particle cloud makes the http call for you, you still need to write the PHP to handle it, and as @stepintostem noted the server needs to be reachable.

Just use www.Ubidots.com

They have a Ubidots library in the online IDE.

It’s pretty simple to send your data to Ubidots to database + graph out the data over time.

It’s the easiest platform I have come across to setup and use, and its FREE to start with.

1 Like

We all do it sometimes! In my opinion, an API model seems like it may work best for this.