Getting data from an online API stored as Json
hi
i've been developing automation code using arduino uno , ethernet shield. basic code works pretty , computer can control relays. can find code @ end of topic.
i have api key site http://openweathermap.org generates data stored json. link: http://api.openweathermap.org/data/2.5/weather?id=3873544&mode=json&appid=7f347ff86d3239c4ef3dbb894f76ea29. i'd "temp" data, temperature in kelvin, in order use value double variable. so, create code perform calculation (for example, calculate , display temperature in celsius).
how that, please?
#include <spi.h>
#include <ethernet.h>
byte mac[] = {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
ipaddress ip(192,168,1,10);
ipaddress gateway(192,168,1,1);
ipaddress subnet(255, 255, 255, 0);
ethernetserver server(80);
int chave=a2;
int motor=3;
int trava=4;
int luz=5;
int timer=0;
float lerchave=0;
char c = 0;
char command[2] = "\0";
void setup()
{
// put setup code here, run once:
ethernet.begin(mac, ip);
server.begin();
pinmode(chave, input);
pinmode(motor, output);
pinmode(trava, output);
pinmode(luz, output);
}
void loop()
{
ethernetclient client = server.available();
if (client)
{
// http request ends blank line
boolean current_line_is_blank = true;
boolean current_line_is_first = true;
while (client.connected())
{
timer = 0;
if (client.available())
{
char c = client.read();
if (c == '\n' && current_line_is_blank)
{
client.println("http/1.1 200 ok");
client.println("content-type: text/html");
client.println();
client.println("<body background-color:#040300>");
client.println("<h1>bem-vindo!</h1><hr>");
client.println("<form method=get name=form>");
client.println("<button name=b value=1 type=submit style=height:40px width:150px>abre portao</button>");
client.println("<button name=b value=2 type=submit style=height:40px width:150px>fecha portao</button>");
client.println("<br/><p>");
client.println("<button name=b value=3 type=submit style=height:40px width:150px>acende luz </button>");
client.println("<button name=b value=4 type=submit style=height:40px width:150px> apaga luz </button>");
client.println("<br/><p>");
break;
}
if (c == '\n') {
current_line_is_first = false;
current_line_is_blank = true;
}
else if (c != '\r') {
current_line_is_blank = false;
}
if (current_line_is_first && c == '=')
{
(int = 0; < 1; i++)
{
c = client.read();
command = c;
}
if (!strcmp(command, "1"))
{
if (lerchave<1020) //se chave de curso está em na
{
while (lerchave<1020 && timer<=10000) //o motor será acionado e permenecerá até chave de curso ir para nf. tal operação deve ser realizada em até 10 s, caso contrário indicará que o portão está trancado.
{
lerchave = analogread(chave);
digitalwrite(motor,high);
delay(100);
timer=timer+100; //conta o tempo de acionamento motor
if(timer>10000 && lerchave<1020)
{
digitalwrite(motor,low);
delay(120000); // caso chave de curso não foi acionada em 10 s, o que indica que o portão estava trancado, será necessário esperar 2 min para ativar o motor.
}
}
if(timer<=10000) //se em até 10 segundos chave de curso passa para nf, trava é acionada e 2 s após o motor é deligado.
{
digitalwrite(trava,high);
delay(2000);
digitalwrite(motor,low);
}
}
}
else if (!strcmp(command, "2"))
{
lerchave = analogread(chave);
if (lerchave > 1020)
{
digitalwrite(trava,low);
delay(3000);
digitalwrite(motor,high);
delay(1500);
digitalwrite(motor,low);
}
}
if (!strcmp(command, "3"))
{
digitalwrite(luz,high);
}
else if (!strcmp(command, "4"))
{
digitalwrite(luz,low);
}
}
}
}
delay(1000);
client.stop();
}
client.println("</body>");
}
i've been developing automation code using arduino uno , ethernet shield. basic code works pretty , computer can control relays. can find code @ end of topic.
i have api key site http://openweathermap.org generates data stored json. link: http://api.openweathermap.org/data/2.5/weather?id=3873544&mode=json&appid=7f347ff86d3239c4ef3dbb894f76ea29. i'd "temp" data, temperature in kelvin, in order use value double variable. so, create code perform calculation (for example, calculate , display temperature in celsius).
how that, please?
#include <spi.h>
#include <ethernet.h>
byte mac[] = {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
ipaddress ip(192,168,1,10);
ipaddress gateway(192,168,1,1);
ipaddress subnet(255, 255, 255, 0);
ethernetserver server(80);
int chave=a2;
int motor=3;
int trava=4;
int luz=5;
int timer=0;
float lerchave=0;
char c = 0;
char command[2] = "\0";
void setup()
{
// put setup code here, run once:
ethernet.begin(mac, ip);
server.begin();
pinmode(chave, input);
pinmode(motor, output);
pinmode(trava, output);
pinmode(luz, output);
}
void loop()
{
ethernetclient client = server.available();
if (client)
{
// http request ends blank line
boolean current_line_is_blank = true;
boolean current_line_is_first = true;
while (client.connected())
{
timer = 0;
if (client.available())
{
char c = client.read();
if (c == '\n' && current_line_is_blank)
{
client.println("http/1.1 200 ok");
client.println("content-type: text/html");
client.println();
client.println("<body background-color:#040300>");
client.println("<h1>bem-vindo!</h1><hr>");
client.println("<form method=get name=form>");
client.println("<button name=b value=1 type=submit style=height:40px width:150px>abre portao</button>");
client.println("<button name=b value=2 type=submit style=height:40px width:150px>fecha portao</button>");
client.println("<br/><p>");
client.println("<button name=b value=3 type=submit style=height:40px width:150px>acende luz </button>");
client.println("<button name=b value=4 type=submit style=height:40px width:150px> apaga luz </button>");
client.println("<br/><p>");
break;
}
if (c == '\n') {
current_line_is_first = false;
current_line_is_blank = true;
}
else if (c != '\r') {
current_line_is_blank = false;
}
if (current_line_is_first && c == '=')
{
(int = 0; < 1; i++)
{
c = client.read();
command = c;
}
if (!strcmp(command, "1"))
{
if (lerchave<1020) //se chave de curso está em na
{
while (lerchave<1020 && timer<=10000) //o motor será acionado e permenecerá até chave de curso ir para nf. tal operação deve ser realizada em até 10 s, caso contrário indicará que o portão está trancado.
{
lerchave = analogread(chave);
digitalwrite(motor,high);
delay(100);
timer=timer+100; //conta o tempo de acionamento motor
if(timer>10000 && lerchave<1020)
{
digitalwrite(motor,low);
delay(120000); // caso chave de curso não foi acionada em 10 s, o que indica que o portão estava trancado, será necessário esperar 2 min para ativar o motor.
}
}
if(timer<=10000) //se em até 10 segundos chave de curso passa para nf, trava é acionada e 2 s após o motor é deligado.
{
digitalwrite(trava,high);
delay(2000);
digitalwrite(motor,low);
}
}
}
else if (!strcmp(command, "2"))
{
lerchave = analogread(chave);
if (lerchave > 1020)
{
digitalwrite(trava,low);
delay(3000);
digitalwrite(motor,high);
delay(1500);
digitalwrite(motor,low);
}
}
if (!strcmp(command, "3"))
{
digitalwrite(luz,high);
}
else if (!strcmp(command, "4"))
{
digitalwrite(luz,low);
}
}
}
}
delay(1000);
client.stop();
}
client.println("</body>");
}
welcome forum!
its easier read code if put code tags </> around it
this stackexchange thread has options parse json.
its easier read code if put code tags </> around it
code: [select]
like this
this stackexchange thread has options parse json.
Arduino Forum > Topics > Home Automation and Networked Objects > Getting data from an online API stored as Json
arduino
Comments
Post a Comment