Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino 此代码应通过php文件将值上载到数据库_Arduino_Iot_Arduino Uno_Gsm_Gprs - Fatal编程技术网

Arduino 此代码应通过php文件将值上载到数据库

Arduino 此代码应通过php文件将值上载到数据库,arduino,iot,arduino-uno,gsm,gprs,Arduino,Iot,Arduino Uno,Gsm,Gprs,我使用arduino和以太网屏蔽将数据上传到服务器。 最近,我从使用本地数据库改为使用web托管服务(000webhost),但我无法让它工作,Arduino IDE中没有显示错误,但它只是停在“进行插入”的行中 当我把数据库放在本地时,一切都很好 当我直接在浏览器中输入url时 mythesisinacap.000webhostapp.com/writemydata.php?value=0将适当的值插入到数据库中可以正常工作……这意味着服务器中的php文件没有问题 这是我的密码 #includ

我使用arduino和以太网屏蔽将数据上传到服务器。 最近,我从使用本地数据库改为使用web托管服务(000webhost),但我无法让它工作,Arduino IDE中没有显示错误,但它只是停在“进行插入”的行中

当我把数据库放在本地时,一切都很好

当我直接在浏览器中输入url时 mythesisinacap.000webhostapp.com/writemydata.php?value=0将适当的值插入到数据库中可以正常工作……这意味着服务器中的php文件没有问题

这是我的密码

#include <Ethernet.h>

int isparked;

byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

// Enter the IP address for Arduino
// Be careful to use , insetead of . when you enter the address here
IPAddress ip(192, 168, 0, 170);

int vcc = 5; //attach pin 2 to vcc
int trig = 6; // attach pin 3 to Trig
int echo = 7; //attach pin 4 to Echo
int gnd = 8; //attach pin 5 to GND

char server[] = "mythesisinacap.000webhostapp.com";

// Initialize the Ethernet server library
EthernetClient client(80);

void setup() 
{

isparked=0;

// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
}

void loop() {

if (client.connect(server, 80))
      {
        Serial.print("CONNECTED");

        Serial.println();
        Serial.print("MAKING INSERTION");
        Serial.println();
        client.print("GET /writemydata.php?value=");
        client.print(isparked5);

        client.println(" HTTP/1.1");
        client.println("Host: mythesisinacap.000webhostapp.com"); 
        client.println("Connection: close"); 
        client.println();
        client.println(); 
        client.stop();    
      }
      else
      {
        Serial.print("NO CONNECTION");
      }
    }
  }
}
}
Serial.println();
Serial.print("FINNISH LOOPING");
Serial.println();
}
#包括
标记为int;
字节mac[]={
0xDE、0xAD、0xBE、0xEF、0xFE、0xED
};
//输入Arduino的IP地址
//小心使用,不要插入。当你在这里输入地址时
ip地址ip(192168,0170);
int-vcc=5//将针脚2连接到vcc
int trig=6;//将销3连接到触发器上
int-echo=7//将针脚4连接到Echo上
int gnd=8//将针脚5连接到GND
char server[]=“mythesisinacap.000webhostapp.com”;
//初始化以太网服务器库
EthernetClient(80);
无效设置()
{
isparked=0;
//启动以太网连接和服务器:
以太网开始(mac,ip);
}
void循环(){
if(客户端连接(服务器,80))
{
串行打印(“连接”);
Serial.println();
连续打印(“制作插入”);
Serial.println();
print(“GET/writemydata.php?value=”);
客户端打印(标记为5);
client.println(“HTTP/1.1”);
client.println(“主机:mythesisinacap.000webhostapp.com”);
client.println(“连接:关闭”);
client.println();
client.println();
client.stop();
}
其他的
{
串行打印(“无连接”);
}
}
}
}
}
Serial.println();
串行打印(“芬兰循环”);
Serial.println();
}

好的,我终于让它与我的网络托管数据库一起工作了,我使用了github的这个示例,并将其改编为我的案例,现在我必须添加我的传感器逻辑和计算

#包括
#包括
//在下面输入控制器的MAC地址。
//较新的以太网屏蔽在屏蔽上的标签上印有MAC地址
字节mac[]={0xDE,0xDD,0xBE,0xEF,0xFE,0xED};
char服务器[]=“mythesis2017.000webhostapp.com”;//谷歌名称地址(使用DNS)
//设置DHCP分配失败时要使用的静态IP地址
ip地址ip(192168,0177);
//初始化以太网客户端库
//具有服务器的IP地址和端口
//要连接到的端口(HTTP的默认端口为80):
以太网络客户端;
无效设置()
{
}
void循环()
{
//如果有可用的传入字节
//从服务器读取并打印它们:
if(client.available()){
char c=client.read();
连续打印(c);
}
//如果服务器已断开连接,请停止客户端:
如果(!client.connected()){
Serial.println();
Serial.println(“断开”);
client.stop();
延迟(10 000);
插入();
}
}
空白插入()
{
//打开串行通信并等待端口打开:
Serial.begin(9600);
而(!串行){
;//等待串行端口连接。仅本机USB端口需要
}
//启动以太网连接:
if(以太网开始(mac)==0){
Serial.println(“未能使用DHCP配置以太网”);
//尝试使用IP地址而不是DHCP连接:
以太网开始(mac,ip);
}
//给以太网屏蔽一秒钟进行初始化:
延迟(1000);
Serial.println(“连接…”);
//如果您获得连接,请通过串行方式报告:
if(客户端连接(服务器,80))
{
Serial.println(“已连接”);
//发出HTTP请求:
println(“GET/writemydata.php?val=1http/1.1”);
client.println(“主机:mythesis2017.000webhostapp.com”);
client.println(“连接:关闭”);
client.println();
}
其他的
{
//如果您没有连接到服务器:
Serial.println(“连接失败”);
}
}

请阅读并确认。试着自己做。一旦你撞到墙,你可以解释你在挣扎什么,你很可能会得到帮助。但是没有人会根据需要帮你翻译一些代码。你说得对,对不起,mac地址是否需要更改,可能需要使用我的ethernet shield上的那个?即使在我有本地数据库时,如果不更改mac地址,它也可以正常工作。请提供一个示例。没有人愿意浏览95%与实际问题无关的代码。当然,你应该使用你的以太网屏蔽的MAC。小猪,我不知道如何才能提供比我的示例中显示的更完整的代码,请告诉我你需要什么样的信息来帮助我解决问题,我没有问题提供所需的信息,关于MAC的事情:当我有一个本地数据库时,为什么它在不改变MAC地址的情况下工作?
if(client.connect(server,80))
仍然没有意义,因为该函数只返回数字1、-1、-2、-3或-4。因此,您将始终输入if块,仅当函数返回1时才应输入该块。所有其他返回值都是连接错误。我建议您制作一个小函数,它将打印不同的错误消息。
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xDD, 0xBE, 0xEF, 0xFE, 0xED };

char server[] = "mythesis2017.000webhostapp.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup()
{

}

void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();

delay(10000);
insert();
}
}

void insert()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");

// if you get a connection, report back via serial:
if (client.connect(server, 80)) 
{
Serial.println("connected");
// Make a HTTP request:
client.println("GET /writemydata.php?val=1 HTTP/1.1");
client.println("Host: mythesis2017.000webhostapp.com");
client.println("Connection: close");
client.println();
}
else 
{
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}