Publish / post on a user facebook wall using CURL

The purpose is to write something (comment or any kind of action) on the wall of one of your users. I won’t use Symfony but only php with curl because it’s easier. I suppose you already have a working connexion with php-sdk installed.

Add “publish_actions” in your permission list

Here is my source in php for Symfony. I get parameters from parameters.yml and the facebookId from the current user. $msg is the message you want to post on fb wall.

        $config = array(
            'appId' => $this->container->getParameter('api_facebook_id'),
            'secret' => $this->container->getParameter('api_facebook_secret'),
            'fileUpload' => false, // optional
            'allowSignedRequest' => false, // optional, but should be set to false for non-canvas apps
        );
 
        $facebook = new \Facebook($config);
 
        $attachment =  array(
            'access_token' => $facebook->getAccessToken(),
            'message' => $msg
        );
 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$this->user->getFacebookId().'/feed');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //to suppress the curl output
        $result = curl_exec($ch);
        curl_close ($ch);

Display graph in realtime with JQuery / AJAX / PHP

The objective was to display my data stored in MySQL. The Arduino chip is continuously sending data into the DB (cf http://www.chergeek.com/2013/12/send-data-mysql-db-temperature-dht11/)

I use JQPlot so install it and its doc. I also want to show date on my graph.

Continue reading “Display graph in realtime with JQuery / AJAX / PHP” »

Send data to MySQL DB (temperature with DHT11)

I wanted to capture data from the DHT11 and save it remotely into a DB. It can be used to send anything to a GET page. I didn’t put the function to get the temperature but I can if you ask me.

Some parameter need to be changed:
– YOUR_IP => XX.XX.XX.XX
– YOUR_IP_WITH_COMA => XX,XX,XX,XX
– YOUR_MAC_HEXA_WITH_COMA => 0xYY, 0xYY, 0xYY, 0x0E, 0x0C, 0xYY

    #include <Ethernet.h>
    #include <SPI.h>
    #include "DHT.h"
    #define DHTPIN 9
    #define DHTTYPE DHT11
 
    float h = 0;
    float t = 0;
    byte mac[] = {YOUR_MAC_HEXA_WITH_COMA};
    IPAddress serverSQL(YOUR_IP_WITH_COMA);
 
    EthernetClient sql;
    DHT dht(DHTPIN, DHTTYPE);
 
    void setup() {
      Serial.begin(9600);
      Serial.println("Starting...");
      Ethernet.begin(mac);
      dht.begin();
    }
 
    void loop() {
      readTempHum();
      delay(1000);
    }
 
    void get_request(float t, float h)
    {
      Serial.println("Connecting to Client...");      
      if (sql.connect(serverSQL, 80))
      {
        // Make a HTTP request:
        sql.print("GET http://YOUR_IP/get.php?t=");
        sql.print(t);
        sql.print("&h=");
        sql.println(h);
        sql.println("HTTP/1.1");
        sql.println();
        Serial.println("Request sent!");
        sql.stop();
      }
      else {
        Serial.println("Client Connection Failed!");
      }
    }
 
    // Show temp & humidity in the console 
    // & send it to the DB
    void readTempHum() {
      h = dht.readHumidity();
      t = dht.readTemperature();
      if(isnan(t) || isnan(h)) {
        Serial.println( "Reading error!");
      } 
      else {
        Serial.print("Humidity :");
        Serial.print(h);
        Serial.print(" %\t");
        Serial.print("Temperature :");
        Serial.print(t);
        Serial.println(" *C");
        get_request(t,h);
      }
    }

The php part

<!--?php  define("DB_HOST","XXXX"); define("DB_USER","XXXX"); define("DB_PASS","XXXX"); define("DB_NAME","XXXXX"); $GLOBALS['con_db'] = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die(exit()); $GLOBALS['con_db']--->set_charset("utf8");
 
$GLOBALS['con_db']-&gt;query("INSERT INTO historic VALUES(NULL,'".intval($_GET['t'])."','".intval($_GET['h'])."',CURRENT_TIMESTAMP);") or die($GLOBALS['con_db']-&gt;error . __LINE__);
 
$GLOBALS['con_db']-&gt;close();
?&gt;

The MySQL part:

CREATE TABLE IF NOT EXISTS `historic` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `temperature` INT(11) NOT NULL,
  `humidity` INT(11) NOT NULL,
  `date` datetime NOT NULL,
  UNIQUE KEY `id_2` (`id`),
  KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CDN Bootstrap JQuery

Html code to be used straight into the head section, simply…

<link href=”//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css” rel=”stylesheet”>
<script src=”//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js”></script>
<script src=”//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js”></script>
<script src=”//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js”></script>