Arduino

Arduino Ethernet with JSON made simple


Working with JSON on Arduino can be a pain if you don’t see a nice example to work with. In this example we are using aJSON for Arduino. You can download it here. Once you’ve downloaded, unzip, and put the aJSON library in your Arduino libraries folder. Then you can copy and paste the Arduino script below and upload it! Make sure you have your Ethernet shield plugged in. In this script, the Arduino submits a GET request to your PHP file on your server. The server can then output any information you need. Anything from MySQL, txt file, whatever, and get it into a JSON format for Arduino to read.

This type of script is awesome for sending GPS cordinates, altitude, speed, xyz, etc, through the internet, and possibly even use a cellular signal (GSM). And get a return from the server. Imagine remote control from your PHP server!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
 
#include <Ethernet.h>
#include <aJSON.h>
#include <String.h>
#include <SPI.h>
 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "YOUR-DOMAIN.COM"; // Change your domain here, or use YAYprogramming.com
IPAddress ip(192,168,1,150);
 
EthernetClient client;
 
String http_response  = "";
int    response_start = 0;
int    response_end   = 0;
 
 
void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
 
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
  Serial.println();
  Serial.println("Connecting...");
  Serial.println();
}
 
 
void loop() {
GetData();      // Send request of varibles to the server, and fetch json, then get strings from json results.
delay(1000);    // give the loop a 1 second pause.
}
 
 
void GetData() { 
  http_response = "";
  response_start = 0;
  response_end = 0;
  char c[] = "";
  char buffer[10];
 
String data= "here is some data here";
String moredata = "even more data here";
 
  String dataInput = "/arduino.php?data="+data+"&moredata="+moredata;              // Upload arduino.php on your server, also change DOMAIN-HERE.COM to your own
  Serial.println(dataInput);
    client.connect(server, 80);
    Serial.println("Fetching Data...");
    Serial.println();
    client.println("GET "+dataInput+" HTTP/1.1");    // Here we send the 'q' field to PHP
    client.println("Host: YOUR-DOMAIN.COM");     // Change your domain here, or use YAYprogramming.com
    client.println("User-Agent: Mozilla/5.0");
    client.println("Connection: close");
    client.println();
    delay(500);
 
 
 for (int i = 0; i < 1000; i++) {     // change 1000 if your query is larger than 1000 characters
       char c = client.read();
    http_response += c;   // We store the response in a string
 }
 
      response_start = http_response.indexOf("<data>")+6; 
      response_end = http_response.indexOf("</data>"); 
      http_response = http_response.substring(response_start,response_end); 
 
      Serial.print("Website response : ");
      Serial.println(http_response);
      Serial.println(); 
 
      client.stop();
 
      char httpJSON[http_response.length()+1];
 
      http_response.toCharArray(httpJSON, http_response.length()+1);
 
  aJsonObject* jsonObject = aJson.parse(httpJSON);
  delay(200);
  aJsonObject* returnvaluejson= aJson.getObjectItem(jsonObject, "returnvalue");    // Get JSON from the returnvalue field.
  aJsonObject* datainfojson= aJson.getObjectItem(jsonObject, "datainfo");          // Get JSON from datainfo field
 
    delay(500);
  String returnvalue= returnvaluejson->valuestring;    // Get the returnvalue into a simple String!
  String datainfo= datainfojson->valuestring;       // Get the datainfo into a simple String!
   delay(500);   
 
   Serial.println();
   Serial.print("JSON returnvalue is :");
   Serial.println(returnvalue);
   Serial.println();
   Serial.print("JSON datainfo is :");
   Serial.println(datainfo);
   Serial.println();
   Serial.println();
   Serial.println();
 
}

Now for the server side PHP part of the script. You can input variables from String’s in the Arduino script. The PHP script will echo a simple JSON format.

arduino.php

1
2
3
4
5
6
7
8
9
<?php
 
$data = $_GET['data'];
$moredata = $_GET['moredata'];
 
echo "<data>{\"returnvalue\": \"got it\", \"datainfo\": \"this is a string from JSON\"}</data>";
 
 
?>

View Comments
There are currently no comments.