└── matrix-esp8266.ino /matrix-esp8266.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | const char* ssid = ".IDEAL"; 8 | const char* wifiPassword = ""; 9 | 10 | const char* user = "esp8266-lcd-display"; 11 | const char* matrixPassword = "esp8266-lcd-display"; 12 | const char* roomId = "!ZUEZXBBVjWJjQeXgbY:matrix.org"; 13 | 14 | #define SERIAL_RX 12 15 | #define SERIAL_TX 13 16 | 17 | SoftwareSerial serial2(SERIAL_RX, SERIAL_TX); 18 | 19 | HTTPClient http; 20 | String accessToken; 21 | String lastMessageToken; 22 | 23 | void createLoginBody(char* buffer, int bufferLen, String user, String password) { 24 | DynamicJsonBuffer jsonBuffer; 25 | JsonObject& root = jsonBuffer.createObject(); 26 | root["type"] = "m.login.password"; 27 | root["user"] = user; 28 | root["password"] = password; 29 | root.printTo(buffer, bufferLen); 30 | } 31 | 32 | void createMessageBody(char* buffer, int bufferLen, String message) { 33 | DynamicJsonBuffer jsonBuffer; 34 | JsonObject& root = jsonBuffer.createObject(); 35 | root["msgtype"] = "m.text"; 36 | root["body"] = message; 37 | root.printTo(buffer, bufferLen); 38 | } 39 | 40 | bool login(String user, String password) { 41 | bool success = false; 42 | 43 | char buffer[512]; 44 | createLoginBody(buffer, 512, user, password); 45 | 46 | String url = "http://matrix.org/_matrix/client/r0/login"; 47 | // Serial.printf("POST %s\n", url.c_str()); 48 | 49 | http.begin("http://matrix.org/_matrix/client/r0/login"); 50 | http.addHeader("Content-Type", "application/json"); 51 | int rc = http.POST(buffer); 52 | if (rc > 0) { 53 | // Serial.printf("%d\n", rc); 54 | if (rc == HTTP_CODE_OK) { 55 | String body = http.getString(); 56 | DynamicJsonBuffer jsonBuffer; 57 | JsonObject& root = jsonBuffer.parseObject(body); 58 | String myAccessToken = root["access_token"]; 59 | accessToken = String(myAccessToken.c_str()); 60 | Serial.println(accessToken); 61 | success = true; 62 | } 63 | } else { 64 | Serial.printf("Error: %s\n", http.errorToString(rc).c_str()); 65 | } 66 | 67 | return success; 68 | } 69 | 70 | bool getMessages(String roomId) { 71 | bool success = false; 72 | 73 | String url = "http://matrix.org/_matrix/client/r0/rooms/" + roomId + "/messages?access_token=" + accessToken + "&limit=1"; 74 | if (lastMessageToken == "") { 75 | url += "&dir=b"; 76 | } else { 77 | url += "&dir=f&from=" + lastMessageToken; 78 | } 79 | Serial.printf("GET %s\n", url.c_str()); 80 | 81 | http.begin(url); 82 | int rc = http.GET(); 83 | if (rc > 0) { 84 | Serial.printf("%d\n", rc); 85 | if (rc == HTTP_CODE_OK) { 86 | DynamicJsonBuffer jsonBuffer; 87 | JsonObject& root = jsonBuffer.parseObject(http.getString()); 88 | if (lastMessageToken != "") { 89 | JsonArray& chunks = root["chunk"]; 90 | JsonObject& chunk = chunks[0]; 91 | String sender = chunk["sender"]; 92 | JsonObject& content = chunk["content"]; 93 | if (content.containsKey("body")) { 94 | String body = content["body"]; 95 | Serial.println(body); 96 | serial2.println(body); 97 | } 98 | } 99 | String myLastMessageToken = root["end"]; 100 | lastMessageToken = String(myLastMessageToken.c_str()); 101 | Serial.println(lastMessageToken); 102 | success = true; 103 | } 104 | } else { 105 | Serial.printf("Error: %s\n", http.errorToString(rc).c_str()); 106 | } 107 | 108 | return success; 109 | } 110 | 111 | bool sendMessage(String roomId, String message) { 112 | bool success = false; 113 | 114 | char buffer[512]; 115 | createMessageBody(buffer, 512, message); 116 | 117 | String url = "http://matrix.org/_matrix/client/r0/rooms/" + roomId + "/send/m.room.message/" + String(millis()) + "?access_token=" + accessToken + "&limit=1"; 118 | Serial.printf("PUT %s\n", url.c_str()); 119 | 120 | http.begin(url); 121 | int rc = http.sendRequest("PUT", buffer); 122 | if (rc > 0) { 123 | // Serial.printf("%d\n", rc); 124 | if (rc == HTTP_CODE_OK) { 125 | success = true; 126 | } 127 | } else { 128 | Serial.printf("Error: %s\n", http.errorToString(rc).c_str()); 129 | } 130 | return success; 131 | } 132 | 133 | 134 | void setup(void) { 135 | pinMode(SERIAL_RX, INPUT); 136 | pinMode(SERIAL_TX, OUTPUT); 137 | serial2.begin(9600); 138 | serial2.println("Connecting to"); 139 | serial2.println(ssid); 140 | 141 | pinMode(LED_BUILTIN, OUTPUT); 142 | digitalWrite(LED_BUILTIN, LOW); 143 | Serial.begin(9600); 144 | WiFi.begin(ssid, wifiPassword); 145 | Serial.println(""); 146 | 147 | // Wait for connection 148 | while (WiFi.status() != WL_CONNECTED) { 149 | delay(500); 150 | Serial.print("."); 151 | } 152 | Serial.println(""); 153 | Serial.print("Connected to "); 154 | Serial.println(ssid); 155 | Serial.print("IP address: "); 156 | Serial.println(WiFi.localIP()); 157 | serial2.println("Connected as"); 158 | serial2.println(WiFi.localIP()); 159 | 160 | http.setReuse(true); 161 | if (login(user, matrixPassword)) { 162 | getMessages(roomId); 163 | // sendMessage(roomId, "Hello from ESP8266"); 164 | } 165 | } 166 | 167 | int nextMillis = 0; 168 | String input; 169 | void loop(void){ 170 | if (serial2.available() > 0) { 171 | char c = serial2.read(); 172 | Serial.print(c); 173 | if ((c == '\r') || (c == '\n')) { 174 | sendMessage(roomId, input); 175 | input = ""; 176 | } else { 177 | input += String(c); 178 | } 179 | } 180 | // serial2.print("Hello\n"); 181 | if (millis() > nextMillis) { 182 | getMessages(roomId); 183 | nextMillis += 5000; 184 | } 185 | // getMessages(); 186 | } 187 | --------------------------------------------------------------------------------