[260] | 1 | #include "r2ca.h"
|
---|
[190] | 2 | #include "ESP8266.h"
|
---|
| 3 | #include "Client_ESP8266.h"
|
---|
| 4 | #include "ThingSpeak.h"
|
---|
| 5 |
|
---|
[235] | 6 | #include "../examples_gdef.h"
|
---|
[190] | 7 |
|
---|
[213] | 8 | ESP8266Client wifi_client;
|
---|
[190] | 9 |
|
---|
| 10 | void setup()
|
---|
| 11 | {
|
---|
| 12 | int ret;
|
---|
| 13 |
|
---|
| 14 | Serial.begin(115200);
|
---|
| 15 | Serial.print("Milkcocoa SDK demo");
|
---|
| 16 |
|
---|
| 17 | // Connect to WiFi access point.
|
---|
| 18 | Serial.println(); Serial.println();
|
---|
| 19 | Serial.print("Connecting to ");
|
---|
[213] | 20 | Serial.println(STA_SSID);
|
---|
[190] | 21 |
|
---|
[213] | 22 | ret = WiFi.begin(Serial5, 115200);
|
---|
[190] | 23 |
|
---|
| 24 | if(ret == 1) {
|
---|
| 25 | Serial.print("Cannot communicate with ESP8266.");
|
---|
| 26 | while(1);
|
---|
| 27 | } else if(ret == 2) {
|
---|
| 28 | Serial.println("FW Version mismatch.");
|
---|
| 29 | Serial.print("FW Version:");
|
---|
[213] | 30 | Serial.println(WiFi.getVersion().c_str());
|
---|
[190] | 31 | Serial.print("Supported FW Version:");
|
---|
| 32 | Serial.println(ESP8266_SUPPORT_VERSION);
|
---|
| 33 | while(1);
|
---|
| 34 | } else {
|
---|
| 35 | Serial.print("begin ok\r\n");
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | Serial.print("FW Version:");
|
---|
[213] | 39 | Serial.println(WiFi.getVersion().c_str());
|
---|
[190] | 40 |
|
---|
[213] | 41 | if (WiFi.setOprToStation()) {
|
---|
[190] | 42 | Serial.print("to station ok\r\n");
|
---|
| 43 | } else {
|
---|
| 44 | Serial.print("to station err\r\n");
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[213] | 47 | if (WiFi.joinAP(STA_SSID, STA_PASSWORD)) {
|
---|
[190] | 48 | Serial.print("Join AP success\r\n");
|
---|
| 49 | Serial.print("IP: ");
|
---|
[213] | 50 | Serial.println(WiFi.getLocalIP().c_str());
|
---|
[190] | 51 | } else {
|
---|
| 52 | Serial.print("Join AP failure\r\n");
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[213] | 55 | if (WiFi.stopServer()) {
|
---|
[190] | 56 | Serial.print("Stop server ok\r\n");
|
---|
| 57 | } else {
|
---|
| 58 | Serial.print("Stop server err\r\n");
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[213] | 61 | if (WiFi.disableMUX()) {
|
---|
[190] | 62 | Serial.print("single ok\r\n");
|
---|
| 63 | } else {
|
---|
| 64 | Serial.print("single err\r\n");
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | //Setup
|
---|
[213] | 68 | if (ThingSpeak.begin(wifi_client)) {
|
---|
[190] | 69 | Serial.print("ThingSpeak.begin() ok\r\n");
|
---|
| 70 | } else {
|
---|
| 71 | Serial.print("ThingSpeak.begin() err\r\n");
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | pinMode(13, OUTPUT);
|
---|
| 75 | digitalWrite(13, LOW);
|
---|
| 76 |
|
---|
| 77 | Serial.println("setup end\r\n");
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[235] | 80 | unsigned long myChannelNumber =THINGSPEAK_CHANNELNUMBER ;
|
---|
| 81 | const char * myWriteAPIKey = THINGSPEAK_WRITEAPIKEY ;
|
---|
| 82 |
|
---|
[190] | 83 | int cnt1;
|
---|
| 84 | int cnt2;
|
---|
| 85 |
|
---|
[235] | 86 | #include <math.h>
|
---|
| 87 | int a;
|
---|
| 88 | float temperature;
|
---|
| 89 | int B=3975; //B value of the thermistor
|
---|
| 90 | float resistance;
|
---|
| 91 |
|
---|
[190] | 92 | void loop() {
|
---|
| 93 | int ret;
|
---|
[213] | 94 |
|
---|
[190] | 95 | ThingSpeak.setField(1,cnt1);
|
---|
| 96 | ThingSpeak.setField(2,cnt2);
|
---|
| 97 |
|
---|
| 98 | cnt1 += 1;
|
---|
| 99 | cnt2 += 2;
|
---|
| 100 |
|
---|
| 101 | // Write the fields that you've set all at once.
|
---|
| 102 | ret = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
|
---|
| 103 | Serial.print("Call writeFields() : return code is ");
|
---|
| 104 | Serial.println(ret);
|
---|
| 105 |
|
---|
| 106 | delay(20000);
|
---|
| 107 | }
|
---|