Arduino
2
Вклад в тег
using System;
using System.IO;
using System.IO.Ports;
using System.Text;
namespace MyProgram
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Opening serial port");
SerialPort sp = new SerialPort("COM3", 115200); //порт и скорость обмена
sp.Open();
WriteLine("Hello!");
while (sp.BytesToRead() <= 0)
{
System.Threading.Thread.Sleep(1000);
}
Console.WriteLine(sp.ReadLine());
sp.Close();
}
}
}
#include<stdlib.h>
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
float data[2];
uint8_t buflen = 8;
const int receive_pin = 2;
String htmlContentType = "text/html";
String jsonContentType = "application/json";
const char html[] PROGMEM = "<!DOCTYPE html>\r\n"
"<html>\r\n"
"<head>\r\n"
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n"
"</head>\r\n"
"<body>\r\n"
"</body>\r\n"
"</html>\r\n";
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void respondPage(EthernetClient client, const prog_char page[])
{
char c;
while((c = pgm_read_byte(page++)))
client.write(c);
}
void responseHeader(EthernetClient client, String contentType) {
client.println("HTTP/1.1 200 OK");
client.print("Content-Type: ");
client.println(contentType);
client.println("Connection: close");
client.println();
}
String readURL(EthernetClient client) {
if(client.find("GET /")) {
String URL = client.readStringUntil(' ');
while(client.available())
client.read();
return URL;
}
return "¬hing";
}
void loop() {
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
while (client.connected()) {
if (client.available()) {
String URL = readURL(client);
Serial.println("'"+URL+"'");
if (URL == "") {
responseHeader(client, htmlContentType);
respondPage(client, html);
}
else if (URL == "data") {
responseHeader(client, jsonContentType);
client.print("[1,1]");
}
break;
}
}
delay(1);
client.stop();
Serial.println("disconnected");
}
}