ESP8266は使い勝手が良く、結構思ったことが簡単に実現できるので気に入ってます。
今回は、測定値を表示するためのI
2CDisplayとWiFiのアドレスやデータの校正情報を個別に保存できるようにSDカードの接続を実験してみました。
I2CもSDカードも実にすんなり使うことが出来るようになりましたが、肝心のBaffaloの無線LanのルーターがDHCPを出してくれないので、ここでとん挫。 Failed status 37って何ですか? reason code とかstatus codeがLogに吐き出されますが、これらのコードの説明が見つからないために中断です。
I
2CとSPIを使い、尚且つToutを使ったADCに、更に温度計と水分計の切り替えの為の1ポートと、目いっぱいの利用で、左のような接続で実験していますが、問題はないようです。
SDカードの読み出しは
myfile = SD.open()
myfile.read()
しかないようなので、一文字ずつバッファに書き出してゆくまどろっこしいコードになりますね。
コメントアウトした部分(未完成)も含めて、備忘録としてアップしておくことにします。
I2CDisplay_update.ino
/*
*
The circuit:
* SD card attached to SPI bus as
follows:
** MOSI - pin 11 on Arduino
Uno/Duemilanove/Diecimila
** MISO - pin 12 on Arduino
Uno/Duemilanove/Diecimila
** CLK - pin 13 on Arduino
Uno/Duemilanove/Diecimila
** CS - depends on your SD card
shield or module.
Pin 4 used here for
consistency with other Arduino examples
*/
#include
extern "C" {
#include
"user_interface.h"
}
#include
#include
#include
#include
Sd2Card card;
const int chipSelect = 2; // IO2 not IO4
ST7032 lcd;
char ssid[30] = "*********************";
char password[30] = "#####################";
char Id[20] = "$$$$$$$$$$$$$$$$$$$";
char host[20] = "?????????????"; // host address ie. 192.168.100.100
File myFile;
float offset = 0.424;
float v = 0.0;
float temp = 0.0;
float vc = 0.00625;
float vref = 1.0;
void setup() {
Serial.begin(115200);
delay(10);
byte mac_addr[6];
WiFi.macAddress(mac_addr);
char buff[20];
Serial.print("Mac address:
");
sprintf(buff,
"%02X:%02X:%02X:%02X:%02X:%02X",
mac_addr[0], mac_addr[1],
mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.println(buff); // Check MAC address
Serial.print("Initializing
SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
Serial.println("");
Serial.println("Started");
// Read ssid, password, Id from
SD card and put 'em to global variables
read_ssid();
read_password();
read_Id();
read_host();
/*
// First connect to WiFi
Serial.print("Connecting to
");
Serial.println(ssid);
WiFi.begin(ssid, password );
//
WiFi.begin("Baffalo-G-4CC0","65rhwri3nnee" );
while (WiFi.status() !=
WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi
connected");
Serial.println("IP address:
");
Serial.println(WiFi.localIP());
*/
// Set up LCD display
lcd.begin(8,2);
lcd.setContrast(25);
delay(40);
}
void loop() {
// put your main code here, to
run repeatedly:
/*
Serial.print("connecting to
");
Serial.println(host);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host,
httpPort)){
Serial.println("connection failed");
return;
}
String url =
"/weather/weather_log.php";
url +=
"?location=" + String(Identifier); // ID to identify location
url +=
"&temperature="+String(t);
url +=
"&humidity=" +String(h);
url +=
"&pressure=" +String(p);
Serial.print("Requesting
URL: ");
Serial.println("GET
"+url);
client.print(String("GET
") + url + " HTTP/1.1\r\n" +
"Host: "
+ host + "\r\n" +
"Connection:
close\r\n\r\n");
delay(50);
// Read all the lines of the
reply from server and print them to Serial
Serial.println("Returned
message from server: ");
while(client.available()){
String line =
client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing
connection");
*/
String s1;
String s2 = "*******";
int temp;
temp = measure_temp();
s1 = String(temp) + " C";
lcd.clear();
lcd.setCursor(2,0);
lcd.print(s1);
lcd.setCursor(0,1);
lcd.print(s2);
Serial.println(" ==== Temp = " + s1 );
delay(1000);
}
int measure_temp(){
uint ADC_Value = 0;
float temp = 0.0;
int i;
for(i = 0;i < 30; i++){
ADC_Value = system_adc_read();
v = float(ADC_Value)/1024.0 *
vref;
temp = temp + (v-offset) / vc;
delay(30);
}
return (int)temp/30;
}
void read_ssid(){
// open the file for reading:
myFile =
SD.open("ssid.txt", FILE_READ);
char *s = ssid;
if (myFile) {
Serial.println("ssid.txt");
// read from the file until
there's nothing else in it:
while (myFile.available()) {
*s++ = myFile.read();
}
*s = '\0';
// close the file:
myFile.close();
Serial.println(ssid );
} else {
// if the file didn't open, print
an error:
Serial.println("error
opening ssid.txt");
}
}
void read_password(){
myFile =
SD.open("password.txt");
if (myFile) {
Serial.println("password.txt");
char *p = password;
// read from the file until
there's nothing else in it:
while (myFile.available()) {
*p++ = myFile.read();
}
*p = '\0';
// close the file:
myFile.close();
Serial.println(password);
} else {
// if the file didn't open,
print an error:
Serial.println("error
opening password.txt");
}
}
void read_Id(){
myFile =
SD.open("Id.txt");
char *p = Id;
if (myFile) {
Serial.println("id.txt");
// read from the file until
there's nothing else in it:
while (myFile.available()) {
*p++ = myFile.read();
}
*p = '\0';
// close the file:
myFile.close();
Serial.println(Id);
} else {I2c
// if the file didn't open,
print an error:
Serial.println("error
opening Id.txt");
}
}
void read_host(){
myFile =
SD.open("host.txt");
char *p = Id;
if (myFile) {
Serial.println("host.txt");
// read from the file until
there's nothing else in it:
while (myFile.available()) {
*p++ = myFile.read();
}
*p = '\0';
// close the file:
myFile.close();
Serial.println(host);
} else {
// if the file didn't open,
print an error:
Serial.println("error
opening host.txt");
}
}