如何将数据从我的网站发送到我的Arduino Netshield

时间:2019-09-13 18:21:31

标签: jquery html arduino

例如,我想通过网站界面打开或关闭LED,但是我的主要问题是,每当我搜索解决方案时,每个人都通过将NetShield本身作为服务器并从中托管网站来做到这一点,但是我想要将网站托管在我自己的PC上,当我单击按钮时,该操作会将一些数据发送到Arduino,然后我将读取数据并打开LED。

我尝试过使用jQuery发送HTTP POST请求,但是通过仔细检查,防护罩给出了HTTP响应405,在我阅读NetShield似乎不允许修改请求之后,因此我尝试使用HTTP GET,但是当我单击按钮时,没有任何东西真正进入Arduino。

关于网络的所有信息都是正确的,并且每个端口/防火墙规则都已打开

这是网站的HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>AgroSystem</title>
        <link rel="icon" href="/res/IconRaster.png" type="image/png">
        <meta name="keywords" content="HTML, CSS, XML, XHTML, JavaScript, Agro, System, Agriculture, Zemedelie, Mashina, Auto, Network, Arduino, Better, Fast, Automatic, Farm, Farming">
        <meta name="author" content="Danislav Chengeliev">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="lib/jquery/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
        <script src="src/script.js"></script>
    </head>
    <body>
        <div>
            <strong><h1>Лампи</h1></strong>
            <ul>
                <a href="" id="button-lamps-start"><li>Старт</li></a>
                <a href="" id="button-lamps-stop"><li>Стоп</li></a>
            </ul>
        </div>
    </body>
</html>

这是我的脚本:

jQuery(document).ready(function() { 
    $("#button-lamps-start").click(function() {
        console.log("test");
        $.get("http://192.168.5.100:80/", "test");
    });
    $("#button-lamps-stop").click(() => {
        alert(10); //for testing purposes
    });
});

这是Arduino来源:

// TODO: Measure real values from moist sensor in real soil

#include <Wire.h>
#include <Ethernet.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

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

// Constructing objects
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 1);
Adafruit_SSD1306 Display(128, 32, &Wire, -1);
IPAddress localip(192,168,5,100);
IPAddress remoteip(192, 168, 5, 111);
EthernetClient client;

sensors_event_t event;

#define LightSensorLED 22

#define FanInStartBtn 23
#define FanInStopBtn 25

#define FanOutStartBtn 27
#define FanOutStopBtn 29

#define FanInLED 28
#define FanOutLED 36

#define moistSensor A8

#define waterLevel A9
#define chemicalLevelSpray A10
#define chemicalLevel A11

#define rainSensor A12

void setup() {
  //temp for debugging
  Serial.begin(9600);

  // Pinouts
  pinMode(LightSensorLED, OUTPUT);
  pinMode(FanInLED, OUTPUT);
  pinMode(FanOutLED, OUTPUT);

  // Pinins
  pinMode(FanInStartBtn, INPUT);
  pinMode(FanInStopBtn, INPUT);
  pinMode(FanOutStartBtn, INPUT);
  pinMode(FanOutStopBtn, INPUT);
  pinMode(moistSensor, INPUT);
  pinMode(waterLevel, INPUT);

  // Init ethernet
  Ethernet.begin(mac, remoteip);
  Serial.println("beg");
  Ethernet.setLocalIP(localip);
  Serial.println("set ip");
  Serial.println(Ethernet.localIP());
  if (!client.connect(remoteip, 80)) {
    Serial.println("noo");
    do {
      Serial.println("try again");
      client.connect({192,168,5,111}, 80);
    } while (!client.connected());
  }
Serial.println("con");
  // Init displays
  Display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  // Init sensors
  tsl.begin();
  tsl.enableAutoRange(true);
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);
}
#define w(x) client.println(x);
void loop() {
  lightSensor(LightSensorLED, 10);
  FanControls(FanInStartBtn, FanInStopBtn, FanInLED);
  FanControls(FanOutStartBtn, FanOutStopBtn, FanOutLED);
  delay(250);
  if (client.available()) {
    for (int i = 0; i < client.available(); i++) {
      Serial.println(client.read());    // THIS SHOULD DUMP ALL THE BYETS FROM THE NETSHIELD TO SERIAL MONITOR
    }
  }
  //Serial.print((817 - analogRead(moistSensor)) / 4); Serial.println("%"); //not finished
  //Serial.println(analogRead(waterLevel) / 6); //not finished
  //Serial.print(analogRead(chemicalLevelSpray) / 6); Serial.println("%"); //not finished
  //Serial.print(analogRead(chemicalLevel) / 6); Serial.println("%"); //not finished
  //Serial.print(analogRead(rainSensor)); //not finished / not for reading but for turning lamp
}

void FanControls(unsigned const char FanStartPort, unsigned const char FanStopPort, unsigned const char FanLEDPort) {
  if (!digitalRead(FanStartPort)) {
    digitalWrite(FanLEDPort, 0);
  } else if (!digitalRead(FanStopPort)) {
    digitalWrite(FanLEDPort, 1);
  }
}

void lightSensor(unsigned const char portNumber, unsigned const int value) {
  // Retrieves the data from the sensor
  tsl.getEvent(&event);

  // Check if its night and turn on the lamps
  if (event.light <= value) {
    digitalWrite(portNumber, 0);
  } else if (event.light > value) {
    digitalWrite(portNumber, 1);
  }
}

2 个答案:

答案 0 :(得分:0)

网站的位置无关紧要(在Arduino,本地PC上或任何地方),但Arduino仍必须充当服务器来侦听传入的事件。 Arduino现在已设置为客户端(EthernetClient client;),这意味着:将数据发送到服务器(不存在)。

将其设为a server以接收事件。

答案 1 :(得分:0)

如果要将数据从HTML Page(Web服务器)发送到Arduino,有两种不同的解决方案:

  1. 在Arduino端实现一个简单的Web服务器来接受HTTP请求
  2. 使用aRest创建一个简单的API来从HTML端调用

我已经在我的博客上写了一个简单的教程,以介绍如何在Arduino上使用aRest以及类似您的示例:https://www.survivingwithandroid.com/arduino-rest-api-iot/

如果您更喜欢使用Web服务器,可以看一下这个示例,该示例显示了如何构建一个简单的Web服务器来接受HTTP请求https://www.survivingwithandroid.com/connect-android-arduino-using-http-web-server/

希望这可以为您提供帮助。