简介
最近一段时间学习Arduino和Processing,经常要给文章里添加它们的代码,可是之前一直使用的显示代码插件不能识别这种代码,所以显示出来的效果很不好。于是到今天,我添加了该插件:Crayon Syntax Highlighter。

支持的语言
Crayon Syntax Highlighter 支持53种编程语言:
| ID | Name | Version | File Extensions | Aliases |
| abap | ABAP | 1.8.4 | ||
| as | ActionScript | 1.8.0 | swf, fla | flash, swf, fla |
| ada | ADA | 0.0.2 | ||
| amigados | AmigaDOS | 1.0 | ||
| apache | Apache | 1.8.3 | ||
| applescript | AppleScript | 1.9.13 | scpt, applescript | |
| arduino | Arduino | 1.0.0 | ||
| asp | ASP | 1.0.0 | ||
| asm | Assembly (x86) | 1.9.1 | x86 | |
| autoit | AutoIt | 1.8.2 | ||
| c | C | 1.8.1 | ||
| c# | C# | 1.8.1 | cs | cs, csharp |
| c++ | C++ | 1.8.1 | h, hh, hpp, hxx, h++, cc, cpp, cxx, c++ | cpp |
| coffee | CoffeeScript | 1.0 | ||
| css | CSS | 1.9.0 | ||
| default | Default | 1.8.1 | ||
| delphi | Delphi/Pascal | 1.0.0 | pas | pascal |
| diff | diff | 1.10.1 | ||
| erlang | Erlang | 1.14 | ||
| go | Go | 1.12 | ||
| haskell | Haskell | 1.9.8 | ||
| ilogic | Inventor iLogic | 1.7.30 | logic, inventor, inv, ilog | |
| java | Java | 1.7.6 | java, class, jar | |
| js | JavaScript | 1.8 | javascript | |
| less | LESS | 1.0 | ||
| lisp | Lisp | 1.0 | ||
| lua | Lua | 1.8.1 | ||
| matlab | MATLAB | 1.0.0 | ||
| reg | Microsoft Registry | 5 | ||
| miva | MIVA Script | 1.11 | mv, mvc, mvt | |
| monkey | Monkey | 1.0 | ||
| batch | MS DOS | 1.1 | ||
| mysql | MySQL | 1.0.0 | ||
| objc | Objective-C | 1.8.1 | m, mm | obj-c |
| plsql | Oracle PL/SQL | 0.1 | pls, ora | |
| perl | Perl | 1.9.2 | pl | pl |
| pgsql | PgSQL | 1.8.0 | sql, mysql | |
| php | PHP | 1.9.4 | ||
| ps | PowerShell | 1.8.2 | powershell | |
| python | Python | 1.1 | py, pyw, pyc, pyo, pyd | py |
| r | R | 1.0.0 | ||
| ruby | Ruby | 1.7.24 | rb, rbx, rhtml | rb |
| sass | Sass | 1.0 | ||
| scala | Scala | 2.10.0 | ||
| scheme | Scheme | 1.8.4 | ||
| sh | Shell | 1.8.1 | shell, unix, bash | |
| tex | TeX | 1.9.8 | ||
| tsql | Transact-SQL | 1.8.0 | ||
| vim | Vim | 1.0 | ||
| vb | Visual Basic | 1.0 | vbs | vbs |
| vbnet | Visual Basic .NET | 1.0 | ||
| xhtml | XHTML | 1.8.0 | xml, html | |
| yaml | YAML | 1.8.1 |
使用方法
不再像之前的那样需要手动输入代码段,然后将代码放到里面。在WordPress自带的编辑器中会出现一个按钮:
![]()
点开之后会跳出插入对话框,里面有很详细,很直观的选项,用起来很简单了。
代码示例
长代码:
/*
Yeelink sensor client example
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <math.h>
int BH1750address = 0x23;
byte buff[2];
// for yeelink api
#define APIKEY "e845dbce4840cc549b7584a4d1e5a12e" // replace your yeelink api key here
#define DEVICEID 3414 // replace your device ID
#define SENSORID 5535 // replace your sensor ID
// assign a MAC address for the ethernet controller.
byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
// initialize the library instance:
EthernetClient client;
char server[] = "api.yeelink.net"; // name address for yeelink API
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 30*1000; // delay between 2 datapoints, 30s
void setup() {
Wire.begin();
// start serial port:
Serial.begin(57600);
// start the Ethernet connection with DHCP:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
for(;;)
;
}
else {
Serial.println("Ethernet configuration OK");
}
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
// read sensor data, replace with your code
int sensorReading = readLightSensor();
//send data to server
sendData(sensorReading);
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void sendData(int thisData) {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.print("POST /v1.0/device/");
client.print(DEVICEID);
client.print("/sensor/");
client.print(SENSORID);
client.print("/datapoints");
client.println(" HTTP/1.1");
client.println("Host: api.yeelink.net");
client.print("Accept: *");
client.print("/");
client.println("*");
client.print("U-ApiKey: ");
client.println(APIKEY);
client.print("Content-Length: ");
// calculate the length of the sensor reading in bytes:
// 8 bytes for {"value":} + number of digits of the data:
int thisLength = 10 + getLength(thisData);
client.println(thisLength);
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Connection: close");
client.println();
// here's the actual content of the PUT request:
client.print("{\"value\":");
client.print(thisData);
client.println("}");
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// note the time that the connection was made or attempted:
lastConnectionTime = millis();
}
// This method calculates the number of digits in the
// sensor reading. Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
// return the number of digits:
return digits;
}
///////////////////////////////////////////////////////////////////////////
// get data from light sensor
// you can replace this code for your sensor
int readLightSensor()
{
uint16_t val=0;
BH1750_Init(BH1750address);
delay(200);
if(2==BH1750_Read(BH1750address))
{
val=((buff[0]<<8)|buff[1])/1.2;
}
Serial.print("Sensor value is: ");
Serial.println((int)val);
return val;
}
int BH1750_Read(int address) //
{
int i=0;
Wire.beginTransmission(address);
Wire.requestFrom(address, 2);
while(Wire.available()) //
{
buff[i] = Wire.read(); // receive one byte
i++;
}
Wire.endTransmission();
return i;
}
void BH1750_Init(int address)
{
Wire.beginTransmission(address);
Wire.write(0x10);//1lx reolution 120ms
Wire.endTransmission();
}
短代码:
int led = 13;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

